Skip to content

Commit 4a3f261

Browse files
committed
fix(media): validate the final sampled UTF-8 sequence
1 parent e8af04c commit 4a3f261

2 files changed

Lines changed: 80 additions & 14 deletions

File tree

‎packages/media-core/src/attachment-classify.test.ts‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ describe("classifyAttachmentBytes", () => {
3737
buffer: completeUtf8.subarray(0, 4096),
3838
expectedClass: "binary",
3939
},
40+
{
41+
name: "an invalid continuation after the sniff boundary",
42+
buffer: Buffer.concat([completeUtf8.subarray(0, 4095), Buffer.from([0xe2, 0x28])]),
43+
expectedClass: "binary",
44+
},
45+
{
46+
name: "an incomplete sequence at the actual 4,097-byte EOF",
47+
buffer: Buffer.concat([completeUtf8.subarray(0, 4095), Buffer.from([0xe2, 0x82])]),
48+
expectedClass: "binary",
49+
},
4050
{
4151
name: "an invalid byte before the sniff boundary",
4252
buffer: Buffer.concat([
@@ -54,6 +64,62 @@ describe("classifyAttachmentBytes", () => {
5464
});
5565
});
5666

67+
it.each([
68+
{ name: "two-byte sequence", prefixLength: 4095, bytes: [0xc2, 0xa3], expectedClass: "text" },
69+
{
70+
name: "three-byte sequence",
71+
prefixLength: 4095,
72+
bytes: [0xe2, 0x82, 0xac],
73+
expectedClass: "text",
74+
},
75+
{
76+
name: "four-byte sequence after its first byte",
77+
prefixLength: 4095,
78+
bytes: [0xf0, 0x9f, 0xa6, 0x80],
79+
expectedClass: "text",
80+
},
81+
{
82+
name: "four-byte sequence after its second byte",
83+
prefixLength: 4094,
84+
bytes: [0xf0, 0x9f, 0xa6, 0x80],
85+
expectedClass: "text",
86+
},
87+
{
88+
name: "four-byte sequence after its third byte",
89+
prefixLength: 4093,
90+
bytes: [0xf0, 0x9f, 0xa6, 0x80],
91+
expectedClass: "text",
92+
},
93+
{
94+
name: "overlong sequence crossing the boundary",
95+
prefixLength: 4095,
96+
bytes: [0xe0, 0x80, 0x80],
97+
expectedClass: "binary",
98+
},
99+
{
100+
name: "new sequence outside a complete sample",
101+
prefixLength: 4096,
102+
bytes: [0xf0, 0x9f, 0xa6, 0x80],
103+
expectedClass: "text",
104+
},
105+
{
106+
name: "invalid byte outside a complete sample",
107+
prefixLength: 4096,
108+
bytes: [0xff],
109+
expectedClass: "text",
110+
},
111+
])("bounds UTF-8 completion for a $name", async ({ prefixLength, bytes, expectedClass }) => {
112+
const buffer = Buffer.concat([
113+
completeUtf8.subarray(0, 4092),
114+
Buffer.alloc(prefixLength - 4092, 0x61),
115+
Buffer.from(bytes),
116+
]);
117+
await expect(classifyAttachmentBytes({ buffer, name: "notes" })).resolves.toEqual({
118+
mime: expectedClass === "text" ? "text/plain" : undefined,
119+
class: expectedClass,
120+
});
121+
});
122+
57123
it("infers delimited text from otherwise untyped bytes", async () => {
58124
await expect(
59125
classifyAttachmentBytes({ buffer: Buffer.from("name,value\nopenclaw,1"), name: "data.bin" }),

‎packages/media-core/src/attachment-classify.ts‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,30 +81,30 @@ function resolveUtf16Charset(buffer: Buffer): AttachmentCharset | undefined {
8181

8282
function textRatios(text: string, includeWordish: boolean): [printable: number, wordish: number] {
8383
let printable = 0;
84-
let control = 0;
8584
let wordish = 0;
85+
let total = 0;
8686
for (const char of text) {
8787
const code = char.codePointAt(0) ?? 0;
88-
if (code === 9 || code === 10 || code === 13 || code === 32) {
89-
printable += 1;
90-
wordish += 1;
91-
} else if (code < 32 || (code >= 0x7f && code <= 0x9f)) {
92-
control += 1;
93-
} else {
94-
printable += 1;
95-
wordish += includeWordish ? Number(WORDISH_CHAR.test(char)) : 0;
96-
}
88+
const whitespace = code === 9 || code === 10 || code === 13 || code === 32;
89+
const isControl = !whitespace && (code < 32 || (code >= 0x7f && code <= 0x9f));
90+
total += 1;
91+
printable += Number(!isControl);
92+
wordish += Number(whitespace || (!isControl && includeWordish && WORDISH_CHAR.test(char)));
9793
}
98-
const total = printable + control;
9994
return total === 0 ? [0, 0] : [printable / total, wordish / total];
10095
}
10196

10297
function looksLikeText(buffer: Buffer): boolean {
10398
const sample = buffer.subarray(0, 4096);
104-
const stream = sample.length < buffer.length;
99+
// Finish the last sampled UTF-8 sequence without starting a new one outside the window.
100+
// Its lead is within the final four bytes; completion needs at most three more.
101+
const tail = sample.subarray(-4);
102+
const leadIndex = tail.findLastIndex((byte) => (byte & 0xc0) !== 0x80);
103+
const lead = tail[leadIndex] ?? 0;
104+
const width = lead >= 0xf0 ? 4 : lead >= 0xe0 ? 3 : lead >= 0xc2 ? 2 : 1;
105+
const end = sample.length + Math.max(0, leadIndex + width - tail.length);
105106
try {
106-
// Only a bounded prefix may leave an incomplete UTF-8 code point buffered.
107-
const text = new TextDecoder("utf-8", { fatal: true }).decode(sample, { stream });
107+
const text = new TextDecoder("utf-8", { fatal: true }).decode(buffer.subarray(0, end));
108108
return textRatios(text, false)[0] > 0.85;
109109
} catch {
110110
const [printable, wordish] = textRatios(new TextDecoder("windows-1252").decode(sample), true);

0 commit comments

Comments
 (0)