Skip to content

Commit e02358f

Browse files
Stop vendored Gradle liveness attesting a version the lockfile overrides (#2004)
* Stop vendored Gradle liveness attesting a version the lockfile overrides Vendored Gradle keeps the original GAV and leaves lock files unchanged, but neither its planner nor its VEX liveness (`wired`, which re-plans) read any lock file. A `gradle.lockfile` pinning the patched GA at another version makes Gradle resolve that version (or fail lock validation), yet vendored liveness still attested the patch. The planner now reads every lock file of every project of the build with the shared `gradle::locks` reader (per-project, buildscript, settings and legacy `gradle/dependency-locks/` files, via `ScriptGraph::lockfile_paths`) and degrades with `gradle_lock_overrides_vendored` when any of them locks the patched GA at a version other than the vendored one, or is not UTF-8. A custom `lockFile` location is degraded with `gradle_custom_lock_file`. Since `wired` requires a re-plan with no degraded warning, both withhold VEX attestation; vendoring itself still applies. Mapping from hosted: hosted pins a suffixed version (below-base lock is invalid, above-base unattested); vendored serves only its own version, so any other locked version is unattested. Hosted rejects every settings classpath lock because its script runs too late; vendored wires that classpath with an in-block exclusiveContent entry, so a settings lock at the vendored version is served. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Address review: route-checked settings locks, shared lock inventory Review findings on the vendored Gradle lock check: - A settings-classpath lock is accepted only when that build's settings script holds the in-block exclusiveContent entry for the GAV in every block that needs one. Liveness (`wired`) now re-plans with `Committed::Only`, so it counts only the committed entry, never one a re-plan would write; vendoring counts the entry it writes. Otherwise `gradle_settings_lock_unrouted`, as hosted rejects it. - A lock at another version of the GA that the vendored index also serves is not an override, so two vendored versions of one GA no longer degrade each other. - A listed lock file that cannot be read as UTF-8 text (I/O error, refused symlink, non-UTF-8) fails closed with its own reason, `gradle_lock_unreadable`, instead of being skipped. - A custom `lockFile` set as a literal `file(...)` (or `"$projectDir/..."`) in a project's own build script, inside the checkout, is read and checked like any lock (missing fails closed); only an unfollowable one is `gradle_custom_lock_file`. - One lock inventory: `ScriptGraph::read_locks` (and `custom_lock_files`), used by hosted discovery and the vendored check; `is_settings_lock` moves to `gradle::locks`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Judge vendored Gradle liveness on committed wiring only `wired` re-plans over the committed tree but ignored the re-plan's writes, so wiring a re-plan would add again stood in for wiring missing from the checkout. With no settings lock, a settings classpath on the patched module whose in-block exclusiveContent entry was removed still attested, and so did every other pending write. The liveness re-plan (`Committed::Only`) now degrades with `gradle_wiring_uncommitted` when it would still write any wiring file: - settings files: in-block `buildscript` / `pluginManagement` entries (a settings `plugins{}` block added since vendoring included) and the apply line of every wired build (`buildSrc`, included builds; only the root's was checked by `references`); - the index (a row whose hash no longer matches the tree); - the derived `maven-metadata.xml`; - `gradle/verification-metadata.xml` (missing patched hashes); - the script (already checked by `is_ours`). Tree files (rebuilt from the committed bytes, hashes checked against the index by the script) and the git hygiene files (`.gitattributes`, `.gitignore`, which Gradle never reads) do not count. Vendoring's own first run is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Treat project(...) blocks with long argument lists as foreign The project(...) exclusion only looked up to 7 tokens ahead for the block's '{', so a named-argument call such as project(path: ':a', configuration: 'x') { lockFile = file(...) } was read as this project's own followable lock and resolved against the wrong directory. Skip the whole argument list, then look for the block. Addresses the Bugbot finding on gradle/graph.rs. Co-Authored-By: Claude <noreply@anthropic.com> * Follow configure() and hook blocks; keep regenerated verification live Second review round on vendored Gradle lock liveness. - FALSE ATTESTATION: "configure" in the foreign-block list never matched (`all_blocks` wants `name {`, Gradle's form is `configure(<projects>) { … }`), and per-project hooks (`gradle.beforeProject`, `afterProject`) were not listed, so a `lockFile` set there was followed against the root's own directory and the configured projects' locks were never read. Every foreign block now skips its argument list and must open right there (or after one `.method`), which also fixes the converse: a project DEPENDENCY (`implementation(project(':lib'))`) followed by the script's own `dependencyLocking` block no longer marks it foreign. - Liveness counted any byte difference in verification-metadata.xml as uncommitted wiring, so after `./gradlew --write-verification-metadata` (the remedy the planner itself names) rewrote the jar entry in Gradle's form, a working vendoring was dead. The write no longer counts when the committed file already pins the patched jar's sha256. - A followed custom `lockFile` equal to an already-listed lock is checked once. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f8cf609 commit e02358f

7 files changed

Lines changed: 1024 additions & 27 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2633,7 +2633,7 @@ with `reason: <reason>: `:
26332633
|---|---|---|
26342634
| `vendor_jvm_shape_unsupported` | refusal, nothing written | `gradle_below_6_8`, `android_or_kmp` (also `available-at` module redirects), `gradle_exclusive_content_conflict` (a user rule claiming the module, in any script), `gradle_range_excludes_vendored` (no declared selector admits the vendored version), `gradle_verification_unparseable`, `gradle_index_unreadable`, `build_file_unreadable` (including a non-UTF-8 settings file), `build_file_outside_root`, `not_build_root` (run from a directory an ancestor settings file includes or may include, from a project with no settings file of its own below one, or below an ancestor settings file that is not UTF-8; `repair` refuses there too), `no_build_file` (no `pom.xml`, Gradle, sbt or scala-cli build at the root), `legacy_maven_root` (the root's ledger still holds a pre-v5 single-POM `maven_pom_repository` entry; the whole root is refused until `vendor --revert`, then vendor again) |
26352635
| `vendor_jvm_upstream_unavailable` | refusal | `classifier_unavailable` (a declared classifier jar no cache or registry has), `module_unavailable` (the pom declares Gradle module metadata that cannot be sourced), `pom_unavailable`, `verification_metadata_unavailable` |
2636-
| `vendor_jvm_degraded` | applied, VEX withheld | `gradle_unscanned_build_logic`, `unwired_build_logic` (a nonliteral included build), `settings_plugins_unwired`, `classifier_unpatched_copy` (a classifier jar carries an unpatched copy of a patched member), `verification_parent_chain_unhandled`, `conflicting_managed_version` / `management_unresolved` (Maven reactor: an imported BOM or an out-of-checkout parent manages the artifact at another version, or its POM is unavailable; that local root is not pinned) |
2636+
| `vendor_jvm_degraded` | applied, VEX withheld | `gradle_unscanned_build_logic`, `unwired_build_logic` (a nonliteral included build), `settings_plugins_unwired`, `classifier_unpatched_copy` (a classifier jar carries an unpatched copy of a patched member), `verification_parent_chain_unhandled`, `gradle_lock_overrides_vendored` (a dependency lock of the build, buildscript and settings classpath locks included, pins the module at a version no vendored row serves), `gradle_settings_lock_unrouted` (a `settings-gradle.lockfile` locks the module but that build's settings script does not hold the in-block repository entry for it), `gradle_lock_unreadable` (a lock file, or a followed custom `lockFile`, is missing, unreadable, outside the checkout or not UTF-8), `gradle_custom_lock_file` (a `lockFile` location that cannot be followed statically: anything but a literal `file(…)` in a project's own build script), `gradle_wiring_uncommitted` (VEX liveness only, never reported by `vendor`: the committed settings files, index, derived `maven-metadata.xml`, script or verification file lack wiring a re-plan would write, such as a removed apply line or in-block entry; the git hygiene files do not count), `conflicting_managed_version` / `management_unresolved` (Maven reactor: an imported BOM or an out-of-checkout parent manages the artifact at another version, or its POM is unavailable; that local root is not pinned) |
26372637
| `vendor_jvm_note` | applied, informational | `range_declared` (a range, prefix or rich selector lists versions from the derived `maven-metadata.xml`), `ide_sources_unavailable` |
26382638
| `vendor_jvm_upstream_unverified` | warning | Upstream metadata was taken offline and not authenticated against registry checksums. |
26392639

‎crates/socket-patch-core/src/gradle/graph.rs‎

Lines changed: 230 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,21 +1354,161 @@ impl ScriptGraph {
13541354
/// The first script setting a custom dependency-lock file location
13551355
/// (`lockFile = …` / `lockFile.set(…)`).
13561356
pub fn custom_lock_file(&self) -> Option<String> {
1357-
self.scripts.iter().find_map(|s| {
1357+
self.custom_lock_files()
1358+
.into_iter()
1359+
.next()
1360+
.map(|c| c.script)
1361+
}
1362+
1363+
/// Every custom dependency-lock file location (`lockFile = …` /
1364+
/// `lockFile.set(…)`), in script order. See [`CustomLockFile::path`]
1365+
/// for when the location can be followed.
1366+
pub fn custom_lock_files(&self) -> Vec<CustomLockFile> {
1367+
let mut out = Vec::new();
1368+
for s in &self.scripts {
13581369
let toks = dsl::tokens(&s.text, s.dsl);
1359-
(0..toks.len())
1360-
.any(|i| {
1361-
is_ident(toks.get(i), "lockFile")
1362-
&& ((is_punct(toks.get(i + 1), b'=') && !is_punct(toks.get(i + 2), b'='))
1363-
|| (is_punct(toks.get(i + 1), b'.')
1364-
&& is_ident(toks.get(i + 2), "set")
1365-
&& is_punct(toks.get(i + 3), b'(')))
1370+
// A project's own build script: `file(…)` resolves against it.
1371+
let project_dir = (s.kind == ScriptKind::Build)
1372+
.then(|| {
1373+
self.builds
1374+
.iter()
1375+
.flat_map(|b| &b.projects)
1376+
.find(|p| p.build_script.as_deref() == Some(s.rel.as_str()))
1377+
.map(|p| p.dir.clone())
13661378
})
1367-
.then(|| s.rel.clone())
1368-
})
1379+
.flatten();
1380+
// Blocks that configure other projects resolve `file(…)`
1381+
// against those: `allprojects { … }`, `subprojects { … }`,
1382+
// `configure(<projects>) { … }`, `project(':x') { … }` (also
1383+
// `project(':x').afterEvaluate { … }`) and the per-project
1384+
// hooks (`gradle.beforeProject { … }`, `afterProject`). The
1385+
// block must open right after the name and its argument list
1386+
// (or one `.method`), so `implementation(project(':lib'))`
1387+
// followed by the script's next block is not one.
1388+
const FOREIGN: [&str; 6] = [
1389+
"allprojects",
1390+
"subprojects",
1391+
"configure",
1392+
"project",
1393+
"beforeProject",
1394+
"afterProject",
1395+
];
1396+
let foreign: Vec<(usize, usize)> = (0..toks.len())
1397+
.filter(|&i| FOREIGN.iter().any(|name| is_ident(toks.get(i), name)))
1398+
.filter_map(|i| {
1399+
// Skip the whole argument list, however long, so a
1400+
// named-argument call (`project(path: ':a', …) {`)
1401+
// still counts.
1402+
let mut after = if is_punct(toks.get(i + 1), b'(') {
1403+
matching_close(&toks, i + 1)? + 1
1404+
} else {
1405+
i + 1
1406+
};
1407+
if is_punct(toks.get(after), b'.')
1408+
&& matches!(
1409+
toks.get(after + 1).map(|t| &t.tok),
1410+
Some(dsl::Tok::Ident(_))
1411+
)
1412+
{
1413+
after += 2;
1414+
}
1415+
let open = Some(after).filter(|&j| is_punct(toks.get(j), b'{'))?;
1416+
matching_close(&toks, open).map(|c| (open, c))
1417+
})
1418+
.collect();
1419+
for i in 0..toks.len() {
1420+
if !is_ident(toks.get(i), "lockFile") {
1421+
continue;
1422+
}
1423+
let value = if is_punct(toks.get(i + 1), b'=') && !is_punct(toks.get(i + 2), b'=') {
1424+
i + 2
1425+
} else if is_punct(toks.get(i + 1), b'.')
1426+
&& is_ident(toks.get(i + 2), "set")
1427+
&& is_punct(toks.get(i + 3), b'(')
1428+
{
1429+
i + 4
1430+
} else {
1431+
continue;
1432+
};
1433+
let path = project_dir
1434+
.as_deref()
1435+
.filter(|_| !foreign.iter().any(|(o, c)| i > *o && i < *c))
1436+
.and_then(|dir| {
1437+
let arg = file_call_arg(&toks, value)?;
1438+
resolve_rel("", dir, &arg)
1439+
});
1440+
out.push(CustomLockFile {
1441+
script: s.rel.clone(),
1442+
line: line_of(&s.text, toks[i].start),
1443+
path,
1444+
});
1445+
}
1446+
}
1447+
out
1448+
}
1449+
1450+
/// Every lock file of every build ([`Self::lockfile_paths`]) with its
1451+
/// parsed content: `None` for a file the listing shows that `read`
1452+
/// cannot give as text (unreadable, not UTF-8, refused). The one lock
1453+
/// inventory the hosted and vendored backends both check.
1454+
pub fn read_locks(
1455+
&self,
1456+
list: ListFn<'_>,
1457+
read: TextReadFn<'_>,
1458+
) -> Vec<(String, Option<locks::LockState>)> {
1459+
self.lockfile_paths(list)
1460+
.into_iter()
1461+
.map(|rel| {
1462+
let state = read(&rel).map(|text| locks::parse(&text));
1463+
(rel, state)
1464+
})
1465+
.collect()
13691466
}
13701467
}
13711468

1469+
/// A custom dependency-lock file location ([`ScriptGraph::custom_lock_files`]).
1470+
#[derive(Debug, Clone, PartialEq, Eq)]
1471+
pub struct CustomLockFile {
1472+
/// The script that sets it.
1473+
pub script: String,
1474+
/// 1-based line of `lockFile`.
1475+
pub line: usize,
1476+
/// The lock file, relative to the graph's read root, when the value is
1477+
/// `file('<path>')` with a literal path (or `"$projectDir/<path>"`) in
1478+
/// a project's own build script, outside any `allprojects`,
1479+
/// `subprojects`, `configure` or `project(…)` block, and the path stays
1480+
/// inside the read root. `None`: it cannot be followed statically.
1481+
pub path: Option<String>,
1482+
}
1483+
1484+
/// The path argument of `file(<string>)` at `toks[at..]`, the call
1485+
/// closing the value: a literal, or a `"$projectDir/…"` /
1486+
/// `"${projectDir}/…"` string with no other interpolation (made
1487+
/// project-relative).
1488+
fn file_call_arg(toks: &[Token], at: usize) -> Option<String> {
1489+
if !is_ident(toks.get(at), "file")
1490+
|| !is_punct(toks.get(at + 1), b'(')
1491+
|| !is_punct(toks.get(at + 3), b')')
1492+
|| is_punct(toks.get(at + 4), b'.')
1493+
{
1494+
return None;
1495+
}
1496+
if let Some(lit) = literal_of(toks.get(at + 2)) {
1497+
return Some(lit.to_string());
1498+
}
1499+
let Some(Token {
1500+
tok: Tok::Str { value, .. },
1501+
..
1502+
}) = toks.get(at + 2)
1503+
else {
1504+
return None;
1505+
};
1506+
let rest = value
1507+
.strip_prefix("$projectDir/")
1508+
.or_else(|| value.strip_prefix("${projectDir}/"))?;
1509+
(!rest.contains('$') && !rest.contains('\\')).then(|| rest.to_string())
1510+
}
1511+
13721512
/// The subproject relation of a directory to an ancestor build: whether
13731513
/// the settings script at `ancestor_settings` makes `rel_dir` (relative to
13741514
/// that script's directory) one of its projects.
@@ -2656,6 +2796,86 @@ other = { group = \"x\", name = \"y\", version = \"1\" }
26562796
)]);
26572797
assert_eq!(g.custom_lock_file(), None);
26582798

2799+
// The location is followed only from a project's own build script,
2800+
// as a literal (or `$projectDir/`) `file(…)` inside the root.
2801+
let path_of = |files: &[(&str, &str)]| -> Vec<Option<String>> {
2802+
graph(files)
2803+
.custom_lock_files()
2804+
.into_iter()
2805+
.map(|c| c.path)
2806+
.collect()
2807+
};
2808+
let lock = |v: &str| format!("dependencyLocking {{ lockFile = {v} }}\n");
2809+
for (value, want) in [
2810+
("file('locks/app.lockfile')", Some("app/locks/app.lockfile")),
2811+
(
2812+
"file(\"$projectDir/locks/x.lockfile\")",
2813+
Some("app/locks/x.lockfile"),
2814+
),
2815+
("file(\"${projectDir}/x.lockfile\")", Some("app/x.lockfile")),
2816+
("file('../shared.lockfile')", Some("shared.lockfile")),
2817+
("file('../../outside.lockfile')", None),
2818+
("file('/abs/x.lockfile')", None),
2819+
("file(\"$rootDir/x.lockfile\")", None),
2820+
("file(name)", None),
2821+
("file('x.lockfile').absoluteFile", None),
2822+
("layout.projectDirectory.file('x.lockfile')", None),
2823+
] {
2824+
let text = lock(value);
2825+
let files = [
2826+
("settings.gradle", "include 'app'\n"),
2827+
("app/build.gradle", text.as_str()),
2828+
];
2829+
assert_eq!(path_of(&files), vec![want.map(str::to_string)], "{value}");
2830+
}
2831+
let kts = [(
2832+
"build.gradle.kts",
2833+
"dependencyLocking { lockFile.set(file(\"gradle/x.lockfile\")) }\n",
2834+
)];
2835+
assert_eq!(path_of(&kts), vec![Some("gradle/x.lockfile".to_string())]);
2836+
// Configuring other projects, or from an applied script: unfollowed.
2837+
for text in [
2838+
"subprojects { dependencyLocking { lockFile = file('x.lockfile') } }\n",
2839+
"allprojects { dependencyLocking { lockFile = file('x.lockfile') } }\n",
2840+
"project(':app') { dependencyLocking { lockFile = file('x.lockfile') } }\n",
2841+
"project(path: ':app', configuration: 'default') { dependencyLocking { lockFile = file('x.lockfile') } }\n",
2842+
"project(':app').afterEvaluate { dependencyLocking { lockFile = file('x.lockfile') } }\n",
2843+
"configure(allprojects) { dependencyLocking { lockFile = file('x.lockfile') } }\n",
2844+
"configure(subprojects.findAll { it.name != 'x' }) { dependencyLocking { lockFile = file('x.lockfile') } }\n",
2845+
"gradle.beforeProject { dependencyLocking { lockFile = file('x.lockfile') } }\n",
2846+
"gradle.afterProject { p -> dependencyLocking { lockFile = file('x.lockfile') } }\n",
2847+
] {
2848+
let files = [
2849+
("settings.gradle", "include 'app'\n"),
2850+
("build.gradle", text),
2851+
];
2852+
assert_eq!(path_of(&files), vec![None], "{text}");
2853+
}
2854+
// A project DEPENDENCY before the script's own block is not a block
2855+
// configuring that project.
2856+
for text in [
2857+
"dependencies { implementation(project(\":lib\")) }\ndependencyLocking { lockFile.set(file(\"locks/app.lockfile\")) }\n",
2858+
"dependencies {\n implementation project(':lib')\n}\ndependencyLocking { lockFile = file('locks/app.lockfile') }\n",
2859+
] {
2860+
let files = [
2861+
("settings.gradle", "include 'app', 'lib'\n"),
2862+
("app/build.gradle", text),
2863+
];
2864+
assert_eq!(
2865+
path_of(&files),
2866+
vec![Some("app/locks/app.lockfile".to_string())],
2867+
"{text}"
2868+
);
2869+
}
2870+
let applied = [
2871+
("build.gradle", "apply from: 'locks.gradle'\n"),
2872+
(
2873+
"locks.gradle",
2874+
"dependencyLocking { lockFile = file('x.lockfile') }\n",
2875+
),
2876+
];
2877+
assert_eq!(path_of(&applied), vec![None]);
2878+
26592879
let g = graph(&[
26602880
(
26612881
"settings.gradle",

‎crates/socket-patch-core/src/gradle/locks.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ pub const LOCKFILE_NAMES: &[&str] = &[
2020
"settings-gradle.lockfile",
2121
];
2222

23+
/// Whether `rel` is a settings-classpath lock (`settings-gradle.lockfile`
24+
/// of any build): it locks what the settings script's own classpath
25+
/// resolves, before the script runs.
26+
pub fn is_settings_lock(rel: &str) -> bool {
27+
rel.rsplit('/').next() == Some("settings-gradle.lockfile")
28+
}
29+
2330
/// The legacy per-configuration lock directory, relative to a project.
2431
pub const LEGACY_LOCK_DIR: &str = "gradle/dependency-locks";
2532

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,10 @@ const VENDORED_INDEX_REL: &str = vendored::INDEX_REL;
9494
const VERIFICATION_REL: &str = vendored::VERIFICATION_REL;
9595
const WRAPPER_PROPERTIES_REL: &str = "gradle/wrapper/gradle-wrapper.properties";
9696

97-
/// Whether `rel` is a settings-classpath lock (`settings-gradle.lockfile`
98-
/// of any build). Gradle resolves that classpath before any settings
99-
/// script runs, so the hosted script can never pin what it locks.
100-
pub fn is_settings_lock(rel: &str) -> bool {
101-
rel.rsplit('/').next() == Some("settings-gradle.lockfile")
102-
}
97+
/// Whether `rel` is a settings-classpath lock. Gradle resolves that
98+
/// classpath before any settings script runs, so the hosted script can
99+
/// never pin what it locks.
100+
pub use crate::gradle::locks::is_settings_lock;
103101

104102
/// Whether `files` (root-relative) hold a root Gradle settings or build
105103
/// script.

0 commit comments

Comments
 (0)