Skip to content

Commit 2c4d120

Browse files
Fix: Malformed MKV could cause an infinite loop
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 99cb019 commit 2c4d120

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

core.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ class FileTypeParser {
654654
let ic = 0; // 0 = A, 1 = B, 2 = C, 3
655655
// = D
656656

657-
while ((msb & mask) === 0) {
657+
while ((msb & mask) === 0 && mask !== 0) {
658658
++ic;
659659
mask >>= 1;
660660
}
@@ -675,7 +675,7 @@ class FileTypeParser {
675675
};
676676
}
677677

678-
async function readChildren(level, children) {
678+
async function readChildren(children) {
679679
while (children > 0) {
680680
const element = await readElement();
681681
if (element.id === 0x42_82) {
@@ -689,7 +689,7 @@ class FileTypeParser {
689689
}
690690

691691
const re = await readElement();
692-
const docType = await readChildren(1, re.len);
692+
const docType = await readChildren(re.len);
693693

694694
switch (docType) {
695695
case 'webm':

fixture/fixture-corrupt.mkv

37.2 KB
Binary file not shown.

test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,3 +614,8 @@ test('supported files types are listed alphabetically', async t => {
614614
currentNode = currentNode.next;
615615
}
616616
});
617+
618+
test('corrupt MKV throws', async t => {
619+
const filePath = path.join(__dirname, 'fixture/fixture-corrupt.mkv');
620+
await t.throwsAsync(fileTypeFromFile(filePath), {message: /out of range/});
621+
});

0 commit comments

Comments
 (0)