-
Notifications
You must be signed in to change notification settings - Fork 415
Solution #374
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 #374
Changes from 1 commit
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 |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| name: Test | ||
|
|
||
| on: | ||
| pull_request: | ||
| branches: [ master ] | ||
|
|
||
| jobs: | ||
| build: | ||
|
|
||
| runs-on: ubuntu-latest | ||
|
|
||
| strategy: | ||
| matrix: | ||
| node-version: [20.x] | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v2 | ||
| - name: Use Node.js ${{ matrix.node-version }} | ||
| uses: actions/setup-node@v1 | ||
| with: | ||
| node-version: ${{ matrix.node-version }} | ||
| - run: npm install | ||
| - run: npm test |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,71 @@ | ||
| /* eslint-disable no-console */ | ||
| 'use strict'; | ||
|
|
||
| const http = require('http'); | ||
| const url = require('url'); | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
|
|
||
| function createServer() { | ||
| /* Write your code here */ | ||
| // Return instance of http.Server class | ||
| const server = http.createServer((req, res) => { | ||
| const normalizedURL = new url.URL(req.url, `http://${req.headers.host}`); | ||
| const fileName = normalizedURL.pathname; | ||
|
|
||
| if (!fileName.startsWith('/file/')) { | ||
| if (fileName === '/file') { | ||
| res.statusCode = 200; | ||
| } else { | ||
| res.statusCode = 400; | ||
| } | ||
| res.setHeader('Content-Type', 'text/plain'); | ||
| res.end('You need to set path starting with "/file/"'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (fileName.includes('//')) { | ||
| res.statusCode = 404; | ||
| res.setHeader('Content-Type', 'text/plain'); | ||
|
|
||
| res.end('You can`t type double slash(/)'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const relativePath = fileName.replace('/file/', ''); | ||
|
|
||
| const publicDir = path.resolve('./public'); | ||
| const resolvedPath = path.resolve(`./public/${relativePath}`); | ||
|
|
||
| if ( | ||
| !resolvedPath.startsWith(publicDir + path.sep) && | ||
| resolvedPath !== publicDir | ||
| ) { | ||
| res.statusCode = 400; | ||
| res.setHeader('Content-Type', 'text/plain'); | ||
| res.end("You can't go outside /file folder"); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| fs.readFile(`./public/${relativePath}`, (err, data) => { | ||
| if (err) { | ||
| res.statusCode = 404; | ||
| res.setHeader('Content-Type', 'text/plain'); | ||
| res.end(`Not Found`); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| res.statusCode = 200; | ||
| res.setHeader('Content-Type', 'text/plain'); | ||
|
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. The Content-Type is always set to |
||
| res.end(data); | ||
| }); | ||
| }); | ||
|
|
||
| return server; | ||
| } | ||
|
|
||
| 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.
The requirements specify that if pathname contains
../it should return 400 status. Currently there's no explicit check for../substring. The path traversal check handles security, but the explicit requirement is to detect../in the pathname itself.