Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

test(message): assert the stored-media download security headers

Yudhi Armyndharis78371a5e02d2
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 35c2ef2f7..bcaa6896b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Turkish (`tr`) dashboard translation. Thanks @codedByCan.
- `AUDIT_RETENTION_DAYS` is checked at boot, so a typo fails startup with a named error instead of silently reverting to the 90-day default. It is validated as non-negative rather than positive, because `0` is the documented switch that disables audit-log pruning entirely.
+- `message.controller.spec.ts` holds the two headers the stored-media download sends, `X-Content-Type-Options: nosniff` and `Content-Disposition: attachment`; deleting either had passed every suite in the repo.
### Fixed
diff --git a/src/modules/message/message.controller.spec.ts b/src/modules/message/message.controller.spec.ts
new file mode 100644
index 000000000..b82574fe9
--- /dev/null
+++ b/src/modules/message/message.controller.spec.ts
@@ -0,0 +1,38 @@
+import { MessageController } from './message.controller';
+import type { MessageService } from './message.service';
+import type { BulkMessageService } from './bulk-message.service';
+import type { Response } from 'express';
+
+/**
+ * `getChatMedia` serves third-party bytes from the API origin. The route shape, the roles and the
+ * status codes are already held by the OpenAPI snapshot and the route-fence gates; the two headers
+ * that keep those bytes inert in a browser were held by nothing — deleting either line from the
+ * controller passed every suite in the repo.
+ */
+describe('MessageController — stored media download', () => {
+ const getChatMedia = jest.fn().mockResolvedValue({ buffer: Buffer.from('GIF89a'), mimetype: 'image/gif' });
+ const controller = new MessageController(
+ { getChatMedia } as unknown as MessageService,
+ {} as unknown as BulkMessageService,
+ );
+
+ /**
+ * Express merges the object form of `res.set` into the header bag, so accumulating is both closer
+ * to the real thing than recording the last call and independent of how many calls the handler
+ * splits its headers across.
+ */
+ const mediaResponseHeaders = async (): Promise<Record<string, string>> => {
+ const headers: Record<string, string> = {};
+ const res = { set: (fields: Record<string, string>) => Object.assign(headers, fields) } as unknown as Response;
+ await controller.getChatMedia('session-1', '[email protected]', 'msg-1', res);
+ return headers;
+ };
+
+ it('sends nosniff, so a wrong Content-Type cannot be re-interpreted as active content', async () => {
+ expect((await mediaResponseHeaders())['X-Content-Type-Options']).toBe('nosniff');
+ });
+
+ it('sends the media as an attachment, so it is never rendered on the API origin', async () => {
+ expect((await mediaResponseHeaders())['Content-Disposition']).toBe('attachment');
+ });
+});