fix: detect the format behind an ID3v2 tag larger than the probe #2975
All Vouch review confidence checks passed
Residual risk 0.17 = Change Risk (0.43) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.43 · 359 lines diff across 4 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.352
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #2975"
general
weight: 0.35
bardrop
What is wrong
A FLAC file with an ID3v2 tag in front fails to parse with
parseBufferorparseBlobwhen no MIME type or file name is given, if the ID3v2 tag is larger than about 4 KB. That is easy to hit: 8 KB of padding or a small cover picture is enough.MpegContentError: Invalid ADTS frame length: 6container: 'FLAC', duration 271.77 s)I checked tag sizes 100, 5000 and 20000: only 100 works before this change, all three work after.
Cause
lib/ParserFactory.ts:115-119peeks onlymediaTypeProbeLength(4100) bytes.detectMediaTypereturns'mpeg'when the ID3v2 tag ends past those bytes (lib/common/MediaFileType.ts:72-73), so the MPEG parser is picked for FLAC (and Musepack) files. Those parsers already skip a leading ID3v2 tag, so only detection is the problem. The writer side (lib/write/core/detect.ts) already skips large tags with small reads.Spec: ID3v2.4.0 structure, section 3.1, the tag size is a synchsafe integer of up to 256 MB, so the 4100 byte probe cannot assume it sees the end of the tag. No formal spec defines ID3v2 in front of FLAC; the reference is the existing reader support for it (
FlacParserextendsAbstractID3Parser).Fix
New helper
getLeadingId3v2Lengthreturns the offset after the leading ID3v2 tags. If detection gave'mpeg'only because the tag runs past the probe, and the tokenizer supports random access, peek the same probe size right after the tag and detect again. If that finds nothing, keep'mpeg'as before. Streams keep the old behavior, so no large lookahead buffer is allocated.Test
Added a case to
test/common-media-file-type.test.ts: a FLAC with an 8192 byte ID3v2 tag throughparseBufferandparseBlob. It fails on master (containeris undefined) and passes with the fix.Full suite: 7329 passed before, 7330 passed after (20 skipped both).
yarn lint:tsandyarn typecheckpass (same 14 existing warnings).Resolves #2978
<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
<!-- coderabbit-ai-generated:v1 -->
Summary by CodeRabbit
Bug Fixes
Improved media type detection for audio files with long or consecutive ID3 metadata prefixes. FLAC files are now more reliably recognized when metadata extends beyond the initial data inspected, including when parsed from buffers or Blobs.
* Corrected parsing of ID3v2.4 tags that include a footer, ensuring the footer is handled when reading the tag.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->