Skip to content

Commit 95cf2e9

Browse files
Abhinav-143xMichael Smith
authored andcommitted
fix: validate registry path for allow-remote tarballs
(cherry picked from commit bf623e0)
1 parent 869cb9a commit 95cf2e9

3 files changed

Lines changed: 174 additions & 5 deletions

File tree

‎test/lib/commands/ci.js‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,37 @@ t.test('--no-audit and --ignore-scripts', async t => {
150150
t.match(joinedOutput(), 'added 1 package in', 'would fail if install script ran')
151151
})
152152

153+
t.test('allow-remote=none blocks same-host tarball outside registry path', async t => {
154+
const token = 'test-path-token'
155+
const registryUrl = 'https://registry.example.com/npm/'
156+
const evilTarball = 'https://registry.example.com/evil/abbrev-1.0.0.tgz'
157+
const lock = JSON.parse(JSON.stringify(packageLock))
158+
lock.packages['node_modules/abbrev'].resolved = evilTarball
159+
lock.dependencies.abbrev.resolved = evilTarball
160+
161+
const { npm } = await loadMockNpm(t, {
162+
config: {
163+
audit: false,
164+
'allow-remote': 'none',
165+
registry: registryUrl,
166+
[`//registry.example.com/npm/:_authToken`]: token,
167+
},
168+
prefixDir: {
169+
abbrev,
170+
'package.json': JSON.stringify(packageJson),
171+
'package-lock.json': JSON.stringify(lock),
172+
},
173+
})
174+
175+
await t.rejects(
176+
npm.exec('ci', []),
177+
{ code: 'EALLOWREMOTE' },
178+
'sibling-path tarball is blocked by allow-remote=none'
179+
)
180+
const nmAbbrev = path.join(npm.prefix, 'node_modules', 'abbrev')
181+
t.equal(fs.existsSync(nmAbbrev), false, 'does not install tarball outside configured registry path')
182+
})
183+
153184
t.test('lifecycle scripts', async t => {
154185
const scripts = []
155186
const { npm, registry } = await loadMockNpm(t, {

‎workspaces/arborist/lib/arborist/reify.js‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -881,17 +881,18 @@ module.exports = cls => class Reifier extends cls {
881881

882882
// When extracting a registry-resolved package, the spec we hand to pacote is name@URL.
883883
// pacote re-parses that with npa and gets spec.type === 'remote', so without an override the allow-remote gate would fire on every registry tarball (both =none and =root mis-fire).
884-
// Returns true only when we are confident this is a registry-mediated install: the node's inbound edges must all be registry-typed (no exotic spec smuggled the URL in) AND the resolved URL's host must match the registry npm-registry-fetch selected for this spec, so a tampered lockfile pointing at an attacker host still hits the gate.
884+
// Returns true only when we are confident this is a registry-mediated install.
885885
#isRegistryResolvedTarball (node) {
886886
if (!node.resolved || !node.isRegistryDependency) {
887887
return false
888888
}
889889
try {
890-
// Hostnames are case-insensitive; lowercase both sides for safety even though WHATWG URL already normalizes.
891-
const resolvedHost = new URL(node.resolved).hostname.toLowerCase()
890+
const resolved = new URL(node.resolved)
892891
// pickRegistry only consults spec.scope, so a bare-name (tag) parse is sufficient and avoids a node.version dependency.
893-
const registryHost = new URL(pickRegistry(npa(node.name), this.options)).hostname.toLowerCase()
894-
return resolvedHost === registryHost
892+
const registry = new URL(pickRegistry(npa(node.name), this.options))
893+
const registryPath = registry.pathname.replace(/\/?$/, '/')
894+
return resolved.origin === registry.origin &&
895+
(registryPath === '/' || resolved.pathname.startsWith(registryPath))
895896
} catch {
896897
return false
897898
}

‎workspaces/arborist/test/arborist/reify.js‎

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3840,6 +3840,143 @@ t.test('should preserve exact ranges, missing actual tree', async (t) => {
38403840
await t.resolves(arb.reify(), 'reify should complete successfully')
38413841
})
38423842

3843+
t.test('allowRemote=none allows registry tarball under registry path without trailing slash', async t => {
3844+
const abbrevPackument5 = JSON.stringify({
3845+
_id: 'abbrev',
3846+
_rev: 'lkjadflkjasdf',
3847+
name: 'abbrev',
3848+
'dist-tags': { latest: '1.1.1' },
3849+
versions: {
3850+
'1.1.1': {
3851+
name: 'abbrev',
3852+
version: '1.1.1',
3853+
dist: {
3854+
tarball: 'https://registry.example.com/npm/abbrev/-/abbrev-1.1.1.tgz',
3855+
},
3856+
},
3857+
},
3858+
})
3859+
3860+
const testdir = t.testdir({
3861+
project: {
3862+
'package.json': JSON.stringify({
3863+
name: 'myproject',
3864+
version: '1.0.0',
3865+
dependencies: {
3866+
abbrev: '1.1.1',
3867+
},
3868+
}),
3869+
},
3870+
})
3871+
3872+
tnock(t, 'https://registry.example.com')
3873+
.get('/npm/abbrev')
3874+
.reply(200, abbrevPackument5)
3875+
3876+
tnock(t, 'https://registry.example.com')
3877+
.get('/npm/abbrev/-/abbrev-1.1.1.tgz')
3878+
.reply(200, abbrevTGZ)
3879+
3880+
const arb = new Arborist({
3881+
path: resolve(testdir, 'project'),
3882+
registry: 'https://registry.example.com/npm',
3883+
cache: resolve(testdir, 'cache'),
3884+
allowRemote: 'none',
3885+
})
3886+
3887+
await t.resolves(arb.reify(), 'registry tarball under configured path is allowed')
3888+
})
3889+
3890+
t.test('allowRemote=none blocks same-origin tarball outside registry path', async t => {
3891+
const abbrevPackument5 = JSON.stringify({
3892+
_id: 'abbrev',
3893+
_rev: 'lkjadflkjasdf',
3894+
name: 'abbrev',
3895+
'dist-tags': { latest: '1.1.1' },
3896+
versions: {
3897+
'1.1.1': {
3898+
name: 'abbrev',
3899+
version: '1.1.1',
3900+
dist: {
3901+
tarball: 'https://registry.example.com/evil/abbrev-1.1.1.tgz',
3902+
},
3903+
},
3904+
},
3905+
})
3906+
3907+
const testdir = t.testdir({
3908+
project: {
3909+
'package.json': JSON.stringify({
3910+
name: 'myproject',
3911+
version: '1.0.0',
3912+
dependencies: {
3913+
abbrev: '1.1.1',
3914+
},
3915+
}),
3916+
},
3917+
})
3918+
3919+
tnock(t, 'https://registry.example.com')
3920+
.get('/npm/abbrev')
3921+
.reply(200, abbrevPackument5)
3922+
3923+
const arb = new Arborist({
3924+
path: resolve(testdir, 'project'),
3925+
registry: 'https://registry.example.com/npm/',
3926+
cache: resolve(testdir, 'cache'),
3927+
allowRemote: 'none',
3928+
})
3929+
3930+
await t.rejects(arb.reify(), { code: 'EALLOWREMOTE' }, 'sibling path tarball is blocked')
3931+
})
3932+
3933+
t.test('allowRemote=none allows same-origin tarball for root registry path', async t => {
3934+
const abbrevPackument5 = JSON.stringify({
3935+
_id: 'abbrev',
3936+
_rev: 'lkjadflkjasdf',
3937+
name: 'abbrev',
3938+
'dist-tags': { latest: '1.1.1' },
3939+
versions: {
3940+
'1.1.1': {
3941+
name: 'abbrev',
3942+
version: '1.1.1',
3943+
dist: {
3944+
tarball: 'https://registry.example.com/other/abbrev-1.1.1.tgz',
3945+
},
3946+
},
3947+
},
3948+
})
3949+
3950+
const testdir = t.testdir({
3951+
project: {
3952+
'package.json': JSON.stringify({
3953+
name: 'myproject',
3954+
version: '1.0.0',
3955+
dependencies: {
3956+
abbrev: '1.1.1',
3957+
},
3958+
}),
3959+
},
3960+
})
3961+
3962+
tnock(t, 'https://registry.example.com')
3963+
.get('/abbrev')
3964+
.reply(200, abbrevPackument5)
3965+
3966+
tnock(t, 'https://registry.example.com')
3967+
.get('/other/abbrev-1.1.1.tgz')
3968+
.reply(200, abbrevTGZ)
3969+
3970+
const arb = new Arborist({
3971+
path: resolve(testdir, 'project'),
3972+
registry: 'https://registry.example.com',
3973+
cache: resolve(testdir, 'cache'),
3974+
allowRemote: 'none',
3975+
})
3976+
3977+
await t.resolves(arb.reify(), 'same-origin tarball is allowed for registry root')
3978+
})
3979+
38433980
t.test('registry with different protocol should swap protocol', async (t) => {
38443981
const abbrevPackument4 = JSON.stringify({
38453982
_id: 'abbrev',

0 commit comments

Comments
 (0)