Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(status): check status media base64 in linear time

Yudhi Armyndharis22e5f8ebd3be
diff --git a/src/modules/status/status.service.spec.ts b/src/modules/status/status.service.spec.ts
index ac691cee0..38c13a333 100644
--- a/src/modules/status/status.service.spec.ts
+++ b/src/modules/status/status.service.spec.ts
@@ -204,6 +204,27 @@ describe('StatusService media validation and selection', () => {
expect(engine.postImageStatus).not.toHaveBeenCalled();
});
+ it('refuses a long whitespace run in linear time, without holding the event loop', async () => {
+ const started = Date.now();
+ await expect(
+ service.postImageStatus('s1', { base64: ' '.repeat(200_000) + '!' }, { recipients: ['[email protected]'] }),
+ ).rejects.toThrow('media must be an absolute http(s) URL or base64');
+ expect(Date.now() - started).toBeLessThan(1000);
+ });
+
+ // Most real payloads end in padding (any file whose size is not a multiple of 3), and line-wrapped
+ // or URL-safe base64 is still base64, so the shape check must keep accepting all of them.
+ it.each(['QQ==', 'QUI=\n', 'QU\r\nJD', 'a-_b=='])('accepts padded, wrapped or URL-safe base64 %j', async base64 => {
+ await service.postImageStatus('s1', { base64, mimetype: 'image/png' }, { recipients: ['[email protected]'] });
+ expect(engine.postImageStatus).toHaveBeenCalledWith(expect.objectContaining({ data: base64 }), expect.anything());
+ });
+
+ it('refuses a character after the base64 padding', async () => {
+ await expect(
+ service.postImageStatus('s1', { base64: 'QQ==x', mimetype: 'image/png' }, { recipients: ['[email protected]'] }),
+ ).rejects.toThrow('media must be an absolute http(s) URL or base64');
+ });
+
describe('voice status', () => {
// Ogg/Opus is the only thing WhatsApp plays as a status voice note, and neither engine
// transcodes — so the default has to be that, not a generic audio type.
diff --git a/src/modules/status/status.service.ts b/src/modules/status/status.service.ts
index c29752c5e..83038e132 100644
--- a/src/modules/status/status.service.ts
+++ b/src/modules/status/status.service.ts
@@ -20,7 +20,10 @@ import { isMediaUrl } from '../../common/media/media-url';
const SAFE_STATUS_MIMETYPE = /^(image|video|audio)\//;
const SCRIPTABLE_SVG_MIMETYPE = /^image\/svg\+xml\s*(;|$)/;
-const BASE64_TEXT = /^[A-Za-z0-9+/_\-\s]*=*\s*$/;
+// 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
+// quadratically, and one request of padded spaces held the event loop for minutes.
+const BASE64_TEXT = /^[A-Za-z0-9+/_\-\s]*(?:=+\s*)?$/;
@Injectable()
export class StatusService {