Skip to content

Commit

Permalink
Fix: Malformed MKV could cause an infinite loop
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
ItalyPaleAle and sindresorhus committed Jul 19, 2022
1 parent 99cb019 commit 2c4d120
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
6 changes: 3 additions & 3 deletions core.js
Expand Up @@ -654,7 +654,7 @@ class FileTypeParser {
let ic = 0; // 0 = A, 1 = B, 2 = C, 3
// = D

while ((msb & mask) === 0) {
while ((msb & mask) === 0 && mask !== 0) {
++ic;
mask >>= 1;
}
Expand All @@ -675,7 +675,7 @@ class FileTypeParser {
};
}

async function readChildren(level, children) {
async function readChildren(children) {
while (children > 0) {
const element = await readElement();
if (element.id === 0x42_82) {
Expand All @@ -689,7 +689,7 @@ class FileTypeParser {
}

const re = await readElement();
const docType = await readChildren(1, re.len);
const docType = await readChildren(re.len);

switch (docType) {
case 'webm':
Expand Down
Binary file added fixture/fixture-corrupt.mkv
Binary file not shown.
5 changes: 5 additions & 0 deletions test.js
Expand Up @@ -614,3 +614,8 @@ test('supported files types are listed alphabetically', async t => {
currentNode = currentNode.next;
}
});

test('corrupt MKV throws', async t => {
const filePath = path.join(__dirname, 'fixture/fixture-corrupt.mkv');
await t.throwsAsync(fileTypeFromFile(filePath), {message: /out of range/});
});

0 comments on commit 2c4d120

Please sign in to comment.