Skip to content
This repository was archived by the owner on May 4, 2024. It is now read-only.

Commit d8eca92

Browse files
authored
fix: prevent directory.bin referencing outside the package root (#177)
1 parent 0d4d6b6 commit d8eca92

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

lib/read-json.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ function bins (file, data, cb) {
352352
return cb(null, data)
353353
}
354354

355-
m = path.resolve(path.dirname(file), m)
355+
m = path.resolve(path.dirname(file), path.join('.', path.join('/', m)))
356356
glob('**', { cwd: m })
357357
.then(binsGlob => bins_(file, data, binsGlob, cb))
358358
.catch(er => cb(er))

test/bin.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,25 @@ tap.test('Empty bin test', function (t) {
4141
t.end()
4242
})
4343
})
44+
45+
tap.test('Bin dir test', function (t) {
46+
var p = path.resolve(__dirname, 'fixtures/bindir.json')
47+
var warn = createWarningCollector()
48+
readJson(p, warn, function (er, data) {
49+
t.equal(warn.warnings.length, 0)
50+
t.equal(data.name, 'bindir-test')
51+
t.strictSame(data.bin, { echo: 'bin/echo' })
52+
t.end()
53+
})
54+
})
55+
56+
tap.test('Bin dir trim prefix test', function (t) {
57+
var p = path.resolve(__dirname, 'fixtures/bindiroutofscope.json')
58+
var warn = createWarningCollector()
59+
readJson(p, warn, function (er, data) {
60+
t.equal(warn.warnings.length, 0)
61+
t.equal(data.name, 'bindiroutofscope-test')
62+
t.strictSame(data.bin, { echo: 'bin/echo' })
63+
t.end()
64+
})
65+
})

test/fixtures/bindir.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "bindir-test",
3+
"description": "my desc",
4+
"repository": {
5+
"type": "git",
6+
"url": "git://github.com/npm/read-package-json.git"
7+
},
8+
"version": "0.0.1",
9+
"readme": "hello world",
10+
"directories": {
11+
"bin": "./bin"
12+
},
13+
"license": "ISC"
14+
}

test/fixtures/bindiroutofscope.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "bindiroutofscope-test",
3+
"description": "my desc",
4+
"repository": {
5+
"type": "git",
6+
"url": "git://github.com/npm/read-package-json.git"
7+
},
8+
"version": "0.0.1",
9+
"readme": "hello world",
10+
"directories": {
11+
"bin": "../../../../../bin"
12+
},
13+
"license": "ISC"
14+
}

0 commit comments

Comments
 (0)