Skip to content

Commit 0cad491

Browse files
committed
Admit an earlier patch by the copy's platform only
Review of the first cut found two ways it could write a record over bytes it was not made for: - The ruby-platform record was admitted on a platform gem's copy that no record names. The gemspec does not say such a copy is that record's distribution (it is matched to it by files alone), and a stored blob may be another record's original content, so a pristine platform copy could be overwritten. That fallback is gone: a record is admitted only on a copy whose gemspec names its platform. - A platform copy patched by the ruby-platform record had its own platform's record forced over that patch. A blob that is one side of another record of the version is a state the manifest describes, not an earlier patch, and no longer admits a record. The contract note says which gated records the rule covers. Refs #2376. Assisted-by: Claude Code:claude-fable-5-2 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011f9aeRLXgywqBzkw2e1bPs
1 parent 5414082 commit 0cad491

5 files changed

Lines changed: 151 additions & 93 deletions

File tree

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,16 @@ newer patch for the same release) is overwritten with the
137137
`content_mismatch_overwritten` warning, except under `--strict`. Only a stored
138138
blob proves this: when it is gone (pruned by `gc` / `scan --prune`, or
139139
`.socket/blobs/` is not shared with the checkout), the earlier output reads as
140-
unknown drift and `apply --force` is the explicit way to replace it. A gem
141-
record that names its platform (`?platform=`) replaces an earlier patch this
142-
way only on a copy that its gemspec gives to the record: a copy of the platform
143-
the record names, or, for the `ruby` record, a platform gem's copy that no
144-
record of the version names. A copy of another platform, or one whose gem home
145-
has no gemspec for it, is left as it is (`no matching variant found`), whatever
146-
its files hold.
140+
unknown drift and `apply --force` is the explicit way to replace it. For a gem
141+
record that is matched to a copy by its files (one that names its platform with
142+
`?platform=`, or one of several records or builds of the version), a copy that
143+
matches neither hash is taken for an earlier patch only when two more things
144+
hold. Its gemspec names the platform the record was written for (a
145+
`ruby`-platform copy for the `ruby` record), and the blob it holds is not one
146+
side of another record of that version in the manifest. Any other such copy is
147+
left as it is (`no matching variant found`): a copy of another platform, one
148+
whose gem home has no gemspec for it, and a platform gem's copy under the
149+
`ruby` record, which is applied to such a copy only while its files match.
147150
`--allow-patch-conflicts` permits the old
148151
warn-and-overwrite behavior: only the full, hash-verified `afterHash` content
149152
can be written, with `content_mismatch_overwritten` diagnostics. `--strict`

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

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,8 +1177,9 @@ pub(crate) fn variant_matches_installed(first_file_status: Option<&VerifyStatus>
11771177
/// Of the manifest's records for one gem version (`variants`), the one
11781178
/// written for the copy at `pkg_path` itself: the record whose `platform`
11791179
/// qualifier names the platform of that (platform gem) copy, when it
1180-
/// matches what is installed there (or under `--force`, which asks no
1181-
/// file). That record alone is then applied to the copy. `None`: the copy
1180+
/// matches what is installed there or the copy holds an earlier patch of
1181+
/// its platform's record ([`variant_is_installed`]), or under `--force`,
1182+
/// which asks no file. That record alone is then applied to the copy. `None`: the copy
11821183
/// is the `ruby` platform's, no record names its platform, or the one that
11831184
/// does is for other bytes; every matching record is applied, as for any
11841185
/// other package.
@@ -1205,25 +1206,32 @@ async fn own_platform_variant<'v>(
12051206

12061207
/// What decides whether a copy that matches neither hash of a record may
12071208
/// still be the record's, holding an earlier Socket patch: the stored
1208-
/// blobs and the run's mismatch policy.
1209+
/// blobs, the manifest's other records and the run's mismatch policy.
12091210
struct EarlierPatch<'a> {
12101211
sources: &'a PatchSources<'a>,
1212+
manifest: &'a PatchManifest,
12111213
policy: MismatchPolicy,
12121214
}
12131215

12141216
/// Whether the copy at `pkg_path` is the distribution of the record
12151217
/// `variant` (one of the base's `variants`), by `telling`, the record's
1216-
/// files that can tell distributions apart: its representative file
1217-
/// holds the record's original or patched content
1218-
/// ([`variant_matches_installed`]), or the copy is a gem's that its
1219-
/// gemspec gives to the record and the file holds an earlier Socket
1220-
/// patch ([`gem_copy_holds_earlier_patch`]).
1218+
/// files that can tell distributions apart. Either its representative
1219+
/// file holds the record's original or patched content
1220+
/// ([`variant_matches_installed`]), or all of this holds:
1221+
///
1222+
/// * the copy is a gem's and its gemspec names the platform the record
1223+
/// was written for, and the file holds a stored Socket blob
1224+
/// ([`gem_copy_holds_earlier_patch`]);
1225+
/// * that blob is not one side of another record of `variants` for the
1226+
/// same file: such a copy is in a state that record describes (the
1227+
/// ruby-platform record's patch on a platform gem's copy, say), not one
1228+
/// an earlier patch left;
1229+
/// * the mismatch policy overwrites Socket content (not `--strict`),
1230+
/// since the engine is then handed the record and writes it over the
1231+
/// earlier patch.
12211232
///
12221233
/// The second way is what lets a newer patch replace an earlier one for
1223-
/// the same release under a qualified record (#2376). The engine then
1224-
/// overwrites the earlier patch's files under the mismatch policy, so it
1225-
/// is open only where the policy overwrites Socket content: not under
1226-
/// `--strict`.
1234+
/// the same release under a record that names its platform (#2376).
12271235
async fn variant_is_installed(
12281236
pkg_path: &Path,
12291237
variant: &str,
@@ -1238,17 +1246,23 @@ async fn variant_is_installed(
12381246
if variant_matches_installed(Some(&verified.status)) {
12391247
return true;
12401248
}
1241-
matches!(
1249+
if !matches!(
12421250
earlier.policy,
12431251
MismatchPolicy::SocketContent | MismatchPolicy::Warn
1244-
) && gem_copy_holds_earlier_patch(
1245-
pkg_path,
1246-
variant,
1247-
variants.iter().map(String::as_str),
1248-
&verified,
1249-
earlier.sources,
1250-
)
1251-
.await
1252+
) {
1253+
return false;
1254+
}
1255+
let of_another_record = variants
1256+
.iter()
1257+
.filter(|other| other.as_str() != variant)
1258+
.filter_map(|other| earlier.manifest.patches.get(other)?.files.get(name))
1259+
.any(|other| {
1260+
let current = verified.current_hash.as_deref();
1261+
current == Some(other.before_hash.as_str())
1262+
|| current == Some(other.after_hash.as_str())
1263+
});
1264+
!of_another_record
1265+
&& gem_copy_holds_earlier_patch(pkg_path, variant, &verified, earlier.sources).await
12521266
}
12531267

12541268
/// The file whose verify status decides whether a release variant
@@ -3571,6 +3585,7 @@ async fn apply_patches_inner(
35713585
);
35723586
let earlier_patch = EarlierPatch {
35733587
sources: &sources,
3588+
manifest: &manifest,
35743589
policy,
35753590
};
35763591
let mut has_errors = results.iter().any(|r| !r.success);
@@ -3805,7 +3820,9 @@ async fn apply_patches_inner(
38053820
// --force). A mismatch *or* a missing file means this
38063821
// variant's distribution isn't the one on disk, so skip it —
38073822
// attempting it would only produce a spurious failure.
3808-
// Mirrors `select_installed_variants`, used by rollback/get.
3823+
// Mirrors `select_installed_variants`, used by rollback/get,
3824+
// but for the earlier-patch case below, which only
3825+
// `apply` admits.
38093826
//
38103827
// Exempt only an UNQUALIFIED singleton (the common bare
38113828
// `pkg:gem/name@ver` manifest key): it names no
@@ -3833,11 +3850,12 @@ async fn apply_patches_inner(
38333850
// copy stays gated rather than overwritten.
38343851
//
38353852
// One mismatch passes the gate: an earlier Socket
3836-
// patch on a gem copy whose GEMSPEC gives it to the
3837-
// record (`variant_is_installed`). The gemspec names
3838-
// the copy's platform whatever its files hold, so a
3839-
// record is still never written over a copy of
3840-
// another platform, or one with no gemspec to say.
3853+
// patch on a gem copy whose GEMSPEC names the
3854+
// record's platform (`variant_is_installed`). The
3855+
// gemspec names the copy's platform whatever its
3856+
// files hold, so a record is still never written
3857+
// over a copy of another platform, or one with no
3858+
// gemspec to say.
38413859
if !args.force
38423860
&& (variants.len() > 1
38433861
|| variants[0] != base_purl

‎crates/socket-patch-cli/tests/apply/gem_patch_update_qualified.rs‎

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,19 @@
1515
//! `content_mismatch_overwritten` warning, as for an unqualified
1616
//! record;
1717
//! * the same for a platform gem's copy and the record of its own
18-
//! platform, and for the ruby-platform record on a platform copy that
19-
//! no record names;
20-
//! * with a sibling record for another platform in the manifest, only
18+
//! platform;
19+
//! * with sibling records for other platforms in the manifest, only
2120
//! the copy's own record is written;
2221
//! * `--strict` still refuses;
2322
//! * what proves the distribution is the copy's gemspec, never the
2423
//! stored blob alone: a copy of ANOTHER platform that holds a stored
25-
//! blob, a copy with no gemspec, and a copy with a local edit all stay
26-
//! refused and untouched.
24+
//! blob, a platform copy under the ruby-platform record (which is
25+
//! matched to such a copy by its files alone), a copy with no gemspec
26+
//! and a copy with a local edit all stay refused and untouched;
27+
//! * a copy in a state ANOTHER record of the manifest describes is not
28+
//! one an earlier patch left: a platform copy that holds the
29+
//! ruby-platform record's patch keeps it, and its own platform's
30+
//! record is not written over it.
2731
2832
use std::path::{Path, PathBuf};
2933

@@ -192,33 +196,76 @@ fn own_platform_record_replaces_earlier_patch() {
192196
assert_updated(code, &stdout, &file, &patched, &purl);
193197
}
194198

195-
/// The ruby-platform record is what a platform copy takes when no record
196-
/// names its platform; an update of it goes the same way.
199+
/// The ruby-platform record is applied to a platform gem's copy only
200+
/// when its files match the record: the gemspec does not say the copy is
201+
/// that record's distribution, so a stored blob there proves nothing and
202+
/// the copy is refused as before.
197203
#[test]
198-
fn ruby_record_replaces_earlier_patch_on_platform_copy_no_record_names() {
204+
fn ruby_record_on_platform_copy_holding_a_stored_blob_stays_refused() {
199205
let tmp = tempfile::tempdir().unwrap();
200206
let root = tmp.path();
201-
let earlier = earlier_patch(root, NOKOGIRI_LINUX_PRISTINE);
207+
let on_disk = earlier_patch(root, NOKOGIRI_LINUX_PRISTINE);
202208
let file = install_copy(
203209
root,
204210
"nokogiri",
205211
"nokogiri-1.16.5-x86_64-linux",
206-
&earlier,
212+
&on_disk,
207213
true,
208214
);
209-
let purl = nokogiri_purl("ruby");
210215
let mut patches = serde_json::Map::new();
211-
let patched = record_b(
216+
record_b(
212217
root,
213218
&mut patches,
214-
&purl,
219+
&nokogiri_purl("ruby"),
215220
"nokogiri",
216221
NOKOGIRI_LINUX_PRISTINE,
217222
);
218223
write_manifest(root, Value::Object(patches));
219224

220225
let (code, stdout, _) = run_gem_apply(root, &["--json"]);
221-
assert_updated(code, &stdout, &file, &patched, &purl);
226+
assert_refused(code, &stdout, &file, &on_disk, NOKOGIRI_BASE);
227+
}
228+
229+
/// A platform copy patched by the ruby-platform record (its own
230+
/// platform's record is for other bytes) is in a state the manifest
231+
/// describes. Its own platform's record sees a stored blob in a copy of
232+
/// its platform, and still must not be written over the patch in place.
233+
#[test]
234+
fn own_platform_record_is_not_written_over_a_sibling_records_patch() {
235+
let tmp = tempfile::tempdir().unwrap();
236+
let root = tmp.path();
237+
let ruby = nokogiri_purl("ruby");
238+
let mut patches = serde_json::Map::new();
239+
let ruby_patched = record_b(root, &mut patches, &ruby, "nokogiri", b"# the source gem\n");
240+
let linux_patched = record_b(
241+
root,
242+
&mut patches,
243+
&nokogiri_purl("x86_64-linux"),
244+
"nokogiri",
245+
NOKOGIRI_LINUX_PRISTINE,
246+
);
247+
write_manifest(root, Value::Object(patches));
248+
let file = install_copy(
249+
root,
250+
"nokogiri",
251+
"nokogiri-1.16.5-x86_64-linux",
252+
&ruby_patched,
253+
true,
254+
);
255+
256+
let (code, stdout, _) = run_gem_apply(root, &["--json"]);
257+
assert_eq!(code, 0, "stdout={stdout}");
258+
assert_eq!(
259+
std::fs::read(&file).unwrap(),
260+
ruby_patched,
261+
"stdout={stdout}"
262+
);
263+
assert_ne!(ruby_patched, linux_patched);
264+
let v = parse_json_envelope(&stdout);
265+
assert_envelope_invariants(&v, "apply");
266+
assert!(event_purls(&v, "applied").is_empty(), "{v:#}");
267+
assert!(has_event(&v, "skipped", "already_patched", &ruby), "{v:#}");
268+
assert!(!event_codes(&v).iter().any(|c| c == OVERWRITTEN), "{v:#}");
222269
}
223270

224271
/// With records for two platforms, the copy takes the newer patch of its

‎crates/socket-patch-core/src/patch/apply.rs‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2172,11 +2172,11 @@ async fn apply_package_patch_at(
21722172
}
21732173

21742174
/// Whether `verified`, the verify result of a file of the gem record
2175-
/// `key`, shows an earlier Socket patch on a copy the record is written
2176-
/// to: the file matches neither hash of the record, its content is a
2177-
/// blob Socket stored, and the gemspec of the copy at `pkg_path` says it
2178-
/// is the record's distribution among the records `keys` of the gem
2179-
/// version ([`copy_takes_record`](crate::patch::gem_ext::copy_takes_record)).
2175+
/// `key`, shows an earlier Socket patch on a copy of the record's own
2176+
/// platform: the file matches neither hash of the record, its content is
2177+
/// a blob Socket stored, and the gemspec of the copy at `pkg_path` names
2178+
/// the platform the record was written for
2179+
/// ([`copy_is_of_record_platform`](crate::patch::gem_ext::copy_is_of_record_platform)).
21802180
///
21812181
/// A release variant is matched to a copy by a file it replaces
21822182
/// ([`select_installed_variants`]), which cannot match once an earlier
@@ -2185,18 +2185,17 @@ async fn apply_package_patch_at(
21852185
/// whose [`MismatchPolicy::SocketContent`] writes the newer patch over
21862186
/// the earlier one. The stored blob alone never admits a record: it says
21872187
/// who wrote the bytes, not which distribution the copy is.
2188-
pub async fn gem_copy_holds_earlier_patch<'k>(
2188+
pub async fn gem_copy_holds_earlier_patch(
21892189
pkg_path: &Path,
21902190
key: &str,
2191-
keys: impl IntoIterator<Item = &'k str>,
21922191
verified: &VerifyResult,
21932192
sources: &PatchSources<'_>,
21942193
) -> bool {
21952194
use crate::crawlers::types::Ecosystem;
21962195
verified.status == VerifyStatus::HashMismatch
21972196
&& Ecosystem::from_purl(key) == Some(Ecosystem::Gem)
21982197
&& is_socket_content(sources, verified.current_hash.as_deref()).await
2199-
&& crate::patch::gem_ext::copy_takes_record(pkg_path, key, keys).await
2198+
&& crate::patch::gem_ext::copy_is_of_record_platform(pkg_path, key).await
22002199
}
22012200

22022201
/// Whether `hash` (a file's current content hash) names a blob Socket

0 commit comments

Comments
 (0)