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

fix: prevent directory.bin referencing outside the package root #177

Merged
merged 3 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/read-json.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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"
}