Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(message): serve chat media as its bare mimetype essence
Yudhi Armyndharis99a5364c5c2b
diff --git a/src/modules/message/message.service.spec.ts b/src/modules/message/message.service.spec.tsindex f2508cf90..e417b709a 100644--- a/src/modules/message/message.service.spec.ts+++ b/src/modules/message/message.service.spec.ts@@ -999,6 +999,21 @@ describe('MessageService', () => {expect(served).toBe('application/octet-stream');});+ // A sender declares the mimetype, and the value becomes the Content-Type header. Parameters are+ // dropped: a comma inside them makes a browser read a second type, and a character above U+00FF+ // makes Node refuse the header, which failed the route with a 500 on every call.+ it.each([+ ['image/png;x=1,text/html', 'image/png'],+ ['image/png;,image/svg+xml', 'image/png'],+ ['image/jpeg;\u0101', 'image/jpeg'],+ ['audio/ogg; codecs=opus', 'audio/ogg'],+ ['IMAGE/PNG', 'image/png'],+ ])('serves %p as its essence %p', async (declared, expected) => {+ const svc = build(archived(declared), storage());+ const { mimetype: served } = await svc.getChatMedia('sess-1', '[email protected]', 'wa-1');+ expect(served).toBe(expected);+ });+it('404s when nothing is archived for the message', async () => {const svc = build({ getMedia: jest.fn().mockResolvedValue(null) }, storage());await expect(svc.getChatMedia('sess-1', '[email protected]', 'wa-1')).rejects.toThrow(NotFoundException);diff --git a/src/modules/message/message.service.ts b/src/modules/message/message.service.tsindex 6da6331ab..bbd1f6a2e 100644--- a/src/modules/message/message.service.ts+++ b/src/modules/message/message.service.ts@@ -148,11 +148,16 @@ export const DEFAULT_PIN_DURATION_SECONDS = 86400;* octet-stream so the media endpoint cannot host active content on the API origin.*/const INERT_MEDIA_MIMETYPE =- /^(image\/(jpeg|png|gif|webp|bmp)|video\/(mp4|webm|quicktime|3gpp)|audio\/(mpeg|mp4|ogg|aac|wav|webm))(;|$)/;+ /^(image\/(jpeg|png|gif|webp|bmp)|video\/(mp4|webm|quicktime|3gpp)|audio\/(mpeg|mp4|ogg|aac|wav|webm))$/;-/** The declared mimetype when it is safe to echo back, else inert octet-stream. */+/**+ * The declared mimetype's normalized essence when it is safe to echo back, else inert octet-stream.+ * Parameters are dropped rather than echoed: the sender wrote them, a comma among them makes a+ * browser read a second type, and a character above U+00FF is refused as a header value.+ */function inertMimetype(mimetype: string): string {- return INERT_MEDIA_MIMETYPE.test(mimetype) ? mimetype : 'application/octet-stream';+ const essence = mimetype.split(';')[0].trim().toLowerCase();+ return INERT_MEDIA_MIMETYPE.test(essence) ? essence : 'application/octet-stream';}@Injectable()