Skip to content
Open
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
17 changes: 14 additions & 3 deletions lib/utils/allow-scripts-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { log } = require('proc-log')
const {
getTrustedRegistryIdentity,
resolvedSourceSpecs,
normalizeFileSpec,
} = require('@npmcli/arborist/lib/script-allowed.js')

// Pure helpers that implement the RFC's pin-mismatch table for
Expand Down Expand Up @@ -56,8 +57,8 @@ const versionedKeyFor = (node) => {
)
return null
}
/* istanbul ignore next: 'file:' and '/' branches are each covered separately */
if (resolved.startsWith('file:') || resolved.startsWith('/')) {
/* istanbul ignore next: 'file:', '/', and Windows drive letter branches are covered */
if (resolved.startsWith('file:') || resolved.startsWith('/') || /^[a-zA-Z]:[\\/]/.test(resolved)) {
return resolved
}
// No trusted source. Refuse to compose a key from attacker-controlled
Expand Down Expand Up @@ -86,7 +87,7 @@ const nameKeyFor = (node) => {
}
return null
}
if (resolved.startsWith('file:') || resolved.startsWith('/')) {
if (resolved.startsWith('file:') || resolved.startsWith('/') || /^[a-zA-Z]:[\\/]/.test(resolved)) {
return resolved
}
// Registry deps: only the URL-derived (or edges-derived, in the
Expand Down Expand Up @@ -179,6 +180,16 @@ const keyTargetsNode = (key, node) => {
}
case 'file':
case 'directory':
return resolvedSourceSpecs(node)
.some(resolved => {
const normResolved = normalizeFileSpec(resolved)
const normSave = normalizeFileSpec(parsed.saveSpec)
const normFetch = normalizeFileSpec(parsed.fetchSpec)
return (
(normSave !== '' && normResolved === normSave) ||
(normFetch !== '' && normResolved === normFetch)
)
})
case 'remote':
return resolvedSourceSpecs(node)
.some(resolved => resolved === parsed.saveSpec || resolved === parsed.fetchSpec)
Expand Down
19 changes: 18 additions & 1 deletion workspaces/arborist/lib/script-allowed.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,25 @@ const matchGit = (node, parsed) => {
return nodeCommittish.startsWith(keyCommittish)
}

const normalizeFileSpec = (spec) => {
if (typeof spec !== 'string' || spec === '') {
return ''
}
const withoutFile = spec.startsWith('file:') ? spec.slice(5) : spec
return withoutFile.replace(/\\/g, '/')
}

const matchFileOrDir = (node, parsed) => {
const normSave = normalizeFileSpec(parsed.saveSpec)
const normFetch = normalizeFileSpec(parsed.fetchSpec)
return resolvedSourceSpecs(node)
.some(resolved => resolved === parsed.saveSpec || resolved === parsed.fetchSpec)
.some(resolved => {
const normResolved = normalizeFileSpec(resolved)
return (
(normSave !== '' && normResolved === normSave) ||
(normFetch !== '' && normResolved === normFetch)
)
})
}

const matchRemote = (node, parsed) => {
Expand Down Expand Up @@ -382,3 +398,4 @@ module.exports.isExactVersionDisjunction = isExactVersionDisjunction
module.exports.getTrustedRegistryIdentity = getTrustedRegistryIdentity
module.exports.resolvedSourceSpecs = resolvedSourceSpecs
module.exports.trustedDisplay = trustedDisplay
module.exports.normalizeFileSpec = normalizeFileSpec
36 changes: 36 additions & 0 deletions workspaces/arborist/test/script-allowed.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,42 @@ t.test('local tarball key — npa parses *.tgz paths as type=file', t => {
t.end()
})

t.test('local tarball key — Windows backslashes and file: prefix matching', t => {
const winTgzNode = node({
name: 'electron-winstaller',
packageName: 'electron-winstaller',
version: '5.3.0',
resolved: 'file:C:\\absolute\\path\\to\\electron-winstaller-5.3.0.tgz',
})

// Matches forward slash file: URI
t.equal(isScriptAllowed(winTgzNode, {
'file:C:/absolute/path/to/electron-winstaller-5.3.0.tgz': true,
}), true)

// Matches backslash file: URI
t.equal(isScriptAllowed(winTgzNode, {
'file:C:\\absolute\\path\\to\\electron-winstaller-5.3.0.tgz': true,
}), true)

// Matches absolute path without file: prefix
t.equal(isScriptAllowed(winTgzNode, {
'C:/absolute/path/to/electron-winstaller-5.3.0.tgz': true,
}), true)

// Deny wins
t.equal(isScriptAllowed(winTgzNode, {
'file:C:/absolute/path/to/electron-winstaller-5.3.0.tgz': false,
}), false)

// Unrelated path does not match
t.equal(isScriptAllowed(winTgzNode, {
'file:C:/other/path/to/pkg.tgz': true,
}), null)

t.end()
})

t.test('remote tarball — exact resolved match', t => {
const remoteNode = node({
name: 'pkg',
Expand Down