Skip to content

Commit 05b6f0f

Browse files
fix(arborist): allow-remote exemption for proxy/mirror-fronted registry tarballs (#9577)
Backport of #9550 to `release/v11`. Co-authored-by: Manzoor Wani <[email protected]>
1 parent 073253f commit 05b6f0f

2 files changed

Lines changed: 105 additions & 3 deletions

File tree

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -888,12 +888,14 @@ module.exports = cls => class Reifier extends cls {
888888
return false
889889
}
890890
try {
891-
const resolved = new URL(node.resolved)
891+
// Match the effective fetch URL, not the raw lockfile value.
892+
// #registryResolved applies replace-registry-host, rewriting a public-registry pin to the configured proxy/mirror so it matches.
893+
const resolvedURL = new URL(this.#registryResolved(node.resolved))
892894
// pickRegistry only consults spec.scope, so a bare-name (tag) parse is sufficient and avoids a node.version dependency.
893895
const registry = new URL(pickRegistry(npa(node.name), this.options))
894896
const registryPath = registry.pathname.replace(/\/?$/, '/')
895-
return resolved.origin === registry.origin &&
896-
(registryPath === '/' || resolved.pathname.startsWith(registryPath))
897+
return resolvedURL.origin === registry.origin &&
898+
(registryPath === '/' || resolvedURL.pathname.startsWith(registryPath))
897899
} catch {
898900
return false
899901
}

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

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4027,6 +4027,106 @@ t.test('should preserve exact ranges, missing actual tree', async (t) => {
40274027
await t.resolves(arb.reify(), 'same-origin tarball is allowed for registry root')
40284028
})
40294029

4030+
t.test('allowRemote=none allows registry tarball whose resolved origin differs from the configured registry', async t => {
4031+
// Proxy/mirror case: a committed lockfile pins resolved to the public registry while a private mirror is configured.
4032+
// replace-registry-host rewrites the host to the configured registry at fetch time, so the effective URL is registry-mediated and must pass allow-remote=none.
4033+
const abbrevPackumentNpmjs = JSON.stringify({
4034+
_id: 'abbrev',
4035+
_rev: 'lkjadflkjasdf',
4036+
name: 'abbrev',
4037+
'dist-tags': { latest: '1.1.1' },
4038+
versions: {
4039+
'1.1.1': {
4040+
name: 'abbrev',
4041+
version: '1.1.1',
4042+
dist: {
4043+
tarball: 'https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz',
4044+
},
4045+
},
4046+
},
4047+
})
4048+
4049+
const testdir = t.testdir({
4050+
project: {
4051+
'package.json': JSON.stringify({
4052+
name: 'myproject',
4053+
version: '1.0.0',
4054+
dependencies: {
4055+
abbrev: '1.1.1',
4056+
},
4057+
}),
4058+
},
4059+
})
4060+
4061+
tnock(t, 'https://registry.example.com')
4062+
.get('/abbrev')
4063+
.reply(200, abbrevPackumentNpmjs)
4064+
4065+
// replace-registry-host (default 'npmjs') rewrites the npmjs.org tarball host to the configured mirror, so the fetch lands here.
4066+
tnock(t, 'https://registry.example.com')
4067+
.get('/abbrev/-/abbrev-1.1.1.tgz')
4068+
.reply(200, abbrevTGZ)
4069+
4070+
const arb = new Arborist({
4071+
path: resolve(testdir, 'project'),
4072+
registry: 'https://registry.example.com',
4073+
cache: resolve(testdir, 'cache'),
4074+
allowRemote: 'none',
4075+
})
4076+
4077+
await t.resolves(arb.reify(), 'mirror-fronted registry tarball is allowed under allow-remote=none')
4078+
})
4079+
4080+
t.test('allowRemote=none allows registry tarball with replaceRegistryHost=always', async t => {
4081+
// replace-registry-host=always routes every registry tarball fetch through the configured registry, so the effective URL is never remote and must pass allow-remote=none.
4082+
const abbrevPackumentNpmjs = JSON.stringify({
4083+
_id: 'abbrev',
4084+
_rev: 'lkjadflkjasdf',
4085+
name: 'abbrev',
4086+
'dist-tags': { latest: '1.1.1' },
4087+
versions: {
4088+
'1.1.1': {
4089+
name: 'abbrev',
4090+
version: '1.1.1',
4091+
dist: {
4092+
tarball: 'https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz',
4093+
},
4094+
},
4095+
},
4096+
})
4097+
4098+
const testdir = t.testdir({
4099+
project: {
4100+
'package.json': JSON.stringify({
4101+
name: 'myproject',
4102+
version: '1.0.0',
4103+
dependencies: {
4104+
abbrev: '1.1.1',
4105+
},
4106+
}),
4107+
},
4108+
})
4109+
4110+
tnock(t, 'https://registry.example.com')
4111+
.get('/npm/abbrev')
4112+
.reply(200, abbrevPackumentNpmjs)
4113+
4114+
// always rewrites the tarball host to the configured registry and prepends the registry path.
4115+
tnock(t, 'https://registry.example.com')
4116+
.get('/npm/abbrev/-/abbrev-1.1.1.tgz')
4117+
.reply(200, abbrevTGZ)
4118+
4119+
const arb = new Arborist({
4120+
path: resolve(testdir, 'project'),
4121+
registry: 'https://registry.example.com/npm',
4122+
cache: resolve(testdir, 'cache'),
4123+
allowRemote: 'none',
4124+
replaceRegistryHost: 'always',
4125+
})
4126+
4127+
await t.resolves(arb.reify(), 'registry tarball routed through the configured registry is allowed')
4128+
})
4129+
40304130
t.test('allowRemote=none allows registry tarball under linked install strategy', async t => {
40314131
// The linked strategy extracts store nodes as IsolatedNode, which has no edges to recompute isRegistryDependency from.
40324132
// The flag must be carried from the source tree node so the registry-tarball allow-remote exemption still applies.

0 commit comments

Comments
 (0)