Skip to content

Commit dbf607d

Browse files
committed
protect against too large bounds
1 parent bec05bc commit dbf607d

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ class Reader {
3232
}
3333

3434
public next(len: number): number[] {
35+
// Prevent massive array allocation by checking bounds first
36+
if (len < 0 || len > this.size - this.offset) {
37+
this.error = true;
38+
return [];
39+
}
3540
const n = new Array();
3641
for (let i = 0; i < len; i++) {
3742
// Stop reading if an error occurred

test/index.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,19 @@ describe('async', () => {
166166
expect(result).toBe(true);
167167
});
168168

169+
it('should not crash on malformed protobuf-like data (issue #80)', async () => {
170+
const buff = Buffer.from(
171+
'82ACE2828045E382805FE1828053E7828045E7878045E8838145E2988445E2948545E2828D4CE2828A44E28280418CF7EC2E',
172+
'hex',
173+
);
174+
175+
expect.assertions(1);
176+
177+
const result = await isBinaryFile(buff);
178+
179+
expect(typeof result).toBe('boolean');
180+
});
181+
169182
it('should return false on a Vai script file', async () => {
170183
const file = path.join(FIXTURE_PATH, 'vai_script.txt');
171184

@@ -306,6 +319,17 @@ describe('sync', () => {
306319

307320
expect(result).toBe(false);
308321
});
322+
323+
it('should not crash on malformed protobuf-like data (issue #80)', () => {
324+
const buff = Buffer.from(
325+
'82ACE2828045E382805FE1828053E7828045E7878045E8838145E2988445E2948545E2828D4CE2828A44E28280418CF7EC2E',
326+
'hex',
327+
);
328+
329+
const result = isBinaryFileSync(buff);
330+
331+
expect(typeof result).toBe('boolean');
332+
});
309333
});
310334

311335
it('should return false on a UTF-8 file with emoji', () => {

0 commit comments

Comments
 (0)