Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
fd06421
Pin a Maven patch only where Maven selects its base version
mikolalysenko Nov 16, 2026
abfabe0
Restore the line breaks of two vendored Maven warnings
mikolalysenko Nov 16, 2026
6c1467a
Read a dependency a pom declares twice as Maven does
mikolalysenko Nov 16, 2026
d5eb740
Pin a vendored Maven patch in the modules that select its version
mikolalysenko Nov 16, 2026
82d9239
Keep vendored Maven pins where they were committed, on every machine
mikolalysenko Nov 16, 2026
fbe0982
Merge the review fixes of the Maven selection work
mikolalysenko Nov 16, 2026
104f968
Never hold a pin that moves a module, and attest only wired modules
mikolalysenko Nov 16, 2026
cf3353c
Keep a Maven selection only while every read it was made from still h…
mikolalysenko Nov 16, 2026
a8d8045
Merge branch 'fix/maven-pin-follows-mediation' into fix/maven-pin-per…
mikolalysenko Nov 16, 2026
7795510
Let a deeper range the model cannot resolve hold the selection back
claude Nov 17, 2026
905c05b
Plan hosted Maven pins over a reactor, and wire ${property} versions
mikolalysenko Nov 17, 2026
9b79a5d
Find and restore hosted Maven pins in every pom of a reactor
mikolalysenko Nov 17, 2026
6ee38a8
Test hosted Maven reactors and property versions against real Maven
mikolalysenko Nov 17, 2026
8d4fbb8
Let a later hosted scan pin the Maven modules an earlier one could no…
mikolalysenko Nov 17, 2026
c377458
Never fail a hosted run on a Maven module pom that cannot be read
mikolalysenko Nov 17, 2026
d879384
Read an earlier release's Maven pin by its shape only in a single pom
mikolalysenko Nov 17, 2026
36c418f
Write a hosted Maven plan whole or not at all, over a reactor read once
mikolalysenko Nov 17, 2026
56ae4ee
Test the hosted Maven invariant against real Maven, and document it
mikolalysenko Nov 17, 2026
7e1f39c
test(maven): pin the hosted property and reactor rewrites in the gold…
mikolalysenko Nov 17, 2026
0d62fd5
Do not wire a hosted Maven pin behind a mirror that would take its re…
mikolalysenko Nov 18, 2026
5370747
Test the hosted Maven mirror guard against real Maven
mikolalysenko Nov 18, 2026
2ce982c
Merge origin/main into fix/maven-hosted-reactors-and-properties
mikolalysenko Nov 18, 2026
b809f08
Merge fix/maven-hosted-reactors-and-properties (with main) into fix/m…
mikolalysenko Nov 18, 2026
dc3ccc2
Never take a reactor pom that is not UTF-8 text for a missing one
mikolalysenko Nov 18, 2026
386d26e
Merge fix/maven-hosted-reactors-and-properties into fix/maven-hosted-…
mikolalysenko Nov 18, 2026
3b8dd67
Merge origin/main into fix/maven-hosted-reactors-and-properties
mikolalysenko Nov 18, 2026
02c2b12
Merge fix/maven-hosted-reactors-and-properties into fix/maven-hosted-…
mikolalysenko Nov 18, 2026
ebd8524
Fix the review findings on the Maven mirror guard
mikolalysenko Nov 19, 2026
ac2213c
Keep the tests' neutral Maven settings away from every real Maven
mikolalysenko Nov 19, 2026
1f0057f
Test that a Bazel child sees no MAVEN_ARGS
mikolalysenko Nov 19, 2026
8641210
Read maven.config as the project's Maven does, and offer vendored mod…
mikolalysenko Nov 19, 2026
28363b9
Merge origin/main into fix/maven-hosted-mirror-guard
claude Nov 19, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions crates/socket-patch-cli/CLI_CONTRACT.md
Original file line number Diff line number Diff line change
Expand Up @@ -2323,6 +2323,7 @@ command's integration tests check its output.
| `redirect_maven_multimodule_unsupported` | `warnings[]` (warning) | scan / get `--mode hosted` (Maven): the root `pom.xml` heads a reactor that cannot be planned: a module pom is missing or not readable as a pom, a `<module>` path is a property or leaves the project, or the grant serves the patched artifact under its original version (a same-GAV grant). The detail carries the reason. The poms and `.mvn/` stay byte-identical and the dep is not counted as redirected. A reactor that can be read is patched module by module: see [Hosted Maven reactors and property versions](#hosted-maven-reactors-and-property-versions). |
| `redirect_maven_pin_would_move_module` | `warnings[]` (warning) | scan / get `--mode hosted` (Maven reactor): a parent pom resolves the base version itself above a module that resolves another version, so the parent takes no pin (Maven's management always reaches the children); the modules below it that select the base version are pinned. Also reported when a pin an earlier run wrote is taken out because a module it reaches now resolves another version, which the pin would hold back (the `redirect_maven_dep_management` edit has `action: "removed"`). Exit 0. |
| `redirect_maven_edit_not_reversible` | `warnings[]` (warning) | scan / get `--mode hosted` (Maven): a pom would take an edit that `rollback` could not undo byte for byte from the pom alone. A pin in a `<dependencyManagement>` that holds no entry (which would be left changed or removed) leaves that one pom as it is: the modules it would have wired stay unpatched, and `vex` withholds the statement while one of them resolves the base version. Any other case (a `<repositories>` section that would not come back as it was) refuses the whole plan for the patch: nothing is written and the dep is not counted as redirected. Exit 0. See [Hosted Maven reactors and property versions](#hosted-maven-reactors-and-property-versions). |
| `redirect_maven_mirror_captures_repository` | `warnings[]` (warning) | scan / get `--mode hosted` (Maven, on disk): the Maven settings of the scanning machine declare a `<mirror>` whose `<mirrorOf>` matches the `socket-patch-<uuid>` repository the pin needs (`*`, `external:*`, or a list without `!socket-patch-<uuid>`), without naming that repository itself (alone, or as one id of a list). The mirror does not hold the patched version, so the build could not resolve it: nothing is written for the dep, `pom.xml` and `.mvn/` stay byte-identical, and the dep is not counted as redirected or attested by the same run's `--vex`, whatever an earlier run left in the poms. Exit 0. Reported only for a dep that would get (or already has) the repository. The detail names the mirror id, its `<mirrorOf>`, the settings file and the exclusion to add, and says that only this machine's settings were read. It offers `scan --mode vendored` with its bound: vendored mode gets past a mirror through `maven.repo.local.tail`, which Maven reads from 3.9.2 on. When the project's Maven Wrapper pins an older Maven the detail does not offer it as the project stands, and says what makes it work (a wrapper at 3.9.2 or later, or the mirror also leaving `socket-patch-vendor` out). See [Hosted Maven behind a mirror](#hosted-maven-behind-a-mirror) |
| `redirect_gem_no_checksums_section` | `warnings[]` (warning) | scan/get `--mode hosted` (gem): the lock has no `CHECKSUMS` section (Bundler < 2.6 does not write one), so it cannot record the sha256 of the patched gems. They are redirected all the same (manifest and lock converged, frozen installs accept the pair) and pinned by source alone. Reported once per run, naming the gems; relock with Bundler ≥ 2.6 (`bundle lock --add-checksums`) and re-run to pin the checksums too. |
| `redirect_gem_merged_lock_layout` | `warnings[]` (warning) | scan/get `--mode hosted` (gem): the lock was written by Bundler < 2.2 (`BUNDLED WITH`), which lists every gem source in ONE `GEM` section, so this run added the patch registry to that section's `remote:` lines. The pair installs with that Bundler, frozen / deployment mode included. Newer Bundlers deprecate such a section and from 2.5 on refuse the pair under frozen / deployment mode: if a newer Bundler installs the project, run `bundle lock` with it, commit the lock and re-run the hosted scan. Raised only by a run that wrote the lock. |
| `redirect_gem_unrecognized_declaration` | `warnings[]` (warning) | scan/get `--mode hosted` (gem): the gem's declaration cannot take a `source:` option in place (the detail gives the reason). The gem is skipped and the manifest and lock stay byte-identical for it; write the declaration as one `gem "<name>", …` call with literal arguments on a line of its own, or use `--mode vendored` / `--mode agent`. |
Expand Down Expand Up @@ -3011,6 +3012,65 @@ not reach that resolves the base version, or whose selection cannot be told on
the machine that runs `vex`, withholds the attestation, with the module named.
`vendor --check` does not ask this.

### Hosted Maven behind a mirror

A `<mirror>` in Maven's `settings.xml` replaces every repository its
`<mirrorOf>` matches. A `<mirrorOf>*</mirrorOf>` (or `external:*`) therefore
replaces the `socket-patch-<uuid>` repository a hosted pin adds, and the
mirror does not hold the suffixed version: the build fails to resolve it.

Before `scan --mode hosted` writes anything for a Maven patch on disk, it
reads the mirrors Maven is configured with on that machine and evaluates
Maven's own rule against the repository the pin would add (its id
`socket-patch-<patch uuid>`, its `https://` URL, the `default` layout):

* settings read: the user settings (`-s` / `--settings` from `MAVEN_ARGS`,
else from `.mvn/maven.config`, else `<user.home>/.m2/settings.xml`;
`.mvn/maven.config` is read as the project's Maven reads it: one
argument per line under a Maven Wrapper that pins 3.9 or later, so a
settings path may hold spaces, split on whitespace under one that pins
an older Maven, and both ways, each as a way the build may be started,
when no wrapper says which) merged
over the global settings (`-gs` / `--global-settings` likewise, else
`conf/settings.xml` of the Maven Wrapper's unpacked distribution, in
either layout the wrapper unpacks to, else of the `mvn` on `PATH`, else,
with none there, of `$MAVEN_HOME` / `$M2_HOME`; the launcher itself has
ignored those two variables since Maven 3.5): the user's mirrors first,
then the global ones whose id the user does not declare. A mirror with no
`<id>` has Maven's default id, `default`. A `.mvn/settings.xml` in the
project is evaluated the same way as a second set (Maven 4 loads it; a
Maven 3 build is often given it with `-s`);
* a mirror whose whole `<mirrorOf>` is the repository's id wins over every
pattern, and is not a capture: it was written for that repository. Nor is
a mirror that lists the id among others (`central,socket-patch-<uuid>`):
whoever runs it chose to serve that repository there;
* otherwise the first mirror, in order, whose pattern matches: `*`,
`external:*` (not `localhost` / `127.0.0.1`, not `file:`), `external:http:*`
(plain HTTP only, so never the Socket repository), or a comma-separated
list of ids and wildcards in which `!id` excludes that repository. Ids are
whole ids and list segments are compared as written (`*, !id`, with a
space, excludes nothing), as in Maven;
* `<mirrorOfLayouts>` must accept `default`.

When a mirror captures the repository, nothing is written for that dep and
the run reports `redirect_maven_mirror_captures_repository` (exit 0). The
dep is not counted as redirected and the same run's `--vex` does not attest
it, also when a pin an earlier run wrote is still in the poms: that pin
stays as it is, and the build behind the mirror cannot resolve it. No file
is touched for a gated dep, `.mvn/checksums/checksums.sha256` included. The
other patches of the run are unaffected.

The gate applies only where a repository is at stake. A dep the rewriter
would not wire anyway (the build selects another version, no pom can take
the pin) is not gated: it is reported under its own reason, with no advice
about a mirror that plays no part in it.

The repository id is one per patch and Maven has no pattern for a family of
ids, so a mirror can only leave Socket's repositories out one id at a time.
The settings read are the scanning machine's: the detail says so, since the
machine that builds may be configured otherwise. The in-memory engine has no
machine and reads no settings.

### Hosted Maven reactors and property versions

Hosted mode patches a multi-module reactor, and a declaration whose
Expand Down
68 changes: 68 additions & 0 deletions crates/socket-patch-cli/tests/common/hermetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,77 @@ pub fn command(bin: &Path) -> Command {
// binary's exit: lift the short grace it gives telemetry still in
// flight (the send's own 5 s request timeout still ends the wait).
cmd.env("SOCKET_TELEMETRY_GRACE_MS", "30000");
// Hosted mode weighs the mirrors of the machine's Maven settings before
// it adds a repository to a pom. The settings of whoever runs a suite
// (a `mirrorOf=*` in `~/.m2/settings.xml`, or in the installation's)
// must not decide what its scan wires.
cmd.env("MAVEN_ARGS", neutral_maven_args());
cmd
}

/// The Maven settings file every socket-patch child is pointed at: real,
/// valid and empty, so it declares no mirror.
const NEUTRAL_MAVEN_SETTINGS: &str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
<settings xmlns=\"http://maven.apache.org/SETTINGS/1.0.0\"/>\n";

/// The file of [`NEUTRAL_MAVEN_SETTINGS`], written on first use: one per
/// account, in the temp directory, outside every test's scratch tree and
/// never removed, so it outlives every test that was handed its path.
pub fn neutral_maven_settings() -> std::path::PathBuf {
static FILE: std::sync::OnceLock<std::path::PathBuf> = std::sync::OnceLock::new();
let file = FILE.get_or_init(|| {
let account: String = std::env::var("USER")
.or_else(|_| std::env::var("USERNAME"))
.unwrap_or_default()
.chars()
.filter(|c| c.is_ascii_alphanumeric() || *c == '-' || *c == '_')
.collect();
let dir = std::env::temp_dir().join(format!("socket-patch-cli-maven-settings-{account}"));
std::fs::create_dir_all(&dir).expect("create the neutral Maven settings directory");
let file = dir.join("settings.xml");
// Written beside and renamed over, so a suite running at the same
// time never reads half a file.
if std::fs::read_to_string(&file).ok().as_deref() != Some(NEUTRAL_MAVEN_SETTINGS) {
let staged = dir.join(format!("settings.xml.{}", std::process::id()));
std::fs::write(&staged, NEUTRAL_MAVEN_SETTINGS)
.expect("write the neutral Maven settings");
std::fs::rename(&staged, &file).expect("place the neutral Maven settings");
}
file
});
assert!(
file.is_file(),
"the neutral Maven settings file is gone: {}",
file.display()
);
file.clone()
}

/// The `MAVEN_ARGS` of a socket-patch child: [`neutral_maven_settings`] as
/// both the user settings (`-s`) and the global settings (`-gs`).
/// `MAVEN_ARGS` is where the CLI looks for the settings a build would be
/// started with, so with this value it reads no settings of the machine
/// at all.
///
/// The value is for the socket-patch process alone. [`command`] is the
/// only place that sets it, and no test builds a Maven or a Gradle child
/// with [`command`]; `jvm_env::isolate_cli`, which Maven children do go
/// through, removes `MAVEN_ARGS` from a command that does not already
/// carry one. A real Maven reads `MAVEN_ARGS` too: it would take these
/// settings over the ones a test names on its command line, and it refuses
/// to start when a settings file named there does not exist, which is why
/// the file is a real one.
pub fn neutral_maven_args() -> String {
let file = neutral_maven_settings();
let file = file.to_string_lossy();
// (The CLI reads a double-quoted argument as one, on every platform.)
if file.contains(char::is_whitespace) {
format!("-s \"{file}\" -gs \"{file}\"")
} else {
format!("-s {file} -gs {file}")
}
}

/// [`command`] for the `socket-patch` binary cargo built for this test run.
pub fn binary_command() -> Command {
command(Path::new(env!("CARGO_BIN_EXE_socket-patch")))
Expand Down
17 changes: 16 additions & 1 deletion crates/socket-patch-cli/tests/common/jvm_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ pub fn stand_in_home() -> PathBuf {
/// creates), carrying the version-manager roots over. Call it before
/// applying a test's own env.
pub fn isolate_cli(cmd: &mut Command) -> &mut Command {
let carried_maven_args = cmd
.get_envs()
.find(|(key, _)| *key == std::ffi::OsStr::new("MAVEN_ARGS"))
.and_then(|(_, value)| value.map(std::ffi::OsStr::to_os_string));
for key in AMBIENT {
cmd.env_remove(key);
}
Expand All @@ -106,5 +110,16 @@ pub fn isolate_cli(cmd: &mut Command) -> &mut Command {
let home = stand_in_home();
cmd.env("HOME", &home)
.env("USERPROFILE", &home)
.env("GRADLE_USER_HOME", home.join(".gradle"))
.env("GRADLE_USER_HOME", home.join(".gradle"));
// The one variable of [`AMBIENT`] a command may already carry on
// purpose: `hermetic::command` gives the socket-patch binary a
// `MAVEN_ARGS` that names an empty settings file, so the mirrors of the
// machine's Maven settings decide nothing. That value is put back. A
// command that carries none (every Maven child: this function is also
// how a real `mvn` is isolated) gets none, so no real Maven is ever
// started with settings a test did not name.
if let Some(args) = carried_maven_args {
cmd.env("MAVEN_ARGS", args);
}
cmd
}
48 changes: 48 additions & 0 deletions crates/socket-patch-cli/tests/common_selftests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,54 @@ mod jvm_env_selftests {
assert!(std::path::Path::new(&home).is_dir());
}

/// The neutral Maven settings can never break a real Maven. The file
/// the socket-patch child is pointed at exists and is a valid, empty
/// settings file (a real Maven refuses to start on a settings file
/// that is named and missing); only a command built for the
/// socket-patch binary carries it; and a Maven child, which is isolated
/// through the same `isolate_cli`, is started with no `MAVEN_ARGS` at
/// all, whatever the test process's environment holds.
#[test]
fn the_neutral_maven_settings_are_a_real_file_and_never_reach_a_maven_child() {
use crate::common::hermetic;
let maven_args = |cmd: &Command| -> Option<Option<String>> {
cmd.get_envs()
.find(|(k, _)| *k == "MAVEN_ARGS")
.map(|(_, v)| v.map(|v| v.to_string_lossy().into_owned()))
};
let settings = hermetic::neutral_maven_settings();
assert!(settings.is_absolute(), "{}", settings.display());
assert!(settings.is_file(), "{}", settings.display());
let text = std::fs::read_to_string(&settings).unwrap();
assert!(
text.contains("<settings") && !text.contains("<mirror"),
"{text}"
);
// Handed out again, it is the same file, still there.
assert_eq!(hermetic::neutral_maven_settings(), settings);

// The socket-patch child: both settings files are that one.
let mut cli = hermetic::command(std::path::Path::new("socket-patch"));
let args = hermetic::neutral_maven_args();
assert_eq!(maven_args(&cli), Some(Some(args.clone())));
let path = settings.to_string_lossy();
assert_eq!(args.matches(path.as_ref()).count(), 2, "{args}");
assert!(args.starts_with("-s ") && args.contains(" -gs "), "{args}");
// ... and it survives the JVM isolation, which removes the
// ambient value.
isolate_cli(&mut cli);
assert_eq!(maven_args(&cli), Some(Some(args)));

// A Maven child is not built by `hermetic::command`: it gets none,
// and an ambient one is removed.
let mut mvn = Command::new("mvn");
isolate_cli(&mut mvn);
assert_eq!(maven_args(&mvn), Some(None));
// A test's own value, set after the isolation, still lands last.
mvn.env("MAVEN_ARGS", "-o");
assert_eq!(maven_args(&mvn), Some(Some("-o".to_string())));
}

/// Without an explicit cache the Gradle user home is the stand-in's
/// `.gradle`: the CLI resolves Gradle's home from the passwd entry on
/// Unix, so pinning `HOME` alone would leak the real `~/.gradle`.
Expand Down
8 changes: 8 additions & 0 deletions crates/socket-patch-cli/tests/e2e_bazel_build/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ const REMOVED: &[&str] = &[
"PYTHONHOME",
"PYTHONPATH",
"PYTHONSTARTUP",
// Maven: `hermetic::command` gives the socket-patch binary a
// `MAVEN_ARGS` that names an empty settings file, so the machine's
// mirrors decide nothing. It is that process's alone: a Maven a
// ruleset started would take those settings over its own.
"MAVEN_ARGS",
// rules_rust: its switch for a repin of a crate hub, which it reads
// under this name and under `CARGO_BAZEL_REPIN` (a prefix below).
"REPIN",
Expand Down Expand Up @@ -1070,6 +1075,9 @@ fn settings_of_a_shell() -> Vec<String> {
"PYTHONHOME",
"PYTHONPATH",
"PYTHONSTARTUP",
// (Also what `hermetic::command` itself sets for the socket-patch
// binary: neither the shell's value nor that one reaches a child.)
"MAVEN_ARGS",
"REPIN",
// One name under each of `REMOVED_PREFIXES`.
"BAZELISK_SETTING",
Expand Down
Loading
Loading