Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(flv-demuxer): skip invalid tag by size check. #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 37 additions & 14 deletions src/demux/flv-demuxer.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ class FLVDemuxer {
offset += 4;
}

let lastValidOffset = offset;
while (offset < chunk.byteLength) {
this._dispatch = true;

Expand All @@ -309,18 +310,31 @@ class FLVDemuxer {
let tagType = v.getUint8(0);
let dataSize = v.getUint32(0, !le) & 0x00FFFFFF;

if (offset + 11 + dataSize + 4 > chunk.byteLength) {
// data not enough for parsing actual data body
break;
}

// unsupported tag? try to figure it out!
if (tagType !== 8 && tagType !== 9 && tagType !== 18) {
Log.w(this.TAG, `Unsupported tag type ${tagType}, skipped`);
// consume the whole tag (skip it)
offset += 11 + dataSize + 4;
// not possible for a tag. should be a wrong byte, skip.
if (offset + 11 + dataSize + 4 > chunk.byteLength) {
offset += 1;
continue;
}

const tagSize = v.getUint32(11 + dataSize, !le);
if (tagSize === 11 + dataSize) {
Log.w(this.TAG, `Consumed an unsupported tag with type ${tagType} of size ${dataSize}`);
offset += 11 + dataSize + 4;
continue;
}

// not a valid tag. should be a wrong byte, skip.
offset += 1;
continue;
}

// not enough for a tag, stop and wait.
if (offset + 11 + dataSize + 4 > chunk.byteLength) {
break;
}

let ts2 = v.getUint8(4);
let ts1 = v.getUint8(5);
let ts0 = v.getUint8(6);
Expand All @@ -330,7 +344,20 @@ class FLVDemuxer {

let streamId = v.getUint32(7, !le) & 0x00FFFFFF;
if (streamId !== 0) {
Log.w(this.TAG, 'Meet tag which has StreamID != 0!');
// invalid tag, should be skipped.
offset += 1;
continue;
}

let prevTagSize = v.getUint32(11 + dataSize, !le);
if (prevTagSize !== 11 + dataSize) {
// invalid tag, should skipped.
offset += 1;
continue;
}

if (lastValidOffset !== offset) {
Log.w(this.TAG, `Skipped ${offset - lastValidOffset} bytes of invalid data`);
}

let dataOffset = offset + 11;
Expand All @@ -347,12 +374,8 @@ class FLVDemuxer {
break;
}

let prevTagSize = v.getUint32(11 + dataSize, !le);
if (prevTagSize !== 11 + dataSize) {
Log.w(this.TAG, `Invalid PrevTagSize ${prevTagSize}`);
}

offset += 11 + dataSize + 4; // tagBody + dataSize + prevTagSize
lastValidOffset = offset;
}

// dispatch parsed frames to consumer (typically, the remuxer)
Expand Down