Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"devDependencies": {
"@faker-js/faker": "^8.4.1",
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.8.6",
"@mate-academy/scripts": "^2.1.3",
"axios": "^1.7.2",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
Expand All @@ -28,5 +28,8 @@
},
"mateAcademy": {
"projectType": "javascript"
},
"dependencies": {
"nodemon": "^3.1.14"
}
}
58 changes: 58 additions & 0 deletions src/createServer.js
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/..')) {
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 path traversal is a good idea, but it can be bypassed using URL-encoded characters (e.g., %2e%2e for ..). A more secure method is to create the full file path, normalize it, and then check if it's still inside your publicDir before attempting to read the file. This ensures you never access files outside the intended directory.

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 is a good first step, but it's not secure enough to prevent all path traversal attacks. A request with URL-encoded characters like /file/%2e%2e/some_file could bypass this includes() check.

As mentioned in the previous review, a more robust way to handle this is to:

  1. Construct the full, absolute path to the requested file.
  2. Normalize this path using path.resolve().
  3. Check if the normalized path still starts with the absolute path of your publicDir.

res.statusCode = 400;
res.setHeader('Content-Type', 'text/plain');
res.end('Bad request');

return;
}

if (filePath === '/file' || !filePath.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.

According to the requirements, a request to /file should serve public/index.html. This condition incorrectly treats /file the same as other invalid paths and returns a hint message instead. You'll need to adjust the logic to handle the /file case specifically.

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 condition correctly handles cases that don't start with /file/, but it incorrectly handles the /file case. According to the requirements, a request to /file should serve public/index.html, just like a request to /file/. Currently, it's returning the hint message.

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 = {
Expand Down
Loading