Skip to content

Commit ddf4808

Browse files
mikolalysenkoclaude
andcommitted
Weigh profile versions of a parent-resolved pin property
/code-review high on #2020: - A live `${prop}` this pom leaves unresolved (a parent pom defines it) dropped every profile alternative, so a pre-v5 vendored ref spelled that way stayed attested while a profile's `<properties>` moved the build to a public release. The profile values are now kept, and the live check weighs an unresolved declaration as one that may be the pin. - A profile without `<id>` is named `default`, as Maven and the vendored reactor name it (it was numbered by `<properties>` block). - Docs: read_properties is last-wins; ScopedDep coordinates read a profile declaration's own `<properties>` only once read_profiles ran, which the hosted rewriter does not do. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 630fc37 commit ddf4808

3 files changed

Lines changed: 54 additions & 15 deletions

File tree

‎crates/socket-patch-core/src/formats/maven/mod.rs‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ struct ProfileProperties {
141141
/// classpaths, `<pluginRepositories>` / `<distributionManagement>` plugin
142142
/// and deploy repositories; `<profiles>` apply only when activated. Shared
143143
/// by the VEX reader ([`parse_pom`]) and the hosted rewriter, so the
144-
/// rewriter edits exactly the markup VEX later attests.
144+
/// rewriter edits exactly the markup VEX later attests. Only the VEX
145+
/// reader weighs profile `<properties>` ([`Self::read_profiles`]).
145146
#[derive(Debug)]
146147
pub(crate) struct PomScope {
147148
/// The pom with comments and CDATA blanked byte-for-byte.
@@ -297,7 +298,17 @@ impl PomScope {
297298
.unwrap_or_else(|| text.to_string());
298299
match self.resolve_in_build(text, pos, interpolate_strict) {
299300
(Some(build), alternatives) => (spelled, build, alternatives),
300-
(None, _) => (spelled, text.to_string(), Vec::new()),
301+
// Unresolved here (a parent pom this reader never sees may
302+
// define it): the build version stays as written, but a value
303+
// some profiles give it is still one the build may resolve.
304+
(None, alternatives) => (
305+
spelled,
306+
text.to_string(),
307+
alternatives
308+
.into_iter()
309+
.filter(|a| a.value != text)
310+
.collect(),
311+
),
301312
}
302313
}
303314

@@ -407,10 +418,15 @@ fn inside(pos: usize, set: &[Element]) -> bool {
407418
pub(crate) struct ScopedDep {
408419
pub(crate) element: Element,
409420
/// `<groupId>` / `<artifactId>` with every `${prop}` the build defines
410-
/// with no profile active (user properties, the project model, the
411-
/// root `<properties>`) interpolated
412-
/// ([`PomScope::interpolate_coordinate`]); an undefined one stays as
413-
/// written, so [`may_name`] can tell it apart.
421+
/// (user properties, the project model, the root `<properties>`)
422+
/// interpolated ([`PomScope::interpolate_coordinate`]); an undefined
423+
/// one stays as written, so [`may_name`] can tell it apart. No profile
424+
/// is active but, for a `<profiles>` declaration, its own, whose
425+
/// `<properties>` count only once [`PomScope::read_profiles`] has run:
426+
/// the VEX reader runs it, the hosted rewriter does not, so a profile
427+
/// declaration whose coordinates only its own profile's `<properties>`
428+
/// define is the pinned GA to VEX (which then withholds the pin) and
429+
/// not to the rewriter.
414430
pub(crate) group: Option<String>,
415431
pub(crate) artifact: Option<String>,
416432
/// Inner-text byte range of the literal `<version>`, when present.
@@ -582,10 +598,12 @@ fn profile_properties(
582598
.iter()
583599
.find(|p| props.start >= p.start && props.start < p.end)
584600
.copied();
601+
// Maven names a profile without an `<id>` `default`, as the
602+
// vendored reactor does.
585603
let id = holder
586604
.and_then(|p| direct_child_text(p.inner(text), "id"))
587605
.filter(|id| !id.is_empty())
588-
.unwrap_or_else(|| format!("#{}", out.len() + 1));
606+
.unwrap_or_else(|| "default".to_string());
589607
let mut map = BTreeMap::new();
590608
read_properties(props.inner(text), &mut map)?;
591609
out.push(ProfileProperties {

‎crates/socket-patch-core/src/formats/maven/properties.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ pub(crate) fn profile_combinations(
230230
}
231231

232232
/// The `name -> trimmed value` pairs of a `<properties>` body, into `map`
233-
/// (a name's first definition wins). An element's name is its tag up to
233+
/// (a name's last definition wins, as in Maven's model reader). An element's name is its tag up to
234234
/// whitespace or `/`, so `<v combine.self="override">` and `<v >` define
235235
/// `v`, and `<v/>` defines it empty; a comment-blanked or malformed child
236236
/// is an error (fail closed).

‎crates/socket-patch-core/src/vex/discover/maven.rs‎

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,11 +1004,19 @@ fn profile_override(
10041004
};
10051005
if !d.in_profile {
10061006
// Off the pin as built, it is no pin (or another bypass).
1007-
if build_version(d) != Some(pinned) {
1007+
// One this pom leaves unresolved (a parent pom's property)
1008+
// may be the pin.
1009+
let built = build_version(d)?;
1010+
let unresolved = built.contains("${");
1011+
if built != pinned && !unresolved {
10081012
return None;
10091013
}
1010-
// The smallest combination of profiles that moves it.
1011-
let moved = d.version_alternatives.first()?;
1014+
// The smallest combination of profiles that moves it: for
1015+
// an unresolved one, to a version this pom does give.
1016+
let moved = d
1017+
.version_alternatives
1018+
.iter()
1019+
.find(|a| a.value != pinned && !(unresolved && a.value.contains("${")))?;
10121020
let declared = d.version_text.as_deref().unwrap_or_default();
10131021
let value = if !matches!(moved.by, crate::formats::maven::MovedBy::Profiles(_)) {
10141022
"resolve a version this reader cannot work out".to_string()
@@ -1018,9 +1026,14 @@ fn profile_override(
10181026
moved.value.clone()
10191027
};
10201028
return Some(format!(
1021-
"{POM}: {section}{named} <version>{declared}</version> resolves the \
1029+
"{POM}: {section}{named} <version>{declared}</version> {} the \
10221030
patched {pinned}, but the <properties> of {} make it {value}; whenever that \
10231031
applies Maven resolves it instead of the pin",
1032+
if unresolved {
1033+
"may resolve"
1034+
} else {
1035+
"resolves"
1036+
},
10241037
moved.by
10251038
));
10261039
}
@@ -2610,7 +2623,15 @@ mod tests {
26102623
/// version of the vendored GA off the vendored version.
26112624
#[tokio::test]
26122625
async fn vendored_ref_under_profile_properties_is_not_attested() {
2613-
for (version, attested) in [("1.10.1", false), ("1.10.0", true)] {
2626+
// `root` is the pom's own `<properties>`; none leaves `${ct}` to a
2627+
// parent pom this reader never sees, which may give the vendored
2628+
// version, while the profile's value is known.
2629+
for (root, version, attested) in [
2630+
("<properties><ct>1.10.0</ct></properties>", "1.10.1", false),
2631+
("<properties><ct>1.10.0</ct></properties>", "1.10.0", true),
2632+
("", "1.10.1", false),
2633+
("", "1.10.0", true),
2634+
] {
26142635
let p = vendored_project(UUID_A);
26152636
let wired = std::fs::read_to_string(p.root().join("pom.xml"))
26162637
.expect("read pom")
@@ -2619,7 +2640,7 @@ mod tests {
26192640
p.write(
26202641
"pom.xml",
26212642
format!(
2622-
"{}<properties><ct>1.10.0</ct></properties>\n<profiles><profile><id>p</id>\
2643+
"{}{root}\n<profiles><profile><id>p</id>\
26232644
<properties><ct>{version}</ct></properties></profile></profiles>\n{}",
26242645
&wired[..at],
26252646
&wired[at..]
@@ -2630,7 +2651,7 @@ mod tests {
26302651
assert_eq!(
26312652
attests(&out, V_PURL),
26322653
attested,
2633-
"{version}: {:?}",
2654+
"{root:?} {version}: {:?}",
26342655
out.unattested
26352656
);
26362657
}

0 commit comments

Comments
 (0)