Skip to content

Commit b535804

Browse files
Nelglorclaude
andcommitted
fix(mobile): measure picker files before keeping original bytes
The picker's reported size is a hint. Android content streams can deliver more bytes than they advertise, so a supported image over the cap could still be read into JS as a full base64 string before the size gate ran. The original-bytes path now trusts only a size measured from the file and renders anything unmeasurable. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent ee0662d commit b535804

2 files changed

Lines changed: 40 additions & 25 deletions

File tree

‎apps/mobile/src/lib/composerFiles.test.ts‎

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -230,15 +230,9 @@ describe("composer file attachments", () => {
230230
it("renders a supported original that exceeds the image limit instead of rejecting it", async () => {
231231
mocks.pickMedia.mockResolvedValue({
232232
canceled: false,
233-
assets: [
234-
{
235-
...photo,
236-
fileName: "photo.jpg",
237-
mimeType: "image/jpeg",
238-
fileSize: PROVIDER_SEND_TURN_MAX_IMAGE_BYTES + 1,
239-
},
240-
],
233+
assets: [{ ...photo, fileName: "photo.jpg", mimeType: "image/jpeg" }],
241234
});
235+
mocks.size.mockReturnValue(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES + 1);
242236

243237
const result = await pickComposerImages({ existingCount: 0 });
244238

@@ -249,18 +243,35 @@ describe("composer file attachments", () => {
249243
]);
250244
});
251245

252-
it("measures the picker file when the picker reports no size", async () => {
246+
it("measures the picker file instead of trusting the reported size", async () => {
247+
// A content stream can deliver more bytes than the picker advertises; a supported
248+
// original only skips rendering when the file itself measures within the limit.
253249
mocks.pickMedia.mockResolvedValue({
254250
canceled: false,
255-
assets: [{ ...photo, fileName: "photo.png", mimeType: "image/png", fileSize: undefined }],
251+
assets: [{ ...photo, fileName: "photo.png", mimeType: "image/png", fileSize: 42 }],
256252
});
257-
mocks.size.mockReturnValue(3);
258-
mocks.readBase64.mockResolvedValue("YWJj");
253+
mocks.size.mockReturnValue(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES + 1);
259254

260255
const result = await pickComposerImages({ existingCount: 0 });
261256

262-
expect(mocks.manipulate).not.toHaveBeenCalled();
263-
expect(result.images).toEqual([expect.objectContaining({ mimeType: "image/png" })]);
257+
expect(mocks.readBase64).not.toHaveBeenCalled();
258+
expect(mocks.manipulate).toHaveBeenCalledWith(photo.uri);
259+
expect(result.images).toEqual([expect.objectContaining({ mimeType: "image/jpeg" })]);
260+
});
261+
262+
it("renders a supported original whose size cannot be measured", async () => {
263+
mocks.pickMedia.mockResolvedValue({
264+
canceled: false,
265+
assets: [
266+
{ ...photo, uri: "content://media/1", fileName: "photo.png", mimeType: "image/png" },
267+
],
268+
});
269+
270+
const result = await pickComposerImages({ existingCount: 0 });
271+
272+
expect(mocks.readBase64).not.toHaveBeenCalled();
273+
expect(mocks.manipulate).toHaveBeenCalledWith("content://media/1");
274+
expect(result.images).toEqual([expect.objectContaining({ mimeType: "image/jpeg" })]);
264275
});
265276

266277
it("checks the rendered JPEG against the image limit", async () => {
@@ -334,7 +345,9 @@ describe("composer file attachments", () => {
334345

335346
it("retains mixed photos and videos, keeping video bytes in durable file storage", async () => {
336347
mocks.pickMedia.mockResolvedValue({ canceled: false, assets: [image, video] });
337-
mocks.size.mockReturnValue(video.fileSize);
348+
mocks.size.mockImplementation((uri: string) =>
349+
uri.endsWith("clip.mov") ? video.fileSize : 3,
350+
);
338351

339352
const result = await pickComposerMedia({ existingCount: 0, maxVideoBytes: 50 * 1024 * 1024 });
340353

@@ -445,7 +458,7 @@ describe("composer file attachments", () => {
445458
canceled: false,
446459
assets: [{ ...video, fileSize: reported }, image],
447460
});
448-
mocks.size.mockReturnValue(stored);
461+
mocks.size.mockImplementation((uri: string) => (uri.endsWith("clip.mov") ? stored : 3));
449462

450463
const result = await pickComposerMedia({ existingCount: 0, maxVideoBytes: limit });
451464

‎apps/mobile/src/lib/composerImages.ts‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -470,22 +470,24 @@ export async function pickComposerMedia(input: {
470470
}
471471

472472
const name = asset.fileName?.trim() || "image";
473-
let sourceBytes = asset.fileSize ?? null;
474-
if (sourceBytes === null) {
475-
try {
476-
const { File } = await import("expo-file-system");
477-
sourceBytes = new File(asset.uri).size;
478-
} catch {
479-
sourceBytes = null;
480-
}
473+
// The picker's reported size is a hint, not a measurement: Android content streams can
474+
// deliver more bytes than they advertise. Only a size read from the file itself decides
475+
// whether the original bytes are safe to load into JS.
476+
let sourceBytes: number | null = null;
477+
try {
478+
const { File } = await import("expo-file-system");
479+
sourceBytes = new File(asset.uri).size;
480+
} catch {
481+
sourceBytes = null;
481482
}
482483
// Originals the provider can read and that fit the cap pass through byte for byte so
483484
// transparency and animation survive. Everything else (HEIC/HEIF, oversized JPEGs,
484-
// unknown sizes) is rendered to a bounded JPEG off the JS thread.
485+
// unmeasurable sources) is rendered to a bounded JPEG off the JS thread.
485486
const originalMimeType =
486487
mimeType !== undefined &&
487488
isProviderSendTurnSupportedImageMimeType(mimeType) &&
488489
sourceBytes !== null &&
490+
sourceBytes > 0 &&
489491
sourceBytes <= PROVIDER_SEND_TURN_MAX_IMAGE_BYTES
490492
? mimeType
491493
: null;

0 commit comments

Comments
 (0)