Skip to content

Commit 17393a7

Browse files
mikolalysenkoclaude
andcommitted
Never let the Maven repository filter refuse a run or fail a build
The filter keeps Maven from asking a Socket repository for anything but the patched version. It is an extra, and the review found eight ways it could cost more than that. Each is fixed here; two questions about the filter's default stay with the owner and change no behaviour. A run is never refused for the filter: - A `.mvn/maven.config` that is a symbolic link, or sits in a directory that does not take the write, could not take the filter's two lines, and the scan refused every ecosystem where it used to land the pin. The engine now tells the rewriter that the config cannot be written; the filter is skipped, the pin lands, and the run says so. A config that already holds the lines needs no write and gets its files. - A link or a directory under the name of a pin's filter file is not read and not written through; that pin lands without a file. - On a rollback or remove the stripped config was staged with an unconditional write, so a linked or since-edited config restored nothing. It is staged only where the flush could write it; otherwise the lines stay and the restore says which. - A filter directory that cannot be listed to its end is no empty directory: nothing in it is deleted, the lines that name it stay, and the restore says so. A build is never failed by it: - A scan emptied the filter file of a repository it took out of the poms. If the wiring came back by a pom-only revert or a merge before the next scan, Maven refused the pin. A retired file is now left exactly as it is: no pom names its repository, so Maven does not read it, and it is the right file if the wiring returns. With nothing emptied, the path that repaired an emptied file is gone. - A filter file is read for what it lists, not compared byte for byte: a CRLF checkout or an added comment no longer makes every scan report a rewrite. Nobody's own filter is switched off without a word: - A scan that took out the last Socket repository stripped the two lines even when the user keeps prefix files of their own in the directory, which are read through those lines. It now leaves them, as a rollback does, and wherever the config cannot take the write. - A prefix filter named in the environment of the machine that scans (`MAVEN_OPTS`, `MAVEN_ARGS`, `JAVA_TOOL_OPTIONS`, `JDK_JAVA_OPTIONS`; Maven 3.9.9 and 4.0.0-rc-7 honour the property in each) is a conflict like one in `.mvn/jvm.config`: nothing is written for the filter. - The run that adds the two lines says what they change for the whole build: Maven reads prefix files from `.mvn/socket-patch/` only. One reader lists the filter directory for the engine, the restore and the vendored eject (hosted mode's own regular files, what else is there, whether it could be listed), and one test tells a hosted file by its path. `retire` no longer marks a run for a file the run itself planned and took back. Outcome changes: three new warning codes (`redirect_maven_repository_filter_enabled`, `redirect_maven_repository_filter_skipped`, `maven_repository_filter_left`); a scan no longer reports a filter file as `removed`, and no longer writes one it retires; a hosted scan or a rollback of a project whose `.mvn/maven.config` is a link now succeeds where the filter alone made it fail. No exit status is added. Golden fixtures: the seventeen Maven pom cases that wire a pin and pin a warning list, and the sbt case whose pom is wired, gain `redirect_maven_repository_filter_enabled`. No expected file or edit changes. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent c14138f commit 17393a7

28 files changed

Lines changed: 1431 additions & 374 deletions

File tree

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

Lines changed: 75 additions & 34 deletions
Large diffs are not rendered by default.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1801,8 +1801,9 @@ impl EjectSnapshot {
18011801
// files only. Anything else in that directory (a subdirectory, a
18021802
// link, a file of the user's) is not the restore's to touch, and
18031803
// must not be read as a file here.
1804+
let disk = socket_patch_core::vendor::lock_inventory::ProjectView::Disk(root);
18041805
planned.extend(
1805-
socket_patch_core::patch::redirect::hosted_maven_filter_dir(root)
1806+
socket_patch_core::patch::redirect::hosted_maven_filter_dir(&disk)
18061807
.await
18071808
.owned,
18081809
);

‎crates/socket-patch-cli/tests/e2e_redirect_maven_build.rs‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1960,6 +1960,90 @@ mod mediation {
19601960
}
19611961
}
19621962

1963+
/// The repository filter is an extra, and a pin never waits for it. A
1964+
/// project whose `.mvn/maven.config` is a symbolic link cannot take the
1965+
/// filter's two lines: a write would replace the link, which a scan
1966+
/// refuses for every file. The scan lands the pin without the filter,
1967+
/// exits 0 and says so, where the two lines used to refuse the whole
1968+
/// run. Maven reads the config through the link and resolves the
1969+
/// patched jar, a re-scan writes nothing, and a rollback puts every
1970+
/// file back.
1971+
#[cfg(unix)]
1972+
#[test]
1973+
#[ignore = "real Maven + Maven Central (dependency plugin); run with --ignored"]
1974+
fn maven_hosted_lands_the_pin_where_the_config_is_a_link() {
1975+
let Some(mvn) = Mvn::detect(SUITE) else {
1976+
return;
1977+
};
1978+
let b = Bench::new();
1979+
let proj = b.root.join("linked");
1980+
fixture::write_project(&proj, Shape::Selected, &b.server);
1981+
let Some(before) = fixture::resolve(SUITE, &mvn, &proj, &b.m2, &b.plain, "target/before")
1982+
else {
1983+
return;
1984+
};
1985+
// The team keeps its Maven arguments in one place and links them
1986+
// in: a setting of its own, and the Trusted Checksums settings (an
1987+
// earlier scan wrote them before the file was moved), so the
1988+
// filter's two lines are the only write the config would take.
1989+
let config = [
1990+
"-B",
1991+
"-Daether.artifactResolver.postProcessor.trustedChecksums=true",
1992+
"-Daether.artifactResolver.postProcessor.trustedChecksums.checksumAlgorithms=SHA-256",
1993+
"-Daether.artifactResolver.postProcessor.trustedChecksums.failIfMissing=false",
1994+
"-Daether.trustedChecksumsSource.summaryFile=true",
1995+
"-Daether.trustedChecksumsSource.summaryFile.basedir=${maven.multiModuleProjectDirectory}/.mvn/checksums",
1996+
"-Daether.trustedChecksumsSource.summaryFile.originAware=false",
1997+
"",
1998+
]
1999+
.join("\n");
2000+
std::fs::create_dir_all(proj.join("ci")).unwrap();
2001+
std::fs::create_dir_all(proj.join(".mvn")).unwrap();
2002+
std::fs::write(proj.join("ci/maven.config"), &config).unwrap();
2003+
let link = proj.join(".mvn/maven.config");
2004+
std::os::unix::fs::symlink("../ci/maven.config", &link).unwrap();
2005+
let pristine = fixture::snapshot(&proj);
2006+
2007+
let (code, env, stderr) = b.scan(&proj);
2008+
assert_eq!(code, Some(0), "{env}\n{stderr}");
2009+
assert_eq!(env["summary"]["applied"], 1, "{env}");
2010+
let wired = std::fs::read_to_string(proj.join("pom.xml")).unwrap();
2011+
assert!(
2012+
wired.contains(&b.suffixed) && wired.contains(&format!("<id>socket-patch-{UUID}</id>")),
2013+
"the pin landed:\n{wired}"
2014+
);
2015+
let skipped = warning(&env, "redirect_maven_repository_filter_skipped")
2016+
.unwrap_or_else(|| panic!("the scan says the filter was skipped: {env}"));
2017+
assert!(
2018+
skipped["detail"]
2019+
.as_str()
2020+
.is_some_and(|detail| detail.contains(".mvn/maven.config cannot be written")),
2021+
"{skipped}"
2022+
);
2023+
assert!(link.is_symlink(), "the link is still a link");
2024+
assert_eq!(
2025+
std::fs::read_to_string(proj.join("ci/maven.config")).unwrap(),
2026+
config,
2027+
"and nothing was written through it"
2028+
);
2029+
assert!(
2030+
!proj.join(".mvn/socket-patch").exists(),
2031+
"no filter file without the lines that would make Maven read it"
2032+
);
2033+
2034+
let after = fixture::resolve_warm(&mvn, &proj, &b.m2, &b.mirrored, "target/after", &[]);
2035+
b.assert_only_the_victim_is_patched("behind a linked config", &before, &after);
2036+
b.assert_rescan_is_a_no_op(&proj);
2037+
2038+
b.rollback(&proj);
2039+
assert!(link.is_symlink());
2040+
assert_eq!(
2041+
fixture::snapshot(&proj),
2042+
pristine,
2043+
"rollback restores every file byte for byte and leaves none behind"
2044+
);
2045+
}
2046+
19632047
/// A company's `<mirrorOf>*</mirrorOf>` sends every repository to its
19642048
/// repository manager, the Socket one included, and the manager does
19652049
/// not hold the patched version:

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ pub(crate) fn owned(file_name: &str) -> Option<&str> {
128128
crate::patch::redirect::generation::pin_name_uuid(id, false).map(|_| id)
129129
}
130130

131+
/// The Socket repository id whose filter the project-relative path `rel`
132+
/// is: a file of [`DIR`] under a name hosted mode writes ([`owned`]).
133+
pub(crate) fn owned_rel(rel: &str) -> Option<&str> {
134+
rel.strip_prefix(DIR)?.strip_prefix('/').and_then(owned)
135+
}
136+
131137
/// The path prefix of one version of an artifact, as the maven2 layout
132138
/// spells it: `/<group/path>/<artifact>/<version>`.
133139
pub(crate) fn prefix(group_id: &str, artifact_id: &str, version: &str) -> String {
@@ -153,6 +159,17 @@ pub(crate) fn file_text(
153159
)
154160
}
155161

162+
/// Whether the filter file `text` lets its repository serve `prefix` on
163+
/// every Maven that reads such a file: its first line is the header Maven 4
164+
/// requires, and one of its lines is the path. Read as Maven reads it, line
165+
/// by line with the line ending off, so a file checked out with CRLF line
166+
/// endings, or one someone added a comment or another path to, says the
167+
/// same as the bytes hosted mode wrote.
168+
pub(crate) fn lists(text: &str, prefix: &str) -> bool {
169+
let mut lines = text.lines();
170+
lines.next() == Some(MAGIC) && lines.any(|line| line == prefix)
171+
}
172+
156173
/// What `.mvn/maven.config` needs for Maven to read the filter files.
157174
#[derive(Debug, PartialEq, Eq)]
158175
pub(crate) enum Config {
@@ -342,6 +359,15 @@ mod tests {
342359
] {
343360
assert_eq!(owned(theirs), None, "{theirs}");
344361
}
362+
assert_eq!(owned_rel(&rel), Some(ID));
363+
for theirs in [
364+
".mvn/socket-patch/prefixes-central.txt",
365+
".mvn/socket-patchy/prefixes-socket-patch-77777777-7777-7777-7777-777777777777.txt",
366+
"prefixes-socket-patch-77777777-7777-7777-7777-777777777777.txt",
367+
".mvn/socket-patch",
368+
] {
369+
assert_eq!(owned_rel(theirs), None, "{theirs}");
370+
}
345371
let text = file_text(ID, "org.slf4j", "slf4j-api", "1.7.36-socket.77777777");
346372
let mut lines = text.lines();
347373
// Maven 4 discards a file whose first line is anything else.
@@ -351,6 +377,37 @@ mod tests {
351377
assert!(text.ends_with('\n'));
352378
}
353379

380+
/// A file is judged by what Maven reads in it, not by its bytes: the
381+
/// header first, and the pinned path on a line of its own.
382+
#[test]
383+
fn a_file_lists_a_path_whatever_its_line_endings_and_comments() {
384+
let path = "/org/slf4j/slf4j-api/1.7.36-socket.77777777";
385+
let written = file_text(ID, "org.slf4j", "slf4j-api", "1.7.36-socket.77777777");
386+
assert!(lists(&written, path));
387+
for same in [
388+
written.replace('\n', "\r\n"),
389+
written.trim_end().to_string(),
390+
format!("{written}# kept for the audit\n"),
391+
format!("{written}/com/example/other/1.0\n"),
392+
format!("## repository-prefixes/2.0\n\n{path}\n"),
393+
] {
394+
assert!(lists(&same, path), "{same:?}");
395+
}
396+
for other in [
397+
String::new(),
398+
// No header: Maven 4 discards the file.
399+
format!("{path}\n"),
400+
format!("# mine\n{written}"),
401+
// Another version, a longer path, the path commented out.
402+
written.replace("77777777", "88888888"),
403+
written.replace(path, &format!("{path}/extra")),
404+
written.replace(path, &format!("# {path}")),
405+
written.replace(path, &format!("{path} ")),
406+
] {
407+
assert!(!lists(&other, path), "{other:?}");
408+
}
409+
}
410+
354411
/// The directory is named through the property the project's Maven
355412
/// reads, the one rule the Trusted Checksums directory follows.
356413
#[test]

0 commit comments

Comments
 (0)