Skip to content

Commit

Permalink
Reduce msi false-positives (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoDF authored and sindresorhus committed Aug 15, 2019
1 parent bca081b commit 702f795
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
Binary file added fixture/fixture-doc.msi
Binary file not shown.
Binary file added fixture/fixture-ppt.msi
Binary file not shown.
Binary file added fixture/fixture-xls.msi
Binary file not shown.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ const fileType = input => {
};
}

if (check([0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1])) {
if (check([0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3E])) {
return {
ext: 'msi',
mime: 'application/x-msi'
Expand Down
25 changes: 25 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ const names = {
]
};

// Define an entry here only if the file type has potential
// for false-positives
const falsePositives = {
msi: [
'fixture-ppt',
'fixture-doc',
'fixture-xls'
]
};

const checkBufferLike = (t, type, bufferLike) => {
const {ext, mime} = fileType(bufferLike) || {};
t.is(ext, type);
Expand All @@ -129,6 +139,15 @@ const testFile = (t, ext, name) => {
checkBufferLike(t, ext, chunk.buffer);
};

const testFalsePositive = (t, ext, name) => {
const file = path.join(__dirname, 'fixture', `${name}.${ext}`);
const chunk = readChunk.sync(file, 0, 4 + 4096);

t.is(fileType(chunk), undefined);
t.is(fileType(new Uint8Array(chunk)), undefined);
t.is(fileType(chunk.buffer), undefined);
};

const testFileFromStream = async (t, ext, name) => {
const file = path.join(__dirname, 'fixture', `${(name || 'fixture')}.${ext}`);
const readableStream = await fileType.stream(fs.createReadStream(file));
Expand Down Expand Up @@ -176,6 +195,12 @@ for (const type of types) {
test(`.stream() method - same fileType - ${type} ${i++}`, testFileFromStream, type);
test(`.stream() method - identical streams - ${type} ${i++}`, testStream, type);
}

if (Object.prototype.hasOwnProperty.call(falsePositives, type)) {
for (const falsePositiveFile of falsePositives[type]) {
test(`false positive - ${type} ${i++}`, testFalsePositive, type, falsePositiveFile);
}
}
}

test('.stream() method - empty stream', async t => {
Expand Down

0 comments on commit 702f795

Please sign in to comment.