Agent mode reads a Maven local repository the build does not use in eleven states: a jar Maven never loads is reported as patched

Đang mở
#2,312 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
rust
Lĩnh vực
backend, cli

Hướng nghiên cứu

Start with core/crawlers/maven_crawler.rs and core/crawlers/maven_crawler/local_repo_tests.rs; read m2_repo_in and its callers to see how Maven’s repository location is resolved. Reproduce the listed Maven 3.9.11 cases and run the three failing local repository tests. Done when agent mode finds the repository Maven uses across the described states and the regression tests pass.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

agent:triaged arch-audit bug priority:p3

[agent] Found by the architecture refactor routine (ledger discussion #2244, lead L51), on main at cfc870f.

Problem

Agent mode patches the jar in the Maven local repository, so it has to find the repository the build uses. docs/ecosystems.md ("Discovery") says it is "found the way Maven finds it". In eleven states it is not. Maven 3.9.11 resolves into one repository, and socket-patch reads another, which is ~/.m2/repository under $HOME in every one of them.

What the user sees depends on whether that other repository holds the artifact:

  • It does (a developer machine or a runner with a persistent home: ~/.m2/repository is the default every other Maven build fills). The scan patches that copy and reports Applied 1 patch and Patched 1 package, exit 0. apply --check says Patches are in sync, and vex writes a not_affected statement. The jar on the build's classpath is unpatched.
  • It does not (a fresh CI runner). scan says No packages found. Run your package manager's install first., exit 0, with no warning. apply over a committed .socket/ exits 1 with The targeted manifest patch matched no installed package, although Maven has just installed it. apply --check exits 0 with Patches are in sync (0 patches checked; 1 patch not installed, skipped).

The states, each run with the real Maven 3.9.11 and the binary built from cfc870f:

# State Maven resolves into socket-patch reads
1 $HOME is not the account's passwd home <passwd home>/.m2/repository $HOME/.m2/repository
2 MAVEN_ARGS=-Duser.home=<dir> <dir>/.m2/repository (and <dir>/.m2/settings.xml) ~/.m2/repository
3 -Duser.home=<dir> in .mvn/maven.config <dir>/.m2/repository ~/.m2/repository
4 -Dmaven.repo.local=<dir> in .mvn/jvm.config <dir> ~/.m2/repository
5 -Duser.home=<dir> in .mvn/jvm.config <dir>/.m2/repository ~/.m2/repository
6 <localRepository> in conf/settings.xml of the Maven on PATH, with MAVEN_HOME and M2_HOME unset that directory ~/.m2/repository
7 ~/.mavenrc sets MAVEN_OPTS=-Dmaven.repo.local=<dir> <dir> ~/.m2/repository
8 -Dmaven.repo.local=<dir> in JAVA_TOOL_OPTIONS <dir> ~/.m2/repository
9 -s ci-settings.xml in .mvn/maven.config, and that file has a <localRepository> that directory ~/.m2/repository
10 MAVEN_ARGS=-s <file>, the same that directory ~/.m2/repository
11 MAVEN_OPTS=-Dmaven.repo.local=.m2/repository (a relative path) <project>/.m2/repository ~/.m2/repository

Controls that are right on main: MAVEN_OPTS=-Duser.home=<dir>, an absolute -Dmaven.repo.local in MAVEN_OPTS, MAVEN_ARGS or .mvn/maven.config, and state 6 with MAVEN_HOME set.

Who it hurts

  • State 11 is the usual way to keep the repository inside the project so a CI cache can hold it (cache: paths: [.m2/repository]).
  • State 1 is a container job whose runner sets HOME to a directory that is not the account's home. GitHub Actions does that in a container: job (HOME=/github/home, while root's passwd home is /root). I did not run GitHub Actions; the state is reproduced below with HOME=<dir>.
  • States 2 and 3 are a container run as a user with no home of its own, where the build passes -Duser.home.
  • States 9 and 10 are a project that commits its settings file.
  • In each, the release build ships the vulnerable jar while the scan step says Patched 1 package, or the apply step of the CI job fails and tells the user to install what is installed.
  • Anyone who reads the VEX document: it says not_affected for a jar the build takes unpatched.

Evidence on main

The project is a pom.xml with one dependency, javax.inject:javax.inject:1. The patch is a whole-jar record (package/javax.inject-1.jar) whose patched jar adds one member, SOCKET_PATCHED.txt, so unzip -l shows which copy was patched. The patch API is a local stand-in that serves the one patch. root's passwd home is /root, and /root/.m2/repository holds the artifact from an earlier build.

State 11:

$ export MAVEN_OPTS=-Dmaven.repo.local=.m2/repository
$ mvn -q dependency:build-classpath -Dmdep.outputFile=/dev/stdout
<w>/p13/.m2/repository/javax/inject/javax.inject/1/javax.inject-1.jar

$ socket-patch scan --mode agent
Found 1 package (1 maven)
Applied 1 patch.
  HIGH  javax.inject/javax.inject@1  CVE-2026-0001
Next steps:
  1. Commit .socket/ to keep the patches.
  2. Run `socket-patch apply` after every dependency install, locally and in CI. A fresh install removes the patches.

Patched 1 package, fixing 1 vulnerability (1 high).
…                                                           (exit 0)

$ for r in .m2/repository ~/.m2/repository; do unzip -l $r/javax/inject/javax.inject/1/javax.inject-1.jar | grep -c SOCKET_PATCHED; done
0                                # the jar Maven puts on the classpath
1                                # ~/.m2/repository
$ socket-patch apply --check
Patches are in sync (1 patch checked).                      (exit 0)
$ socket-patch vex --product pkg:generic/app@1 -O - | jq -c '.statements[] | {status, impact_statement}'
{"status":"not_affected","impact_statement":"Patched via Socket patch ffffffff-0000-4000-8000-000000000001"}

The same state with nothing in ~/.m2/repository:

$ socket-patch scan --mode agent
No packages found. Run your package manager's install first.        (exit 0)
$ socket-patch scan --mode agent --json | jq -c '{status, warnings: [.warnings[]?.code], events: [.events[]? | [.action, .purl, .errorCode]]}'
{"status":"success","warnings":[],"events":[]}

State 1, with a committed .socket/ and a freshly resolved repository, as in CI:

$ export HOME=<w>/ghhome                 # root's passwd home is /root
$ java -XshowSettings:properties -version 2>&1 | grep user.home
    user.home = /root
$ mvn -q dependency:build-classpath -Dmdep.outputFile=/dev/stdout
/root/.m2/repository/javax/inject/javax.inject/1/javax.inject-1.jar

$ socket-patch apply
Error: The targeted manifest patch matched no installed package:
  - pkg:maven/javax.inject/javax.inject@1
Check that the packages are installed and --cwd points to the right directory.

Summary: 0 of 1 targeted patch applied, 0 already patched, 1 not found on disk     (exit 1)
$ socket-patch apply --check
Patches are in sync (0 patches checked; 1 patch not installed, skipped).           (exit 0)
$ HOME=/root socket-patch apply
Summary: 1 of 1 targeted patch applied, 0 already patched, 0 not found on disk     (exit 0)

With the artifact under $HOME/.m2/repository as well, the scan patches that copy, reports Patched 1 package, and vex attests it; the copy in /root/.m2/repository, which Maven reads, has no marker.

The other states gave the same two outcomes. For each I ran mvn -q dependency:build-classpath and socket-patch scan --mode agent under one environment, then listed the marker in every copy of the jar. Three of them, condensed:

S2  MAVEN_ARGS=-Duser.home=<w>/uh2
  maven reads: <w>/uh2/.m2/repository/javax/inject/javax.inject/1/javax.inject-1.jar
  scan: Patched 1 package, fixing 1 vulnerability (1 high).              (exit 0)
  marker: 1 /root/.m2/repository/…/javax.inject-1.jar    0 <w>/uh2/.m2/repository/…/javax.inject-1.jar
  vex:  {"status":"not_affected", …}
S6  MVN on PATH is <w>/maven-install/bin/mvn; its conf/settings.xml has <localRepository><w>/repo7</localRepository>
  maven reads: <w>/repo7/javax/inject/javax.inject/1/javax.inject-1.jar
  marker: 1 /root/.m2/repository/…    0 <w>/repo7/…
  with MAVEN_HOME=<w>/maven-install:  marker: 1 <w>/repo7/…              (right)
S9  .mvn/maven.config is the two lines "-s" and "ci-settings.xml"
  maven reads: <w>/repo11/javax/inject/javax.inject/1/javax.inject-1.jar
  marker: 1 /root/.m2/repository/…    0 <w>/repo11/…

States 3, 4, 5, 7, 8 and 10 read the same way. In state 2 Maven also takes the user settings from <dir>/.m2/settings.xml: a <localRepository> there is followed, and one in /root/.m2/settings.xml is not.

Only Maven 3.9.11 ran (the sandbox refuses to run a Maven downloaded during the run). States 1 and 4 to 8 are the JVM's and the launcher script's doing, not that release's. Not run: an sbt or scala-cli build, whose Coursier and Ivy cache lookups take $HOME first as well (core crawlers/coursier_cache.rs, line 231, and crawlers/ivy_cache.rs, line 79).

Cause

One function, m2_repo_in (core crawlers/maven_crawler.rs, lines 694 to 777), which every caller goes through (JvmEnv::for_project, JvmEnv::from_process):

  • user.home (lines 724 to 739) is -Duser.home in MAVEN_OPTS, else $HOME. The JVM takes it from the passwd entry on Linux and macOS, whatever $HOME says. utils::fs::passwd_home (core utils/fs.rs, line 449) says so in its doc comment, and GradleHome::resolve (core gradle/home.rs, line 52) follows it: "on Unix the passwd entry's home directory (getpwuid(getuid())->pw_dir), NOT $HOME". JvmEnv::for_project gives the Maven lookup no passwd home at all (line 570). Maven also sets a command-line -Duser.home before it derives ~/.m2, and the launcher adds .mvn/jvm.config to the JVM's arguments; neither is read.
  • -Dmaven.repo.local (lines 753 to 760) is read from MAVEN_ARGS, .mvn/maven.config and MAVEN_OPTS, and not from .mvn/jvm.config or JAVA_TOOL_OPTIONS. A relative value from the environment is dropped by rooted (line 749) with no warning, and the lookup goes on to ~/.m2/repository.
  • The settings files (lines 764 to 770) are <user.home>/.m2/settings.xml and conf/settings.xml under $MAVEN_HOME or $M2_HOME. A -s or -gs argument is not read, and neither is the install of the mvn on PATH, which the launcher uses whether or not the variable is set.

The class has been fixed three times, one site at a time: Gradle's passwd home (#646, 0685ba8c), cargo's home with HOME unset (#1135, 230d4939), and the Maven lookup's first version (#423, ca6dc059), whose message says Maven "fixes user.home before command-line -D flags in MAVEN_ARGS apply". State 2 shows Maven 3.9.11 does the opposite.

A unit test pins that claim: maven_args_user_home_does_not_move_the_m2_directory (core crawlers/maven_crawler/local_repo_tests.rs, line 303) asserts that MAVEN_ARGS=-Duser.home=<dir> leaves the repository under $HOME.

The failing tests

Three unit tests for crawlers/maven_crawler/local_repo_tests.rs, written with that file's helpers. All three fail on main:

/// Java's `user.home` on Unix is the account's passwd entry, whatever
/// `HOME` says. The Gradle lookup follows it; so must Maven's.
#[test]
fn unix_user_home_is_the_passwd_entry_for_maven_as_for_gradle() {
    let home = tempfile::tempdir().unwrap();
    let passwd = tempfile::tempdir().unwrap();
    let e = env(&[("HOME", home.path())]);
    let jvm = JvmEnv::resolve(&e, Os::Unix, Some(passwd.path()));
    assert_eq!(
        jvm.gradle.as_ref().map(|g| g.user_home.clone()),
        Some(passwd.path().join(".gradle")),
        "Gradle follows the passwd entry"
    );
    assert_eq!(
        jvm.m2_repo,
        Some(passwd.path().join(".m2").join("repository")),
        "Maven reads user.home too"
    );
}

/// `mvn -Duser.home=<dir>` (MAVEN_ARGS, or a line of `.mvn/maven.config`)
/// moves `~/.m2`: Maven 3.9.11 then resolves into `<dir>/.m2/repository`.
#[test]
fn command_line_user_home_moves_the_m2_directory() {
    let home = tempfile::tempdir().unwrap();
    let elsewhere = tempfile::tempdir().unwrap();
    let mut e = env(&[("HOME", home.path())]);
    e.insert(
        "MAVEN_ARGS".into(),
        format!("-Duser.home={}", elsewhere.path().display()),
    );
    assert_eq!(
        m2_repo(&e, Os::current()),
        Some(elsewhere.path().join(".m2").join("repository"))
    );
}

/// `.mvn/jvm.config` holds JVM arguments the launcher adds to
/// `MAVEN_OPTS`: a `-Dmaven.repo.local` there names the local repository.
#[test]
fn jvm_config_names_the_local_repository() {
    let home = tempfile::tempdir().unwrap();
    let project = tempfile::tempdir().unwrap();
    let data = tempfile::tempdir().unwrap();
    maven_build(project.path());
    write(
        &project.path().join(".mvn").join("jvm.config"),
        &format!("-Xmx512m -Dmaven.repo.local={}\n", data.path().display()),
    );
    let e = env(&[("HOME", home.path())]);
    assert_eq!(
        m2_repo_in(&e, project.path()),
        Some(data.path().to_path_buf())
    );
}
$ cargo test -p socket-patch-core --lib -- crawlers::maven_crawler::local_repo_tests       # condensed; temporary paths named by what they are
---- unix_user_home_is_the_passwd_entry_for_maven_as_for_gradle ----
assertion `left == right` failed: Maven reads user.home too
  left: Some("<$HOME>/.m2/repository")
 right: Some("<passwd home>/.m2/repository")
---- command_line_user_home_moves_the_m2_directory ----
assertion `left == right` failed
  left: Some("<$HOME>/.m2/repository")
 right: Some("<dir>/.m2/repository")
---- jvm_config_names_the_local_repository ----
assertion `left == right` failed
  left: Some("<$HOME>/.m2/repository")
 right: Some("<dir>")
test result: FAILED. 0 passed; 3 failed

In the first test the Gradle assertion passes and the Maven one fails: the two lookups get one environment and disagree.

Planned fix

In m2_repo_in, so every caller gets it:

  1. user.home in Maven's order: a command-line -Duser.home (MAVEN_ARGS, .mvn/maven.config), then the JVM's (MAVEN_OPTS, .mvn/jvm.config, JAVA_TOOL_OPTIONS), then on Unix the passwd entry, as GradleHome::resolve takes it, then $HOME. Add a note like gradle_user_home_differs when the passwd home and $HOME differ.
  2. -Dmaven.repo.local from .mvn/jvm.config and JAVA_TOOL_OPTIONS too, below the command line's.
  3. The settings files Maven reads: -s and -gs from MAVEN_ARGS and .mvn/maven.config, and the conf/settings.xml of the mvn on PATH when no variable names the install.
  4. A relative -Dmaven.repo.local from the environment: take it from the project directory for a project run, as the .mvn/maven.config value already is, and warn instead of reading another repository where that cannot be done (--global).
  5. ~/.mavenrc is a shell script and cannot be read in general. Warn when it exists and mentions maven.repo.local or user.home, and point at MAVEN_REPO_LOCAL.
  6. Replace maven_args_user_home_does_not_move_the_m2_directory with the tests above, and add one per state.

The behavior change: with $HOME and the passwd home different, the repository read moves from $HOME/.m2/repository to the passwd home's, which is where Maven writes. docs/ecosystems.md states the rule ("found the way Maven finds it … then .m2/repository in user.home"), so I read this as a fix, not a decision. Tests that isolate the Maven repository through HOME alone will need MAVEN_REPO_LOCAL, as the Gradle tests set GRADLE_USER_HOME. Say so here if $HOME should keep winning for Maven.

Not claimed: three of this routine's PRs are open, so this run opens none.


Generated by Claude Code

Ngôn ngữ chính
Rust
Star
9
Fork
2
Merge trung bình
1 ngày 8 giờ
Pull request đã merge (30 ngày)
313

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của SocketDev/socket-patch

Tất cả issue của SocketDev/socket-patch

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.