Skip to content

Commit d39d2c0

Browse files
fix: fix protobuf processing causing index out of range errors
1 parent 4a7b840 commit d39d2c0

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ class Reader {
3737
public next(len: number): number[] {
3838
const n = new Array();
3939
for (let i = 0; i < len; i++) {
40+
// Stop reading if an error occurred
41+
if (this.error) {
42+
return n;
43+
}
4044
n[i] = this.nextByte();
4145
}
4246
return n;
@@ -54,6 +58,11 @@ function readProtoVarInt(reader: Reader): number {
5458
if ((b & 0x80) === 0) {
5559
break;
5660
}
61+
if (idx >= 10) {
62+
// Varint can be between 1 and 10 bytes. This is too large.
63+
reader.error = true;
64+
break;
65+
}
5766
idx++;
5867
}
5968

test/fixtures/vai_script.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ꕢꕎꕌ ꔖꔜꕯꕊ (1) ꕉꕜꕮ ꔔꘋ ꖸ ꔰ ꗋꘋ ꕮꕨ ꔔꘋ ꖸ ꕎ ꕉꖸꕊ ꕴꖃ ꕃꔤꘂ ꗱ, ꕉꖷ ꗪꗡ ꔻꔤ ꗏꗒꗡ ꕎ ꗪ ꕉꖸꕊ ꖏꕎ. ꕉꕡ ꖏ ꗳꕮꕊ ꗏ ꕪ ꗓ ꕉꖷ ꕉꖸ ꕘꕞ ꗪ. ꖏꖷ ꕉꖸꔧ ꖏ ꖸ ꕚꕌꘂ ꗷꔤ ꕞ ꘃꖷ ꘉꔧ ꗠꖻ ꕞ ꖴꘋ ꔳꕩ ꕉꖸ ꗳ.
2+
3+
ꕢꕎꕌ ꗱꕞꕯꕊ (2) ꗞ ꗏꗒ ꔰ ꕚ ꖷ ꗋꖺꕒꕌ ꖸ ꔰ ꕞ ꕺꖃ ꘈꗢ ꗏ ꗷꔤ ꕞ ꘃꖷ ꗞ ꗏ ꗓꖺ ꔰ ꔇꔀ ꕉ ꕮ, ꕉꔤ ꔳ ꗞ ꔱꔤꕮ ꖏ ꖺ ꗞ ꗬꔤꕮ ꖺ ꗞ ꕺꖃ ꕮꔧ ꕮꔕ, ꕉꔤ ꔳ ꖷꖬ ꖏ ꖺ ꕪꔤ, ꕉꔤ ꕢ ꔽꔤ ꕮ ꖺ ꔵꘉ, ꖺ ꕐꕌꔳ, ꖺ ꖏ ꘀꗫ ꕃꔤ ꖺ ꗞꗢ ꗃꕎ ꕸꖃ ꖷ ꗏ ꖺ ꗞ ꖷ ꖸ ꗏ, ꕉꔤ ꔳ ꔻꔤ ꗞꖻ ꖏ ꖺ ꔌꘋ ꕴꕱ ꗞꖻ, ꕉꔤ ꕢ ꕴꖃ ꕃꔤ ꕮ ꔧ ꖏ ꕮꔕ ꗷꔤ ꔰ. ꕉꔤ ꕒꕢ ꕉ ꔫꔤ ꕞ, ꗞ ꕮ ꗞ ꖸ ꗏ ꗓꖺ ꕮꕨ ꔻꔤ ꖏ ꗱ, ꖺ ꕢꕎꕌ ꖏ ꖴꕮ ꖺ ꗷꔤ ꖷ ꖦꖕꕰꕊ ꗪ ꗞꗢ ꕞ ꕴꖃ ꕸꖃꔀ ꗱ ꗡꕯ, ꖺ ꕐꕌꔳ ꖷ ꗏ ꗞꗢ ꗃꕎ, ꕉꔤ ꔳ ꕸꖃ ꖴꘋ ꖏ, ꔧ ꗨꗡ ꕉꕌ ꕸꖃꔀ ꗪ ꕸꖃ ꕮꔕ ꗛꖺ, ꔧ ꗨꗡ ꕉꕌ ꕸꖃꔀ ꗪ ꖴꗷ ꕢꕮ ꕒꕩ ꗏ ꖺ ꗷꔤ ꕮꔕ ꔰ ꕞ.

test/index.test.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { isBinaryFile, isBinaryFileSync } from '../src/index';
22

33
import * as fs from 'fs';
4-
import {promises as fsPromises} from 'fs';
4+
import { promises as fsPromises } from 'fs';
55
import * as path from 'path';
66

77
const FIXTURE_PATH = "./test/fixtures";
@@ -166,6 +166,16 @@ describe('async', () => {
166166
expect(result).toBe(true);
167167
});
168168

169+
it("should return false on a Vai script file", async () => {
170+
const file = path.join(FIXTURE_PATH, "vai_script.txt");
171+
172+
expect.assertions(1);
173+
174+
const result = await isBinaryFile(file);
175+
176+
expect(result).toBe(false);
177+
});
178+
169179
});
170180

171181
describe('sync', () => {
@@ -286,11 +296,19 @@ describe('sync', () => {
286296
const files = fs.readdirSync(encodingDir);
287297

288298
files.forEach((file) => {
289-
if (!/big5/.test(file) && !/gb/.test(file) && !/kr/.test(file)){
299+
if (!/big5/.test(file) && !/gb/.test(file) && !/kr/.test(file)) {
290300
expect(isBinaryFileSync(path.join(encodingDir, file))).toBe(false);
291301
}
292302
});
293303
});
304+
305+
it("should return false on a Vai script file", () => {
306+
const file = path.join(FIXTURE_PATH, "vai_script.txt");
307+
308+
const result = isBinaryFileSync(file);
309+
310+
expect(result).toBe(false);
311+
});
294312
});
295313

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

0 commit comments

Comments
 (0)