Skip to content

Commit 412796f

Browse files
Add an experimental patch mirror for hosted mode behind an internal artifact host (#2180)
* Add an experimental patch mirror for hosted mode behind an internal artifact host Hosted mode pins each patched dependency to a URL on Socket's patch server. A build that may only download from an internal artifact host cannot fetch from there. This adds an experimental setting that names a mirror of the patch server (a caching proxy, such as an Artifactory generic remote repository). Hosted mode then writes its pins under that base, with the same path and the same integrity. The setting is SOCKET_PATCH_MIRROR_URL, or the flag --patch-mirror-url, which is hidden from --help. It is for Socket Enterprise customers and is outside the semver policy while it is experimental. A repository file cannot set it. With the setting: - scan and get in hosted mode move each granted reference onto the mirror. A Go reference is untouched (its pin is a module path). Cargo and NuGet are skipped with redirect_mirror_unsupported, because their index names the patch server's own download URLs. - Before a new mirrored pin is written, the run sends one HEAD for it and follows no redirect. 2xx pins. 401, 403, 407 or a redirect pins with a redirect_mirror_unverified warning. Anything else skips that pin with redirect_mirror_unavailable. SOCKET_PATCH_MIRROR_AUTHORIZATION (env only) authenticates the check and is scrubbed from child processes. - The run's own downloads still read the patch server. - list, vex, rollback, remove, vendor and repair read a pin on the mirror's origin as a hosted patch. - yarn berry, Bun, Bundler, Poetry and PDM cannot move a pin that is already on the patch server in place. They warn redirect_mirror_pin_not_moved and name the remedy. For every hosted run, mirror or not: a pin of the same patch on a host the run was not given is left alone (redirect_pin_on_unconfigured_host), on disk and in the in-memory engine. A run without the setting never moves a mirrored pin back onto the patch server. Without the setting nothing else changes. Tests: unit tests for the base parser, the candidate rewrite and the guard. tests/patch_mirror.rs (hermetic) covers what a live mirror cannot show. tests/e2e_artifactory_mirror.rs (ignored, live) pins a free patch through a real Artifactory and installs it with npm, pnpm, yarn classic, yarn berry, pip, uv and Bundler. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * Apply the code review of the patch mirror - A mirror is matched by its base, not its host. An allowlist entry for a mirror (`mirror+<base>`) holds only the URLs at or below the base, so a URL in another repository of the mirror's host is not a hosted pin for list, vex, rollback or the unknown-host guard. A --patch-server-url entry still holds its whole origin. - The yarn berry, Bun, Poetry and PDM rewriters take over an earlier pin under a mirror only when it sits under the mirror's base. A fork of the same name and version elsewhere on that host stays the project's own source. - With a mirror, the patch server is always the first origin, so an upstream restore (the yarn berry checksum) never asks the mirror. - The unknown-host guard reads a composer.lock with escaped slashes as the URL it is. A project pinned on the patch server that way was skipped as pinned on a host called "https". - The mirror check stops at the first request that gets no answer and withholds the pins it has not asked about, instead of waiting out the timeout for each one. It builds one HTTP client for the run. - A pin the mirror refused stays withheld when a later request is answered 401, 403, 407 or a redirect; the warning says so. - A re-run does not ask the mirror again about a pin a composer.lock holds with escaped slashes. - apply ignores a mirror base it cannot use instead of exiting 2: it does not use the mirror and runs unattended from install hooks. - The Cargo and NuGet skip no longer says the package stays unpatched. - The live suite's yarn berry and Bundler legs no longer depend on CI defaults (immutable installs, lockfile checksums before Bundler 4). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * Pass the mirror sources in the pixi stale-install test The test landed on main while this branch changed the function it calls. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * Use the shared envelope reader and break a module import cycle Two repository hygiene tests failed in CI: - The two new test files defined their own `warning_codes`. They read the envelope through `common/envelope.rs` now. - `vlt_heal` imported `hosted_unwind`, which imports it. It builds its own origin list again, with the mirror listed as a mirror's base. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * Compare parsed URLs in the mirror-base check, and test it for Bun and yarn berry Review feedback on the check that an earlier pin sits under the mirror's base before a rewriter takes it over: - It compared text, so `<base>/../other/x` passed as a URL under the base. It now reads both URLs as a client does: the earlier pin in any spelling a lock uses, with `.` and `..` segments resolved. - Poetry and PDM had a test for it. Bun and yarn berry call the same check and had none; they have one now. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
1 parent e6c5f50 commit 412796f

28 files changed

Lines changed: 3851 additions & 58 deletions

‎crates/socket-patch-cli/CLI_CONTRACT.md‎

Lines changed: 30 additions & 6 deletions
Large diffs are not rendered by default.

‎crates/socket-patch-cli/src/args.rs‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,26 @@ pub struct GlobalArgs {
172172
)]
173173
pub patch_server_url: Option<String>,
174174

175+
/// Base URL of a mirror of Socket's patch server: a caching proxy in
176+
/// front of https://patch.socket.dev, such as an Artifactory remote
177+
/// repository. Hosted mode writes its patch references under this base
178+
/// instead of the patch server's own host (same path), and every
179+
/// command accepts lockfile URLs on this origin as hosted patch
180+
/// references. Set it the same way wherever socket-patch runs for the
181+
/// project.
182+
//
183+
// EXPERIMENTAL (hidden from `--help`): for organizations that install
184+
// through their own artifact host. The flag, its variables and its
185+
// warning codes carry no stability guarantee yet; see "Patch mirror
186+
// (experimental)" in CLI_CONTRACT.md.
187+
#[arg(
188+
help_heading = GLOBAL_OPTIONS,
189+
long = "patch-mirror-url",
190+
env = "SOCKET_PATCH_MIRROR_URL",
191+
hide = true
192+
)]
193+
pub patch_mirror_url: Option<String>,
194+
175195
/// Disable network access; fail if remote data is needed.
176196
#[arg(
177197
help_heading = GLOBAL_OPTIONS,
@@ -626,6 +646,42 @@ impl GlobalArgs {
626646
.map_or_else(|| self.telemetry_auth(), TelemetryAuth::for_client)
627647
}
628648

649+
/// Refuse a `--patch-mirror-url` that cannot prefix a committed URL,
650+
/// once, before any command reads the project. The message says what
651+
/// is wrong and never repeats the value: a base given with credentials
652+
/// in it must not be echoed into a terminal or a CI log.
653+
pub fn validate_patch_mirror(&self) -> Result<(), String> {
654+
let Some(raw) = self
655+
.patch_mirror_url
656+
.as_deref()
657+
.filter(|url| !url.trim().is_empty())
658+
else {
659+
return Ok(());
660+
};
661+
socket_patch_core::patch::redirect::mirror::PatchMirror::parse(raw)
662+
.map(|_| ())
663+
.map_err(|why| {
664+
format!(
665+
"--patch-mirror-url (or SOCKET_PATCH_MIRROR_URL) is not a usable patch \
666+
mirror base URL: {why}"
667+
)
668+
})
669+
}
670+
671+
/// The patch-server mirror this run was given (`--patch-mirror-url`).
672+
/// [`Self::validate_patch_mirror`] refused a base that does not parse
673+
/// before the command started; a struct literal with one has no mirror.
674+
pub(crate) fn patch_mirror(
675+
&self,
676+
) -> Option<socket_patch_core::patch::redirect::mirror::PatchMirror> {
677+
self.patch_mirror_url
678+
.as_deref()
679+
.filter(|url| !url.trim().is_empty())
680+
.and_then(|url| {
681+
socket_patch_core::patch::redirect::mirror::PatchMirror::parse(url).ok()
682+
})
683+
}
684+
629685
/// The vendoring-service config every vendor entry point (`vendor`,
630686
/// `scan`/`get --mode vendored`) builds from the same flags, so they
631687
/// commit byte-identical artifacts for the same patch. Clap validated
@@ -728,6 +784,7 @@ pub const GLOBAL_ARG_ENV_VARS: &[&str] = &[
728784
"SOCKET_VENDOR_SOURCE",
729785
"SOCKET_VENDOR_URL",
730786
"SOCKET_PATCH_SERVER_URL",
787+
"SOCKET_PATCH_MIRROR_URL",
731788
"SOCKET_OFFLINE",
732789
"SOCKET_STRICT",
733790
"SOCKET_GLOBAL",
@@ -800,6 +857,7 @@ impl Default for GlobalArgs {
800857
maven_config: None,
801858
vendor_url: None,
802859
patch_server_url: None,
860+
patch_mirror_url: None,
803861
offline: false,
804862
strict: false,
805863
allow_patch_conflicts: false,

‎crates/socket-patch-cli/src/commands/agent_download.rs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,9 @@ pub struct DownloadParams {
398398
/// `--patch-server-url`: the extra origin whose URLs count as hosted
399399
/// when lockfile discovery reads the project's hosted pins.
400400
pub patch_server_url: Option<String>,
401+
/// `--patch-mirror-url`: the patch-server mirror whose origin counts as
402+
/// hosted the same way.
403+
pub patch_mirror_url: Option<String>,
401404
}
402405

403406
impl DownloadParams {
@@ -1202,8 +1205,10 @@ pub(crate) async fn vendored_download_refusals(
12021205
prior: Option<&crate::ecosystem_dispatch::NpmCrawlSnapshot>,
12031206
) -> VendorRefusals {
12041207
let cwd = params.cwd.as_path();
1205-
let origins =
1206-
crate::commands::hosted_unwind::patch_server_origins_of(params.patch_server_url.as_deref());
1208+
let origins = crate::commands::hosted_unwind::patch_server_origins_of(
1209+
params.patch_server_url.as_deref(),
1210+
params.patch_mirror_url.as_deref(),
1211+
);
12071212
let pins = hosted_pins(cwd, origins.clone()).await;
12081213
let options = params.crawler_options();
12091214
let installed = crate::commands::vendor::installed_lookup(&options, prior);

‎crates/socket-patch-cli/src/commands/get.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2321,6 +2321,7 @@ fn get_download_params(args: &GetArgs, save_only: bool, persist_blobs: bool) ->
23212321
ecosystems: args.common.ecosystems.clone(),
23222322
persist_blobs,
23232323
patch_server_url: args.common.patch_server_url.clone(),
2324+
patch_mirror_url: args.common.patch_mirror_url.clone(),
23242325
}
23252326
}
23262327

@@ -4209,6 +4210,7 @@ mod tests {
42094210
ecosystems: None,
42104211
persist_blobs: false,
42114212
patch_server_url: None,
4213+
patch_mirror_url: None,
42124214
}
42134215
}
42144216

@@ -4960,6 +4962,7 @@ mod tests {
49604962
// The vendor-detached posture this fn exists for.
49614963
persist_blobs: false,
49624964
patch_server_url: None,
4965+
patch_mirror_url: None,
49634966
}
49644967
}
49654968

‎crates/socket-patch-cli/src/commands/hosted_unwind.rs‎

Lines changed: 78 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,53 @@ impl HostedLegOutcome {
4747
}
4848

4949
/// The patch-server origins that count as hosted, besides Socket's own:
50-
/// the operator's `--patch-server-url` (discovery's allowlist).
50+
/// the operator's `--patch-server-url` and the origin of their
51+
/// `--patch-mirror-url` (discovery's allowlist).
5152
pub(crate) fn patch_server_origins(common: &GlobalArgs) -> Vec<String> {
52-
patch_server_origins_of(common.patch_server_url.as_deref())
53+
patch_server_origins_of(
54+
common.patch_server_url.as_deref(),
55+
common.patch_mirror_url.as_deref(),
56+
)
5357
}
5458

55-
/// [`patch_server_origins`] from the `--patch-server-url` value itself, for
56-
/// callers that carry it outside a [`GlobalArgs`].
57-
pub(crate) fn patch_server_origins_of(patch_server_url: Option<&str>) -> Vec<String> {
58-
patch_server_url
59-
.into_iter()
60-
.filter(|url| !url.trim().is_empty())
61-
.map(str::to_string)
62-
.collect()
59+
/// [`patch_server_origins`] from the `--patch-server-url` and
60+
/// `--patch-mirror-url` values themselves, for callers that carry them
61+
/// outside a [`GlobalArgs`].
62+
///
63+
/// * `--patch-server-url` is listed as given, and holds its whole origin.
64+
/// * The mirror is listed as a mirror's base
65+
/// ([`socket_patch_core::origin::mirror_entry`]), which holds only what
66+
/// is under that base: a mirrored reference is the patch server's own
67+
/// path under it, and the rest of the mirror's host is not the mirror. A
68+
/// base that does not parse ([`PatchMirror::parse`]) is not listed.
69+
/// * With a mirror, the FIRST entry is always the patch server (the
70+
/// operator's, else Socket's own): an upstream restore downloads from the
71+
/// first entry, and what it downloads is not a patch the mirror was
72+
/// checked for, nor something the run holds the mirror's credentials for.
73+
pub(crate) fn patch_server_origins_of(
74+
patch_server_url: Option<&str>,
75+
patch_mirror_url: Option<&str>,
76+
) -> Vec<String> {
77+
use socket_patch_core::patch::redirect::mirror::PatchMirror;
78+
fn given(url: Option<&str>) -> Option<&str> {
79+
url.map(str::trim).filter(|url| !url.is_empty())
80+
}
81+
let server = given(patch_server_url).map(str::to_string);
82+
let mirror = given(patch_mirror_url)
83+
.and_then(|url| PatchMirror::parse(url).ok())
84+
.map(|mirror| socket_patch_core::origin::mirror_entry(mirror.base()));
85+
match mirror {
86+
None => server.into_iter().collect(),
87+
Some(mirror) => vec![
88+
server.unwrap_or_else(|| {
89+
format!(
90+
"https://{}",
91+
socket_patch_core::origin::SOCKET_PATCH_SERVER_HOST
92+
)
93+
}),
94+
mirror,
95+
],
96+
}
6397
}
6498

6599
/// The gem leg of the unwind's installed-tree care (the "Gem stale-install
@@ -291,3 +325,37 @@ pub(crate) async fn run_hosted_leg(
291325
.extend(crate::commands::vlt_heal::rollback_heal(common, &unwound).await);
292326
out
293327
}
328+
329+
#[cfg(test)]
330+
mod tests {
331+
use super::patch_server_origins_of;
332+
333+
/// The list every reader recognizes hosted pins against: the patch
334+
/// server as given, the mirror as a mirror's base, and with a mirror
335+
/// the patch server first, because an upstream restore downloads from
336+
/// the first entry and must never ask the mirror.
337+
#[test]
338+
fn the_patch_server_comes_first_and_the_mirror_is_its_base() {
339+
let mirror = "https://repo.example/artifactory/socket-patch";
340+
let listed = format!("mirror+{mirror}");
341+
assert!(patch_server_origins_of(None, None).is_empty());
342+
assert!(patch_server_origins_of(Some(" "), Some("")).is_empty());
343+
assert_eq!(
344+
patch_server_origins_of(Some("http://127.0.0.1:4545"), None),
345+
["http://127.0.0.1:4545"]
346+
);
347+
assert_eq!(
348+
patch_server_origins_of(None, Some(&format!("{mirror}/"))),
349+
["https://patch.socket.dev", listed.as_str()]
350+
);
351+
assert_eq!(
352+
patch_server_origins_of(Some("http://127.0.0.1:4545"), Some(mirror)),
353+
["http://127.0.0.1:4545", listed.as_str()]
354+
);
355+
// A base that does not parse is no mirror: it is not listed, and
356+
// its userinfo never becomes an accepted origin.
357+
assert!(
358+
patch_server_origins_of(None, Some("https://user:secret@repo.example/x")).is_empty()
359+
);
360+
}
361+
}

0 commit comments

Comments
 (0)