Skip to content
Open
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
100 changes: 88 additions & 12 deletions crates/socket-patch-cli/src/commands/vlt_heal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,13 @@ fn reinstall_detail(tally: &HealTally) -> String {
held,
)
} else if tally.undeterminable > 0 {
with_optional_kept(
undeterminable_detail(tally.undeterminable),
tally.optional_left,
held,
)
let mut detail = unchecked_detail(
"vlt-lock.json pins Socket-patched packages",
tally.undeterminable,
"patched",
);
detail.push('.');
with_optional_kept(detail, tally.optional_left, held)
} else if tally.invalidated > 0 {
let mut detail = with_optional_kept(
format!(
Expand Down Expand Up @@ -201,7 +203,11 @@ fn rollback_detail(tally: &HealTally, restored: usize) -> Option<String> {
tally.stale_left
)
} else if tally.undeterminable > 0 {
undeterminable_detail(tally.undeterminable)
unchecked_detail(
&format!("restored registry pins for {restored} packages"),
tally.undeterminable,
"registry",
)
} else if tally.invalidated > 0 && tally.optional_left > 0 {
format!(
"restored registry pins for {restored} packages; removed {} patched installed \
Expand Down Expand Up @@ -235,7 +241,11 @@ fn takeover_detail(tally: &HealTally, vendored: usize) -> Option<String> {
tally.stale_left
)
} else if tally.undeterminable > 0 {
undeterminable_detail(tally.undeterminable)
unchecked_detail(
&format!("vendored {vendored} hosted-pinned packages"),
tally.undeterminable,
"vendored",
)
} else if tally.invalidated > 0 && tally.optional_left > 0 {
format!(
"vendored {vendored} hosted-pinned packages; removed {} hosted installed copies, so \
Expand All @@ -259,11 +269,15 @@ fn takeover_detail(tally: &HealTally, vendored: usize) -> Option<String> {
Some(with_optional_kept(detail, tally.optional_left, held))
}

fn undeterminable_detail(n: usize) -> String {
/// The arm for copies the heal could not judge and so kept. `did` is what
/// the run did to the lock and `installs` the kind of package the lock now
/// installs: a rollback's lock no longer pins a Socket patch, so it must
/// not ask for the patched packages the hosted rewrite asks for.
fn unchecked_detail(did: &str, unchecked: usize, installs: &str) -> String {
format!(
"vlt-lock.json pins Socket-patched packages, but socket-patch could not check {n} \
installed copies (node_modules is a link, or node_modules/.vlt-lock.json is missing \
or unreadable); run `vlt ci` to be sure the patched packages are installed."
"{did}, but socket-patch could not check {unchecked} installed copies (node_modules \
is a link, or node_modules/.vlt-lock.json is missing or unreadable); run `vlt ci` to \
be sure the {installs} packages are installed"
)
}

Expand Down Expand Up @@ -465,6 +479,63 @@ mod tests {
assert!(reinstall_detail(&tally).contains("still holds 3 unpatched copies and"));
}

/// A copy the heal could not check is reported in the direction of the
/// run, like every other arm: after a rollback the lock no longer pins
/// a Socket-patched package, and after a takeover the hosted pin is
/// gone.
#[test]
fn an_unchecked_copy_is_reported_in_the_direction_of_the_run() {
let tally = HealTally {
undeterminable: 1,
..HealTally::default()
};
let unchecked = "socket-patch could not check 1 installed copies (node_modules is a \
link, or node_modules/.vlt-lock.json is missing or unreadable)";
assert_eq!(
reinstall_detail(&tally),
format!(
"vlt-lock.json pins Socket-patched packages, but {unchecked}; run `vlt ci` to \
be sure the patched packages are installed."
)
);
assert_eq!(
rollback_detail(&tally, 2).unwrap(),
format!(
"restored registry pins for 2 packages, but {unchecked}; run `vlt ci` to be \
sure the registry packages are installed"
)
);
assert_eq!(
takeover_detail(&tally, 2).unwrap(),
format!(
"vendored 2 hosted-pinned packages, but {unchecked}; run `vlt ci` to be sure \
the vendored packages are installed"
)
);

// The kept optional copies follow as their own sentence.
let tally = HealTally {
optional_left: 3,
..tally
};
let rollback = rollback_detail(&tally, 2).unwrap();
assert!(
rollback.contains(
"be sure the registry packages are installed. node_modules also still holds 3 \
patched copies of optional dependencies; "
),
"{rollback}"
);
let takeover = takeover_detail(&tally, 2).unwrap();
assert!(
takeover.contains(
"be sure the vendored packages are installed. node_modules also still holds 3 \
installed copies of the vendored optional dependencies; "
),
"{takeover}"
);
}

#[test]
fn kept_optional_copies_are_reported_alongside_every_variant() {
let optional = |invalidated, stale_left, undeterminable| HealTally {
Expand Down Expand Up @@ -505,7 +576,12 @@ mod tests {
);
assert_eq!(
reinstall_detail(&optional(0, 0, 1)),
format!("{} {unpatched}", undeterminable_detail(1))
format!(
"vlt-lock.json pins Socket-patched packages, but socket-patch could not check 1 \
installed copies (node_modules is a link, or node_modules/.vlt-lock.json is \
missing or unreadable); run `vlt ci` to be sure the patched packages are \
installed. {unpatched}"
)
);

assert_eq!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,38 @@ async fn vlt_failed_vendor_after_the_takeover_revert_keeps_the_hosted_pin() {
);
}

/// A takeover that cannot check the hosted store copy (no hidden lock)
/// keeps it and reports it as a takeover: the package is vendored now, so
/// the advisory does not speak of a pin in `vlt-lock.json`.
#[tokio::test(flavor = "multi_thread")]
async fn vlt_hosted_then_vendored_takeover_reports_an_unchecked_copy_as_a_takeover() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
let server = MockServer::start().await;
mock_api(&server).await;
hosted_project(root, &server).await;
std::fs::remove_file(root.join("node_modules/.vlt-lock.json")).unwrap();

let (code, env, stderr) = scan(root, &server, "vendored", &[]);

assert_eq!(code, 0, "{env:#}\n{stderr}");
assert!(
hosted::read(root, "vlt-lock.json").contains(&rel()),
"{env:#}"
);
assert_eq!(
detail_of(&env, "redirect_vlt_reinstall_required"),
"vendored 1 hosted-pinned packages, but socket-patch could not check 1 installed copies \
(node_modules is a link, or node_modules/.vlt-lock.json is missing or unreadable); run \
`vlt ci` to be sure the vendored packages are installed"
);
assert_eq!(
std::fs::read(hosted::store_dir(root, TILDE_ID).join("index.js")).unwrap(),
PATCHED,
"a copy the heal cannot judge is kept"
);
}

/// An optional hosted pin taken over by `scan --mode vendored`: the
/// hosted store copy stays (vlt would not reinstall a removed optional
/// node), and the advisory says how to refresh it.
Expand Down
57 changes: 57 additions & 0 deletions crates/socket-patch-cli/tests/rollback/rollback_render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,63 @@ fn vlt_hosted_rollback_dry_run_keeps_the_store_and_wet_human_run_heals() {
assert!(!root.join("node_modules/.vlt-lock.json").exists());
}

/// A rollback that cannot check an installed copy (vlt's hidden lock is
/// gone, as after a heal that removed another package's stale copy) keeps
/// the copy and reports it as a rollback, in human and `--json` mode: the
/// registry pins are restored and `vlt ci` installs the registry packages.
/// The hosted flow's "vlt-lock.json pins Socket-patched packages" is false
/// here, since the run has just taken the pin out.
#[test]
fn vlt_hosted_rollback_reports_an_unchecked_copy_as_a_rollback() {
let unchecked = "restored registry pins for 1 packages, but socket-patch could not check 1 \
installed copies (node_modules is a link, or node_modules/.vlt-lock.json \
is missing or unreadable); run `vlt ci` to be sure the registry packages \
are installed";
let registry = NpmRegistry::start(&[("left-pad", "1.3.0")]);
for json in [false, true] {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
let store = write_vlt_hosted_fixture(root);
std::fs::remove_file(root.join("node_modules/.vlt-lock.json")).unwrap();

let mut args = vec!["rollback", "--yes"];
if json {
args.push("--json");
}
let (code, stdout, stderr) = run_hosted(root, &args, Some(&registry));
assert_eq!(code, 0, "json={json}\n{stdout}\n{stderr}");
let restored = std::fs::read_to_string(root.join("vlt-lock.json")).unwrap();
assert!(
restored.contains(UPSTREAM_INTEGRITY) && !restored.contains("patch.socket.dev"),
"json={json}: the registry pin is back:\n{restored}"
);
assert!(
store.join("index.js").exists(),
"json={json}: a copy the heal cannot judge is kept"
);
if json {
let doc = common::envelope::parse_json_envelope(&stdout);
let details: Vec<&str> = doc["warnings"]
.as_array()
.into_iter()
.flatten()
.filter(|w| w["code"] == "redirect_vlt_reinstall_required")
.filter_map(|w| w["detail"].as_str())
.collect();
assert_eq!(details, [unchecked], "{doc:#}");
} else {
assert!(
stderr.contains(&format!("Warning: {unchecked}")),
"the advisory speaks of the rollback, not of a pin:\n{stderr}"
);
assert!(
!stderr.contains("vlt-lock.json pins Socket-patched packages"),
"{stderr}"
);
}
}
}

/// Human twin of `manifest_write_failure_warns_and_exits_one`: the failed
/// manifest update prints the "Error: Failed to update the manifest:"
/// stderr line, exit 1, manifest byte-identical — after the file restore
Expand Down
4 changes: 3 additions & 1 deletion docs/testing/vlt-coverage.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@
],
"redirect_vlt_reinstall_required: copies could not be checked": [
"scan_redirect_vlt_heal_undeterminable_node_modules_symlink_not_deleted",
"scan_redirect_vlt_undeterminable_vex_not_attested"
"scan_redirect_vlt_undeterminable_vex_not_attested",
"vlt_hosted_rollback_reports_an_unchecked_copy_as_a_rollback",
"vlt_hosted_then_vendored_takeover_reports_an_unchecked_copy_as_a_takeover"
],
"redirect_vlt_reinstall_required: none stale": [
"scan_redirect_vlt_rerun_does_not_invalidate_healthy_tree"
Expand Down
Loading