fix: prevent file corruption when ID3 bytes appear in audio data #185
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes a critical bug that can cause severe file corruption when using
removeTags()followed bywrite()on MP3 files that contain the bytes "ID3" in their audio data.The Problem
The current implementation of
getFramePosition()searches for "ID3" patterns throughout the entire buffer, not just at the beginning where ID3v2 tags are supposed to be. This can lead to catastrophic data loss when:Real-world impact:
The Solution
This PR modifies
getFramePosition()to only look for ID3 tags at position 0 (the beginning of the buffer), which aligns with the ID3v2 specification that mandates ID3v2 tags MUST be at the beginning of files.Changes:
Testing
Added comprehensive test cases in
test/test.jsunder the#removeTagsFromBuffer()section that:All 74 tests pass, including the new corruption prevention tests.
Compatibility
This fix maintains 100% compatibility with all ID3v2 versions:
ID3v1 is unaffected as node-id3 does not support ID3v1 tags (they use "TAG" not "ID3").
Root Cause Analysis
The bug occurs because:
removeTags()correctly removes ID3 tags from the filewrite()reads the file and callsremoveTagsFromBuffer()againExample from affected file:
49 44 33 04 00 3d 30 32 01 33References
Both specifications clearly state that ID3v2 tags are prepended to files (i.e., at position 0).
Breaking Changes
None. This is a bug fix that prevents incorrect behavior while maintaining all existing functionality.