Skip to content

Commit f290bba

Browse files
committed
fix: a chat attachment is checked like every other upload
Avatars are capped at 5MB and sniffed for magic bytes — the one thing a client cannot lie about. Chat attachments arrive by a different road, base64 data URLs rather than multipart, and that road had neither: the extension came from the declared mime and nothing ever looked at the bytes. Verified against the running API: six megabytes of 'AAAA…' declared as image/png returned 201 and landed on disk as a .png, and the agent was then told to read it as an image. Four attachments are allowed and the body limit is 25MB, so that was the ceiling. Size is now measured from the base64 length before decoding, so an oversized attachment is refused without materialising it, and the file is named after what the bytes actually are. Exporting the sniffer meant teaching it GIF, which chat allows and avatars do not — so the avatar path now checks the sniffed type against its allow-list rather than accepting anything recognisable, or a .gif renamed .png would have passed.
1 parent 6dd2ec2 commit f290bba

4 files changed

Lines changed: 109 additions & 6 deletions

File tree

‎HANDOFF.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,27 @@ lost their borders — a bordered button inside a bordered capsule was the
757757
same double-frame problem as the chat bubble; they are now text/icons with
758758
a hover wash, and the single solid fill marks Sign Up as the one primary.
759759

760+
## 04:50 (7-29) — 聊天附件走的是另一条路,而那条路没有任何检查
761+
762+
头像上传会限制 5MB 并**嗅探magic bytes**(客户端唯一无法撒谎的东西)。
763+
聊天里贴的图片走的是完全不同的一条路——base64 data URL 而不是 multipart
764+
——那条路**两样都没有**:扩展名直接取自客户端声称的 mime,字节从来没人看过。
765+
766+
实测:发 6MB 的 `AAAA…` 并声称 `image/png`,HTTP 201,**文件以 .png 落盘,
767+
6144KB**,然后 agent 被告知"读这张图片"。数量上限是 4 张,单张没有上限,
768+
25MB 的 body 上限装得下四张 6MB。
769+
770+
- `sniff` 从 file_check 导出(聊天附件走另一条路,但需要同一个答案),
771+
补上 GIF ——聊天允许 gif,嗅探器得能叫出它的名字。
772+
- `decodeImage` 现在:**先按 base64 长度估算大小再解码**(超限的附件不必
773+
先在内存里materialise出来),然后嗅探,文件名按**实际类型**取扩展名。
774+
- 顺带把导出 sniff 带来的口子堵上:头像路径原本只要"能认出来"就放行,
775+
加了 GIF 之后一个改名成 .png 的 gif 就会通过——现在改成对照白名单校验。
776+
777+
验证:4 个 sniff 单测(含 6MB 单字节 blob、`<script>`、PDF 头、太短的
778+
buffer、RIFF/WAVE 不能冒充 WebP);真实 API 四发四态——6MB blob 和伪装成
779+
png 的脚本都没有落盘,真 png 落盘,声称是 png 的 gif 以 **.gif** 落盘。
780+
760781
## 04:20 (7-29) — 停用账号此前只是一句建议
761782

762783
admin 控制台有个 "set user active" 开关。登录会检查 `isActive`,**但别的

‎backend/src/chat/project-agent.ts‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { HarnessAgent } from '@ai-sdk/harness/agent';
66
import { claudeCode, createClaudeCode } from '@ai-sdk/harness-claude-code';
77
import { codex, createCodex } from '@ai-sdk/harness-codex';
88
import { getProjectsDir } from '../common/utils/common-path';
9+
import { sniff } from '../common/security/file_check';
910
import {
1011
AVAILABLE_MODELS,
1112
DEFAULT_MODEL,
@@ -241,6 +242,9 @@ const EXTENSIONS: Record<string, string> = {
241242
* inline image part is not.
242243
*/
243244
/** A data URL turned into the bytes and file name it should be saved under. */
245+
/** Same ceiling the avatar and cover paths enforce. */
246+
const MAX_ATTACHMENT_BYTES = 5 * 1024 * 1024;
247+
244248
function decodeImage(
245249
image: string,
246250
): { name: string; bytes: Buffer } | undefined {
@@ -250,14 +254,33 @@ function decodeImage(
250254
return undefined;
251255
}
252256
const [, mime, data] = match;
253-
const extension = EXTENSIONS[mime];
254-
if (!extension) {
257+
if (!EXTENSIONS[mime]) {
255258
logger.warn(`Skipping attachment of unsupported type ${mime}`);
256259
return undefined;
257260
}
261+
262+
// Base64 is 4 characters per 3 bytes; measuring before decoding means a
263+
// huge attachment is refused without first materialising it in memory.
264+
if ((data.length * 3) / 4 > MAX_ATTACHMENT_BYTES) {
265+
logger.warn('Skipping attachment over 5MB');
266+
return undefined;
267+
}
268+
269+
const bytes = Buffer.from(data, 'base64');
270+
271+
// The declared type decided the extension and nothing looked at the bytes,
272+
// so anything at all could be written to disk as a .png and handed to the
273+
// agent as an image. Name the file after what it actually is — the same
274+
// rule uploads follow, by the same sniffer.
275+
const actual = sniff(bytes);
276+
if (!actual || !EXTENSIONS[actual]) {
277+
logger.warn('Skipping attachment that is not an image');
278+
return undefined;
279+
}
280+
258281
return {
259-
name: `${randomUUID()}.${extension}`,
260-
bytes: Buffer.from(data, 'base64'),
282+
name: `${randomUUID()}.${EXTENSIONS[actual]}`,
283+
bytes,
261284
};
262285
}
263286

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { sniff } from '../file_check';
2+
3+
const PNG = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
4+
const JPEG = Buffer.from([0xff, 0xd8, 0xff, 0xe0]);
5+
const GIF = Buffer.from('GIF89a', 'ascii');
6+
const webp = () =>
7+
Buffer.concat([
8+
Buffer.from('RIFF', 'ascii'),
9+
Buffer.alloc(4),
10+
Buffer.from('WEBP', 'ascii'),
11+
]);
12+
13+
/**
14+
* The one check a client cannot lie about. Both upload roads depend on it:
15+
* multipart avatars and base64 chat attachments.
16+
*/
17+
describe('sniff', () => {
18+
it('names each format by its magic bytes', () => {
19+
expect(sniff(Buffer.concat([PNG, Buffer.alloc(20)]))).toBe('image/png');
20+
expect(sniff(Buffer.concat([JPEG, Buffer.alloc(20)]))).toBe('image/jpeg');
21+
expect(sniff(webp())).toBe('image/webp');
22+
expect(sniff(Buffer.concat([GIF, Buffer.alloc(20)]))).toBe('image/gif');
23+
});
24+
25+
it('refuses bytes that are not an image, whatever they claim', () => {
26+
// The exact shape that reached disk as a .png: a big blob of one byte.
27+
expect(sniff(Buffer.alloc(6 * 1024 * 1024, 0x41))).toBeUndefined();
28+
expect(sniff(Buffer.from('<script>alert(1)</script>'))).toBeUndefined();
29+
expect(sniff(Buffer.from('%PDF-1.7'))).toBeUndefined();
30+
});
31+
32+
it('refuses a buffer too short to carry a signature', () => {
33+
expect(sniff(Buffer.alloc(0))).toBeUndefined();
34+
expect(sniff(PNG.subarray(0, 4))).toBeUndefined();
35+
expect(sniff(Buffer.from('RIFF', 'ascii'))).toBeUndefined();
36+
});
37+
38+
it('does not mistake a RIFF container that is not WebP', () => {
39+
const wav = Buffer.concat([
40+
Buffer.from('RIFF', 'ascii'),
41+
Buffer.alloc(4),
42+
Buffer.from('WAVE', 'ascii'),
43+
]);
44+
expect(sniff(wav)).toBeUndefined();
45+
});
46+
});

‎backend/src/common/security/file_check.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ const ALLOWED_EXTENSIONS = ['.jpg', '.jpeg', '.png', '.webp'];
1919
* script renamed `evil.png` was stored and then served from this origin.
2020
* Returns undefined when the header matches no image this service accepts.
2121
*/
22-
function sniff(buffer: Buffer): string | undefined {
22+
/**
23+
* What a buffer actually is, by its magic bytes — the one thing a client
24+
* cannot lie about. Exported because chat attachments arrive by a different
25+
* road (base64 data URLs, not multipart) and need the same answer.
26+
*/
27+
export function sniff(buffer: Buffer): string | undefined {
2328
if (
2429
buffer.length >= 8 &&
2530
buffer.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]))
@@ -36,6 +41,11 @@ function sniff(buffer: Buffer): string | undefined {
3641
) {
3742
return 'image/webp';
3843
}
44+
// GIF87a / GIF89a. Not accepted for avatars or covers, but the agent can
45+
// be handed one, so the sniffer has to be able to name it.
46+
if (buffer.length >= 6 && buffer.subarray(0, 3).toString('ascii') === 'GIF') {
47+
return 'image/gif';
48+
}
3949
return undefined;
4050
}
4151

@@ -86,8 +96,11 @@ export async function validateAndBufferFile(
8696
// The decisive check, and the only one the client cannot lie about. The
8797
// sniffed type is also what gets returned, so the stored file's extension
8898
// describes what is actually in it.
99+
// Checked against the allow-list, not merely for being recognisable: the
100+
// sniffer also knows GIF, which chat attachments accept and this path does
101+
// not — otherwise a .gif renamed .png would pass here.
89102
const actual = sniff(buffer);
90-
if (!actual) {
103+
if (!actual || !ALLOWED_MIME_TYPES.includes(actual)) {
91104
throw new BadRequestException(
92105
'That file is not a JPEG, PNG, or WebP image.',
93106
);

0 commit comments

Comments
 (0)