← Back to Pull requests

fix: detect the format behind an ID3v2 tag larger than the probe #2975

Closed
unknown closed this pull request without merging on Nov 27, 2026 15:15 UTC
View on GitHub
unknown unknown commented on Nov 26, 2026 09:22 UTC
Author

What is wrong

A FLAC file with an ID3v2 tag in front fails to parse with parseBuffer or parseBlob when 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.

const size = 5000;
const id3 = Uint8Array.of(0x49, 0x44, 0x33, 4, 0, 0, (size >> 21) & 0x7f, (size >> 14) & 0x7f, (size >> 7) & 0x7f, size & 0x7f);
const flac = fs.readFileSync('test/samples/flac/flac.flac');
await parseBuffer(Buffer.concat([id3, Buffer.alloc(size), flac]));

  • Actual: MpegContentError: Invalid ADTS frame length: 6

  • Expected: FLAC, same as with a 100 byte tag (container: '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-119 peeks only mediaTypeProbeLength (4100) bytes. detectMediaType returns '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 (FlacParser extends AbstractID3Parser).

    Fix

    New helper getLeadingId3v2Length returns 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 through parseBuffer and parseBlob. It fails on master (container is undefined) and passes with the fix.

    Full suite: 7329 passed before, 7330 passed after (20 skipped both). yarn lint:ts and yarn typecheck pass (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 -->

    Diff: 359 lines (+269 / -90) across 4 files Reviewer: bardrop Review duration: 1793m
    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
    0.43
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 42.6% (MEDIUM) · Higher = larger blast radius
    42.6%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 16.8% · Safe to merge
    16.8%