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 2 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
4 changes: 2 additions & 2 deletions lib/read-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,11 @@ function bins (file, data, cb) {
data = normalizePackageBin(data)

var m = data.directories && data.directories.bin
if (data.bin || !m) {
if (data.bin || !m || typeof m !== 'string') {
wraithgar marked this conversation as resolved.
Show resolved Hide resolved
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"
}