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

Commit

Permalink
fix: prevent directory.bin referencing outside the package root (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed May 4, 2023
1 parent 0d4d6b6 commit d8eca92
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/read-json.js
Expand Up @@ -352,7 +352,7 @@ function bins (file, data, cb) {
return cb(null, data)
}

m = path.resolve(path.dirname(file), m)
m = path.resolve(path.dirname(file), path.join('.', path.join('/', m)))
glob('**', { cwd: m })
.then(binsGlob => bins_(file, data, binsGlob, cb))
.catch(er => cb(er))
Expand Down
22 changes: 22 additions & 0 deletions test/bin.js
Expand Up @@ -41,3 +41,25 @@ tap.test('Empty bin test', function (t) {
t.end()
})
})

tap.test('Bin dir test', function (t) {
var p = path.resolve(__dirname, 'fixtures/bindir.json')
var warn = createWarningCollector()
readJson(p, warn, function (er, data) {
t.equal(warn.warnings.length, 0)
t.equal(data.name, 'bindir-test')
t.strictSame(data.bin, { echo: 'bin/echo' })
t.end()
})
})

tap.test('Bin dir trim prefix test', function (t) {
var p = path.resolve(__dirname, 'fixtures/bindiroutofscope.json')
var warn = createWarningCollector()
readJson(p, warn, function (er, data) {
t.equal(warn.warnings.length, 0)
t.equal(data.name, 'bindiroutofscope-test')
t.strictSame(data.bin, { echo: 'bin/echo' })
t.end()
})
})
14 changes: 14 additions & 0 deletions test/fixtures/bindir.json
@@ -0,0 +1,14 @@
{
"name": "bindir-test",
"description": "my desc",
"repository": {
"type": "git",
"url": "git://github.com/npm/read-package-json.git"
},
"version": "0.0.1",
"readme": "hello world",
"directories": {
"bin": "./bin"
},
"license": "ISC"
}
14 changes: 14 additions & 0 deletions test/fixtures/bindiroutofscope.json
@@ -0,0 +1,14 @@
{
"name": "bindiroutofscope-test",
"description": "my desc",
"repository": {
"type": "git",
"url": "git://github.com/npm/read-package-json.git"
},
"version": "0.0.1",
"readme": "hello world",
"directories": {
"bin": "../../../../../bin"
},
"license": "ISC"
}

0 comments on commit d8eca92

Please sign in to comment.