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
101 changes: 99 additions & 2 deletions crates/socket-patch-cli/CLI_CONTRACT.md

Large diffs are not rendered by default.

136 changes: 118 additions & 18 deletions crates/socket-patch-cli/src/commands/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,13 @@ async fn run_check(args: &ApplyArgs, manifest_path: &Path) -> i32 {
if reason == NO_MATCHING_VARIANT {
detail.push_str(&no_variant_check_remedy(&args.common, &failed.purl));
}
if reason == MULTI_RELEASE_UNPATCHED_COPY {
// Which jar, which class, which copy.
let jars = outcome.multi_release_copies.iter();
let jars = jars.filter(|(purl, _)| *purl == failed.purl);
let jars: Vec<&str> = jars.map(|(_, what)| what.as_str()).collect();
detail.push_str(&format!(" ({})", jars.join("; ")));
}
drifts.push((failed.purl, reason.to_string(), detail));
}
}
Expand Down Expand Up @@ -694,9 +701,12 @@ async fn run_check(args: &ApplyArgs, manifest_path: &Path) -> i32 {
}
// A go consumer-sync drift names its own go command; apply
// cannot regenerate vendor/ or go.sum. Nor does it change a
// copy no release variant matches: that drift says so itself.
// copy no release variant matches, or a versioned copy of a
// multi-release jar its patch does not list: those drifts say
// so themselves.
if drifts.iter().any(|(_, code, _)| {
code != NO_MATCHING_VARIANT
&& code != MULTI_RELEASE_UNPATCHED_COPY
&& code != socket_patch_core::vendor::go_consumer_sync::VENDOR_MODULES_TXT_CODE
&& code != socket_patch_core::vendor::go_consumer_sync::REQUIREMENTS_CODE
&& code != socket_patch_core::vendor::go_consumer_sync::NATIVE_VENDOR_CODE
Expand Down Expand Up @@ -1000,10 +1010,21 @@ fn describe_check_failure(reason: &str) -> &'static str {
"an installed copy matches none of the manifest's release variants \
(no matching variant found)"
}
MULTI_RELEASE_UNPATCHED_COPY => {
"the patch's files are in place, but a jar holds a patched class again for newer \
Java releases without the patch (`apply` cannot change that copy, the patch has \
to list it), or could not be checked for such a copy"
}
_ => "an installed copy does not verify",
}
}

/// `errorCode` of a Maven patch whose members are all in place while its
/// jar, a multi-release one, holds one of them again under
/// `META-INF/versions/<n>/` without the patch: `apply` fails that copy
/// with it, and `apply --check` reports it as drift.
const MULTI_RELEASE_UNPATCHED_COPY: &str = socket_patch_core::vex::MULTI_RELEASE_UNPATCHED_COPY;

/// What follows the `no_matching_variant` drift of `base_purl` in the
/// `apply --check` report: `apply` leaves such a copy as it is, so the
/// report's closing `apply` remedy is not for it. A gem names the command
Expand Down Expand Up @@ -1700,7 +1721,7 @@ fn collect_apply_failures(
let mut failures: Vec<ApplyFailure> = Vec::new();
for r in results {
let (code, error, cause) = match caveats.incomplete_of(r) {
Some(copy) => (gem_ext::NOT_REBUILT_REASON, copy.detail.clone(), None),
Some(copy) => (copy.code, copy.detail.clone(), None),
None if !r.success => (
if caveats.is_no_variant(r) {
NO_MATCHING_VARIANT
Expand Down Expand Up @@ -3113,18 +3134,23 @@ impl LeftCopy {
}
}

/// One gem copy whose patch is incomplete
/// ([`gem_ext::GemCaveat::Incomplete`]): the files of the patch are
/// written there and stay written, and the compiled code the copy runs is
/// not the patch's. Its [`ApplyResult`] is among the run's results, as the
/// successful write it is (so its sidecar advisory and per-file warnings
/// are reported like any write's); this entry is what makes the run fail
/// and the copy read as not patched.
/// One copy whose patch is incomplete: the files of the patch are written
/// there and stay written, and what the copy runs is not all the patch's.
/// A gem copy whose compiled code was not rebuilt from the patched sources
/// ([`gem_ext::GemCaveat::Incomplete`]), or a multi-release jar that holds
/// a patched class again under `META-INF/versions/<n>/` without the patch
/// ([`check_versioned_copies`]). Its [`ApplyResult`] is among the run's
/// results, as the successful write it is (so its sidecar advisory and
/// per-file warnings are reported like any write's); this entry is what
/// makes the run fail and the copy read as not patched.
struct IncompleteCopy {
/// The manifest key of the patch.
purl: String,
/// The result's `package_path`: which copy.
path: String,
/// The `errorCode` of the copy's event, and the code of its run
/// warning: what is not in effect.
code: &'static str,
/// What was written and what is not in effect, in words.
detail: String,
}
Expand All @@ -3133,7 +3159,7 @@ impl IncompleteCopy {
/// The copy's event: `failed`, with the files this run wrote.
fn event(&self, result: &ApplyResult) -> PatchEvent {
PatchEvent::new(PatchAction::Failed, self.purl.clone())
.with_error(gem_ext::NOT_REBUILT_REASON, self.detail.as_str())
.with_error(self.code, self.detail.as_str())
.with_files(patched_files(result))
}

Expand Down Expand Up @@ -3173,11 +3199,12 @@ struct GemCaveats {
/// Copies with nothing of the patch in their build: skipped, and
/// patched as far as they can be. They have no result.
not_in_build: Vec<LeftCopy>,
/// Copies whose patch is incomplete. They fail the run.
/// Copies whose patch is incomplete: a gem's, or a multi-release jar's
/// ([`IncompleteCopy`]). They fail the run.
incomplete: Vec<IncompleteCopy>,
/// Copies skipped because patching them is not supported yet: a
/// compiled conda build of a Python package (the one entry here that
/// is not a gem's). They have no result and do not fail the run.
/// compiled conda build of a Python package. They have no result and
/// do not fail the run.
unsupported: Vec<UnsupportedCopy>,
/// Installed copies whose files match no record of their release
/// (gem or PyPI). Each has a failed result, keyed by the base purl.
Expand Down Expand Up @@ -3224,11 +3251,9 @@ impl GemCaveats {
/// Whether `warning` is the coded copy of an incomplete patch's
/// `Error:` line, which the human path already printed.
fn restated_by(&self, warning: &RunWarning) -> bool {
warning.code == gem_ext::NOT_REBUILT_REASON
&& self
.incomplete
.iter()
.any(|copy| copy.warning_detail() == warning.detail)
self.incomplete
.iter()
.any(|copy| copy.code == warning.code && copy.warning_detail() == warning.detail)
}
}

Expand Down Expand Up @@ -3622,6 +3647,7 @@ async fn apply_patches_inner(
};
let out = Box::pin(apply_maven_base(&maven)).await;
has_errors |= out.failed;
caveats.incomplete.extend(out.incomplete);
unreported.extend(out.unreported);
matched_manifest_purls.extend(out.matched);
run_warnings.extend(out.warnings);
Expand Down Expand Up @@ -3890,6 +3916,7 @@ async fn apply_patches_inner(
caveats.incomplete.push(IncompleteCopy {
purl: variant_purl.clone(),
path: outcome.result.package_path.clone(),
code: gem_ext::NOT_REBUILT_REASON,
detail,
});
warn_mismatch_overwrites(&outcome.result, &args.common);
Expand Down Expand Up @@ -4368,6 +4395,10 @@ struct MavenApplied {
applied: bool,
/// See [`ApplyOutcome::unreported`].
unreported: Vec<(String, ApplyFailureCause)>,
/// Copies whose jar holds the patch's members and still runs old code
/// on some Java runtimes ([`check_versioned_copies`]). They fail the
/// run; their results stay the writes they are.
incomplete: Vec<IncompleteCopy>,
}

impl MavenApplied {
Expand Down Expand Up @@ -4624,6 +4655,8 @@ async fn apply_maven_base(m: &MavenBase<'_>) -> MavenApplied {
} = copy;
check_derived_copies(&mut out, &mut result, &jar_leaf, args, m.derived)
.await;
check_versioned_copies(&mut out, &result, &jar_leaf, &patch.files, args)
.await;
// A copy's own coded refusal is a run warning too,
// as a whole-swap one is.
if let Some(refusal) = refusal {
Expand Down Expand Up @@ -4876,6 +4909,72 @@ async fn check_derived_copies(
}
}

/// After a copy's jar `jar_leaf` holds the members of the record `files`
/// patched (swapped in by this run, or found so): a multi-release jar may
/// hold one of them again under `META-INF/versions/<n>/`, and a Java
/// runtime of that release or later loads that copy instead
/// (`socket_patch_core::patch::multi_release`). The swap requires every
/// entry the record does not list to be the installed jar's own, so such a
/// copy is upstream's, unpatched, unless the record lists it.
///
/// The copy's patch is then incomplete ([`IncompleteCopy`],
/// `multi_release_unpatched_copy`), as a gem's is whose compiled code is
/// not the patch's: the run fails, because a patch that is only partly in
/// effect is not a success, and nothing is undone. The patched jar stays
/// where it is, with its backup, and `rollback` restores it as usual; a
/// runtime that reads the base entry (Java 8) runs the patched class. A
/// jar that cannot be checked is reported the same way.
///
/// A dry run is told the same, as it is told of a gem patch that would not
/// take effect: the swap leaves every entry the record does not list as
/// it is, so the jar on disk already holds the copy in question.
async fn check_versioned_copies(
out: &mut MavenApplied,
result: &ApplyResult,
jar_leaf: &str,
files: &HashMap<String, PatchFileInfo>,
args: &ApplyArgs,
) {
use socket_patch_core::patch::multi_release::{check_file_off_thread, UNPATCHED_COPY};
if !result.success {
return;
}
let jar = PathBuf::from(&result.package_path).join(jar_leaf);
let shown = jar.display().to_string();
// A check that does not finish comes back as a jar that could not be
// checked, never as a clean one: `apply --check` and `vex` ask the same
// helper and get the same answer.
let Some(found) = check_file_off_thread(jar, files.clone()).await else {
return;
};
let kept = match args.common.dry_run {
true => "Applying patches the class itself and leaves the rest of the jar as it is",
false => "The patched class is in place and was left there",
};
// A copy that is there needs a patch that lists it. A jar that could
// not be checked may hold none: all that is known is that it was not
// checked.
let next = match found.established() {
true => {
"The patch has to change that copy too, so applying it again does not help: it \
takes a patch that lists the versioned copy"
}
false => "A jar that could not be checked is not reported as patched",
};
let copy = IncompleteCopy {
purl: result.package_key.clone(),
path: result.package_path.clone(),
code: UNPATCHED_COPY,
detail: format!("{}. {kept}. {next}.", found.describe(&shown)),
};
if args.prints_errors() {
eprintln!("{}", format_patch_incomplete(&copy.purl, &copy.detail));
}
out.warn(UNPATCHED_COPY, copy.warning_detail());
out.incomplete.push(copy);
out.failed = true;
}

/// The `package_not_installed` detail of a lockfile-resolved purl.
const LOCKFILE_ONLY_DETAIL: &str =
"Resolved by the project lockfile but not installed on this host (lockfile-only)";
Expand Down Expand Up @@ -5910,6 +6009,7 @@ mod tests {
incomplete: vec![IncompleteCopy {
purl: GEM_PURL.to_string(),
path: result.package_path.clone(),
code: gem_ext::NOT_REBUILT_REASON,
detail: "the patch's files were written, but not built".to_string(),
}],
..GemCaveats::default()
Expand Down
53 changes: 53 additions & 0 deletions crates/socket-patch-cli/src/commands/vex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,29 @@ async fn generate_vex(
outcome
};

// A vendored jar that could not be checked for unpatched versioned
// copies at all (`Plan::unchecked_jars`) is not attested: a jar that is
// not checked is not taken for patched. Verification refuses a jar it
// cannot open, and its own reason is the one reported. What still
// comes out applied is omitted here: every such purl of a run that
// does not verify, and one whose check died over a jar that verifies.
for (purl, what) in &plan.unchecked_jars {
const CODE: &str = socket_patch_core::vex::MULTI_RELEASE_UNPATCHED_COPY;
let Some(at) = outcome.applied.iter().position(|applied| applied == purl) else {
continue;
};
outcome.applied.remove(at);
outcome.vendored.retain(|vendored| vendored != purl);
outcome
.vendored_out_of_sync
.retain(|drifted| drifted != purl);
outcome.failed.push(FailedPatch {
purl: purl.clone(),
reason: CODE.to_string(),
});
note_warning(warnings, common, CODE, format!("{purl}: {what}."));
}

// In-run `scan --mode hosted --vex`: the bytes of deps THAT RUN confirmed
// redirected live on the patch server until the next install, so their
// verification against the local tree would spuriously fail
Expand Down Expand Up @@ -1433,6 +1456,26 @@ async fn generate_vex(
// verification failure.
outcome.failed.extend(plan.gated.iter().cloned());

// A multi-release jar that holds a patched class again per Java
// release, without the patch (#2199): the skip carries the code, this
// says which jar and which copy. Only for a purl still omitted for it.
// (A vendored jar's is a note of the plan.)
//
// A hosted pin is judged only where this run read its jar, which is
// the gap every installed evidence of a hosted pin has: a pin whose
// suffixed jar no local cache holds is attested from the pin (the
// lockfile basis above), and so is a pin this very run wrote or
// confirmed (`assume_applied`), whatever a cache holds. Neither reads
// the jar, so neither sees such a copy; nothing downloads it to look.
// The contract and `docs/ecosystems.md` say so.
for (purl, what) in std::mem::take(&mut outcome.multi_release_copies) {
const CODE: &str = socket_patch_core::vex::MULTI_RELEASE_UNPATCHED_COPY;
let omitted = |f: &FailedPatch| f.purl == purl && f.reason == CODE;
if outcome.failed.iter().any(omitted) {
note_warning(warnings, common, CODE, format!("{purl}: {what}."));
}
}

// Vendored disclosure: the committed artifact verified (the attestation
// stands — the plan only takes the vendored basis for an artifact a
// discovered lockfile / config reference, or the JVM wiring check, shows
Expand Down Expand Up @@ -2891,6 +2934,11 @@ fn omission_phrase(reason: &str) -> &'static str {
"another package bundles its own copy, which no Socket wiring reaches (the \
warning above names it)"
}
socket_patch_core::vex::MULTI_RELEASE_UNPATCHED_COPY => {
"its jar holds a patched class again for newer Java releases \
(META-INF/versions) without the patch, which those runtimes load, or could not \
be checked for such a copy (the warning above says which)"
}
_ => "the patch could not be verified",
}
}
Expand Down Expand Up @@ -3636,6 +3684,7 @@ mod tests {
GO_WORKSPACE_OVERRIDE,
GO_VERSION_NOT_SELECTED,
GO_INSTALLED_TOOL_STALE,
socket_patch_core::vex::MULTI_RELEASE_UNPATCHED_COPY,
] {
assert_ne!(
omission_phrase(tag),
Expand Down Expand Up @@ -4073,6 +4122,7 @@ mod mirror_refusal_tests {
hosted,
gated: Vec::new(),
notes: Vec::new(),
unchecked_jars: Default::default(),
};
let mut params = VexEmbedArgs {
vex: Some(tmp.path().join(format!("{refused}-{no_verify}.json"))),
Expand Down Expand Up @@ -4195,6 +4245,7 @@ mod mirror_refusal_tests {
hosted,
gated: Vec::new(),
notes: Vec::new(),
unchecked_jars: Default::default(),
};
let mut params = VexEmbedArgs {
vex: Some(tmp.path().join(format!("{index}.json"))),
Expand Down Expand Up @@ -4374,6 +4425,7 @@ mod unjudged_tests {
hosted,
gated: Vec::new(),
notes: Vec::new(),
unchecked_jars: Default::default(),
};
let mut params = VexEmbedArgs {
vex: Some(tmp.path().join(format!("{no_verify}.json"))),
Expand Down Expand Up @@ -4501,6 +4553,7 @@ mod bazel_tests {
hosted: BTreeMap::from([(purls[2].to_string(), wiring)]),
gated: Vec::new(),
notes: Vec::new(),
unchecked_jars: Default::default(),
};
for (global, no_bazel) in [(false, false), (true, false), (false, true)] {
let common = GlobalArgs {
Expand Down
Loading
Loading