Skip to content

fix: Write every ID3v2 frame in an encoding and layout its version defines, and bound the work on crafted tags - #276

Merged
decriptor merged 7 commits into
mainfrom
fix/id3v23-encoding
Dec 4, 2026
Merged

decriptor merged 7 commits into
mainfrom
fix/id3v23-encoding

Conversation

@decriptor

@decriptor decriptor commented Dec 4, 2026 •

Copy link
Copy Markdown
Owner

Description

ID3v2 tags are now always written with frames and encodings their version defines, and every value written reads back through this library. Each issue has its own commit.

Commit Change
36846dc0 (#255) ID3v2.3 encodings. Every frame written into a v2.3 tag (or a v2.2 tag, which is saved as v2.3) uses encoding 0 or 1: ISO-8859-1 when the text fits, otherwise UTF-16 with a BOM. Before, the default UTF-8 (3) was written into v2.3, which that version does not define. This library's own reader then dropped the picture: the QuickStart "add album art" sample on a v2.3 MP3 saved a cover that read back as no pictures. v2.3 pictures and other frames that 0.6 and earlier wrote in encoding 2 or 3 now read, and the next save rewrites them validly. v2.4 output is unchanged.
afe3b147 (#261) Chapter sub-frames in v2.3. The frames that CHAP and CTOC embed are written and read with the tag version's frame header and flags. A v2.3 chapter title of 128 bytes or more used to read back as null.
1a4fd501 (#262) No more ?. A frame set to text its encoding cannot hold switches to one that can: UTF-8 in v2.4, UTF-16 with a BOM in v2.3. Before, Japanese text set on an ISO-8859-1 frame was written as ??.
09680515 (#265) Chapters keep every embedded frame. CHAP and CTOC kept only their title and dropped every other embedded frame (subtitle, URL, image, unknown frames) on any save. Now an unedited chapter is written as it was read, a title edit replaces the title in place, and a chapter moved between tag versions converts what it can.
d6fe68e8 (#266) URLs. A URL frame percent-encodes characters outside ISO-8859-1 as UTF-8 (RFC 3987 §3.1), where it wrote ?. A read returns the encoded form.
7c6cd009 (#272) A compressed, grouped v2.3 frame reads its decompressed size before its group byte, as ID3v2.3 §3.3.1 orders them. Such a frame used to read as nothing.
4f6639e3 (#275) Bounded work on crafted tags. Compressed frames share one inflation budget per file read (64 MiB) and per save (16 MiB). A frame past the budget is not decoded; it is kept as stored, byte for byte. A save never throws on size: a tag too large for its size field fails the save with a reason and writes nothing, on all 42 save entry points of the 8 ID3v2 carriers. Before, a crafted 819 KB tag made a save allocate about 9.4 GB and throw; now it allocates about 165 MB. A crafted AIFF with 40 ID3 chunks read with 1.9 GB; now about 50 MiB. Also fixes the size an appended MPEG-PS update tag wrote at the limit, which used to be masked.

Checked against ffprobe, exiftool (plain, -a and -validate) and MediaInfo on real v2.2, v2.3 and v2.4 MP3s:

  • the cover now reads back;
  • every frame byte is valid for its version;
  • long chapter titles show in ffprobe;
  • Japanese text and URLs are written correctly.

Motivation and Context

#255 was found by the review of the 0.7.0 docs (PR #254), which ran the QuickStart album-art sample and read the file back. Sweeping every frame type that carries a text encoding then found #261, #262 and #266. #265 and #272 came from the review of those fixes. All of them date from the first ID3v2 implementation.

Fixes #255
Fixes #261
Fixes #262
Fixes #265
Fixes #266
Fixes #272
Fixes #275

Related and still open, moved to v0.8.0:

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Performance improvement
  • Code refactoring (no functional changes)

Behaviour callers may rely on that changes:

  • A custom field in a v2.3 tag is written as ISO-8859-1 when it fits, where it was always UTF-16.
  • A frame's public RenderContent () writes the v2.4 form. It is the same as before, except that text the frame's encoding cannot hold now switches encoding.
  • A URL with characters outside ISO-8859-1 reads back percent-encoded.
  • Compressed frames past a tag's 64 MiB inflation budget are not decoded, so their values do not read. They are kept byte for byte.
  • A save whose ID3v2 tag would exceed 256 MiB returns a failure instead of throwing. The public Id3v2Tag.Render () throws a documented InvalidOperationException in that case.

No public API is added or removed.

Checklist

  • My code follows the project's code style (dotnet format --verify-no-changes passes)
  • I have added tests that prove my fix/feature works (red first for each fix)
  • All new and existing tests pass (dotnet test): see Testing
  • I have updated documentation as needed (CHANGELOG, XML docs)
  • My changes generate no new warnings (Release build: 0 warnings)

Implementation guide

  • Twin table: every ID3v2 carrier (MP3, raw AAC, MPEG-PS, WAV, AIFF, DSF, DFF, AVI) × v2.3 and v2.4 × every save entry point each carrier has, pinned by matrix tests
  • Kit run: corpus grid, fuzz (502,110 cases), black-box readers, mutation checks on every changed line
  • Pre-review: ten review rounds; the last found nothing

Testing

At head 4f6639e3: a Release build of all target frameworks with 0 warnings, and dotnet format --verify-no-changes clean.

Suite Passed Skipped Failed
net10.0 31,956 80 0
net10.0 with the sample corpus 31,978 58 0
net8.0 31,956 80 0
net8.0 with the sample corpus 31,978 58 0

@codecov

codecov Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.70014% with 114 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.94%. Comparing base (e49fa1a) to head (4f6639e).
⚠️ Report is 7 commits behind head on main.

Files with missing lines Patch % Lines
src/TagLibSharp2/Dsf/DsfFile.cs 78.57% 10 Missing and 5 partials ⚠️
...harp2.Tests/Id3/Id3v2/Id3v2WrittenEncodingTests.cs 96.58% 2 Missing and 10 partials ⚠️
src/TagLibSharp2/Dff/DffFile.cs 81.66% 10 Missing and 1 partial ⚠️
src/TagLibSharp2/Mpeg/Mp3File.cs 82.81% 8 Missing and 3 partials ⚠️
src/TagLibSharp2/Aac/AacFile.cs 84.37% 8 Missing and 2 partials ⚠️
src/TagLibSharp2/Mpeg/MpegPsFile.cs 89.47% 7 Missing and 1 partial ⚠️
src/TagLibSharp2/Id3/Id3v2/Id3v2Tag.FrameWalk.cs 91.66% 1 Missing and 6 partials ⚠️
...agLibSharp2/Id3/Id3v2/Id3v2TagTooLargeException.cs 60.00% 6 Missing ⚠️
src/TagLibSharp2/Riff/AviFile.cs 85.00% 6 Missing ⚠️
...bSharp2.Tests/Id3/Id3v2/Id3v2InflateBudgetTests.cs 98.31% 0 Missing and 6 partials ⚠️
... and 7 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #276      +/-   ##
==========================================
+ Coverage   93.88%   93.94%   +0.05%     
==========================================
  Files         633      643      +10     
  Lines      117480   119109    +1629     
  Branches    15694    15950     +256     
==========================================
+ Hits       110300   111900    +1600     
- Misses       4146     4160      +14     
- Partials     3034     3049      +15     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@decriptor
decriptor force-pushed the fix/id3v23-encoding branch 2 times, most recently from 747653a to 04bae2d Compare December 4, 2026 22:32
… read or save, and fail a save whose tag is too large for its size (#275)
@decriptor
decriptor force-pushed the fix/id3v23-encoding branch from 04bae2d to 4f6639e Compare December 4, 2026 22:51
@decriptor decriptor changed the title fix: Write every ID3v2 frame in an encoding and layout its version defines fix: Write every ID3v2 frame in an encoding and layout its version defines, and bound the work on crafted tags Dec 4, 2026
@decriptor

Copy link
Copy Markdown
Owner Author

Review: 0 findings at 4f6639e.

What was checked:

  • Ten review rounds. Every finding was fixed in the commit it belongs to, red first. The last round found nothing.

  • [Bug]: Performer roles in an ID3v2.3 tag are written as v2.4-only TMCL and TIPL frames #263 was taken out of this branch and moved to v0.8.0, because a first fix lost producer and engineer credits.

  • Writes: 1,408 saves across 8 carriers × v2.3/v2.4 × every save entry, with adversarial text in every encoding. Every frame byte is valid for its version, and every value reads back. The same probe finds 7,744 invalid writes on main.

  • Chapters: 1,248 save rows; unedited CHAP and CTOC frames stay byte-identical.

  • [Bug]: A crafted ID3v2 tag of compressed frames makes a save allocate gigabytes and throw #275:

    • The size bound held on 7,744 adversarial tags.
    • 4,320 save paths with the size limit lowered: none throw, every refused save leaves the source byte-identical, and a smaller tag saves.
    • A crafted 819 KB tag that made a save allocate about 9.4 GB now allocates about 165 MB.
    • A 10 MB-cover save allocates the same as main.
  • Fuzz: 714,558 cases, no exception.

  • Black box: ffprobe, exiftool (plain, -a, -validate) and MediaInfo agree with what this library reads.

  • Gates at the head: a Release build of all target frameworks with 0 warnings, dotnet format --verify-no-changes clean, and the test suites below.

    Suite Passed Skipped Failed
    net10.0 31,956 80 0
    net10.0 with the sample corpus 31,978 58 0
    net8.0 31,956 80 0
    net8.0 with the sample corpus 31,978 58 0
  • CI: build and test on Ubuntu, Windows and macOS, format, pack, security scan and Codecov are green.

History: 7 commits, one per issue, so this is rebase-merged rather than squashed.

@decriptor decriptor added the approved AI has reviewed and signed off label Dec 4, 2026
@decriptor
decriptor merged commit 7bac6b5 into main Dec 4, 2026
8 checks passed
@decriptor
decriptor deleted the fix/id3v23-encoding branch December 4, 2026 23:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment