Skip to content

Commit a4ff71f

Browse files
committed
init
0 parents  commit a4ff71f

File tree

6 files changed

+202
-0
lines changed

6 files changed

+202
-0
lines changed

.gitignore

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Compiled source #
2+
###################
3+
*.com
4+
*.class
5+
*.dll
6+
*.exe
7+
*.o
8+
*.so
9+
10+
# Packages #
11+
############
12+
# it's better to unpack these files and commit the raw source
13+
# git has its own built in compression methods
14+
*.7z
15+
*.dmg
16+
*.gz
17+
*.iso
18+
*.jar
19+
*.rar
20+
*.tar
21+
*.zip
22+
23+
# Logs and databases #
24+
######################
25+
*.log
26+
*.sql
27+
*.sqlite
28+
29+
# OS generated files #
30+
######################
31+
.DS_Store*
32+
# Icon?
33+
ehthumbs.db
34+
Thumbs.db
35+
36+
# Node.js #
37+
###########
38+
lib-cov
39+
*.seed
40+
*.log
41+
*.csv
42+
*.dat
43+
*.out
44+
*.pid
45+
*.gz
46+
47+
pids
48+
logs
49+
results
50+
51+
node_modules
52+
npm-debug.log
53+
54+
# Components #
55+
##############
56+
57+
/build
58+
/components
59+
/public

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test/

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
# Resolve Path
3+
4+
Resolve a path against a root path with validation against common malicious attacks.
5+
6+
This module would protect against commons attacks like `GET /../file.js` which reaches outside the root folder.
7+
8+
## API
9+
10+
### absolutePath = resolve(rootPath, relativePath)
11+
12+
```js
13+
var resolve = require('resolve-path')
14+
15+
var filename = resolve(process.cwd(), '/public/favicon.ico')
16+
// => ~/public/favicon.ico
17+
```
18+
19+
`relativePath` is generally a path given by a server. For example, in Express, it's probably `req.path.slice(1)`, removing the leading `/` to make the path relative.
20+
21+
`rootPath` defaults to `process.cwd()`.
22+
23+
`absolutePath` is the resolved path.
24+
25+
This function __throws__.
26+
27+
## License
28+
29+
The MIT License (MIT)
30+
31+
Copyright (c) 2014 Jonathan Ong [email protected]
32+
33+
Permission is hereby granted, free of charge, to any person obtaining a copy
34+
of this software and associated documentation files (the "Software"), to deal
35+
in the Software without restriction, including without limitation the rights
36+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
37+
copies of the Software, and to permit persons to whom the Software is
38+
furnished to do so, subject to the following conditions:
39+
40+
The above copyright notice and this permission notice shall be included in
41+
all copies or substantial portions of the Software.
42+
43+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
44+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
45+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
46+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
47+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
48+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
49+
THE SOFTWARE.

index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
var resolve = require('path').resolve
3+
4+
module.exports = function resolvePath(root, path) {
5+
// just like path.resolve, make root optional
6+
if (arguments.length === 1) {
7+
path = root
8+
root = process.cwd()
9+
} else {
10+
root = root || process.cwd()
11+
}
12+
13+
// path should never be absolute
14+
if (resolve(path) === path) error(400, 'malicious path')
15+
16+
// null byte(s)
17+
if (~path.indexOf('\0')) error(400, 'null bytes')
18+
19+
path = resolve(root, path)
20+
21+
// out of bounds
22+
if (path.indexOf(root)) error(400, 'malicious path')
23+
24+
return path
25+
}
26+
27+
function error(status, message) {
28+
var err = new Error(message)
29+
err.status = status
30+
throw err
31+
}

package.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "resolve-path",
3+
"description": "resolve a relative path from a root path with validation",
4+
"version": "1.0.0",
5+
"author": {
6+
"name": "Jonathan Ong",
7+
"email": "[email protected]",
8+
"url": "http://jongleberry.com",
9+
"twitter": "https://twitter.com/jongleberry"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "https://github.com/expressjs/resolve-path.git"
14+
},
15+
"bugs": {
16+
"url": "https://github.com/expressjs/resolve-path/issues",
17+
"email": "[email protected]"
18+
},
19+
"devDependencies": {
20+
"should": "^3.0",
21+
"mocha": "^1.13"
22+
},
23+
"scripts": {
24+
"test": "NODE_ENV=test mocha --require should --reporter spec --bail"
25+
},
26+
"license": "MIT"
27+
}

test/resolve.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
var assert = require('assert')
3+
var path = require('path')
4+
5+
var resolve = require('..')
6+
7+
describe('Resolve Path', function () {
8+
it('should default to process.cwd()', function () {
9+
resolve('index.js').should.equal(path.resolve('index.js'))
10+
})
11+
12+
it('should work with a root', function () {
13+
resolve(__dirname, 'resolve.js').should.equal(path.resolve(__dirname, 'resolve.js'))
14+
})
15+
16+
describe('should throw if path', function () {
17+
it('is absolute', function () {
18+
assert.throws(function () {
19+
resolve('/home')
20+
})
21+
})
22+
23+
it('contains a null byte', function () {
24+
assert.throws(function () {
25+
resolve('klajsdkfjasdf\0lkjalksjdfklasf')
26+
})
27+
})
28+
29+
it('is out of bounds', function () {
30+
assert.throws(function () {
31+
resolve(__dirname, '../index.js')
32+
})
33+
})
34+
})
35+
})

0 commit comments

Comments
 (0)