fix(media): harden unknown mime handling from #39199 (thanks @nicolasgrasset)

Co-authored-by: Nicolas Grasset <nicolas.grasset@gmail.com>
This commit is contained in:
Peter Steinberger
2026-03-07 23:30:32 +00:00
parent dc92f2e19d
commit 1aaca517e3
7 changed files with 23 additions and 14 deletions

View File

@@ -457,7 +457,7 @@ describe("local media root guard", () => {
}),
).resolves.toEqual(
expect.objectContaining({
kind: "unknown",
kind: undefined,
}),
);
@@ -468,7 +468,7 @@ describe("local media root guard", () => {
}),
).resolves.toEqual(
expect.objectContaining({
kind: "unknown",
kind: undefined,
}),
);
});
@@ -498,7 +498,7 @@ describe("local media root guard", () => {
}),
).resolves.toEqual(
expect.objectContaining({
kind: "unknown",
kind: undefined,
}),
);
});

View File

@@ -19,7 +19,7 @@ import { resolveUserPath } from "../utils.js";
export type WebMediaResult = {
buffer: Buffer;
contentType?: string;
kind: MediaKind;
kind: MediaKind | undefined;
fileName?: string;
};
@@ -284,12 +284,12 @@ async function loadWebMediaInternal(
const clampAndFinalize = async (params: {
buffer: Buffer;
contentType?: string;
kind: MediaKind;
kind: MediaKind | undefined;
fileName?: string;
}): Promise<WebMediaResult> => {
// If caller explicitly provides maxBytes, trust it (for channels that handle large files).
// Otherwise fall back to per-kind defaults.
const cap = maxBytes !== undefined ? maxBytes : maxBytesForKind(params.kind);
const cap = maxBytes !== undefined ? maxBytes : maxBytesForKind(params.kind ?? "document");
if (params.kind === "image") {
const isGif = params.contentType === "image/gif";
if (isGif || !optimizeImages) {
@@ -324,7 +324,7 @@ async function loadWebMediaInternal(
if (/^https?:\/\//i.test(mediaUrl)) {
// Enforce a download cap during fetch to avoid unbounded memory usage.
// For optimized images, allow fetching larger payloads before compression.
const defaultFetchCap = maxBytesForKind("unknown");
const defaultFetchCap = maxBytesForKind("document");
const fetchCap =
maxBytes === undefined
? defaultFetchCap