Skip to content

Commit

Permalink
Change error to 403 Forbidden when outside root
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Jan 2, 2015
1 parent e9feed0 commit 2e69ec0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
unreleased
==========

* Change error to 403 Forbidden when outside root
* Fix argument type errors to be consistent
* Fix path traversal vulnerability
* Use `http-errors` module directly
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function resolvePath(rootPath, relativePath) {

// path outside root
if ((path + sep).substr(0, root.length) !== root) {
throw createError(400, 'Malicious Path')
throw createError(403)
}

return path
Expand Down
10 changes: 5 additions & 5 deletions test/resolvePath.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ describe('resolvePath(relativePath)', function () {
})

describe('when relativePath resolves outside cwd', function () {
it('should throw Malicious Path error', function () {
it('should throw Forbidden error', function () {
assert.throws(resolvePath.bind(null, '../index.js'),
expectError(400, 'Malicious Path'))
expectError(403, 'Forbidden'))
})
})
})
Expand Down Expand Up @@ -129,14 +129,14 @@ describe('resolvePath(rootPath, relativePath)', function () {
})

describe('when relativePath resolves outside rootPath', function () {
it('should throw Malicious Path error', function () {
it('should throw Forbidden error', function () {
assert.throws(resolvePath.bind(null, __dirname, '../index.js'),
expectError(400, 'Malicious Path'))
expectError(403, 'Forbidden'))
})

it('should not be tricked by missing separator', function () {
assert.throws(resolvePath.bind(null, __dirname, join('..', basename(__dirname) + '2', 'index.js')),
expectError(400, 'Malicious Path'))
expectError(403, 'Forbidden'))
})
})
})
Expand Down

2 comments on commit 2e69ec0

@jonathanong
Copy link
Member

Choose a reason for hiding this comment

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

👍 i love how thorough you are with this tiny module. hahaha

@dougwilson
Copy link
Contributor Author

Choose a reason for hiding this comment

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

lol

Please sign in to comment.