Skip to content

Commit 393a491

Browse files
authored
Website guides: highlight potential security risk with above code (#4779)
To better highlight potential risks with the above code. New developers who may scour through the website guides might be more prone to a copy-and-paste method of testing things out. Nothing's wrong with doing that for learning purposes, but some might be too quick to do that and skip reading through the paragraph.
1 parent 60e9e10 commit 393a491

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

locale/en/knowledge/HTTP/servers/how-to-serve-static-files.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ http.createServer(function (req, res) {
2626
}).listen(8080);
2727
```
2828

29-
This example takes the path requested and it serves that path, relative to the local directory. This works fine as a quick solution; however, there are a few problems with this approach. First, this code does not correctly handle mime types. Additionally, a proper static file server should really be taking advantage of client side caching, and should send a "Not Modified" response if nothing has changed. Furthermore, there are security bugs that can enable a malicious user to break out of the current directory. (for example, `GET /../../../`).
29+
This example takes the path requested and it serves that path, relative to the local directory. This works fine as a quick solution; however, there are a few problems with this approach. First, this code does not correctly handle mime types. Additionally, a proper static file server should really be taking advantage of client side caching, and should send a "Not Modified" response if nothing has changed. **Furthermore, there are security bugs that can enable a malicious user to break out of the current directory. (for example, `GET /../../../`)**.
3030

3131
Each of these can be addressed individually without much difficulty. You can send the proper mime type header. You can figure how to utilize the client caches. You can take advantage of `path.normalize` to make sure that requests don't break out of the current directory. But why write all that code when you can just use someone else's library?
3232

0 commit comments

Comments
 (0)