Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(status): serve only a single well-formed status media type
Yudhi Armyndharis5ed6c6a7c592
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.mdindex 68b86fffd..8c3dbfe6a 100644--- a/docs/06-api-specification.md+++ b/docs/06-api-specification.md@@ -4243,7 +4243,7 @@ Stream a stored status's media bytes (the file behind a `mediaUrl` returned abov**Response** `200` — the raw media bytes as the response body, served as an **attachment** (`Content-Disposition: attachment`, `X-Content-Type-Options: nosniff`). `Content-Type` is the stored mimetype when it belongs to the image/video/audio families — except `image/svg+xml` in any parameterized or whitespace-padded form, which is scriptable despite the `image/` prefix — and `application/octet-stream` otherwise, so no stored status media is ever served as active content on the API origin. Streamed via `StreamableFile` from whatever backs `StorageService` (local disk or S3 — the route does not care which).-The stored mimetype is trimmed and lowercased before either check, and the served `Content-Type` is that normalized value, since MIME types are case-insensitive.+Only the essence of the stored mimetype is checked and served: the part before any `;`, trimmed and lowercased, since MIME types are case-insensitive. Parameters are dropped, and a value that is not a single well-formed type (a comma-joined list, for example) is served as `application/octet-stream`.**Errors:** `401` missing/invalid API key, or key not scoped to this session · `404` `Status media not found or expired` — the status is text-only, its media was omitted (e.g. over the configured size cap), or the 24h TTL has since purged the rowdiff --git a/src/modules/status/status.service.spec.ts b/src/modules/status/status.service.spec.tsindex a6017fc85..105a8c8f9 100644--- a/src/modules/status/status.service.spec.ts+++ b/src/modules/status/status.service.spec.ts@@ -174,7 +174,21 @@ describe('StatusService media validation and selection', () => {},);- it('still serves a parameterized NON-scriptable image with its declared mimetype', async () => {+ // A browser splits a Content-Type on commas and takes the last valid type, so a stored value+ // that is not one single type must not be echoed back.+ it.each(['image/png,text/html', 'image/svg+xml,', 'image/png, image/svg+xml'])(+ 'serves the comma-joined form %s as inert octet-stream',+ async mimetype => {+ store.getMedia.mockResolvedValue({ path: 'statuses/sess/x', mimetype });+ storageService.getFile.mockResolvedValue(Buffer.from('x'));++ const media = await service.getStatusMedia('sess', 'w1');++ expect(media.mimetype).toBe('application/octet-stream');+ },+ );++ it('still serves a parameterized NON-scriptable image as its essence', async () => {// The exclusion is scoped to SVG: a perfectly ordinary `image/jpeg` with a charset parameter// (or any other image/video/audio type) must not be dragged down to octet-stream by it.store.getMedia.mockResolvedValue({ path: 'statuses/sess/x.jpg', mimetype: 'image/jpeg;charset=ISO-8859-1' });@@ -182,13 +196,14 @@ describe('StatusService media validation and selection', () => {const media = await service.getStatusMedia('sess', 'w1');- expect(media.mimetype).toBe('image/jpeg;charset=iso-8859-1');+ expect(media.mimetype).toBe('image/jpeg');});it.each([['text/HTML', 'application/octet-stream'],['IMAGE/JPEG', 'image/jpeg'],- ['audio/ogg; codecs=opus', 'audio/ogg; codecs=opus'],+ ['audio/ogg; codecs=opus', 'audio/ogg'],+ ['image/png;x=1,text/html', 'image/png'],])('serves a stored %s as %s', async (stored, served) => {store.getMedia.mockResolvedValue({ path: 'statuses/sess/x', mimetype: stored });storageService.getFile.mockResolvedValue(Buffer.from('x'));diff --git a/src/modules/status/status.service.ts b/src/modules/status/status.service.tsindex e4377f5f7..cb11945e2 100644--- a/src/modules/status/status.service.ts+++ b/src/modules/status/status.service.ts@@ -14,11 +14,11 @@ import { isMediaUrl } from '../../common/media/media-url';* status type — without it the dashboard's audio player is handed an octet-stream it cannot play.* `image/svg+xml` is excluded despite the image/ prefix: SVG is scriptable, so serving it with its* declared Content-Type would make the endpoint stored-XSS material — same exclusion the chat-media- * path applies. The stored mimetype is engine-reported verbatim, so the exclusion matches what a- * browser will parse it as: MIME parameters (`;charset=…`) and trailing whitespace are stripped by- * the Content-Type parser, and `image/svg+xml;charset=utf-8` still renders as SVG. */-const SAFE_STATUS_MIMETYPE = /^(image|video|audio)\//;-const SCRIPTABLE_SVG_MIMETYPE = /^image\/svg\+xml\s*(;|$)/;+ * path applies. The stored mimetype is engine-reported verbatim, so only its essence (the part before+ * any `;`, trimmed and lowercased) is checked and served, and it must be a single well-formed type:+ * a parameter list or a comma-joined second type could otherwise make a browser read the header as+ * something else. */+const SAFE_STATUS_MIMETYPE = /^(image|video|audio)\/[a-z0-9][a-z0-9.+-]*$/;// The padding and its trailing whitespace are one optional group: a bare `=*\s*` after a class that// also matches whitespace let a long whitespace run followed by any other character backtrack@@ -95,12 +95,10 @@ export class StatusService {try {const buffer = await this.storageService.getFile(media.path);// MIME types are case-insensitive, and a browser treats `image/SVG+XML` exactly as it treats- // `image/svg+xml`: both checks run on the normalized form, and that form is what gets served.- const declared = media.mimetype.trim().toLowerCase();+ // `image/svg+xml`: the check runs on the normalized essence, and that is what gets served.+ const essence = media.mimetype.split(';')[0].trim().toLowerCase();const mimetype =- SAFE_STATUS_MIMETYPE.test(declared) && !SCRIPTABLE_SVG_MIMETYPE.test(declared)- ? declared- : 'application/octet-stream';+ SAFE_STATUS_MIMETYPE.test(essence) && essence !== 'image/svg+xml' ? essence : 'application/octet-stream';return { buffer, mimetype };} catch (error) {// The row outlived its file: purgeExpired (or a concurrent delete) removed it between the