Skip to content

Commit 8bbd70d

Browse files
fix(arborist): allow-remote exemption for proxy/mirror-fronted registry tarballs (#9550)
With `allow-remote=none` (the default on npm 12) or `allow-remote=root`, a `npm install` fails with `EALLOWREMOTE` on ordinary registry dependencies when the configured registry origin differs from the lockfile `resolved` origin. ``` npm error code EALLOWREMOTE npm error Fetching packages of type "remote" have been disabled ``` This is the common proxy/mirror case: a committed `package-lock.json` whose `resolved` URLs point to `https://registry.npmjs.org/...`, while the machine (or CI) is configured to use a private registry proxy/mirror with a different origin. It affects both the hoisted and the linked install strategy. ## Why When extracting a registry-resolved package, reify hands pacote a `name@URL` spec, which pacote re-parses as `type=remote` and gates with allow-remote. To avoid mis-firing on registry tarballs, `#isRegistryResolvedTarball` exempts them — but it compared the raw lockfile `resolved` URL against the configured registry origin. With a proxy/mirror configured, `resolved` is the canonical `registry.npmjs.org` URL while the configured registry is the proxy, so the origins never matched, the exemption returned `false`, and the registry tarball was rejected as remote. Crucially, reify already fetches a different URL than the raw `resolved`: `#registryResolved` applies `replace-registry-host` (default `npmjs`), rewriting the `registry.npmjs.org` host to the configured registry while preserving the path. So npm fetches the tarball from the proxy correctly; only the allow-remote check was evaluating the wrong (pre-rewrite) URL. ## How Evaluate the effective URL npm actually fetches, not the raw lockfile value. `#isRegistryResolvedTarball` now parses `this.#registryResolved(node.resolved)` — the host-rewritten URL — before the same origin + registry-path-prefix comparison. After rewriting, a public-registry-pinned tarball resolves to the configured registry and is correctly recognized as registry-mediated. The existing security boundary is preserved: under the default `replace-registry-host`, a same-origin tarball pointing outside the registry path is not rewritten and is still rejected, and a genuinely URL-declared dependency still fails the `node.isRegistryDependency` guard. Under `replace-registry-host=always`, every tarball is routed through the configured registry, so registry dependencies are no longer treated as remote — consistent with what `always` means. ## References Fixes #9548
1 parent 315e3bd commit 8bbd70d

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
@@ -905,12 +905,14 @@ module.exports = cls => class Reifier extends cls {
905905
return false
906906
}
907907
try {
908-
const resolved = new URL(node.resolved)
908+
// Match the effective fetch URL, not the raw lockfile value.
909+
// #registryResolved applies replace-registry-host, rewriting a public-registry pin to the configured proxy/mirror so it matches.
910+
const resolvedURL = new URL(this.#registryResolved(node.resolved))
909911
// pickRegistry only consults spec.scope, so a bare-name (tag) parse is sufficient and avoids a node.version dependency.
910912
const registry = new URL(pickRegistry(npa(node.name), this.options))
911913
const registryPath = registry.pathname.replace(/\/?$/, '/')
912-
return resolved.origin === registry.origin &&
913-
(registryPath === '/' || resolved.pathname.startsWith(registryPath))
914+
return resolvedURL.origin === registry.origin &&
915+
(registryPath === '/' || resolvedURL.pathname.startsWith(registryPath))
914916
} catch {
915917
return false
916918
}

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

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

3917+
t.test('allowRemote=none allows registry tarball whose resolved origin differs from the configured registry', async t => {
3918+
// Proxy/mirror case: a committed lockfile pins resolved to the public registry while a private mirror is configured.
3919+
// 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.
3920+
const abbrevPackumentNpmjs = JSON.stringify({
3921+
_id: 'abbrev',
3922+
_rev: 'lkjadflkjasdf',
3923+
name: 'abbrev',
3924+
'dist-tags': { latest: '1.1.1' },
3925+
versions: {
3926+
'1.1.1': {
3927+
name: 'abbrev',
3928+
version: '1.1.1',
3929+
dist: {
3930+
tarball: 'https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz',
3931+
},
3932+
},
3933+
},
3934+
})
3935+
3936+
const testdir = t.testdir({
3937+
project: {
3938+
'package.json': JSON.stringify({
3939+
name: 'myproject',
3940+
version: '1.0.0',
3941+
dependencies: {
3942+
abbrev: '1.1.1',
3943+
},
3944+
}),
3945+
},
3946+
})
3947+
3948+
tnock(t, 'https://registry.example.com')
3949+
.get('/abbrev')
3950+
.reply(200, abbrevPackumentNpmjs)
3951+
3952+
// replace-registry-host (default 'npmjs') rewrites the npmjs.org tarball host to the configured mirror, so the fetch lands here.
3953+
tnock(t, 'https://registry.example.com')
3954+
.get('/abbrev/-/abbrev-1.1.1.tgz')
3955+
.reply(200, abbrevTGZ)
3956+
3957+
const arb = new Arborist({
3958+
path: resolve(testdir, 'project'),
3959+
registry: 'https://registry.example.com',
3960+
cache: resolve(testdir, 'cache'),
3961+
allowRemote: 'none',
3962+
})
3963+
3964+
await t.resolves(arb.reify(), 'mirror-fronted registry tarball is allowed under allow-remote=none')
3965+
})
3966+
3967+
t.test('allowRemote=none allows registry tarball with replaceRegistryHost=always', async t => {
3968+
// 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.
3969+
const abbrevPackumentNpmjs = JSON.stringify({
3970+
_id: 'abbrev',
3971+
_rev: 'lkjadflkjasdf',
3972+
name: 'abbrev',
3973+
'dist-tags': { latest: '1.1.1' },
3974+
versions: {
3975+
'1.1.1': {
3976+
name: 'abbrev',
3977+
version: '1.1.1',
3978+
dist: {
3979+
tarball: 'https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz',
3980+
},
3981+
},
3982+
},
3983+
})
3984+
3985+
const testdir = t.testdir({
3986+
project: {
3987+
'package.json': JSON.stringify({
3988+
name: 'myproject',
3989+
version: '1.0.0',
3990+
dependencies: {
3991+
abbrev: '1.1.1',
3992+
},
3993+
}),
3994+
},
3995+
})
3996+
3997+
tnock(t, 'https://registry.example.com')
3998+
.get('/npm/abbrev')
3999+
.reply(200, abbrevPackumentNpmjs)
4000+
4001+
// always rewrites the tarball host to the configured registry and prepends the registry path.
4002+
tnock(t, 'https://registry.example.com')
4003+
.get('/npm/abbrev/-/abbrev-1.1.1.tgz')
4004+
.reply(200, abbrevTGZ)
4005+
4006+
const arb = new Arborist({
4007+
path: resolve(testdir, 'project'),
4008+
registry: 'https://registry.example.com/npm',
4009+
cache: resolve(testdir, 'cache'),
4010+
allowRemote: 'none',
4011+
replaceRegistryHost: 'always',
4012+
})
4013+
4014+
await t.resolves(arb.reify(), 'registry tarball routed through the configured registry is allowed')
4015+
})
4016+
39174017
t.test('allowRemote=none allows registry tarball under linked install strategy', async t => {
39184018
// The linked strategy extracts store nodes as IsolatedNode, which has no edges to recompute isRegistryDependency from.
39194019
// The flag must be carried from the source tree node so the registry-tarball allow-remote exemption still applies.

0 commit comments

Comments
 (0)