musicbrainz: prove reader panic-free; harden MBID parser
- Commit
cc3791851b57691441884be1b82f15a9818e7bf1- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-20 23:32 UTC (3 months ago)
- Parents
6d3dd64
Diff
scripts/musicbrainz/mb_ingest/src/pack.rs
diff --git a/scripts/musicbrainz/mb_ingest/src/pack.rs b/scripts/musicbrainz/mb_ingest/src/pack.rsindex 7a86448d88..02286b3e45 100644--- a/scripts/musicbrainz/mb_ingest/src/pack.rs+++ b/scripts/musicbrainz/mb_ingest/src/pack.rs@@ -315,10 +315,30 @@ pub fn pack_date(year: Option<i64>, month: Option<i64>, day: Option<i64>) -> u32 // --- MBIDs --- +/// Parse an MBID.+///+/// This is the ONE canonical parser — `location_share_server` should call it+/// rather than writing a second one, because two parsers that disagree on case or+/// on brace forms would return "not found" for a perfectly valid MBID.+///+/// Accepts: canonical hyphenated, bare 32-hex, any case, optional surrounding+/// whitespace, and optional `{...}` or `urn:uuid:` wrappers, all of which appear+/// in the wild. Rejects anything that is not exactly 32 hex digits once the+/// permitted decoration is removed. pub fn parse_mbid(s: &str) -> Option<Mbid> {+ let mut t = s.trim();+ if let Some(rest) = t.strip_prefix('{') {+ t = rest.strip_suffix('}')?;+ }+ for prefix in ["urn:uuid:", "URN:UUID:", "urn:UUID:"] {+ if let Some(rest) = t.strip_prefix(prefix) {+ t = rest;+ break;+ }+ } let mut out = [0u8; 16]; let mut nibble = 0usize;- for c in s.chars() {+ for c in t.chars() { if c == '-' { continue; }@@ -1052,6 +1072,31 @@ mod tests { assert_eq!(parse_mbid("f27ec8db-af05-4f36-916e-3d57f91ecf5e-extra"), None); } + #[test]+ fn mbid_parses_the_forms_that_turn_up_in_the_wild() {+ // One canonical parser, so the server cannot disagree with the pack about+ // whether a valid MBID exists.+ let m = parse_mbid("f27ec8db-af05-4f36-916e-3d57f91ecf5e").unwrap();+ for variant in [+ " f27ec8db-af05-4f36-916e-3d57f91ecf5e ",+ "{f27ec8db-af05-4f36-916e-3d57f91ecf5e}",+ "urn:uuid:f27ec8db-af05-4f36-916e-3d57f91ecf5e",+ "URN:UUID:F27EC8DB-AF05-4F36-916E-3D57F91ECF5E",+ "F27ec8Db-aF05-4f36-916E-3d57F91EcF5e",+ ] {+ assert_eq!(parse_mbid(variant), Some(m), "should accept {variant:?}");+ }+ for bad in [+ "{f27ec8db-af05-4f36-916e-3d57f91ecf5e",+ "f27ec8db-af05-4f36-916e-3d57f91ecf5",+ "g27ec8db-af05-4f36-916e-3d57f91ecf5e",+ "f27ec8db af05 4f36 916e 3d57f91ecf5e",+ "urn:uuid:",+ ] {+ assert_eq!(parse_mbid(bad), None, "should reject {bad:?}");+ }+ }+ #[test] fn isrc_round_trips() { for s in ["GBAYE0601498", "USRC17607839", "ZZZ990000000", "AAA000000000"] {
scripts/musicbrainz/mb_ingest/tests/round_trip.rs
diff --git a/scripts/musicbrainz/mb_ingest/tests/round_trip.rs b/scripts/musicbrainz/mb_ingest/tests/round_trip.rsindex e2c2d3218c..6e8693d191 100644--- a/scripts/musicbrainz/mb_ingest/tests/round_trip.rs+++ b/scripts/musicbrainz/mb_ingest/tests/round_trip.rs@@ -535,6 +535,53 @@ fn the_string_pool_is_sorted_so_compression_can_work() { ); } +/// Exercise every public query on a pack, ignoring results. The assertion is+/// simply that nothing panics.+fn hammer_every_query(pack: &MbPack<'_>) {+ let c = pack.counts();+ let _ = pack.section_sizes();+ let _ = pack.dump_date();+ let _ = pack.strings_compressed();+ // Deliberately probe past the end as well as inside it.+ for idx in [0u32, 1, c.artists / 2, c.artists, c.artists.wrapping_add(1), u32::MAX] {+ let _ = pack.artist(idx);+ let _ = pack.artist_mbid(idx);+ let _ = pack.artist_release_groups(idx);+ }+ for idx in [0u32, 1, c.release_groups, u32::MAX] {+ let _ = pack.release_group(idx);+ let _ = pack.release_group_mbid(idx);+ let _ = pack.release_group_releases(idx);+ }+ for idx in [0u32, 1, c.releases, u32::MAX] {+ let _ = pack.release(idx);+ let _ = pack.release_mbid(idx);+ let _ = pack.release_media(idx);+ let _ = pack.release_track_count(idx);+ let _ = pack.release_tracklist(idx);+ let _ = pack.medium_tracks(idx, 0);+ let _ = pack.medium_tracks(idx, usize::MAX);+ }+ for idx in [0u32, 1, c.recordings, u32::MAX] {+ let _ = pack.recording(idx);+ let _ = pack.recording_mbid(idx);+ let _ = pack.recording_isrcs(idx);+ let _ = pack.recording_first_release(idx);+ let _ = pack.track_mbid(idx);+ }+ for m in ["00000000-0000-0000-0000-000000000000", fixture::REL_DARK_SIDE] {+ let key = mb(m);+ let _ = pack.artist_by_mbid(&key);+ let _ = pack.release_group_by_mbid(&key);+ let _ = pack.release_by_mbid(&key);+ }+ for q in ["", "!!!", "a", "dark side of the moon", "zzzznotathing", "\u{fc}", "0"] {+ let _ = pack.search_artists(q, 25);+ let _ = pack.search_release_groups(q, 25);+ let _ = pack.search_recordings(q, 25);+ }+}+ #[test] fn truncated_pack_is_rejected_not_read() { let built = build_fixture(BuildOptions::default());@@ -543,3 +590,104 @@ fn truncated_pack_is_rejected_not_read() { assert!(err.is_err(), "a pack truncated to {cut} bytes must be refused"); } }++/// Truncate a valid pack and confirm `open` either refuses it or yields a reader+/// on which every query is survivable.+///+/// The server asked whether the reader is panic-free on malformed input. Reading+/// the code is not evidence; this is. Exhaustive over the header and directory+/// (where the offsets and lengths live) and strided over the body.+#[test]+fn every_truncation_is_survivable() {+ let built = build_fixture(BuildOptions::default());+ let head = 64 + 32 * 16;+ let offsets = (0..head.min(built.bytes.len()))+ .chain((head..built.bytes.len()).step_by(1009))+ .chain([built.bytes.len().saturating_sub(1), built.bytes.len()]);+ for cut in offsets {+ match MbPack::open(&built.bytes[..cut.min(built.bytes.len())]) {+ Err(_) => {}+ Ok(pack) => hammer_every_query(&pack),+ }+ }+}++/// Corrupt a valid pack in many single- and multi-byte ways and confirm the same.+/// Deterministic PRNG so any failure is reproducible from the seed.+#[test]+fn corrupted_packs_never_panic() {+ let built = build_fixture(BuildOptions::default());+ let mut state: u64 = 0x2026_0820;+ let mut next = |bound: usize| -> usize {+ // xorshift64*, adequate and dependency-free.+ state ^= state >> 12;+ state ^= state << 25;+ state ^= state >> 27;+ (state.wrapping_mul(0x2545_F491_4F6C_DD1D) >> 33) as usize % bound.max(1)+ };++ // Every byte of the header and section directory set to adversarial values --+ // this is where every length and offset the reader trusts comes from.+ let head = 64 + 32 * 16;+ for off in 0..head.min(built.bytes.len()) {+ for v in [0x00u8, 0x01, 0x7f, 0x80, 0xff] {+ let mut b = built.bytes.clone();+ b[off] = v;+ if let Ok(pack) = MbPack::open(&b) {+ hammer_every_query(&pack);+ }+ }+ }++ // Scatter-shot corruption across the whole file.+ for _ in 0..300 {+ let mut b = built.bytes.clone();+ let n = 1 + next(64);+ for _ in 0..n {+ let off = next(b.len());+ b[off] = next(256) as u8;+ }+ if let Ok(pack) = MbPack::open(&b) {+ hammer_every_query(&pack);+ }+ }++ // Directory entries pointed at deliberately hostile extents.+ for entry in 0..32usize {+ for &(offset, len) in &[+ (0u64, u64::MAX),+ (u64::MAX, 0),+ (u64::MAX, u64::MAX),+ (built.bytes.len() as u64, 8),+ (7u64, 8u64),+ (8u64, built.bytes.len() as u64),+ ] {+ let mut b = built.bytes.clone();+ let base = 64 + entry * 16;+ b[base..base + 8].copy_from_slice(&offset.to_le_bytes());+ b[base + 8..base + 16].copy_from_slice(&len.to_le_bytes());+ if let Ok(pack) = MbPack::open(&b) {+ hammer_every_query(&pack);+ }+ }+ }+}++/// A VALID pack must never panic either. That would surface as intermittent 500s+/// and be misdiagnosed as corruption.+#[test]+fn a_valid_pack_survives_every_query_including_out_of_range() {+ for opts in [+ BuildOptions::default(),+ BuildOptions::tier_a(),+ BuildOptions { compress_strings: false, ..BuildOptions::default() },+ BuildOptions { official_only: true, ..BuildOptions::default() },+ BuildOptions { include_recording_search: false, ..BuildOptions::default() },+ BuildOptions { include_isrcs: false, ..BuildOptions::default() },+ BuildOptions { include_recording_mbids: false, ..BuildOptions::default() },+ ] {+ let built = build_fixture(opts);+ let pack = MbPack::open(&built.bytes).expect("valid pack must open");+ hammer_every_query(&pack);+ }+}