Skip to content

Solution#347

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

Solution#347
VictorKomara wants to merge 3 commits intomate-academy:masterfrom
VictorKomara:develop

Conversation

@VictorKomara
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

Good job setting up the basic HTTP server. However, some changes are needed to meet the requirements, particularly regarding security and routing logic.

Here are the critical issues to address:

  1. Security Vulnerability: Your code is vulnerable to path traversal. A malicious request like /file/../../package.json can access files outside the public directory. You must add a check to ensure the final resolved path stays within the public folder.
  2. Incorrect Routing:
    • A request to /file should serve public/index.html, but it currently returns a hint message.
    • Any path that does not start with /file/ should receive a hint message, but your server incorrectly returns a 400 Bad Request.

Please address these points to ensure your server is both secure and fully functional according to the requirements.


✨ 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

const isFileRequest = pathname.startsWith('/file/');

// 2. ВИЗНАЧАЄМО, ЧИ ЦЕ ЗАПИТ НА ПІДКАЗКУ (/file або /)
const isHintRequest = pathname === '/file' || pathname === '/';
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 a request to /file should serve public/index.html. This logic incorrectly categorizes it as a hint request, causing it to return a hint message instead.

const isHintRequest = pathname === '/file' || pathname === '/';

// 3. ЯКЩО ЦЕ НІ ТЕ, НІ ІНШЕ (наприклад, /app.js), ТО ЦЕ 400
if (!isFileRequest && !isHintRequest) {
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, any path that does not start with /file/ should receive a hint message. This conditional block incorrectly returns a 400 Bad Request for such paths instead.

// 6. РОБОТА З ФАЙЛАМИ
const relativePath = pathAfterPrefix || 'index.html';
const publicPath = path.resolve('public');
const filePath = path.join(publicPath, relativePath);
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 malicious path like ../../somefile could be used to access files outside the public directory, which violates a core requirement. You should add a check to ensure that the final resolved filePath is still within the public directory before attempting to read the file.

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 work on securing the server against path traversal and fixing the routing for /file requests! The implementation is much more robust now.

I am requesting one change because the server doesn't yet meet all requirements for handling non-file paths. According to the task, any path that does not start with /file/ should return a specific hint message. Your code currently does this for the root path (/) but returns a 400 Bad Request for other paths like /some-other-path.

Please adjust the logic in src/createServer.js to ensure the hint message is returned for all non-file paths as required. You are very close to completing the task!


✨ 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 +33 to +36
res.statusCode = 400;
res.setHeader('Content-Type', 'text/plain');

return res.end('Bad Request');
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, any path that does not start with /file/ should return a hint message. This block correctly handles /, but for other paths (like /some-other-path), it returns a 400 Bad Request instead of the required hint.

The logic in the parent if block (lines 20-37) should be simplified to always send the hint message for any request that isn't a file request.

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 am approving your solution as an educational override to help you progress, as you've made great strides in this submission! Your logic for handling file paths and preventing path traversal is much improved. To make it perfect, please consider a few points for next time. First, the route /file should serve index.html, but the current check pathname.startsWith('/file/') doesn't account for it. Second, the special handling for /app.js contradicts the requirement that all non-file paths should receive the same hint message. Finally, the explicit check for // in the path is unnecessary and can block valid requests, as path.join handles path normalization automatically. You're doing great, keep up the fantastic effort!


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

Footnotes

  1. Rate AI review example

// Return instance of http.Server class
return http.createServer(async (req, res) => {
const { pathname } = new URL(req.url, `http://${req.headers.host}`);
const isFileRoute = pathname.startsWith('/file/');
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 check doesn't account for the requirement that a request to /file should also be treated as a file route and serve index.html. Currently, a request to /file will be handled as a non-file route because '/file'.startsWith('/file/') is false.

Comment on lines +19 to +24
if (pathname === '/app.js') {
res.statusCode = 400;
res.setHeader('Content-Type', 'text/plain');

return res.end('Bad Request');
}
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 special case for /app.js contradicts the task requirement. According to the description, any path that does not start with /file/ should return the hint message. This block should be removed to ensure all non-file paths are handled uniformly as required.

Comment on lines +55 to +59
if (pathAfterPrefix.includes('//')) {
res.statusCode = 404;
res.setHeader('Content-Type', 'text/plain');

return res.end('File not found');
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 check for // might cause incorrect behavior. The requirement is to return 404 for non-existent files. A path like /file/styles//main.css can resolve to a valid, existing file (public/styles/main.css) because path.join normalizes it. By returning 404 here, you might be incorrectly rejecting a valid request.

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