From 46eaf8a129989c0df4c055f2a568c8f53249f96f Mon Sep 17 00:00:00 2001 From: David McKay Date: Thu, 3 Dec 2026 07:50:44 -0800 Subject: [PATCH] Composer: leave a file the browser names as another format to the server The browser's type for a file is whatever the computer registered for its extension. A .pdb protein structure is chemical/x-pdb on Linux, and a Windows machine with chemistry software names .mol and .cif too, though each is plain text. The composer refused any type that named a format OpenBot doesn't take, so those files never reached the server, which reads the bytes and takes text as text. Only images are refused by name now (an SVG, a HEIC), as the server refuses them. Every other type goes to the server, held to the largest limit, and a file that isn't text (a .zip, an old .doc) is refused there by its bytes. --- .../channels/composer/picked-files.test.ts | 80 ++++++++++++------- .../channels/composer/picked-files.ts | 32 ++++---- 2 files changed, 66 insertions(+), 46 deletions(-) diff --git a/app/src/components/channels/composer/picked-files.test.ts b/app/src/components/channels/composer/picked-files.test.ts index ae6cf5110a..464fe40de8 100644 --- a/app/src/components/channels/composer/picked-files.test.ts +++ b/app/src/components/channels/composer/picked-files.test.ts @@ -223,22 +223,20 @@ describe("screenPickedFiles", () => { ); }); - test("still refuses the older Office formats by name", () => { - const result = screenPickedFiles( - [ - file("old.doc", "application/msword"), - file("old.xls", "application/vnd.ms-excel"), - file("macros.xlsm", "application/vnd.ms-excel.sheet.macroEnabled.12"), - ], - { alreadyStaged: 0, limits }, - ); + /* + * The older Office formats go to the server, which refuses them by their bytes (an OLE file, or a + * package naming a macro-enabled main part). The file dialog still does not offer them. + */ + test("leaves the older Office formats to the server, and the dialog does not offer them", () => { + const picked = [ + file("old.doc", "application/msword"), + file("old.xls", "application/vnd.ms-excel"), + file("macros.xlsm", "application/vnd.ms-excel.sheet.macroEnabled.12"), + ]; + const result = screenPickedFiles(picked, { alreadyStaged: 0, limits }); - expect(result.accepted).toEqual([]); - expect(result.rejected.map(({ reason }) => reason)).toEqual([ - "'old.doc' isn't a file type you can attach.", - "'old.xls' isn't a file type you can attach.", - "'macros.xlsm' isn't a file type you can attach.", - ]); + expect(result.rejected).toEqual([]); + expect(result.accepted).toEqual(picked); const offered = FILE_PICKER_ACCEPT.split(","); for (const legacy of [".doc", ".xls", ".ppt", ".xlsm"]) expect(offered).not.toContain(legacy); @@ -288,33 +286,59 @@ describe("screenPickedFiles", () => { expect(result.rejected[0].reason).toMatch(/too large|larger than/); }); - test("a claim that names a format it does not accept is still refused here", () => { - // The fall-through is only for claims that name NOTHING. `application/zip` names a format, and - // `sniffMimeType` hands that name straight back for the server to refuse by name, so refusing - // it at pick time is the two halves agreeing rather than disagreeing. - const zip = file("archive.zip", "application/zip"); - const result = screenPickedFiles([zip], { alreadyStaged: 0, limits }); + /** + * EvoPointBio's protein structures and molecules. Each is plain text, and the browser's type for + * it is whatever the computer registered for the extension: Linux calls a `.pdb` + * `chemical/x-pdb`, and a Windows machine with chemistry software installed names `.mol` and + * `.cif` too. The server takes every one of them as text, so this screen must not refuse them by + * that name. + */ + test("lets a text file the browser names as another format through for the server to read", () => { + const picked = [ + file("model2.pdb", "chemical/x-pdb"), + file("model2.pdb", "application/x-pilot"), + file("1crn.cif", "chemical/x-cif"), + file("water.xyz", "chemical/x-xyz"), + file("water.mol", "chemical/x-mdl-molfile"), + file("plate-layout.xml", "application/xml"), + file("pipeline.yaml", "application/yaml"), + file("run.sh", "application/x-sh"), + ]; + const result = screenPickedFiles(picked, { alreadyStaged: 0, limits }); + + expect(result.rejected).toEqual([]); + expect(result.accepted).toEqual(picked); + }); + + test("holds a file the browser names as another format to the larger ceiling", () => { + const huge = sized("model2.pdb", "chemical/x-pdb", MAX_IMAGE_BYTES + 1); + const result = screenPickedFiles([huge], { alreadyStaged: 0, limits }); expect(result.accepted).toEqual([]); - expect(result.rejected).toHaveLength(1); + expect(result.rejected[0].reason).toBe( + "'model2.pdb' is too large. The limit is 8MB.", + ); }); - test("rejects an unsupported file type with a reason distinct from the SVG one", () => { + test("a zip goes to the server, which refuses it by its bytes", () => { const zip = file("archive.zip", "application/zip"); const result = screenPickedFiles([zip], { alreadyStaged: 0, limits }); - expect(result.accepted).toEqual([]); - expect(result.rejected[0].reason).not.toMatch(/SVG/); + expect(result.rejected).toEqual([]); + expect(result.accepted).toEqual([zip]); }); test("reports one rejection per bad file, each naming its own file", () => { const svg = file("a.svg", "image/svg+xml"); - const zip = file("b.zip", "application/zip"); - const result = screenPickedFiles([svg, zip], { alreadyStaged: 0, limits }); + const heic = file("b.heic", "image/heic"); + const result = screenPickedFiles([svg, heic], { alreadyStaged: 0, limits }); expect(result.rejected).toHaveLength(2); expect(result.rejected[0].name).toBe("a.svg"); - expect(result.rejected[1].name).toBe("b.zip"); + expect(result.rejected[1].name).toBe("b.heic"); + expect(result.rejected[1].reason).toBe( + "'b.heic' isn't a supported image format.", + ); }); test("counts the cap after kind and size, against already-staged files", () => { diff --git a/app/src/components/channels/composer/picked-files.ts b/app/src/components/channels/composer/picked-files.ts index 5676df3ba3..27104bbd28 100644 --- a/app/src/components/channels/composer/picked-files.ts +++ b/app/src/components/channels/composer/picked-files.ts @@ -6,7 +6,6 @@ import { largestAttachmentBytes, MAX_ATTACHMENTS_PER_MESSAGE, mediaTypeOf, - namesNoFormat, } from "@/lib/channels/attachments"; import { newId } from "@/lib/new-id"; import type { RejectedFile } from "./rejected-files"; @@ -69,28 +68,25 @@ export function screenPickedFiles( } /* - * THE BROWSER TOLD US NOTHING, SO THE SERVER GETS TO LOOK — AND ONLY THEN. + * A TYPE OPENBOT DOES NOT TAKE IS NOT A REFUSAL HERE: THE SERVER READS THE BYTES. * - * A claim that names no format is not a refusal, it is an absence: `sniffMimeType` throws - * exactly these claims away and reads the bytes, so the server accepts the plain text file - * behind an `application/octet-stream` that this screen used to turn away with "not a file type - * this chat accepts" — a sentence written by the half of the system that had not looked at it. + * The browser's type for a file is whatever the computer has registered for its extension, and + * for a plain text file that is often the name of something else. A `.pdb` protein structure + * is `chemical/x-pdb` on Linux, and a Windows machine with chemistry software installed names + * `.mol` and `.cif` too. Every one of those is text, and the server takes it as text: it keeps + * only a claim OpenBot acts on and reads the bytes for the rest, so a text file is stored as + * `text/plain` and anything else is refused by its bytes. Refusing the name here turned the + * person away from a file the server would have taken. * - * A claim that DOES name a format is still refused here, because there the two halves already - * agree: `sniffMimeType` hands such a name straight back for the server to refuse by name. + * The same holds for a browser that named nothing (`application/octet-stream`, `""`), which is + * where this branch began: `sniffMimeType` throws those claims away and reads the bytes. * - * The cost, stated because it is real: a genuinely unreadable file the browser could not name + * The cost, stated because it is real: a genuinely unreadable file (a `.zip`, an old `.doc`) * now takes a round trip to be refused, in the server's words rather than ours. That is the * right way round. The server has the bytes; this screen has a string somebody else wrote. + * Images are still refused here by name, as the server refuses them: an SVG, a HEIC. */ - const unnamed = kind === "unsupported" && namesNoFormat(mediaType); - - if (kind === "unsupported" && !unnamed) { - rejected.push( - reject(file, `'${file.name}' isn't a file type you can attach.`), - ); - continue; - } + const unnamed = kind === "unsupported"; if (kind === "image" && file.size > limits.imageBytes) { rejected.push( @@ -122,7 +118,7 @@ export function screenPickedFiles( continue; } - // The LOOSE ceiling for a file nobody has named yet, because this screen does not know which + // The LOOSE ceiling for a file whose kind only its bytes will tell, because this screen does not know which // of the limits applies to it. Guessing the tight one would put back exactly the refusal this // branch exists to remove; the server applies the right limit once it has the bytes. const loosest = largestAttachmentBytes(limits);