Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion crates/socket-patch-cli/CLI_CONTRACT.md
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,17 @@ the model is **not uniform** today:
is matched to its own release variant. A build is then not the only candidate, so the default
mismatch policy never overwrites it: a build whose bytes match no manifest variant is left
untouched, fails `apply` (`no matching variant found`) and withholds the `vex` statement, and
`rollback` skips it. PyPI follows the same rule: when the crawler resolves
`rollback` skips it. `vex` and `apply --check` leave out one kind of dir: a build whose gem
home has no gemspec for it (`specifications/<leaf>.gemspec`, or
`specifications/default/<leaf>.gemspec`) while another copy of the same version in that home has
its own. RubyGems and Bundler load a gem by that file alone, so nothing loads the dir. It is what
the stale-install remedy (`rm -f specifications/<leaf>.gemspec … && bundle install`) leaves of a
precompiled build once the `ruby` platform build is installed beside it, and an unpatched one
no longer withholds the `vex` statement (#2087); `bundle clean --force` removes it. The dir
stays a copy when no copy of the version in its home has a gemspec (so a gem never loses its
last copy in a home this way), when its own gemspec cannot be probed, and when a standalone
bundle's `bundler/setup.rb` names it. `apply` and `rollback` write and restore it like any
other dir. PyPI follows the every-copy rule too: when the crawler resolves
one release in several site-packages dirs (a Pipenv WORKON_HOME venv beside an auto-detected
`./.venv`, or the user site beside a system dir in global scope), agent `apply` patches **every**
copy, one summary event per copy, because any of them may be the one the interpreter imports.
Expand Down
14 changes: 14 additions & 0 deletions crates/socket-patch-cli/src/ecosystem_dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,20 @@ pub async fn find_manifest_package_copies_reusing(
}
}
}
// The dir of a gem build whose gemspec is gone, beside a copy of the
// version that the gem home does list, is no copy RubyGems or Bundler
// loads, so an unpatched one must not block the attestation (#2087).
// Rollback and remove, which resolve without this, still restore it.
for (_, paths) in copies
.iter_mut()
.filter(|(purl, paths)| purl.starts_with("pkg:gem/") && paths.len() > 1)
{
*paths = socket_patch_core::crawlers::ruby_crawler::retain_loaded_gem_builds(
std::mem::take(paths),
)
.await;
}
copies.retain(|_, paths| !paths.is_empty());
copies
}

Expand Down
64 changes: 64 additions & 0 deletions crates/socket-patch-cli/tests/apply/in_process_gem_multicopy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,70 @@ fn vex_and_check_refuse_an_unpatched_platform_variant_copy() {
);
}

/// #2087: the hosted scan's stale-install remedy (`rm -f
/// specifications/<leaf>.gemspec cache/<leaf>.gem && bundle install`)
/// leaves the old platform build's `gems/<leaf>/` dir behind with no
/// gemspec, beside the installed ruby-platform build. RubyGems and Bundler
/// load a gem by its gemspec alone, so that dir is not an installed copy:
/// `vex` attests the patched ruby-platform build and `apply --check`
/// reports it in sync. With its gemspec back, the platform build is a copy
/// again and both gates refuse it (#1092). `apply` and `rollback` write and
/// restore the dir either way.
#[test]
fn the_leftover_dir_of_an_unlisted_platform_build_is_not_a_copy() {
let tmp = tempfile::tempdir().unwrap();
let (root, plain_file, platform_file, original, patched) =
build_platform_variant_project(tmp.path());
let specs = root.join("vendor/bundle/ruby/3.2.0/specifications");
std::fs::create_dir_all(&specs).unwrap();
std::fs::write(specs.join("rack-3.1.0.gemspec"), "").unwrap();
std::fs::write(&plain_file, &patched).unwrap();

let (code, stdout, stderr) = run_raw(&root, &["vex", "--product", "pkg:gem/app@1.0.0"]);
assert_eq!(code, 0, "vex\nstdout:\n{stdout}\nstderr:\n{stderr}");
assert!(stdout.contains("not_affected"), "vex doc:\n{stdout}");
assert!(
!stderr.contains("not_applied"),
"vex must not judge the dir nothing loads\nstderr:\n{stderr}"
);
let (code, stdout, stderr) = run_raw(&root, &["apply", "--check"]);
assert_eq!(
code, 0,
"apply --check\nstdout:\n{stdout}\nstderr:\n{stderr}"
);
assert_eq!(std::fs::read(&platform_file).unwrap(), original);

// The platform build installed again (its gemspec is back): RubyGems
// loads it, and it is unpatched.
std::fs::write(specs.join("rack-3.1.0-x86_64-linux.gemspec"), "").unwrap();
let (code, stdout, stderr) = run_raw(&root, &["vex", "--product", "pkg:gem/app@1.0.0"]);
assert!(
!stdout.contains("not_affected"),
"vex must not attest an unpatched installed platform copy (exit {code})\
\nstdout:\n{stdout}\nstderr:\n{stderr}"
);
let (code, stdout, stderr) = run_raw(&root, &["apply", "--check"]);
assert_ne!(
code, 0,
"apply --check must flag the installed platform copy\
\nstdout:\n{stdout}\nstderr:\n{stderr}"
);

// The write paths never skipped the dir: with its gemspec gone again,
// `apply` still patches it and `rollback` still restores it, so no
// patched file is left that nothing tracks.
std::fs::remove_file(specs.join("rack-3.1.0-x86_64-linux.gemspec")).unwrap();
std::fs::write(&plain_file, &original).unwrap();
let (code, v) = run_json(&root, &["apply"]);
assert_eq!(code, 0, "apply; envelope={v}");
assert_eq!(std::fs::read(&plain_file).unwrap(), patched);
assert_eq!(std::fs::read(&platform_file).unwrap(), patched);
let (code, v) = run_json(&root, &["rollback", "--yes"]);
assert_eq!(code, 0, "rollback; envelope={v}");
assert_eq!(std::fs::read(&plain_file).unwrap(), original);
assert_eq!(std::fs::read(&platform_file).unwrap(), original);
}

/// #1092, global mode: `apply -g --global-prefix <gem home>/gems` patches
/// the platform build too (plain RubyGems activates it, no Bundler needed).
#[test]
Expand Down
71 changes: 71 additions & 0 deletions crates/socket-patch-cli/tests/e2e_vex_redirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2808,3 +2808,74 @@ fn gem_hosted_ref_is_verified_under_an_out_of_tree_config_bundle_path() {
// Read-only: vex never writes the refused root.
assert_eq!(std::fs::read(bundle.join(installed)).unwrap(), patched);
}

/// Gem (#2087): a project that moved from a precompiled build to the
/// `ruby` platform build so hosted mode could patch it, then ran the scan's
/// remedy (`rm -f specifications/<leaf>.gemspec cache/<leaf>.gem && bundle
/// install`), has the patched `rails-7.0.0/` beside a `rails-7.0.0-
/// x86_64-linux/` dir with no gemspec. Nothing loads that dir, so `vex`
/// attests the gem, in the project's bundle path and in an out-of-tree
/// one alike. With its gemspec back the platform build is installed and
/// unpatched, and `vex` refuses again (#1092).
#[test]
fn gem_hosted_ref_is_attested_beside_a_leftover_platform_dir_with_no_gemspec() {
const U: &str = "77777777-7777-7777-7777-777777777777";
let purl = "pkg:gem/rails@7.0.0";
let (pristine, patched) = (
&b"module Rails; STATUS = :vulnerable; end\n"[..],
&b"module Rails; STATUS = :patched; end\n"[..],
);
for out_of_tree in [false, true] {
let tmp = tempfile::tempdir().unwrap();
let cwd = tmp.path().join("app");
std::fs::create_dir_all(&cwd).unwrap();
for file in ["Gemfile", "Gemfile.lock"] {
let text = redirect_fixture(&format!("gem/bundler/basic/expected/{file}"));
std::fs::write(cwd.join(file), text).unwrap();
}
let (bundle, configured) = if out_of_tree {
let bundle = tmp.path().join("outside bundle");
(bundle.clone(), bundle.display().to_string())
} else {
(cwd.join("vendor/bundle"), "vendor/bundle".to_string())
};
put(
&cwd,
".bundle/config",
format!("---\nBUNDLE_PATH: \"{configured}\"\n").as_bytes(),
);
let (_rt, server) = serve_patch_views(vec![(
U.to_string(),
one_file_view(U, purl, "lib/rails.rb", pristine, patched),
)]);
let args = ["--proxy-url", &server.uri()];
let home = bundle.join("ruby/3.3.0");
put(&home, "gems/rails-7.0.0/lib/rails.rb", patched);
put(&home, "specifications/rails-7.0.0.gemspec", b"");
let leftover = "gems/rails-7.0.0-x86_64-linux/lib/rails.rb";
put(&home, leftover, pristine);

let (code, env) = vex_json(&cwd, &args);
let label = format!("leftover platform dir (out of tree: {out_of_tree})");
assert_attested(&cwd, code, &env, U, &label);
// Read-only: the leftover dir is not touched.
assert_eq!(std::fs::read(home.join(leftover)).unwrap(), pristine);

put(
&home,
"specifications/rails-7.0.0-x86_64-linux.gemspec",
b"",
);
let (code, env) = vex_json(&cwd, &args);
assert_eq!(
code,
Some(1),
"an installed unpatched platform build (out of tree: {out_of_tree}): {env}"
);
let reason = skipped_reason(&env, purl);
assert!(
reason == "not_applied" || reason == "hash_mismatch",
"the platform build was read and failed verification, got {reason}: {env}"
);
}
}
Loading
Loading