From ec89bd92d95bc55cb8312e7147755934647cc084 Mon Sep 17 00:00:00 2001 From: Mikola Lysenko Date: Wed, 4 Nov 2026 11:58:11 -0600 Subject: [PATCH] Resolve Maven pin properties through profiles and nesting Closes the two Maven false-attestation gaps #2005 left open in VEX discovery. 1. Profile redefining a pin's property. A live ${v} that the root resolve to the Socket pin resolves something else whenever an active profile's redefine v (or a property v names): Maven merges active profile properties over the pom's own before interpolating. Vendored mode refuses to pin over such a property (version_override); discovery attested it. PomScope now reads each profile's , and every PomDep carries version_alternatives: the other versions its build_version takes under any profile or set of profiles active together (user properties and project.* model references stay fixed; a value some set leaves unresolved, cyclic or too deep counts as the text as written). profile_override now walks live main-jar declarations too (same main_jar_declarations scope and management-governs filter): one whose build version is the pin but has an alternative off it marks the ref unattested with the existing UnattestedKind::MavenProfileUnpatched (CLI note and CLI_CONTRACT.md updated), in hosted and the pre-v5 vendored wiring alike. Profile declarations and the shared classifier check (unpatched_classifier_copy) count every alternative as a version the declaration may resolve. A declaration's own ${v} reads its own profile's first. 2. Property versions resolved one level deep. ${a} with ${b} now resolves through every property it names, in both views PomDep keeps: `version` (as the pom spells it: model, own profile, root properties) and `build_version` (.mvn/maven.config user properties first at every level; project.* stays the model's). Limits are the vendored reactor's: at most 16 levels, and a cycle, undefined name or unterminated ${ never resolves (the version stays as written, which is never a pin). A partly literal version (${a}.${b}-x) resolves too, and ${project.version} resolves from the model in both views. The vendored reactor's interpolator (interpolate_by and the duplicate body of Reactor::interpolate_with) moves to formats/maven as the pure interpolate_strict, built on interpolate_each, which enumerates the values a name may take (capped at 16 results). Both now detect cycles explicitly and bound work (1024 lookups, 4096 bytes), so a pom whose properties each name the next many times over fails closed instead of expanding exponentially. Reactor::interpolate drops its always-zero depth argument. Vendored behavior is otherwise unchanged. Tests (each fails before this change): - vex::discover::maven::tests::profile_properties_redefining_a_pin_property_are_not_attested - vex::discover::maven::tests::profile_declaration_reads_its_own_profile_properties - vex::discover::maven::tests::version_property_resolves_through_nested_properties (16-deep chain resolves, 17 does not; two cycles; nested user overrides) - vex::discover::maven::tests::vendored_ref_under_profile_properties_is_not_attested - formats::maven::tests::{version_properties_resolve_recursively_and_fail_closed, profile_properties_give_version_alternatives, interpolation_is_bounded} The golden corpus is unchanged. Co-Authored-By: Claude Opus 5.5 --- crates/socket-patch-cli/CLI_CONTRACT.md | 4 +- .../src/commands/vex_sources.rs | 10 +- .../src/formats/maven/mod.rs | 570 ++++++++++++++++-- .../src/vendor/jvm/maven_reactor.rs | 75 +-- .../src/vex/discover/maven.rs | 319 +++++++++- .../socket-patch-core/src/vex/discover/mod.rs | 7 +- 6 files changed, 840 insertions(+), 145 deletions(-) diff --git a/crates/socket-patch-cli/CLI_CONTRACT.md b/crates/socket-patch-cli/CLI_CONTRACT.md index 7d9a61c76..d1e97dfe2 100644 --- a/crates/socket-patch-cli/CLI_CONTRACT.md +++ b/crates/socket-patch-cli/CLI_CONTRACT.md @@ -547,7 +547,7 @@ Recognition rules that hold for every ecosystem: * **Patch hosts.** A hosted reference counts only on `https://patch.socket.dev` or the `--patch-server-url` / `SOCKET_PATCH_SERVER_URL` origin, with no userinfo. The uuid is the URL's LAST canonical-uuid path segment, because grant tokens may themselves be uuid-shaped. The Go module prefix is fixed. `socket-patch-` registry / repository / source names count only through a pin. For a URL on any other host, see **Patch hosts** above. * **Pins, not definitions.** A registry, index or source *definition* alone (cargo `[registries]`, nuget ``, pom ``, uv index tables, `.npmrc`) never makes a reference, because it survives a reverted pin. Sections the package manager ignores are not read: npm's v2 `dependencies` mirror, a `.cargo/config.toml` shadowed by `.cargo/config`. A Socket pin inside a maven `` is diagnosed, never a reference. * **Contested locks.** When one lock wires a package to a patch and another lock resolves the same `name@version` from a non-Socket source, the build's bytes depend on which package manager runs. The reference is then dropped with a `patched_ref_unattributable` diagnostic naming both files. This applies across npm / pnpm / yarn / bun and across uv / pylock / poetry / pdm / Pipfile.lock / requirements. PEP 723 script locks neither contest nor are contested. The root `requirements.txt` and its in-root `-r` includes count as one lock here: pip reads them as one requirement set, where a direct reference wins over a compatible `==` pin of the same version in another file of the set (#1086). A PyPI version matches under PEP 440 equality, so a requirements line pinned in any spelling that can only install the wired release (`==1.2.3.0`, `==v1.2.3`, `===1.2.3`) contests it like `==1.2.3` does; a range or wildcard (`~=1.2.3`, `==1.2.*`) does not (#1602). A **bundled** npm copy (`inBundle: true`, or v1 `bundled: true`, beneath a dependency that bundles it; the root project's own bundles install from the lock, #1618) of the same `name@version` contests the reference too, in the same lock, in the other npm lock of a shrinkwrap/package-lock pair, or in any other lock. npm unpacks it from the parent package's tarball, so no rewire reaches it and it stays unpatched. Bun and vlt unpack bundled copies the same way (#469, #471). pnpm unpacks bundled copies too, but its lock cannot tie one to a reference (see **Unattested references** below). For Bun, that is a `bun.lock` entry whose meta is `{ "bundled": true }`, or a `bun.lockb` record that a dependency edge with the `bundled` behavior bit reaches, including one Bun shares with a regular install. Such an entry is never a reference, and it contests the reference the same way. For vlt, the lock records no node for a bundled copy, so the copy is found in the installed store: a real package directory inside a store package's own `node_modules`. yarn classic's `yarn.lock` records nothing about bundled copies either (#758), so for a lock with a Socket wiring they are found in the installed `node_modules`: each package a parent's `bundleDependencies` / `bundledDependencies` names inside the parent's own `node_modules`, the dependencies of those beside them, and everything nested under them. Such a copy contests the reference in `yarn.lock` (the diagnostic names the parent's lock entry and the copy's directory); a lock-only `vex` with no `node_modules` can't see it. Hosted and vendored scans skip these copies with `redirect_bun_bundled_instance_skipped` / `redirect_vlt_bundled_instance_skipped` / `vendor_bundled_instance_skipped`, and a yarn classic hosted scan names one with `redirect_copy_unpatched`. When a bundled copy is the only instance, vendoring refuses with `vendor_lock_entry_not_rewritable`. A reference withheld only because such an unreachable copy installs beside it (a bundled npm / Bun / vlt copy, an npm copy beneath a `hasShrinkwrap` package (#753) or one npm installs from a git / url / `file:` spec, or a yarn classic git or `file:` directory block of the same `name@version`) is still the rewriter's own wiring of exactly one package version: it is never attested, but `rollback`, `remove`, `list` and the hosted → vendored takeover treat it as an ordinary hosted pin and restore its upstream registry entry (#828), rather than refusing it with `hosted_wiring_contested`. Another entry of the **same** npm, Bun or yarn lock that resolves the wired `name@version` from a non-Socket source (for example a workspace member added after the rewire, then `npm install` / `bun install` / `yarn install`; for yarn berry, a registry locator such as `left-pad@npm:1.3.0` beside the hosted `…::__archiveUrl=` one) contests the reference too (#588). The package manager installs both entries, and that copy stays unpatched. Re-running `scan` / `vendor` rewires every copy. -* **Unattested references.** Some evidence shows a build may run a copy no wiring reaches, but cannot be tied to the reference's exact `name@version` or cannot say the build runs it. The reference then stays a reference: `list`, `rollback` and `remove` find it, and the ledgers' liveness gates (`vendor --check`, `scan`) keep treating the wiring as live, because re-running `scan` / `vendor` could never clear the evidence. Only `vex` omits it, as a run warning and as the `failed[].reason`. Six cases besides Gradle's (`vex_gradle_lock_above_base`): **pnpm bundled copies** (`vex_pnpm_bundled_copy`): a `packages:` entry's `bundledDependencies:` names the copies pnpm unpacks from that package's own tarball, but pnpm never locks them, so the bundled version is not in the lock. A pnpm reference whose package name a `bundledDependencies` list in the same lock names is omitted whatever its version (a missed attestation when the bundled copy is another version, never a false one), and `bundledDependencies: true` (or a value that cannot be read) omits every reference of that lock. It reaches no other lock. **deno.lock** (`vex_deno_lock_copy`): `deno install` installs a `package.json` project's npm dependencies from `deno.lock` and never reads `package-lock.json` / `pnpm-lock.yaml` / `yarn.lock`, so an npm-family reference whose `name@version` the `deno.lock` npm section also locks is omitted (#406). Whether Deno or another package manager populates `node_modules` (`nodeModulesDir: "manual"` allows either) is not in the files, so this holds whatever `nodeModulesDir` says. **Maven reactor roots** (`vex_maven_reactor_root`): a hosted pin in a root `pom.xml` that declares `` / ``, or whose subprojects Maven 4 discovers (see the hosted Maven rewriter), is omitted, because a module's own literal `` overrides a root pin and module poms are not read (#261); hosted scan refuses such roots (`redirect_maven_multimodule_unsupported`), so this only covers pins an older release wrote. **Maven classifier copies** (`vex_maven_classifier_unpatched`): a `` dependency of a hosted-pinned GA is its own coordinate; the classifier-less pin does not retarget it and the grant serves only the main jar, so Maven fetches it from the next repository. When a classifier copy other than `sources` / `javadoc` (`tests`, a native or JDK-specific build), live or inside ``, resolves any version other than the suffixed pin — its own literal ``, a `` entry with the same classifier (a profile's overriding the live one), or no version in the root pom — the pin is omitted. A pre-v5 single-pom vendored repository holds only the main jar, so any such copy omits its reference. `sources` / `javadoc` copies never run on a classpath and do not withhold the statement; hosted scan only warns about them (`redirect_maven_classifier_unsupported`) and skips the dep when an executable copy names the patched release. **Maven profile declarations** (`vex_maven_profile_unpatched`): a `` declaration of a pinned GA's main jar in the root `pom.xml` at any version other than the pin (the base release, another release, a `${property}` the pom does not resolve) overrides the pin whenever that profile is active, so the reference is omitted. A profile `` entry always counts; a profile `` entry counts only when management governs the pin (no direct `` outside ``), since a direct literal version beats management. A declaration without a `` takes the live pin, and a `pom` (or `java-source` / `javadoc`) typed one carries no code; neither withholds the statement. Activation is not evaluated: every profile counts, as in vendored mode, which rewrites profile declarations too; hosted scan leaves them as written (`redirect_maven_profile_dependency_unpatched`). The same holds for a pre-v5 single-pom vendored repository, which serves only the original version. **Maven user properties** (`vex_maven_config_override`): Maven resolves a `${property}` `` from `.mvn/maven.config`'s `-D` user properties before the pom's `` (`${project.*}` / `${pom.*}` always come from the model). A pin the pom spells through a property that `.mvn/maven.config` redefines resolves that other version, so the reference is omitted. A Socket version that only a `-D` user property supplies is not `pom.xml` wiring (hosted scan never writes one, and `rollback` / `remove` never edit `.mvn/maven.config`): it is no reference at all, only a diagnostic. +* **Unattested references.** Some evidence shows a build may run a copy no wiring reaches, but cannot be tied to the reference's exact `name@version` or cannot say the build runs it. The reference then stays a reference: `list`, `rollback` and `remove` find it, and the ledgers' liveness gates (`vendor --check`, `scan`) keep treating the wiring as live, because re-running `scan` / `vendor` could never clear the evidence. Only `vex` omits it, as a run warning and as the `failed[].reason`. Six cases besides Gradle's (`vex_gradle_lock_above_base`): **pnpm bundled copies** (`vex_pnpm_bundled_copy`): a `packages:` entry's `bundledDependencies:` names the copies pnpm unpacks from that package's own tarball, but pnpm never locks them, so the bundled version is not in the lock. A pnpm reference whose package name a `bundledDependencies` list in the same lock names is omitted whatever its version (a missed attestation when the bundled copy is another version, never a false one), and `bundledDependencies: true` (or a value that cannot be read) omits every reference of that lock. It reaches no other lock. **deno.lock** (`vex_deno_lock_copy`): `deno install` installs a `package.json` project's npm dependencies from `deno.lock` and never reads `package-lock.json` / `pnpm-lock.yaml` / `yarn.lock`, so an npm-family reference whose `name@version` the `deno.lock` npm section also locks is omitted (#406). Whether Deno or another package manager populates `node_modules` (`nodeModulesDir: "manual"` allows either) is not in the files, so this holds whatever `nodeModulesDir` says. **Maven reactor roots** (`vex_maven_reactor_root`): a hosted pin in a root `pom.xml` that declares `` / ``, or whose subprojects Maven 4 discovers (see the hosted Maven rewriter), is omitted, because a module's own literal `` overrides a root pin and module poms are not read (#261); hosted scan refuses such roots (`redirect_maven_multimodule_unsupported`), so this only covers pins an older release wrote. **Maven classifier copies** (`vex_maven_classifier_unpatched`): a `` dependency of a hosted-pinned GA is its own coordinate; the classifier-less pin does not retarget it and the grant serves only the main jar, so Maven fetches it from the next repository. When a classifier copy other than `sources` / `javadoc` (`tests`, a native or JDK-specific build), live or inside ``, resolves any version other than the suffixed pin — its own literal ``, a `` entry with the same classifier (a profile's overriding the live one), or no version in the root pom — the pin is omitted. A pre-v5 single-pom vendored repository holds only the main jar, so any such copy omits its reference. `sources` / `javadoc` copies never run on a classpath and do not withhold the statement; hosted scan only warns about them (`redirect_maven_classifier_unsupported`) and skips the dep when an executable copy names the patched release. **Maven profile declarations** (`vex_maven_profile_unpatched`): a `` declaration of a pinned GA's main jar in the root `pom.xml` at any version other than the pin (the base release, another release, a `${property}` the pom does not resolve) overrides the pin whenever that profile is active, so the reference is omitted. A profile `` entry always counts; a profile `` entry counts only when management governs the pin (no direct `` outside ``), since a direct literal version beats management. A declaration without a `` takes the live pin, and a `pom` (or `java-source` / `javadoc`) typed one carries no code; neither withholds the statement. Activation is not evaluated: every profile counts, as in vendored mode, which rewrites profile declarations too; hosted scan leaves them as written (`redirect_maven_profile_dependency_unpatched`). A pin spelled through a property counts the same way: a `${property}` `` is interpolated through every property it names (at most 16 levels deep; a cycle or an undefined name never resolves to a pin), and when any profile's `` — or several profiles' together — redefine one of those properties so that it resolves anything other than the pin (or nothing), the reference is omitted too; a `.mvn/maven.config` user property wins over every profile's. A profile declaration reads its own profile's `` first. The same holds for a pre-v5 single-pom vendored repository, which serves only the original version. **Maven user properties** (`vex_maven_config_override`): Maven resolves a `${property}` `` from `.mvn/maven.config`'s `-D` user properties before the pom's `` (`${project.*}` / `${pom.*}` always come from the model). A pin the pom spells through a property that `.mvn/maven.config` redefines resolves that other version, so the reference is omitted. A Socket version that only a `-D` user property supplies is not `pom.xml` wiring (hosted scan never writes one, and `rollback` / `remove` never edit `.mvn/maven.config`): it is no reference at all, only a diagnostic. * **Shrinkwrap without a twin (#899).** npm >= 12 never reads `npm-shrinkwrap.json`: on a project whose only npm lock is the shrinkwrap it resolves the tree from the registry and writes a fresh `package-lock.json`, so a Socket reference there reaches npm <= 11 only. The reference is still discovered, so `list`, `rollback` and `remove` manage it, but `vex` omits the patch (`vex_npm_shrinkwrap_only`, as a run warning and as the `failed[].reason`). A `package-lock.json` beside it (wired the same way) makes it attestable again. Hosted and vendored runs still rewire the shrinkwrap and warn `redirect_npm_shrinkwrap_only` / `vendor_npm_shrinkwrap_only`. * **Lockless pins.** With no lock to name a version, a `Cargo.toml` pin (every declaration on `socket-patch-`, that registry defined on the patch host for the same uuid) or an exclusive nuget exact-id mapping is never a reference on its own, so v5.0 does not attest it (nor does `list` show it, or `rollback` / `remove` restore it — restore those files from version control). Only a pre-v5 redirect-ledger record naming a version the pin admits keeps it live. The same holds for a gem wired only in the `Gemfile` (the pre-bundler-2.6 mixed state, lock not converged). @@ -2678,7 +2678,7 @@ match it, withholds the statement: | `vex_deno_lock_copy` | run warning and `failed[].reason` | An npm pin is wired, but `deno.lock` locks the same `name@version`, which `deno install` installs without the wiring; no statement while it does. `vendor --check` and `scan` still treat the wiring as live. | | `vex_maven_reactor_root` | run warning and `failed[].reason` | A hosted Maven pin sits in a root `pom.xml` that declares `` / `` (a profile's included), where a module's own literal `` overrides it; only the root pom is read, so no statement (#261). `list`, `rollback` and `remove` still find the pin. | | `vex_maven_classifier_unpatched` | run warning and `failed[].reason` | A hosted Maven pin's GA also has an executable `` copy (not `sources` / `javadoc`; live or inside ``) at a version other than the pin, which the classifier-less pin does not retarget and the grant does not serve; no statement while it does. `list`, `rollback` and `remove` still find the pin. | -| `vex_maven_profile_unpatched` | run warning and `failed[].reason` | A Maven pin's GA main jar is also declared inside `` in the root `pom.xml` at a version other than the pin (a profile `` entry only where management governs the pin), which overrides the pin whenever that profile is active; no statement while it does. `list`, `rollback` and `remove` still find the pin. | +| `vex_maven_profile_unpatched` | run warning and `failed[].reason` | A Maven pin's GA main jar is also declared inside `` in the root `pom.xml` at a version other than the pin (a profile `` entry only where management governs the pin), or a profile's `` redefine a property the pin's `${property}` version names so it resolves another version, which overrides the pin whenever that profile is active; no statement while it does. `list`, `rollback` and `remove` still find the pin. | | `vex_maven_config_override` | run warning and `failed[].reason` | A hosted Maven pin is spelled through a `${property}` that a `-D` user property in `.mvn/maven.config` redefines, so Maven resolves another version; no statement while it does. `list`, `rollback` and `remove` still find the pin. | | `vex_gradle_derived_cache_unchecked` | run warning | The derived-cache walk was cut short (a very large transforms cache); the statement is still emitted, the copies the walk did reach were checked. | diff --git a/crates/socket-patch-cli/src/commands/vex_sources.rs b/crates/socket-patch-cli/src/commands/vex_sources.rs index 322b05f49..51c98e35b 100644 --- a/crates/socket-patch-cli/src/commands/vex_sources.rs +++ b/crates/socket-patch-cli/src/commands/vex_sources.rs @@ -233,7 +233,8 @@ const NOTE_MAVEN_REACTOR_ROOT: &str = "vex_maven_reactor_root"; /// (`vex::Unattested`). const NOTE_MAVEN_CLASSIFIER_UNPATCHED: &str = "vex_maven_classifier_unpatched"; /// Omission tag and note: a hosted Maven pin's GA is also declared inside -/// `` at another version, which overrides the pin whenever that +/// `` at another version, or a profile's `` move +/// the pin's `${property}` version, which overrides the pin whenever that /// profile is active (`vex::Unattested`). const NOTE_MAVEN_PROFILE_UNPATCHED: &str = "vex_maven_profile_unpatched"; /// Omission tag and note: a hosted Maven pin is spelled through a @@ -278,9 +279,10 @@ fn unattested_note(kind: UnattestedKind) -> (&'static str, &'static str) { ), UnattestedKind::MavenProfileUnpatched => ( NOTE_MAVEN_PROFILE_UNPATCHED, - "not attested while a profile declares it at another version; the wiring itself is \ - intact, so re-running `scan` does not change this (drop that profile or \ - set it to the pin, or re-patch with `scan --mode vendored`)", + "not attested while a profile declares it at, or its move it to, \ + another version; the wiring itself is intact, so re-running `scan` does not change \ + this (drop that profile or property or set it to the pin, or re-patch \ + with `scan --mode vendored`)", ), UnattestedKind::MavenConfigOverride => ( NOTE_MAVEN_CONFIG_OVERRIDE, diff --git a/crates/socket-patch-core/src/formats/maven/mod.rs b/crates/socket-patch-core/src/formats/maven/mod.rs index 6625d3e1e..6c8f99209 100644 --- a/crates/socket-patch-core/src/formats/maven/mod.rs +++ b/crates/socket-patch-core/src/formats/maven/mod.rs @@ -2,7 +2,7 @@ //! [`parse_pom`]), the Maven coordinate grammars and the hosted //! `-socket.` version split. Pure: text in, structure out. -use std::collections::BTreeMap; +use std::collections::{BTreeMap, BTreeSet}; use super::xml::{blank_non_markup, child_text, elements, open_tags, Element}; @@ -41,15 +41,27 @@ pub(crate) struct PomRepo { pub(crate) struct PomDep { pub(crate) group: String, pub(crate) artifact: String, - /// Trimmed literal version, `${prop}` resolved (one level) from the - /// root `` when possible: the version the pom itself - /// spells, which the hosted rewriter writes and rolls back. `None` when - /// the declaration has none. + /// Trimmed version, every `${prop}` interpolated + /// ([`interpolate_strict`]) from the project model (`project.*` / + /// `pom.*`), the `` of the profile holding the declaration + /// (if any), then the root ``: the version the pom itself + /// spells, which the hosted rewriter writes and rolls back. As written + /// when that fails (undefined, cyclic, too deep); `None` when the + /// declaration has none. pub(crate) version: Option, - /// [`Self::version`] as the build resolves it: `${prop}` resolved from - /// the build's user properties first (`.mvn/maven.config`'s `-D`), - /// else the root `` — Maven's own lookup order. + /// [`Self::version`] as the build resolves it: the build's user + /// properties (`.mvn/maven.config`'s `-D`) come before the profile's + /// and the root `` at every level — Maven's own lookup + /// order (the model still comes first). pub(crate) build_version: Option, + /// The trimmed `` text as written. + pub(crate) version_text: Option, + /// The other versions [`Self::build_version`] becomes when some set of + /// profiles is active whose `` redefine a property it + /// names (transitively): a superset ([`interpolate_each`]), plus the + /// text as written when some such set leaves it unresolved. Empty when + /// no profile can move it. + pub(crate) version_alternatives: Vec, /// The non-empty ``: a sibling artifact of the GA (sources, /// tests, a native build), managed and resolved apart from the main jar. pub(crate) classifier: Option, @@ -60,6 +72,10 @@ pub(crate) struct PomDep { pub(crate) in_profile: bool, } +/// One `` inside `` (`name -> trimmed value`), with +/// the `` element that holds it when the scan finds one. +type ProfileProperties = (Option, BTreeMap); + /// Where Maven reads a root pom's model from: its text with comments and /// CDATA blanked (offsets kept), and the sections that are not the /// project's own resolution. `` / `` hold plugin @@ -75,14 +91,18 @@ pub(crate) struct PomScope { profiles: Vec, /// The root `` (see [`properties`]). properties: BTreeMap, - /// [`Self::properties`] with the build's user properties laid over - /// them, except `project.*` / `pom.*`, which Maven reads from the model - /// before any user property: what a `${name}` version resolves to in - /// the build. - build_properties: BTreeMap, - /// [`Self::properties`] plus the `${project.*}` model references a - /// coordinate may name (see [`model_refs`]). + /// The build's user properties, except `project.*` / `pom.*`, which + /// Maven reads from the model before any user property. + user: BTreeMap, + /// The `${project.*}` model references (see [`model_refs`]). + model: BTreeMap, + /// [`Self::properties`] plus the user properties and model references + /// a coordinate may name. coordinate_refs: BTreeMap, + /// Each `` inside ``, with the `` that + /// holds it (when the scan finds one): merged over the root's while + /// that profile is active. + profile_properties: Vec, } impl PomScope { @@ -115,33 +135,125 @@ impl PomScope { .concat(); let profiles = elements(&masked, "profiles")?; let properties = properties(&masked, &ignored, &profiles)?; - let mut build_properties = properties.clone(); - build_properties.extend( - user.iter() - .filter(|(k, _)| !is_model_reference(k)) - .map(|(k, v)| (k.clone(), v.clone())), - ); let mut coordinate_refs = properties.clone(); coordinate_refs.extend(user.iter().map(|(k, v)| (k.clone(), v.clone()))); + let user: BTreeMap = user + .iter() + .filter(|(k, _)| !is_model_reference(k)) + .map(|(k, v)| (k.clone(), v.clone())) + .collect(); + let model = model_refs(&masked, &coordinate_refs)?; // A `project.*` / `pom.*` reference names the model, whatever a // same-named property says; the deprecated bare form yields to one. - for (name, value) in model_refs(&masked, &coordinate_refs.clone())? { - if is_model_reference(&name) { - coordinate_refs.insert(name, value); + for (name, value) in &model { + if is_model_reference(name) { + coordinate_refs.insert(name.clone(), value.clone()); } else { - coordinate_refs.entry(name).or_insert(value); + coordinate_refs.entry(name.clone()).or_insert(value.clone()); } } + let profile_properties = profile_properties(&masked, &ignored, &profiles)?; Ok(PomScope { masked, ignored, profiles, properties, - build_properties, + user, + model, coordinate_refs, + profile_properties, }) } + /// The indices into [`Self::profile_properties`] of the profile that + /// holds `pos` (none outside one). + fn own_profile(&self, pos: usize) -> Vec { + (0..self.profile_properties.len()) + .filter(|&i| { + self.profile_properties[i] + .0 + .is_some_and(|p| pos >= p.start && pos < p.end) + }) + .collect() + } + + /// Whether no profile's `` can change what `${name}` + /// reads: a model reference, or (in the build's view) a user property, + /// both of which Maven looks up first. + fn fixed(&self, name: &str, build: bool) -> bool { + is_model_reference(name) || (build && self.user.contains_key(name)) + } + + /// What `${name}` reads while only the profile `own` is active (if + /// any): a model reference, else (in the build's view) a user + /// property, else that profile's ``, else the root's, else + /// a deprecated bare model reference (`${version}`). + fn lookup(&self, name: &str, own: &[usize], build: bool) -> Option { + if is_model_reference(name) { + if let Some(v) = self.model.get(name) { + return Some(v.clone()); + } + } + if build { + if let Some(v) = self.user.get(name) { + return Some(v.clone()); + } + } + if !self.fixed(name, build) { + if let Some(v) = own + .iter() + .find_map(|&i| self.profile_properties[i].1.get(name)) + { + return Some(v.clone()); + } + } + self.properties + .get(name) + .or_else(|| self.model.get(name)) + .cloned() + } + + /// Every value `${name}` may read in the build while `own` and any + /// other profiles are active: [`Self::lookup`]'s, then every other + /// profile's definition. Empty when [`Self::lookup`] finds none: some + /// set of profiles leaves it undefined. + fn lookup_any(&self, name: &str, own: &[usize]) -> Vec { + let Some(base) = self.lookup(name, own, true) else { + return Vec::new(); + }; + let mut values = vec![base]; + if !self.fixed(name, true) { + values.extend( + self.profile_properties + .iter() + .enumerate() + .filter(|(i, _)| !own.contains(i)) + .filter_map(|(_, (_, props))| props.get(name).cloned()), + ); + } + values + } + + /// The `` text of the declaration at `pos`, resolved as + /// [`PomDep::version`], [`PomDep::build_version`] and + /// [`PomDep::version_alternatives`] describe. + fn resolve_version(&self, text: &str, pos: usize) -> (String, String, Vec) { + if !text.contains("${") { + return (text.to_string(), text.to_string(), Vec::new()); + } + let own = self.own_profile(pos); + let spelled = interpolate_strict(text, &|name| self.lookup(name, &own, false)) + .unwrap_or_else(|| text.to_string()); + let Some(build) = interpolate_strict(text, &|name| self.lookup(name, &own, true)) else { + return (spelled, text.to_string(), Vec::new()); + }; + let alternatives = match interpolate_each(text, &|name| self.lookup_any(name, &own)) { + Some(all) => all.into_iter().filter(|v| *v != build).collect(), + None => vec![text.to_string()], + }; + (spelled, build, alternatives) + } + /// Inside a plugin / deploy section (never project resolution). pub(crate) fn is_ignored(&self, pos: usize) -> bool { inside(pos, &self.ignored) @@ -261,17 +373,20 @@ pub(crate) fn parse_pom_with(raw: &str, user: &BTreeMap) -> Resu let (Some(group), Some(artifact)) = (dep.group, dep.artifact) else { continue; }; + let (version, build_version, version_alternatives) = match &dep.version_text { + Some(text) => { + let (spelled, build, alternatives) = scope.resolve_version(text, dep.element.start); + (Some(spelled), Some(build), alternatives) + } + None => (None, None, Vec::new()), + }; pom.deps.push(PomDep { group, artifact, - version: dep - .version_text - .as_deref() - .map(|v| resolve_property(v, &scope.properties)), - build_version: dep - .version_text - .as_deref() - .map(|v| resolve_property(v, &scope.build_properties)), + version, + build_version, + version_text: dep.version_text, + version_alternatives, classifier: dep.classifier, type_text: dep.type_text, managed: inside(dep.element.start, &managed), @@ -298,40 +413,175 @@ fn properties( { continue; } - let body = props.inner(text); - let mut from = 0; - while let Some(rel) = body[from..].find('<') { - let lt = from + rel; - let Some(gt) = body[lt..].find('>').map(|r| lt + r) else { - return Err("unterminated tag in ".to_string()); - }; - let name = &body[lt + 1..gt]; - if !is_property_name(name) { - from = gt + 1; - continue; - } - let close = format!(""); - let Some(end) = body[gt + 1..].find(&close).map(|r| gt + 1 + r) else { - return Err(format!("unterminated <{name}> in ")); - }; - map.entry(name.to_string()) - .or_insert_with(|| body[gt + 1..end].trim().to_string()); - from = end + close.len(); - } + read_properties(props.inner(text), &mut map)?; } Ok(map) } -/// `${name}` → the property's value (one level, no recursion); anything -/// else, or an undefined property, is returned unchanged. -fn resolve_property(version: &str, properties: &BTreeMap) -> String { - version - .strip_prefix("${") - .and_then(|rest| rest.strip_suffix('}')) - .filter(|name| is_property_name(name)) - .and_then(|name| properties.get(name)) - .cloned() - .unwrap_or_else(|| version.to_string()) +/// Each `` inside `` (outside ignored sections) as +/// `name -> trimmed value`, with the `` element that holds it. +fn profile_properties( + text: &str, + ignored: &[Element], + profiles: &[Element], +) -> Result, String> { + let live = |pos: usize| inside(pos, profiles) && !inside(pos, ignored); + // A `` no `` is found around (a scan this simple + // cannot place) belongs to no profile: it is then only ever an + // alternative, never a declaration's own, which reads more versions + // into [`PomDep::version_alternatives`], not fewer. + let holders: Vec = elements(text, "profile") + .unwrap_or_default() + .into_iter() + .filter(|p| live(p.start)) + .collect(); + let mut out = Vec::new(); + for props in elements(text, "properties")? { + if !live(props.start) { + continue; + } + let holder = holders + .iter() + .find(|p| props.start >= p.start && props.start < p.end) + .copied(); + let mut map = BTreeMap::new(); + read_properties(props.inner(text), &mut map)?; + out.push((holder, map)); + } + Ok(out) +} + +/// The `name -> trimmed value` pairs of a `` body, into `map` +/// (a name's first definition wins). +fn read_properties(body: &str, map: &mut BTreeMap) -> Result<(), String> { + let mut from = 0; + while let Some(rel) = body[from..].find('<') { + let lt = from + rel; + let Some(gt) = body[lt..].find('>').map(|r| lt + r) else { + return Err("unterminated tag in ".to_string()); + }; + let name = &body[lt + 1..gt]; + if !is_property_name(name) { + from = gt + 1; + continue; + } + let close = format!(""); + let Some(end) = body[gt + 1..].find(&close).map(|r| gt + 1 + r) else { + return Err(format!("unterminated <{name}> in ")); + }; + map.entry(name.to_string()) + .or_insert_with(|| body[gt + 1..end].trim().to_string()); + from = end + close.len(); + } + Ok(()) +} + +/// Nesting depth past which [`interpolate_strict`] gives up: a property +/// named through more than this many others (Maven itself fails on a +/// cycle, which this bounds too). +pub(crate) const MAX_INTERPOLATION_DEPTH: usize = 16; + +/// Most distinct results [`interpolate_each`] enumerates before giving up. +const MAX_RESOLUTIONS: usize = 16; + +/// Most lookups one interpolation makes before giving up: a value naming +/// the next property many times over would otherwise cost exponentially +/// many with every level ("billion laughs"). +const MAX_LOOKUPS: usize = 1024; + +/// Longest text [`interpolate_each`] builds before giving up. +const MAX_RESOLVED_LEN: usize = 4096; + +/// `value` with every `${name}` replaced by `lookup(name)`, itself +/// interpolated the same way, as Maven's model interpolator resolves a +/// property defined through another. `None` when a name is undefined, a +/// `${` is unterminated, or the nesting is cyclic or deeper than +/// [`MAX_INTERPOLATION_DEPTH`] (or the work bounds are hit): an unresolved +/// value never stands for a version. Shared by the vendored reactor +/// (its `lookup` walks the parent chain) and the VEX reader +/// ([`PomScope`]). +pub(crate) fn interpolate_strict( + value: &str, + lookup: &dyn Fn(&str) -> Option, +) -> Option { + interpolate_each(value, &|name| lookup(name).into_iter().collect())? + .into_iter() + .next() +} + +/// Every text `value` may interpolate to when each `${name}` may take any +/// of the values `lookup(name)` gives (an empty list: undefined), each +/// interpolated in turn ([`interpolate_strict`] with a single value per +/// name). A name that occurs twice may take a different value each time, +/// so the set may hold texts no single assignment builds: a superset, +/// which only ever withholds an attestation. `None` when any choice leaves +/// a name undefined, a `${` is unterminated, a name is reached through +/// itself (a cycle) or through more than [`MAX_INTERPOLATION_DEPTH`] +/// others, or the result or the work exceeds its bound. +pub(crate) fn interpolate_each( + value: &str, + lookup: &dyn Fn(&str) -> Vec, +) -> Option> { + let lookups = std::cell::Cell::new(MAX_LOOKUPS); + expand(value, lookup, &mut Vec::new(), &lookups) +} + +fn expand( + value: &str, + lookup: &dyn Fn(&str) -> Vec, + stack: &mut Vec, + lookups: &std::cell::Cell, +) -> Option> { + if stack.len() > MAX_INTERPOLATION_DEPTH { + return None; + } + let mut texts = BTreeSet::from([String::new()]); + let mut rest = value; + loop { + let (literal, reference) = match rest.find("${") { + Some(at) => (&rest[..at], Some(&rest[at + 2..])), + None => (rest, None), + }; + let Some(after) = reference else { + return concat(&texts, &BTreeSet::from([literal.to_string()])); + }; + let close = after.find('}')?; + let name = &after[..close]; + if stack.iter().any(|n| n == name) { + return None; + } + lookups.set(lookups.get().checked_sub(1)?); + let raws = lookup(name); + if raws.is_empty() { + return None; + } + stack.push(name.to_string()); + let mut values = BTreeSet::new(); + for raw in &raws { + values.extend(expand(raw, lookup, stack, lookups)?); + } + stack.pop(); + let prefixed = concat(&texts, &BTreeSet::from([literal.to_string()]))?; + texts = concat(&prefixed, &values)?; + rest = &after[close + 1..]; + } +} + +/// Every `a + b`, or `None` past [`MAX_RESOLUTIONS`] / [`MAX_RESOLVED_LEN`]. +fn concat(heads: &BTreeSet, tails: &BTreeSet) -> Option> { + let mut out = BTreeSet::new(); + for head in heads { + for tail in tails { + if head.len() + tail.len() > MAX_RESOLVED_LEN { + return None; + } + out.insert(format!("{head}{tail}")); + if out.len() > MAX_RESOLUTIONS { + return None; + } + } + } + Some(out) } /// Interpolation rounds before a still-unresolved reference is given up on @@ -841,6 +1091,198 @@ mod tests { } } + /// A `${property}` version is interpolated through every property it + /// names, user properties first at each level; a cycle, an undefined + /// name or a chain deeper than [`super::MAX_INTERPOLATION_DEPTH`] + /// leaves it as written. + #[test] + fn version_properties_resolve_recursively_and_fail_closed() { + // (as the pom spells it, as the build resolves it) + let both = |props: &str, user: &[(&str, &str)], text: &str| { + let pom = format!( + "{props}\ + ga\ + {text}" + ); + let user = user + .iter() + .map(|(k, v)| (k.to_string(), v.to_string())) + .collect(); + let dep = super::parse_pom_with(&pom, &user).unwrap().deps.remove(0); + assert!(dep.version_alternatives.is_empty(), "{props}"); + (dep.version.unwrap(), dep.build_version.unwrap()) + }; + let version = |props: &str, user: &[(&str, &str)], text: &str| { + let (spelled, built) = both(props, user, text); + if user.is_empty() { + assert_eq!(spelled, built, "{props}"); + } + built + }; + assert_eq!( + both("${b}1.0", &[("b", "2.0")], "${a}"), + ("1.0".to_string(), "2.0".to_string()), + "a nested user property moves the build's version, not the pom's" + ); + // `${p1}` ... `1.0`. + let chain = |n: usize| -> String { + (0..n) + .map(|i| match i + 1 == n { + true => format!("1.0"), + false => format!("${{p{}}}", i + 1), + }) + .collect() + }; + assert_eq!( + version("${b}${c}1.0", &[], "${a}"), + "1.0" + ); + assert_eq!( + version("${b}1.0", &[("b", "2.0")], "${a}"), + "2.0" + ); + assert_eq!(version("10", &[], "${a}.${b}-x"), "1.0-x"); + assert_eq!( + version("", &[], "${project.version}"), + "${project.version}", + "no project version here" + ); + assert_eq!(version(&chain(16), &[], "${p0}"), "1.0"); + assert_eq!(version(&chain(17), &[], "${p0}"), "${p0}", "too deep"); + for cyclic in [ + "${a}", + "${b}${a}", + "x${b}${a}y", + ] { + assert_eq!(version(cyclic, &[], "${a}"), "${a}", "{cyclic}"); + } + assert_eq!(version("${b}", &[], "${a}"), "${a}", "undefined"); + assert_eq!(version("1", &[], "${a"), "${a", "unterminated"); + } + + /// The versions a profile's `` may move a declaration to: + /// every profile, and every set of profiles active together, counts; a + /// declaration inside a profile reads that profile's own first; user + /// properties and model references no profile can redefine. + #[test] + fn profile_properties_give_version_alternatives() { + let resolve = |props: &str, profiles: &[&str], user: &[(&str, &str)], deps: &str| { + let profiles: String = profiles + .iter() + .map(|p| format!("{p}")) + .collect(); + let pom = format!( + "9{props}\ + {deps}{profiles}" + ); + let user = user + .iter() + .map(|(k, v)| (k.to_string(), v.to_string())) + .collect(); + super::parse_pom_with(&pom, &user) + .unwrap() + .deps + .into_iter() + .map(|d| (d.build_version.unwrap(), d.version_alternatives)) + .collect::>() + }; + let dep = |v: &str| { + format!( + "ga\ + {v}" + ) + }; + let one = |props, profiles: &[&str], user: &[(&str, &str)]| { + resolve(props, profiles, user, &dep("${v}")).remove(0) + }; + let s = |v: &[&str]| v.iter().map(|v| v.to_string()).collect::>(); + assert_eq!(one("1", &["2"], &[]), ("1".into(), s(&["2"]))); + assert_eq!(one("1", &["1"], &[]), ("1".into(), s(&[]))); + assert_eq!(one("1", &["2"], &[]), ("1".into(), s(&[]))); + assert_eq!( + one("${w}1", &["2", "3"], &[]), + ("1".into(), s(&["2", "3"])) + ); + // Two profiles that move it only together. + assert_eq!( + one("11", &["${w}", "2"], &[]), + ("1".into(), s(&["2"])) + ); + // A profile value this pom cannot resolve, or a cycle. + assert_eq!( + one("1", &["${nope}"], &[]), + ("1".into(), s(&["${v}"])) + ); + assert_eq!( + one("1", &["${v}"], &[]), + ("1".into(), s(&["${v}"])) + ); + // User properties and model references win over every profile's. + assert_eq!( + one("1", &["2"], &[("v", "3")]), + ("3".into(), s(&[])) + ); + assert_eq!( + resolve( + "", + &["2"], + &[], + &dep("${project.version}") + ) + .remove(0), + ("9".into(), s(&[])) + ); + // More alternatives than are enumerated: unresolved. + let many: Vec = (0..17).map(|i| format!("{i}")).collect(); + let many: Vec<&str> = many.iter().map(String::as_str).collect(); + assert_eq!(one("x", &many, &[]), ("x".into(), s(&["${v}"]))); + + // A profile's own declaration reads its own properties first. + let pom = format!( + "1{}\ + a2\ + {}\ + b3", + dep("${v}"), + dep("${v}") + ); + let deps: Vec<_> = super::parse_pom(&pom) + .unwrap() + .deps + .into_iter() + .map(|d| { + ( + d.in_profile, + d.build_version.unwrap(), + d.version_alternatives, + ) + }) + .collect(); + assert_eq!( + deps, + [ + (false, "1".to_string(), s(&["2", "3"])), + (true, "2".to_string(), s(&["3"])), + ] + ); + } + + /// The shared interpolator's work bounds: a value naming the next + /// property many times over fails instead of growing exponentially. + #[test] + fn interpolation_is_bounded() { + let props: std::collections::BTreeMap = (0..12) + .map(|i| (format!("p{i}"), format!("${{p{}}}", i + 1).repeat(8))) + .chain([("p12".to_string(), "x".to_string())]) + .collect(); + let lookup = |name: &str| props.get(name).cloned(); + assert_eq!(super::interpolate_strict("${p0}", &lookup), None); + assert_eq!( + super::interpolate_strict("${p11}", &lookup).as_deref(), + Some("xxxxxxxx") + ); + } + #[test] fn suffix_grammar_is_exact() { assert_eq!( diff --git a/crates/socket-patch-core/src/vendor/jvm/maven_reactor.rs b/crates/socket-patch-core/src/vendor/jvm/maven_reactor.rs index 42a853fff..8b8e9b6cc 100644 --- a/crates/socket-patch-core/src/vendor/jvm/maven_reactor.rs +++ b/crates/socket-patch-core/src/vendor/jvm/maven_reactor.rs @@ -11,7 +11,9 @@ use std::ops::Range; use serde_json::{json, Value}; -use crate::formats::maven::maven_config_properties as cli_properties; +use crate::formats::maven::{ + interpolate_strict, maven_config_properties as cli_properties, MAX_INTERPOLATION_DEPTH, +}; use super::super::state::{WiringAction, WiringRecord}; use super::layout::{self, safe_coordinates}; @@ -51,8 +53,6 @@ const NESTED_MVN_FILES: &[&str] = &[ const SHAPE_UNSUPPORTED: &str = "vendor_jvm_shape_unsupported"; const UPSTREAM_UNAVAILABLE: &str = "vendor_jvm_upstream_unavailable"; const DEGRADED: &str = "vendor_jvm_degraded"; -/// Property interpolation depth cap (Maven itself fails on cycles). -const MAX_INTERPOLATION_DEPTH: usize = 16; /// Whether the pom declares a `` or `` of the project /// or of a profile (plugin configuration, comments and CDATA do not count). @@ -737,7 +737,7 @@ fn restore_versions( let resolve = |v: &str| { reactor .filter(|r| r.poms.contains_key(rel)) - .and_then(|r| r.interpolate(rel, v, 0)) + .and_then(|r| r.interpolate(rel, v)) }; let mut edits = Vec::new(); for (dep, key) in doc.keyed_declarations(c.group_id, c.artifact_id, &resolve) { @@ -1055,30 +1055,6 @@ struct ExternalPom { pom: Pom, } -/// `${name}` interpolation through `lookup`; `None` when a name is -/// undefined or the nesting is too deep. -fn interpolate_by( - value: &str, - lookup: &dyn Fn(&str) -> Option, - depth: usize, -) -> Option { - if depth > MAX_INTERPOLATION_DEPTH { - return None; - } - let mut out = String::new(); - let mut rest = value; - while let Some(at) = rest.find("${") { - out.push_str(&rest[..at]); - let after = &rest[at + 2..]; - let close = after.find('}')?; - let raw = lookup(&after[..close])?; - out.push_str(&interpolate_by(&raw, lookup, depth + 1)?); - rest = &after[close + 1..]; - } - out.push_str(rest); - Some(out) -} - impl Reactor { /// Management of `patch`'s g:a that a pin in local root `root` would /// override and that does not manage it at the patch's base version: @@ -1120,7 +1096,7 @@ impl Reactor { }) }; let resolve = |value: &str, what: &str| -> Result { - interpolate_by(value, &props, 0).ok_or_else(|| { + interpolate_strict(value, &props).ok_or_else(|| { Missing::Unresolved(format!("{rel}: {what} {value} is not defined")) }) }; @@ -1207,8 +1183,7 @@ impl Reactor { ) -> Result, Missing> { let mut chain: Vec = Vec::new(); let mut parent = self.poms[root].parent.as_ref().map(|p| { - let local = - |v: &Option| v.as_deref().and_then(|v| self.interpolate(root, v, 0)); + let local = |v: &Option| v.as_deref().and_then(|v| self.interpolate(root, v)); (local(&p.group), local(&p.artifact), local(&p.version)) }); while let Some((pg, pa, pv)) = parent { @@ -1374,7 +1349,7 @@ fn bom_manages<'b>( chain.iter().find_map(|e| e.pom.props.get(name).cloned()) }; let resolve = |value: &str| -> Result { - interpolate_by(value, &props, 0).ok_or_else(|| { + interpolate_strict(value, &props).ok_or_else(|| { Missing::Unresolved(format!( "the BOM {}:{}:{} manages {g}:{a} at {value}, which it does not define", bom.0, bom.1, bom.2 @@ -1729,13 +1704,13 @@ impl Reactor { fn keyed_declarations(&self, rel: &str, g: &str, a: &str) -> Vec<(usize, String)> { self.poms[rel] .doc - .keyed_declarations(g, a, &|v| self.interpolate(rel, v, 0)) + .keyed_declarations(g, a, &|v| self.interpolate(rel, v)) } /// Interpolate every `${name}` in `value` in the context of `rel`; /// `None` when any name is undefined in the checkout. - fn interpolate(&self, rel: &str, value: &str, depth: usize) -> Option { - self.interpolate_with(rel, value, depth, None) + fn interpolate(&self, rel: &str, value: &str) -> Option { + self.interpolate_with(rel, value, None) } /// [`Self::interpolate`] with `overlay`'s properties (a profile's) @@ -1744,24 +1719,9 @@ impl Reactor { &self, rel: &str, value: &str, - depth: usize, overlay: Option>, ) -> Option { - if depth > MAX_INTERPOLATION_DEPTH { - return None; - } - let mut out = String::new(); - let mut rest = value; - while let Some(at) = rest.find("${") { - out.push_str(&rest[..at]); - let after = &rest[at + 2..]; - let close = after.find('}')?; - let raw = self.lookup_with(rel, &after[..close], overlay)?; - out.push_str(&self.interpolate_with(rel, &raw, depth + 1, overlay)?); - rest = &after[close + 1..]; - } - out.push_str(rest); - Some(out) + interpolate_strict(value, &|name| self.lookup_with(rel, name, overlay)) } /// User properties win over model properties, as in Maven; then the @@ -1827,17 +1787,14 @@ impl Reactor { // has redefined something it names: that moves it too. let undefined = || "an undefined value".to_string(); let plain = (reader != rel) - .then(|| { - self.interpolate(reader, declared, 0) - .unwrap_or_else(undefined) - }) + .then(|| self.interpolate(reader, declared).unwrap_or_else(undefined)) .filter(|v| moved(v)) .map(|v| (v, reader.to_string())); plain.or_else(|| { self.chain(reader).find_map(|at| { self.poms[at].profile_props.iter().find_map(|(id, props)| { let v = self - .interpolate_with(reader, declared, 0, Some((at, props))) + .interpolate_with(reader, declared, Some((at, props))) .unwrap_or_else(undefined); moved(&v).then(|| (v, format!("{reader} under profile {id} of {at}"))) }) @@ -1897,7 +1854,7 @@ impl Reactor { // literal SV must not reach. if doc.has_property_coordinate(dep) { let other = self.inheritors(rel).find(|p| { - let resolve = |v: &str| self.interpolate(p, v, 0); + let resolve = |v: &str| self.interpolate(p, v); doc.coordinate(dep, "groupId", &resolve).as_deref() != Some(patch.group_id) || doc.coordinate(dep, "artifactId", &resolve).as_deref() != Some(patch.artifact_id) @@ -1924,7 +1881,7 @@ impl Reactor { }; let declared = doc.text_of(version); let resolved = if declared.contains("${") { - match self.interpolate(rel, &declared, 0) { + match self.interpolate(rel, &declared) { Some(v) => v, None => { warnings.push(degraded( @@ -2012,7 +1969,7 @@ impl Reactor { uncertain: &mut BTreeSet, ) { let doc = &self.poms[rel].doc; - let resolve = |v: &str| self.interpolate(rel, v, 0); + let resolve = |v: &str| self.interpolate(rel, v); for dep in doc.declarations() { let (Some(g), Some(a)) = ( doc.child_text(dep, "groupId"), diff --git a/crates/socket-patch-core/src/vex/discover/maven.rs b/crates/socket-patch-core/src/vex/discover/maven.rs index acb5bd0ca..2c4e7f2a2 100644 --- a/crates/socket-patch-core/src/vex/discover/maven.rs +++ b/crates/socket-patch-core/src/vex/discover/maven.rs @@ -54,14 +54,18 @@ //! pin (a profile's `` only where management governs //! the pin): the rewriter leaves it as written, and whenever that profile //! is active it overrides the pin (vendored mode treats every profile as -//! live and rewrites it too). A `${property}` version is resolved one -//! level from the root `` — the pin as the pom spells it — and -//! again with `.mvn/maven.config`'s `-D` user properties first, as the -//! build resolves it (Maven's order; `${project.*}` stays the model's): a -//! pin the build config moves keeps the ref but marks it unattested, and a -//! Socket version only the build config supplies is diagnosed, never a -//! ref (hosted mode never writes one; rollback / remove never edit -//! `.mvn/maven.config`). +//! live and rewrites it too). A `${property}` version is interpolated +//! through every property it names (the vendored reactor's interpolator: +//! at most 16 deep, a cycle never resolves) from the root `` — +//! the pin as the pom spells it — and again with `.mvn/maven.config`'s +//! `-D` user properties first at every level, as the build resolves it +//! (Maven's order; `${project.*}` stays the model's): a pin the build +//! config moves keeps the ref but marks it unattested, and a Socket +//! version only the build config supplies is diagnosed, never a ref +//! (hosted mode never writes one; rollback / remove never edit +//! `.mvn/maven.config`). A pin the build resolves but that any profile's +//! (or set of profiles') `` make resolve anything else keeps +//! the ref unattested too, as a profile declaration does. //! //! Integrity: when the pom sha256 was known the rewriter also writes Maven //! Trusted Checksums — `.mvn/checksums/checksums.sha256` lines @@ -104,7 +108,8 @@ //! (plugin classpaths) are not project resolution and are skipped silently; //! `` are only read when activated, so a Socket repository or pin //! there is diagnosed, never a ref, and a profile declaration of a pinned -//! GA at another version withholds the pin's attestation. Submodule poms +//! GA at another version, or a profile's `` that move the +//! pin's `${property}`, withholds the pin's attestation. Submodule poms //! and parent poms are not read (root-only). Gradle builds are discovered //! by `gradle.rs` (the hosted index) and the vendored JVM backend's own //! probes. @@ -698,6 +703,20 @@ fn build_version(d: &PomDep) -> Option<&str> { d.build_version.as_deref() } +/// Every version the build may resolve for `d` (`None` when it has no +/// ``), each flagged when a profile gives it: [`build_version`] +/// (a profile's when `in_profile`), then each version some set of active +/// profiles' `` moves it to ([`PomDep::version_alternatives`]). +fn resolutions(d: &PomDep, in_profile: bool) -> Vec<(Option<&str>, bool)> { + std::iter::once((build_version(d), in_profile)) + .chain( + d.version_alternatives + .iter() + .map(|v| (Some(v.as_str()), true)), + ) + .collect() +} + /// Every GA with a versioned, classifier-less declaration Maven always /// reads: the candidates for a pin. fn live_gas(deps: &[PomDep]) -> BTreeSet<(String, String)> { @@ -842,12 +861,12 @@ fn unpatched_classifier_copy( d.managed && d.in_profile == in_profile }) .filter(|(_, c)| *c == classifier) - .map(|(d, _)| (build_version(d), in_profile)) + .flat_map(|(d, _)| resolutions(d, in_profile)) .collect() }; classifier_declarations(deps, group, artifact, |d| !d.managed).find_map(|(d, classifier)| { let resolved = match build_version(d) { - Some(version) => vec![(Some(version), false)], + Some(_) => resolutions(d, false), None => { let mut live = managed(classifier, false); if live.is_empty() { @@ -894,7 +913,14 @@ const NON_CODE_TYPES: &[&str] = &["pom", "java-source", "javadoc"]; /// says. Activation is not evaluated, so every profile counts, as in /// vendored mode. A declaration without a version takes the live one and /// moves nothing, and a pom (or sources / javadoc) typed one carries no -/// code. +/// code. Every version a profile's `` may give the +/// declaration counts too ([`PomDep::version_alternatives`]). +/// +/// The same holds for a live declaration that resolves `pinned` through a +/// property some profile's `` redefine (or one it names, or +/// several profiles together) to another version: Maven merges an active +/// profile's properties over the pom's own before it interpolates. +/// Vendored mode refuses to pin over such a property (`version_override`). fn profile_override( deps: &[PomDep], group: &str, @@ -902,7 +928,7 @@ fn profile_override( pinned: &str, pin_managed: bool, ) -> Option { - main_jar_declarations(deps, group, artifact, |d| d.in_profile) + main_jar_declarations(deps, group, artifact, |_| true) .filter(|d| { !d.type_text .as_deref() @@ -910,7 +936,6 @@ fn profile_override( }) .filter(|d| !d.managed || pin_managed) .find_map(|d| { - let version = build_version(d).filter(|v| *v != pinned)?; let section = if d.managed { " " } else { @@ -925,6 +950,23 @@ fn profile_override( d.group, d.artifact ) }; + if !d.in_profile { + // Off the pin as built, it is no pin (or another bypass). + if build_version(d) != Some(pinned) { + return None; + } + let moved = d.version_alternatives.first()?; + let declared = d.version_text.as_deref().unwrap_or_default(); + return Some(format!( + "{POM}: {section}{named} {declared} resolves the \ + patched {pinned}, but a redefinition of a property \ + it names makes it {moved}; whenever that profile is active Maven resolves \ + it instead of the pin" + )); + } + let version = resolutions(d, true) + .into_iter() + .find_map(|(v, _)| v.filter(|v| *v != pinned))?; Some(format!( "{POM}: a {section}declaration of {named} carries \ {version}, not the patched {pinned}; whenever that profile \ @@ -1902,6 +1944,225 @@ mod tests { } } + /// Whether `out` attests `purl`: a ref no unattested entry withholds. + fn attests(out: &Discovery, purl: &str) -> bool { + out.refs.iter().any(|r| { + r.purl == purl + && !out + .unattested + .iter() + .any(|u| u.purl == r.purl && u.uuid == r.uuid) + }) + } + + /// A hosted project pinning slf4j-api through `decl` (a + /// `` / `` block), with the root + /// `` `props` and one `` per entry of `profiles` + /// (each a profile body). + fn property_project(props: &str, decl: &str, profiles: &[&str]) -> Project { + let repo = hosted_repo(&format!("socket-patch-{UUID_A}"), ®istry_url(UUID_A)); + let profiles: String = profiles + .iter() + .enumerate() + .map(|(i, body)| format!("p{i}\n{body}\n")) + .collect(); + let p = Project::new(); + p.write( + "pom.xml", + pom(&format!( + "{props}\n{decl}\n{repo}\n\ + \n{profiles}\n" + )), + ); + p + } + + fn direct_decl(version: &str) -> String { + format!( + "\n{}\n", + dep("org.slf4j", "slf4j-api", Some(version)) + ) + } + + /// A pin spelled through a property is what Maven resolves only while + /// no active profile's `` redefines that property (or one + /// it names) to anything else: Maven merges an active profile's + /// properties over the pom's own before it interpolates the + /// ``. Vendored mode refuses to pin over such a property + /// (`version_override`), so the ref stays (rollback / remove / list + /// find it) but is never attested. User properties + /// (`.mvn/maven.config`) win over every profile's. + #[tokio::test] + async fn profile_properties_redefining_a_pin_property_are_not_attested() { + let pin = suffixed(UUID_A); + let v = |value: &str| format!("{value}"); + let w = |value: &str| format!("{value}"); + let in_props = |s: String| format!("{s}"); + let direct = direct_decl("${slf4j.version}"); + let managed = format!( + "\n{}\n\ + \n{}\n", + dep("org.slf4j", "slf4j-api", Some("${slf4j.version}")), + dep("org.slf4j", "slf4j-api", None), + ); + let withheld = [ + // The base release. + (v(&pin), &direct, vec![in_props(v("1.7.36"))]), + (v(&pin), &managed, vec![in_props(v("1.7.36"))]), + // Through a property the pin's property names. + (v("${w}") + &w(&pin), &direct, vec![in_props(w("1.7.30"))]), + // A value this pom cannot resolve, or a cycle. + (v(&pin), &direct, vec![in_props(v("${undefined}"))]), + (v(&pin), &direct, vec![in_props(v("${slf4j.version}"))]), + // Two profiles that move the pin only together. + ( + v(&pin) + &w(&pin), + &direct, + vec![in_props(v("${w}")), in_props(w("1.7.36"))], + ), + ]; + for (root, decl, profiles) in &withheld { + let profiles: Vec<&str> = profiles.iter().map(String::as_str).collect(); + let out = run(&property_project(root, decl, &profiles)).await; + assert_refs(&out, &[(FX_PURL, UUID_A, WiringMode::Hosted)]); + assert_eq!( + out.unattested.len(), + 1, + "{root} {profiles:?}: {:?}", + out.unattested + ); + let u = &out.unattested[0]; + assert_eq!( + u.kind, + UnattestedKind::MavenProfileUnpatched, + "{profiles:?}" + ); + assert!( + u.detail.contains("org.slf4j:slf4j-api") && u.detail.contains(""), + "{}", + u.detail + ); + } + + for (root, profile, config) in [ + // The pin itself, or an unrelated property. + (v(&pin), in_props(v(&pin)), None), + (v(&pin), in_props("1".to_string()), None), + (v("${w}") + &w(&pin), in_props(w(&pin)), None), + // A user property wins over every profile's. + ( + v(&pin), + in_props(v("1.7.36")), + Some(format!("-Dslf4j.version={pin}\n")), + ), + ] { + let p = property_project(&root, &direct, &[&profile]); + if let Some(config) = &config { + p.write(".mvn/maven.config", config); + } + let out = run(&p).await; + assert_refs(&out, &[(FX_PURL, UUID_A, WiringMode::Hosted)]); + assert!( + attests(&out, FX_PURL), + "{root} {profile}: {:?}", + out.unattested + ); + } + } + + /// A `` declaration's `${property}` version reads its own + /// profile's `` first: that profile is active whenever the + /// declaration applies. + #[tokio::test] + async fn profile_declaration_reads_its_own_profile_properties() { + let pin = suffixed(UUID_A); + let profile = |value: &str| { + format!( + "{value}\n\ + \n{}\n", + dep("org.slf4j", "slf4j-api", Some("${slf4j.version}")) + ) + }; + let direct = direct_decl(&pin); + for (root, own, attested) in [ + // The root resolves the pin, the profile the base release. + (pin.as_str(), "1.7.36", false), + // The root resolves the base release, the profile the pin. + ("1.7.36", pin.as_str(), true), + ] { + let p = property_project( + &format!("{root}"), + &direct, + &[&profile(own)], + ); + let out = run(&p).await; + assert_refs(&out, &[(FX_PURL, UUID_A, WiringMode::Hosted)]); + assert_eq!( + attests(&out, FX_PURL), + attested, + "{own}: {:?}", + out.unattested + ); + } + } + + /// A `${property}` version resolves through every property it names, + /// with user properties first at each level, as Maven interpolates; a + /// cycle, or a chain deeper than the vendored reactor follows, is + /// never a pin. + #[tokio::test] + async fn version_property_resolves_through_nested_properties() { + let pin = suffixed(UUID_A); + // `${p1}` ... `{end}`. + let chain = |links: usize, end: &str| -> String { + (0..links) + .map(|i| { + let value = if i + 1 == links { + end.to_string() + } else { + format!("${{p{}}}", i + 1) + }; + format!("{value}") + }) + .collect() + }; + let split = format!( + "${{p1}}-socket.${{p2}}1.7.36{}", + &UUID_A[..8] + ); + let user_pin = format!("-Dp1={pin}\n"); + for (props, config, attested) in [ + (chain(2, &pin), None, true), + (chain(3, &pin), None, true), + (split, None, true), + // The deepest chain the vendored reactor resolves, and one more. + (chain(16, &pin), None, true), + (chain(17, &pin), None, false), + // A user property redefines a nested one: off the pin, or onto + // a Socket version only the build config supplies (never a + // pom.xml ref). + (chain(2, &pin), Some("-Dp1=1.7.36\n"), false), + (chain(2, "1.7.36"), Some(user_pin.as_str()), false), + // A cycle. + ("${p1}${p0}".to_string(), None, false), + ("${p0}".to_string(), None, false), + ] { + let p = property_project(&props, &direct_decl("${p0}"), &[]); + if let Some(config) = config { + p.write(".mvn/maven.config", config); + } + let out = run(&p).await; + assert_eq!( + attests(&out, FX_PURL), + attested, + "{props} {config:?}: {:?} {:?} {:?}", + out.refs, + out.unattested, + out.diagnostics + ); + } + } + /// A versioned declaration whose coordinate is a property this pom /// does not define may be the pinned GA, so the pin is not attested /// past it (#683). @@ -2171,6 +2432,36 @@ mod tests { } } + /// The same for a profile's `` that move a `${property}` + /// version of the vendored GA off the vendored version. + #[tokio::test] + async fn vendored_ref_under_profile_properties_is_not_attested() { + for (version, attested) in [("1.10.1", false), ("1.10.0", true)] { + let p = vendored_project(UUID_A); + let wired = std::fs::read_to_string(p.root().join("pom.xml")) + .expect("read pom") + .replace("1.10.0", "${ct}"); + let at = wired.rfind("").expect("project close"); + p.write( + "pom.xml", + format!( + "{}1.10.0\np\ + {version}\n{}", + &wired[..at], + &wired[at..] + ), + ); + let out = run(&p).await; + assert_refs(&out, &[(V_PURL, UUID_A, WiringMode::Vendored)]); + assert_eq!( + attests(&out, V_PURL), + attested, + "{version}: {:?}", + out.unattested + ); + } + } + #[tokio::test] async fn vendored_url_spellings() { for spelled in [ diff --git a/crates/socket-patch-core/src/vex/discover/mod.rs b/crates/socket-patch-core/src/vex/discover/mod.rs index ca636edcb..3a8a80b00 100644 --- a/crates/socket-patch-core/src/vex/discover/mod.rs +++ b/crates/socket-patch-core/src/vex/discover/mod.rs @@ -609,8 +609,11 @@ pub enum UnattestedKind { /// A Maven pin (hosted, or the pre-v5 single-pom vendored repository) /// whose GA a `` declaration of the root pom gives another /// version (the base release, another release, an - /// unresolved `${property}`): whenever that profile is active it - /// overrides the pin, so the build resolves the unpatched jar. + /// unresolved `${property}`), or whose `${property}` version a + /// profile's `` redefine (directly or through a property + /// it names) to another or an unresolvable version: whenever that + /// profile is active it overrides the pin, so the build resolves the + /// unpatched jar. MavenProfileUnpatched, /// A hosted Maven pin the root pom spells through a `${property}` that /// `.mvn/maven.config` redefines (`-D`, which Maven reads before the