Skip to content

Commit 1e63b45

Browse files
Fix npm overrides to a URL treated as registry (#1965) (#1966)
* Start fix for #1965 Assisted-by: Claude Code:claude-opus-5-5 * Test npm overrides that swap in a URL spec A registry range that the root package.json overrides with a remote tarball URL, git or path spec is installed by npm from that spec, not from the lock's resolved URL. These tests show the copy is not yet treated as a non-registry install, so hosted and vendored scans rewire it and VEX attests a patch that npm ci never installs (#1965). Assisted-by: Claude Code:claude-opus-5-5 * Skip npm copies an override sends to a URL When the root package.json overrides a registry range with a remote tarball URL, git or path spec, npm installs that spec and ignores the lock's resolved URL. Hosted and vendored scans now treat such a copy like any other non-registry install: hosted skips it with redirect_npm_non_registry_entry_skipped, vendored refuses it, and VEX no longer attests a patch that npm ci never installs. When the winning override can't be decided exactly, any rule for the package with a non-registry value keeps the copy unpatched and reported, matching the module's cautious rule for unclear cases. Fixes #1965 Assisted-by: Claude Code:claude-opus-5-5 * Keep URL-overridden npm copies unwindable Test the hosted rewriter, the vendored lock writer and VEX lockfile discovery against a registry range that the root overrides send to a remote tarball URL: hosted skips it loudly, vendored refuses it, and VEX attests nothing (#1965). A pin an older release wrote at such a copy is still the rewriters' own output, so VEX discovery keeps it shadowed and rollback and remove can still unwind it. Assisted-by: Claude Code:claude-opus-5-5 * E2E: hosted npm skips a URL-overridden copy Real npm installs a local foo that depends on left-pad ^1.3.0 while the root overrides send left-pad to a remote tarball URL. The hosted scan now skips that copy with redirect_npm_non_registry_entry_skipped, leaves the lock byte-identical and attests nothing (#1965). Assisted-by: Claude Code:claude-opus-5-5 * Look up npm URL overrides only when one exists Settle registry edges whose package has no non-registry override rule before walking the dependent's chain, so large locks with an unrelated overrides block don't pay a per-edge search. Assisted-by: Claude Code:claude-opus-5-5 * Match npm overrides scoped to a workspace member npm reaches a workspace member through the root's link edge, so an override keyed by the member's name applies to its dependencies. The override lookup now starts a dependent's ancestor chain with its workspace member, so a URL override scoped to a member skips that member's copy instead of rewiring and attesting it. Found in review. Assisted-by: Claude Code:claude-opus-5-5 * Name npm workspace members as npm does A member without a name field keeps its @scope/ folder parent, and only the root-level link entry names a member: a nested link to the same folder may be an alias. Member-scoped overrides now match the member's real name. Found in review. Assisted-by: Claude Code:claude-opus-5-5 --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 568e376 commit 1e63b45

5 files changed

Lines changed: 887 additions & 43 deletions

File tree

‎crates/socket-patch-cli/tests/e2e_redirect_npm_build.rs‎

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,3 +1433,217 @@ async fn npm_agent_keeps_crlf_shebang_of_bin_npm_did_not_link() {
14331433
);
14341434
}
14351435
}
1436+
1437+
/// #1965: a local `foo` depends on left-pad by a registry range, and the
1438+
/// project's `overrides` replace it with a remote tarball url. npm records
1439+
/// that copy with a registry-looking `resolved`, but installs it from the
1440+
/// override spec, so a hosted pin would never install. The hosted scan
1441+
/// must skip it loudly (`redirect_npm_non_registry_entry_skipped`), leave
1442+
/// the lock byte-identical, and attest nothing in the in-run VEX.
1443+
#[tokio::test(flavor = "multi_thread")]
1444+
#[ignore = "wall-bound real-npm install; runs on the e2e CI matrix leg"]
1445+
async fn npm_redirect_skips_a_registry_edge_overridden_to_a_url() {
1446+
let suite = "e2e_redirect_npm_build (overridden-to-url)";
1447+
if npm_e2e_common::npm_major().is_none() {
1448+
npm_e2e_common::skip(suite, "`npm` not installed");
1449+
return;
1450+
}
1451+
if !npm_supports_overrides() {
1452+
println!("N/A {suite}: this npm predates `overrides` (npm 8.3)");
1453+
return;
1454+
}
1455+
let tmp = tempfile::tempdir().unwrap();
1456+
let proj = tmp.path().join("proj");
1457+
let foo = tmp.path().join("foo");
1458+
let cache = tmp.path().join("npm-cache");
1459+
std::fs::create_dir_all(&proj).unwrap();
1460+
std::fs::create_dir_all(&foo).unwrap();
1461+
std::fs::write(
1462+
foo.join("package.json"),
1463+
format!(
1464+
r#"{{"name":"foo","version":"1.0.0","dependencies":{{"{DEP}":"^{DEP_VERSION}"}}}}"#
1465+
),
1466+
)
1467+
.unwrap();
1468+
std::fs::write(
1469+
foo.join("index.js"),
1470+
format!("module.exports = require('{DEP}');\n"),
1471+
)
1472+
.unwrap();
1473+
let packed = npm_e2e_common::npm(&foo, &["pack", "--cache", cache.to_str().unwrap()]);
1474+
assert!(
1475+
packed.status.success(),
1476+
"npm pack foo: {}",
1477+
npm_e2e_common::output_text(&packed)
1478+
);
1479+
std::fs::rename(foo.join("foo-1.0.0.tgz"), proj.join("foo-1.0.0.tgz")).unwrap();
1480+
let tarball = format!("https://registry.npmjs.org/{DEP}/-/{DEP}-{DEP_VERSION}.tgz");
1481+
std::fs::write(
1482+
proj.join("package.json"),
1483+
format!(
1484+
r#"{{"name":"app","version":"1.0.0","private":true,"dependencies":{{"foo":"file:foo-1.0.0.tgz"}},"overrides":{{"{DEP}":"{tarball}"}}}}"#
1485+
),
1486+
)
1487+
.unwrap();
1488+
// npm >= 12 refuses a url override unless remote installs are allowed.
1489+
std::fs::write(proj.join(".npmrc"), "allow-remote=all\n").unwrap();
1490+
let out = npm_e2e_common::npm(
1491+
&proj,
1492+
&[
1493+
"install",
1494+
"--no-audit",
1495+
"--no-fund",
1496+
"--cache",
1497+
cache.to_str().unwrap(),
1498+
],
1499+
);
1500+
if !out.status.success() {
1501+
npm_e2e_common::skip(
1502+
suite,
1503+
&format!(
1504+
"`npm install` of the overrides fixture failed (registry unreachable?):\n{}",
1505+
npm_e2e_common::output_text(&out)
1506+
),
1507+
);
1508+
return;
1509+
}
1510+
// The #1965 premise: the copy's `resolved` looks like a registry entry,
1511+
// and `foo`'s entry keeps its registry range.
1512+
let lock_before = std::fs::read(proj.join("package-lock.json")).unwrap();
1513+
let lock: serde_json::Value = serde_json::from_slice(&lock_before).unwrap();
1514+
assert_eq!(
1515+
lock["packages"][format!("node_modules/{DEP}")]["resolved"],
1516+
tarball.as_str(),
1517+
"{lock}"
1518+
);
1519+
assert_eq!(
1520+
lock["packages"]["node_modules/foo"]["dependencies"][DEP],
1521+
format!("^{DEP_VERSION}"),
1522+
"{lock}"
1523+
);
1524+
1525+
let server = MockServer::start().await;
1526+
let hosted_url = format!(
1527+
"{}/patch/npm/{DEP}/{DEP_VERSION}/{TOKEN}/{UUID}/{DEP}-{DEP_VERSION}.tgz",
1528+
server.uri()
1529+
);
1530+
Mock::given(method("POST"))
1531+
.and(path(format!("/v0/orgs/{ORG}/patches/batch")))
1532+
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
1533+
"packages": [{
1534+
"purl": PURL,
1535+
"patches": [{
1536+
"uuid": UUID, "purl": PURL, "tier": "free",
1537+
"cveIds": [], "ghsaIds": [], "severity": "high",
1538+
"title": "overridden-to-url fixture"
1539+
}]
1540+
}],
1541+
"canAccessPaidPatches": false,
1542+
})))
1543+
.mount(&server)
1544+
.await;
1545+
Mock::given(method("GET"))
1546+
.and(path_regex(format!(
1547+
"^/v0/orgs/{ORG}/patches/by-package/.+$"
1548+
)))
1549+
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
1550+
"patches": [{
1551+
"uuid": UUID, "purl": PURL,
1552+
"publishedAt": "2026-01-01T00:00:00Z",
1553+
"description": "x", "license": "MIT", "tier": "free",
1554+
"vulnerabilities": {}
1555+
}],
1556+
"canAccessPaidPatches": false,
1557+
})))
1558+
.mount(&server)
1559+
.await;
1560+
Mock::given(method("POST"))
1561+
.and(path(format!("/v0/orgs/{ORG}/patches/package")))
1562+
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
1563+
"results": {
1564+
UUID: {
1565+
"status": "granted",
1566+
"url": hosted_url,
1567+
"purl": PURL,
1568+
"artifacts": [{
1569+
"kind": "tarball",
1570+
"url": hosted_url,
1571+
"integrity": { "sha512": "sha512-UEFUQ0hFRA==" }
1572+
}],
1573+
"registryOverride": null
1574+
}
1575+
}
1576+
})))
1577+
.mount(&server)
1578+
.await;
1579+
Mock::given(method("GET"))
1580+
.and(path(format!("/v0/orgs/{ORG}/patches/view/{UUID}")))
1581+
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
1582+
"uuid": UUID,
1583+
"purl": PURL,
1584+
"publishedAt": "2026-01-01T00:00:00Z",
1585+
"files": {},
1586+
"vulnerabilities": {
1587+
GHSA: {
1588+
"cves": ["CVE-2026-1111"],
1589+
"summary": "overridden-to-url vuln",
1590+
"severity": "high",
1591+
"description": "d"
1592+
}
1593+
},
1594+
"description": "x", "license": "MIT", "tier": "free"
1595+
})))
1596+
.mount(&server)
1597+
.await;
1598+
1599+
let uri = server.uri();
1600+
let argv = [
1601+
"scan",
1602+
"--mode=hosted",
1603+
"--json",
1604+
"--yes",
1605+
"--cwd",
1606+
proj.to_str().unwrap(),
1607+
"--api-url",
1608+
&uri,
1609+
"--org",
1610+
ORG,
1611+
"--api-token",
1612+
"fake",
1613+
"--vex",
1614+
"out.vex.json",
1615+
"--vex-product",
1616+
PRODUCT,
1617+
];
1618+
let (_code, stdout, stderr) = run_socket(&proj, &argv);
1619+
let env: serde_json::Value = serde_json::from_str(&stdout)
1620+
.unwrap_or_else(|e| panic!("not JSON ({e}):\n{stdout}\nstderr:\n{stderr}"));
1621+
let skipped = env["warnings"]
1622+
.as_array()
1623+
.and_then(|w| {
1624+
w.iter()
1625+
.find(|w| w["code"] == "redirect_npm_non_registry_entry_skipped")
1626+
})
1627+
.unwrap_or_else(|| panic!("the overridden copy must be skipped loudly: {env}"));
1628+
assert!(
1629+
skipped["detail"]
1630+
.as_str()
1631+
.is_some_and(|d| d.contains("overrides")),
1632+
"the skip names the override: {skipped}"
1633+
);
1634+
assert_eq!(
1635+
std::fs::read(proj.join("package-lock.json")).unwrap(),
1636+
lock_before,
1637+
"the lock is left byte-identical: {env}"
1638+
);
1639+
let vex_path = proj.join("out.vex.json");
1640+
if vex_path.exists() {
1641+
let vex: serde_json::Value =
1642+
serde_json::from_slice(&std::fs::read(&vex_path).unwrap()).unwrap();
1643+
let not_affected = vex["statements"].as_array().is_some_and(|s| {
1644+
s.iter()
1645+
.any(|s| s["status"].as_str() == Some("not_affected"))
1646+
});
1647+
assert!(!not_affected, "nothing is attested not_affected: {vex}");
1648+
}
1649+
}

‎crates/socket-patch-core/src/patch/redirect/mod.rs‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21340,6 +21340,87 @@ mod tests {
2134021340
assert!(!r.files.contains_key("package.json"));
2134121341
}
2134221342

21343+
/// #1965: `foo` depends on left-pad by a registry range and the root
21344+
/// `overrides` swap it for a remote tarball url, so npm installs that
21345+
/// url whatever the lock's (registry-looking) `resolved` says. The
21346+
/// rewriter must skip the entry loudly and keep it out of the VEX.
21347+
#[test]
21348+
fn issue_1965_a_registry_edge_overridden_to_a_url_is_unredirected() {
21349+
let url = "https://registry.npmjs.org/left-pad/-/left-pad-1.3.0.tgz";
21350+
let lock = json!({
21351+
"name": "app",
21352+
"lockfileVersion": 3,
21353+
"packages": {
21354+
"": { "name": "app", "version": "0.0.0", "dependencies": { "foo": "file:foo-1.0.0.tgz" } },
21355+
"node_modules/foo": {
21356+
"version": "1.0.0",
21357+
"resolved": "file:foo-1.0.0.tgz",
21358+
"dependencies": { "left-pad": "^1.3.0" }
21359+
},
21360+
"node_modules/left-pad": {
21361+
"version": "1.3.0",
21362+
"resolved": url,
21363+
"integrity": "sha512-UPSTREAM=="
21364+
}
21365+
}
21366+
});
21367+
let overrides = vec![npm_override(
21368+
"left-pad",
21369+
"1.3.0",
21370+
"http://patch.test/lp.tgz",
21371+
"sha512-PATCHED==",
21372+
)];
21373+
let files_with = |manifest_overrides: Value| {
21374+
let mut files = BTreeMap::new();
21375+
files.insert(
21376+
"package-lock.json".to_string(),
21377+
serde_json::to_string_pretty(&lock).unwrap(),
21378+
);
21379+
files.insert(
21380+
"package.json".to_string(),
21381+
json!({
21382+
"name": "app",
21383+
"dependencies": { "foo": "file:foo-1.0.0.tgz" },
21384+
"overrides": manifest_overrides
21385+
})
21386+
.to_string(),
21387+
);
21388+
files
21389+
};
21390+
for manifest_overrides in [
21391+
json!({ "left-pad": url }),
21392+
json!({ "foo": { "left-pad": url } }),
21393+
json!({ "left-pad": "github:someone/left-pad" }),
21394+
] {
21395+
let r = rewrite_registry_redirect(&files_with(manifest_overrides.clone()), &overrides);
21396+
assert!(r.files.is_empty(), "{manifest_overrides}: {:?}", r.edits);
21397+
let skipped = r
21398+
.warnings
21399+
.iter()
21400+
.find(|w| w.code == "redirect_npm_non_registry_entry_skipped")
21401+
.unwrap_or_else(|| panic!("{manifest_overrides}: {:?}", r.warnings));
21402+
assert!(
21403+
skipped.detail.contains("overrides"),
21404+
"the warning names the override: {}",
21405+
skipped.detail
21406+
);
21407+
assert!(
21408+
r.bundled_skipped_uuids.contains(&overrides[0].patch_uuid),
21409+
"{manifest_overrides}: the skipped copy keeps the patch out of the in-run VEX"
21410+
);
21411+
}
21412+
// Control: a registry override (or none) still redirects.
21413+
for manifest_overrides in [json!({}), json!({ "left-pad": "1.3.0" })] {
21414+
let r = rewrite_registry_redirect(&files_with(manifest_overrides.clone()), &overrides);
21415+
let rewritten: Value = serde_json::from_str(&r.files["package-lock.json"]).unwrap();
21416+
assert_eq!(
21417+
rewritten["packages"]["node_modules/left-pad"]["resolved"],
21418+
"http://patch.test/lp.tgz",
21419+
"{manifest_overrides}"
21420+
);
21421+
}
21422+
}
21423+
2134321424
#[test]
2134421425
fn npm_non_registry_entries_are_skipped_with_loud_warning() {
2134521426
let url = "https://registry.npmjs.org/left-pad/-/left-pad-1.3.0.tgz";

‎crates/socket-patch-core/src/vendor/npm_lock.rs‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2825,6 +2825,56 @@ mod tests {
28252825
);
28262826
}
28272827

2828+
/// #1965: a registry range the root `overrides` swap for a remote
2829+
/// tarball url is installed from that url by `npm ci`, so vendoring
2830+
/// the copy would report `applied` over unpatched bytes. With no other
2831+
/// copy the vendor refuses and writes nothing.
2832+
#[tokio::test]
2833+
async fn issue_1965_a_registry_edge_overridden_to_a_url_is_refused() {
2834+
let url = "https://registry.npmjs.org/left-pad/-/left-pad-1.3.0.tgz";
2835+
let lock = json!({
2836+
"name": "fixture",
2837+
"version": "1.0.0",
2838+
"lockfileVersion": 3,
2839+
"packages": {
2840+
"": { "name": "fixture", "version": "1.0.0",
2841+
"dependencies": { "foo": "file:foo-1.0.0.tgz" } },
2842+
"node_modules/foo": {
2843+
"version": "1.0.0",
2844+
"resolved": "file:foo-1.0.0.tgz",
2845+
"dependencies": { "left-pad": "^1.3.0" }
2846+
},
2847+
"node_modules/left-pad": {
2848+
"version": "1.3.0",
2849+
"resolved": url,
2850+
"integrity": "sha512-orig=="
2851+
}
2852+
}
2853+
});
2854+
let fx = fixture_with("left-pad", "1.3.0", lock.clone()).await;
2855+
tokio::fs::write(
2856+
fx.root().join("package.json"),
2857+
format!(
2858+
r#"{{"name":"fixture","version":"1.0.0","dependencies":{{"foo":"file:foo-1.0.0.tgz"}},"overrides":{{"left-pad":"{url}"}}}}"#
2859+
),
2860+
)
2861+
.await
2862+
.unwrap();
2863+
expect_refused(fx.vendor(false).await, "vendor_lock_entry_not_rewritable");
2864+
assert_eq!(fx.read_lock().await, lock, "nothing is written");
2865+
2866+
// Control: a registry override is vendored.
2867+
tokio::fs::write(
2868+
fx.root().join("package.json"),
2869+
r#"{"name":"fixture","version":"1.0.0","dependencies":{"foo":"file:foo-1.0.0.tgz"},"overrides":{"left-pad":"1.3.0"}}"#,
2870+
)
2871+
.await
2872+
.unwrap();
2873+
let (result, entry, _warnings) = expect_done(fx.vendor(false).await);
2874+
assert!(result.success, "{:?}", result.error);
2875+
assert!(entry.is_some());
2876+
}
2877+
28282878
#[tokio::test]
28292879
async fn non_registry_only_instances_refuse_and_write_nothing() {
28302880
let url = "https://registry.npmjs.org/left-pad/-/left-pad-1.3.0.tgz";

0 commit comments

Comments
 (0)