Skip to content

added solution#360

Open
yahohulia wants to merge 3 commits intomate-academy:masterfrom
yahohulia:develop
Open

added solution#360
yahohulia wants to merge 3 commits intomate-academy:masterfrom
yahohulia:develop

Conversation

@yahohulia
Copy link
Copy Markdown

No description provided.

Copy link
Copy Markdown

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

This is a good start, but I'm requesting a few changes as the current implementation has some critical issues that don't meet the task requirements.

Here are the key areas to address:

  1. Path Traversal Vulnerability: Your server is vulnerable to path traversal attacks. A request like /file/../../package.json can access files outside the public directory. You must validate that the resolved file path is always within the public folder before serving it.
  2. Incorrect Root Handling: Requests to /file/ and /file should serve public/index.html, but your code currently returns a text message. Please adjust the logic to handle these cases as requests for index.html.
  3. Hardcoded Content-Type: The Content-Type is always set to text/html. This prevents files like main.css from being interpreted correctly by the browser. You'll need to dynamically set the content type based on the requested file's extension.

Fixing these issues will ensure your server is both secure and fully functional according to the requirements. You're on the right track!


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

Comment on lines +31 to +37
if (!fileName) {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Enter the correct path');

return;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the requirements, requests to /file/ and /file should return public/index.html. This logic incorrectly returns a message instead. You should modify this block to set the fileName to index.html when it's empty, and then allow the code to proceed to the file reading part.

return;
}

const realPath = path.join(__dirname, '..', 'public', fileName);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation is vulnerable to path traversal attacks. A request like /file/../../package.json could allow access to files outside of the public directory, which violates the requirements. You need to validate that the resolved file path is still within the public folder before serving the file.

const file = await fsp.readFile(realPath, 'utf-8');

res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Content-Type is hardcoded to text/html. This will cause issues when serving other types of files, like the main.css file mentioned in the task description. The server should dynamically determine the correct Content-Type based on the requested file's extension.

Copy link
Copy Markdown

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

Great job handling the /file and /file/ routes! I am requesting changes because two critical requirements are not yet met. First, the server has a path traversal vulnerability, allowing requests like /file/../../package.json to access files outside the public directory. Please add validation to ensure the requested file path stays within the public folder. Second, the Content-Type header is hardcoded to text/plain, which prevents browsers from rendering files like CSS correctly. This should be set dynamically based on the file's extension. Fixing these two issues will make your server secure and fully functional.


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

/* Write your code here */
// Return instance of http.Server class
const server = http.createServer(async (req, res) => {
res.setHeader('Content-Type', 'text/plain');
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Content-Type is still hardcoded to text/plain. For the browser to correctly interpret different file types (like CSS), this header needs to be set dynamically based on the requested file's extension. For example, a .css file should have a Content-Type of text/css.

Comment on lines +32 to +37
if (!fileName) {
res.statusCode = 200;

res.end('Enter the correct path');

return;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This conditional check is currently unreachable. The logic on line 22 (|| 'index.html') ensures that fileName will always be a non-empty string, so this if block will never execute. You can safely remove it.

return;
}

const realPath = path.join(__dirname, '..', 'public', fileName);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The path traversal vulnerability mentioned in the previous review is still present. A malicious request like /file/../../package.json could access files outside the public directory. You must validate that the final realPath is still within the public folder before attempting to read the file. A common way to do this is to resolve the absolute paths and check if the requested file path starts with the public directory's absolute path.

Copy link
Copy Markdown

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

I'm approving this submission as an educational override to help you progress, but there are a couple of critical items to address. You've done an excellent job implementing the dynamic Content-Type header! However, the path traversal vulnerability still needs to be fixed; you must ensure that the resolved file path is always within the public directory to prevent access to sensitive files. Additionally, requests to /file and /file/ should serve index.html, which isn't happening currently. Please adjust your logic to handle these root requests correctly. Keep up the great effort!


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Footnotes

  1. Rate AI review example

Comment on lines +42 to +48
if (!fileName) {
res.setHeader('Content-Type', 'text/plain');
res.statusCode = 200;
res.end('To load file, you need use "/file/fileName"');

return;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The requirements state that requests to /file and /file/ should serve public/index.html. This logic currently returns a hint message instead. Consider what fileName should be in this case to allow the existing file-reading logic to handle it correctly.

return;
}

const realPath = path.join(__dirname, '..', 'public', fileName);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation is still vulnerable to path traversal. A malicious request like /file/../../package.json can access files outside of the public directory. To fix this, you need to ensure the final resolved path is actually inside the public folder before serving the file. You can use path.resolve() to get the absolute path and then verify it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants