Skip to content

Commit

Permalink
Obscuring path from HTTP 404 response for static files
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolay committed Jun 29, 2017
1 parent 03f3bc6 commit c41eb49
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "web-service",
"version": "0.5.5",
"version": "0.5.6",
"description": "Instantiates web services: REST Api, file upload, etc",
"main": "index.common.js",
"//module": "index.es6.js",
Expand Down
29 changes: 24 additions & 5 deletions source/web service.js
Expand Up @@ -345,15 +345,34 @@ export default function web_service(options = {})
ctx.throw(405, 'Only HEAD and GET HTTP methods are allowed for requesting static files')
}

if (!await koa_send(ctx, ctx.path,
{
maxAge,
root: path.resolve(filesystem_path)
}))
let not_found

try
{
// `koa-send` throws if file not found
// but returns `undefined` if the path is a directory.
// Strange behaviour, so manually sending 404 Not found.
not_found = !await koa_send(ctx, ctx.path,
{
maxAge,
root: path.resolve(filesystem_path)
})
}
catch (error)
{
// Prevents exposing filesystem path in the HTTP response
if (error.statusCode === 404)
{
not_found = true
}
else
{
throw error
}
}

if (not_found)
{
ctx.throw(404, 'File not found')
}
}))
Expand Down

0 comments on commit c41eb49

Please sign in to comment.