Skip to content

Commit 0bac235

Browse files
Gigioxxclaude
andcommitted
fix(server): refuse steered images too large to scale safely
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f41cf95 commit 0bac235

3 files changed

Lines changed: 28 additions & 16 deletions

File tree

‎apps/server/src/provider/Layers/ClaudeAdapter.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1670,6 +1670,13 @@ const buildUserMessageEffect = Effect.fn("buildUserMessageEffect")(function* (
16701670
),
16711671
)
16721672
: bytes;
1673+
if (fittedBytes === null) {
1674+
return yield* new ProviderAdapterRequestError({
1675+
provider: PROVIDER,
1676+
method: "turn/start",
1677+
detail: `Image '${attachment.name}' is too large to send while Claude is working. Resize it or send it after the turn finishes.`,
1678+
});
1679+
}
16731680

16741681
sdkContent.push(
16751682
buildClaudeImageContentBlock({

‎apps/server/src/provider/Layers/claudeImageSize.test.ts‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,20 @@ function png(width: number, height: number, alpha: number, colorType: 0 | 6 = 6)
1111
return PNG.sync.write(image, { colorType });
1212
}
1313

14+
function fitted(mimeType: string, bytes: Uint8Array) {
15+
const result = fitClaudeImage(mimeType, bytes);
16+
if (!result) throw new Error("expected a fitted image");
17+
return Buffer.from(result);
18+
}
19+
1420
describe("fitClaudeImage", () => {
1521
it("leaves an image within 2000px untouched", () => {
1622
const bytes = png(2000, 1200, 255);
1723
expect(fitClaudeImage("image/png", bytes)).toBe(bytes);
1824
});
1925

2026
it("scales a tall phone screenshot to 2000px and keeps its transparency", () => {
21-
const fitted = fitClaudeImage("image/png", png(1080, 2424, 128));
22-
const decoded = PNG.sync.read(Buffer.from(fitted));
27+
const decoded = PNG.sync.read(fitted("image/png", png(1080, 2424, 128)));
2328
expect({ width: decoded.width, height: decoded.height }).toEqual({ width: 891, height: 2000 });
2429
expect(decoded.data[3]).toBe(128);
2530
});
@@ -30,21 +35,21 @@ describe("fitClaudeImage", () => {
3035
for (let offset = 0; offset < image.data.length; offset += 4) {
3136
image.data.fill((offset / 4) % 2 === 0 ? 255 : 0, offset, offset + 4);
3237
}
33-
const decoded = PNG.sync.read(Buffer.from(fitClaudeImage("image/png", PNG.sync.write(image))));
38+
const decoded = PNG.sync.read(fitted("image/png", PNG.sync.write(image)));
3439
expect([...decoded.data.subarray(0, 4)]).toEqual([255, 255, 255, 128]);
3540
});
3641

37-
it("sends an image too large to decode safely as is", () => {
42+
it("refuses a PNG too large to decode safely", () => {
3843
const header = Buffer.alloc(24);
3944
header.set([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 13], 0);
4045
header.write("IHDR", 12, "ascii");
4146
header.writeUInt32BE(20_000, 16);
4247
header.writeUInt32BE(20_000, 20);
43-
expect(fitClaudeImage("image/png", header)).toBe(header);
48+
expect(fitClaudeImage("image/png", header)).toBeNull();
4449
});
4550

4651
it("keeps a grayscale PNG grayscale", () => {
47-
const decoded = PNG.sync.read(Buffer.from(fitClaudeImage("image/png", png(2400, 100, 90, 0))));
52+
const decoded = PNG.sync.read(fitted("image/png", png(2400, 100, 90, 0)));
4853
expect({ width: decoded.width, colorType: decoded.colorType }).toEqual({
4954
width: 2000,
5055
colorType: 0,
@@ -67,7 +72,7 @@ describe("fitClaudeImage", () => {
6772
90,
6873
);
6974
// Orientation 6 swaps the reported sides.
70-
expect(readImageDimensions(fitClaudeImage("image/jpeg", data))).toEqual({
75+
expect(readImageDimensions(fitted("image/jpeg", data))).toEqual({
7176
width: 667,
7277
height: 2000,
7378
});

‎apps/server/src/provider/Layers/claudeImageSize.ts‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,29 @@ const CLAUDE_MAX_IMAGE_EDGE = 2000;
1212

1313
/**
1414
* A highly compressible PNG fits the attachment byte cap at any size, and
15-
* decoding expands it to 4 bytes per pixel. Past this bound the image is sent
16-
* as is, as before. 50MP still covers an 8000x6000 photo.
15+
* decoding expands it to 4 bytes per pixel, so past this bound a PNG or JPEG
16+
* is not decoded. 50MP still covers an 8000x6000 photo.
1717
*/
1818
const MAX_DECODED_PIXELS = 50_000_000;
1919

2020
/**
2121
* Scales a PNG or JPEG down to fit CLAUDE_MAX_IMAGE_EDGE. Images already
2222
* within the limit come back untouched, so only oversized ones pay for a
23-
* decode.
23+
* decode. Returns null for a PNG or JPEG too large to decode safely, since
24+
* sending it unscaled would break the thread.
2425
*
2526
* ponytail: decodes on the event loop, about 150ms for a phone screenshot,
2627
* 400ms for a Retina screenshot, and 0.7s for a 12MP photo. Only steered
2728
* images pay it. Move it to a worker if that stall shows up.
2829
*/
29-
export function fitClaudeImage(mimeType: string, bytes: Uint8Array): Uint8Array {
30+
export function fitClaudeImage(mimeType: string, bytes: Uint8Array): Uint8Array | null {
3031
const dimensions = readImageDimensions(bytes);
31-
if (
32-
!dimensions ||
33-
Math.max(dimensions.width, dimensions.height) <= CLAUDE_MAX_IMAGE_EDGE ||
34-
dimensions.width * dimensions.height > MAX_DECODED_PIXELS
35-
) {
32+
if (!dimensions || Math.max(dimensions.width, dimensions.height) <= CLAUDE_MAX_IMAGE_EDGE) {
3633
return bytes;
3734
}
35+
const decodable = dimensions.width * dimensions.height <= MAX_DECODED_PIXELS;
3836
if (mimeType === "image/png") {
37+
if (!decodable) return null;
3938
const image = PNG.sync.read(Buffer.from(bytes));
4039
const { width, height, data } = downscale(image);
4140
const scaled = new PNG({ width, height });
@@ -46,6 +45,7 @@ export function fitClaudeImage(mimeType: string, bytes: Uint8Array): Uint8Array
4645
return PNG.sync.write(scaled, { colorType });
4746
}
4847
if (mimeType === "image/jpeg") {
48+
if (!decodable) return null;
4949
const image = jpeg.decode(bytes, { useTArray: true });
5050
// The spread keeps the decoder's `exifBuffer`, so the EXIF orientation of
5151
// a phone photo survives the re-encode.

0 commit comments

Comments
 (0)