-
Notifications
You must be signed in to change notification settings - Fork 404
Solution #354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Solution #354
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,66 @@ | ||
| /* eslint-disable no-console */ | ||
| 'use strict'; | ||
|
|
||
| const http = require('node:http'); | ||
| const fs = require('node:fs'); | ||
| const url = require('node:url'); | ||
| const path = require('node:path'); | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| return http.createServer((req, res) => { | ||
| const publicDir = path.resolve(__dirname, '../public'); | ||
| const normilizedURL = new url.URL(req.url, `http://${req.headers.host}`); | ||
| const filePath = normilizedURL.pathname; | ||
|
|
||
| if (req.url === '/app.js' || req.url.includes('/file/..')) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check is a good first step, but it's not secure enough to prevent all path traversal attacks. A request with URL-encoded characters like As mentioned in the previous review, a more robust way to handle this is to:
|
||
| res.statusCode = 400; | ||
| res.setHeader('Content-Type', 'text/plain'); | ||
| res.end('Bad request'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (filePath === '/file' || !filePath.startsWith('/file/')) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to the requirements, a request to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This condition correctly handles cases that don't start with |
||
| res.statusCode = 200; | ||
| res.setHeader('Content-Type', 'text/plain'); | ||
| res.end('Use /file/<path-to-file> to load files from public folder'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| let relativePath = filePath.slice('/file/'.length); | ||
|
|
||
| if (relativePath === '') { | ||
| relativePath = 'index.html'; | ||
| } | ||
|
|
||
| if (relativePath.includes('//')) { | ||
| res.statusCode = 404; | ||
| res.setHeader('Content-Type', 'text/plain'); | ||
| res.end('File not found'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| fs.readFile(path.join(publicDir, relativePath), (err, data) => { | ||
| if (err) { | ||
| res.statusCode = 404; | ||
| res.setHeader('Content-Type', 'text/plain'); | ||
| res.end('File not found'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const extension = path.extname(relativePath); | ||
| const contentType = extension === '.css' ? 'text/css' : 'text/html'; | ||
|
|
||
| res.statusCode = 200; | ||
| res.setHeader('Content-Type', contentType); | ||
| res.end(data); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check for path traversal is a good idea, but it can be bypassed using URL-encoded characters (e.g.,
%2e%2efor..). A more secure method is to create the full file path, normalize it, and then check if it's still inside yourpublicDirbefore attempting to read the file. This ensures you never access files outside the intended directory.