Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): read an outgoing audio file in a child process

Yudhi Armyndharis0fd76936fd53
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 55e50de39..5fd710d39 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -110,6 +110,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Preserve webhook outbox payloads until delivery or durable failure storage succeeds.
- Keep one terminal webhook failure per receiver idempotency key.
- Reject poll votes with no matching options while preserving explicit vote clearing.
+- Read the duration and waveform of an outgoing Baileys audio file in a separate process, so a crafted file can no longer abort or exhaust the gateway.
### Documentation
diff --git a/src/engine/adapters/baileys-audio-probe.spec.ts b/src/engine/adapters/baileys-audio-probe.spec.ts
new file mode 100644
index 000000000..b115d6b02
--- /dev/null
+++ b/src/engine/adapters/baileys-audio-probe.spec.ts
@@ -0,0 +1,71 @@
+import { probeAudio, WAVEFORM_BARS } from './baileys-audio-probe';
+
+const u32be = (value: number): Buffer => {
+ const out = Buffer.alloc(4);
+ out.writeUInt32BE(value);
+ return out;
+};
+
+/** FORM/AIFF whose ID3 chunk declares 0xffffff00 bytes: music-metadata's file read aborts its process on it. */
+function oversizedAiff(): Buffer {
+ const body = Buffer.concat([
+ Buffer.from('AIFF'),
+ Buffer.from('ID3 '),
+ u32be(0xffffff00),
+ Buffer.from('ID3\x04', 'latin1'),
+ ]);
+ return Buffer.concat([Buffer.from('FORM'), u32be(body.length), body]);
+}
+
+/** Unsigned 8-bit mono PCM at 8 kHz: a square wave, so every stretch of it is as loud as any other. */
+function wav(seconds: number): Buffer {
+ const rate = 8000;
+ const samples = Buffer.alloc(rate * seconds);
+ for (let i = 0; i < samples.length; i++) samples[i] = Math.floor(i / 20) % 2 === 0 ? 200 : 56;
+ const header = Buffer.alloc(44);
+ header.write('RIFF', 0, 'latin1');
+ header.writeUInt32LE(36 + samples.length, 4);
+ header.write('WAVEfmt ', 8, 'latin1');
+ header.writeUInt32LE(16, 16);
+ header.writeUInt16LE(1, 20); // PCM
+ header.writeUInt16LE(1, 22); // mono
+ header.writeUInt32LE(rate, 24);
+ header.writeUInt32LE(rate, 28); // bytes per second
+ header.writeUInt16LE(1, 32); // block align
+ header.writeUInt16LE(8, 34); // bits per sample
+ header.write('data', 36, 'latin1');
+ header.writeUInt32LE(samples.length, 40);
+ return Buffer.concat([header, samples]);
+}
+
+describe('probeAudio', () => {
+ // Each probe is a Node process that loads a parser, and a decoder for a voice note.
+ jest.setTimeout(60_000);
+
+ it('reads the duration of an audio file', async () => {
+ const probe = await probeAudio(wav(3), false);
+
+ expect(probe.seconds).toBe(3);
+ expect(probe.waveform).toBeUndefined();
+ });
+
+ it('reads the waveform of a voice note as 64 bars scaled so the loudest is 100', async () => {
+ const probe = await probeAudio(wav(3), true);
+
+ expect(probe.seconds).toBe(3);
+ expect(probe.waveform).toBeInstanceOf(Uint8Array);
+ expect(probe.waveform).toHaveLength(WAVEFORM_BARS);
+ expect(Math.max(...(probe.waveform as Uint8Array))).toBe(100);
+ });
+
+ it('answers nothing for bytes no parser reads, rather than rejecting', async () => {
+ await expect(probeAudio(Buffer.from('not audio at all'), true)).resolves.toEqual({});
+ });
+
+ it('survives a file that aborts the process of a reader that opens it', async () => {
+ // Opened as a file, music-metadata dies on this one with SIGABRT in node::fs::Read. Whatever the
+ // child does with it, the caller must get an answer and keep running.
+ await expect(probeAudio(oversizedAiff(), false)).resolves.toEqual({});
+ await expect(probeAudio(oversizedAiff(), true)).resolves.toEqual({});
+ });
+});
diff --git a/src/engine/adapters/baileys-audio-probe.ts b/src/engine/adapters/baileys-audio-probe.ts
new file mode 100644
index 000000000..de84c4e47
--- /dev/null
+++ b/src/engine/adapters/baileys-audio-probe.ts
@@ -0,0 +1,136 @@
+import { spawn } from 'node:child_process';
+import { join } from 'node:path';
+import { ConcurrencyLimiter } from '../../common/utils/concurrency-limiter';
+
+/** What could be read from an outgoing audio file; a field is absent when it could not be read. */
+export interface AudioProbe {
+ /** Playing time in whole seconds. */
+ seconds?: number;
+ /** The amplitude bars WhatsApp draws on a voice note, 0 to 100 each. */
+ waveform?: Uint8Array;
+}
+
+/** Bars on a voice note, the count Baileys' own waveform uses. */
+export const WAVEFORM_BARS = 64;
+
+/** Wall-clock ceiling for one probe; the child is killed past it. */
+const PROBE_TIMEOUT_MS = 30_000;
+/** What the child may print: a duration and 64 small integers, a line each. */
+const MAX_OUTPUT_CHARS = 4096;
+/** Probes running at once. Each is a whole Node process with a decoder loaded in it. */
+const probes = new ConcurrencyLimiter(2);
+/** The directory holding node_modules, whether this file runs from src/ or from dist/. */
+const APP_ROOT = join(__dirname, '..', '..', '..');
+
+/**
+ * Run in the child, with the file on stdin. The same two reads Baileys makes for an outgoing audio
+ * message, music-metadata for the duration and audio-decode for the voice-note bars, with the same
+ * bar arithmetic, so a file both can read gets the same answer here.
+ */
+const PROBE_SCRIPT = `
+const bars = ${WAVEFORM_BARS};
+const wantWaveform = process.argv[1] === 'ptt';
+const chunks = [];
+process.stdin.on('data', chunk => chunks.push(chunk));
+process.stdin.on('end', async () => {
+ const input = Buffer.concat(chunks);
+ try {
+ const mm = await import('music-metadata');
+ const metadata = await mm.parseBuffer(input, undefined, { duration: true });
+ const duration = metadata.format.duration;
+ if (typeof duration === 'number' && Number.isFinite(duration) && duration >= 0) {
+ process.stdout.write(JSON.stringify({ seconds: Math.floor(duration) }) + '\\n');
+ }
+ } catch {}
+ if (wantWaveform) {
+ try {
+ const { default: decode } = await import('audio-decode');
+ const raw = (await decode(input)).getChannelData(0);
+ const block = Math.floor(raw.length / bars);
+ const averages = [];
+ for (let i = 0; i < bars; i++) {
+ let sum = 0;
+ for (let j = 0; j < block; j++) sum += Math.abs(raw[block * i + j]);
+ averages.push(sum / block);
+ }
+ const peak = Math.max(...averages);
+ if (peak > 0 && Number.isFinite(peak)) {
+ process.stdout.write(JSON.stringify({ waveform: averages.map(value => Math.floor((100 * value) / peak)) }) + '\\n');
+ }
+ } catch {}
+ }
+});
+`;
+
+function parseProbe(output: string): AudioProbe {
+ const probe: AudioProbe = {};
+ for (const line of output.split('\n')) {
+ let value: { seconds?: unknown; waveform?: unknown };
+ try {
+ value = JSON.parse(line) as { seconds?: unknown; waveform?: unknown };
+ } catch {
+ continue;
+ }
+ if (typeof value.seconds === 'number' && Number.isInteger(value.seconds) && value.seconds >= 0) {
+ probe.seconds = value.seconds;
+ }
+ const bars = value.waveform;
+ if (
+ Array.isArray(bars) &&
+ bars.length === WAVEFORM_BARS &&
+ bars.every(bar => typeof bar === 'number' && Number.isInteger(bar) && bar >= 0 && bar <= 100)
+ ) {
+ probe.waveform = Uint8Array.from(bars as number[]);
+ }
+ }
+ return probe;
+}
+
+function runProbe(data: Buffer, ptt: boolean): Promise<AudioProbe> {
+ return new Promise(resolve => {
+ let settled = false;
+ let output = '';
+ const child = spawn(process.execPath, ['-e', PROBE_SCRIPT, ptt ? 'ptt' : 'audio'], {
+ cwd: APP_ROOT,
+ env: { PATH: process.env.PATH ?? '' },
+ stdio: ['pipe', 'pipe', 'ignore'],
+ });
+ const finish = (probe: AudioProbe): void => {
+ if (settled) return;
+ settled = true;
+ clearTimeout(timer);
+ resolve(probe);
+ };
+ const timer = setTimeout(() => {
+ child.kill('SIGKILL');
+ finish(parseProbe(output));
+ }, PROBE_TIMEOUT_MS);
+ child.stdout.setEncoding('utf8');
+ child.stdout.on('data', (chunk: string) => {
+ output += chunk;
+ if (output.length > MAX_OUTPUT_CHARS) {
+ child.kill('SIGKILL');
+ finish({});
+ }
+ });
+ child.on('error', () => finish({}));
+ // Whatever the child printed before it died still counts: the duration is printed before the decode.
+ child.on('close', () => finish(parseProbe(output)));
+ // The child may end before it has read everything it was sent.
+ child.stdin.on('error', () => undefined);
+ child.stdin.end(data);
+ });
+}
+
+/**
+ * Read the duration, and for a voice note the waveform, of an outgoing audio file in a separate process.
+ *
+ * Baileys makes both reads in the gateway's own process when the message does not carry them. They run
+ * parsers and decoders over bytes a caller chose: a 30-byte AIFF file aborts the process in
+ * music-metadata's file read, and a few hundred kilobytes of Opus packets decode into gigabytes of
+ * samples. Neither is an error the caller of the gateway can catch. Here a file that does either
+ * costs the child, and the answer is simply missing. Never rejects.
+ */
+export function probeAudio(data: Buffer, ptt: boolean): Promise<AudioProbe> {
+ return probes.run(() => runProbe(data, ptt)).catch(() => ({}));
+}
diff --git a/src/engine/adapters/baileys-messaging.ts b/src/engine/adapters/baileys-messaging.ts
index da68b32d9..7c1a8e5ba 100644
--- a/src/engine/adapters/baileys-messaging.ts
+++ b/src/engine/adapters/baileys-messaging.ts
@@ -44,6 +44,10 @@ import { EngineNotSentError } from '../../common/errors/engine-not-sent.error';
import { parseWaId, userPart } from '../identity/wa-id';
import { BAILEYS_QUERY_BUDGET_MS, withQueryDeadline } from './baileys-query-deadline';
import { ConcurrencyLimiter } from '../../common/utils/concurrency-limiter';
+import { probeAudio, WAVEFORM_BARS } from './baileys-audio-probe';
+
+/** Height of every bar of a voice note whose waveform could not be read: visible, and not a loud one. */
+const FLAT_WAVEFORM_BAR = 40;
/**
* Messaging-domain operations extracted from BaileysAdapter. The adapter keeps the public
@@ -459,13 +463,26 @@ export class BaileysMessaging {
async sendAudioMessage(chatId: string, media: MediaInput): Promise<MessageResult> {
this.host.ensureReady();
const { data, mimetype } = await resolveMediaBuffer(media, this.host.sessionProxyUrl(), 'audio/mpeg');
+ const ptt = media.ptt ?? false;
+ // Baileys reads the duration, and a voice note's waveform, in this process whenever the content does
+ // not carry them, which a crafted file turns into an abort or an out-of-memory kill of every session.
+ // Both are always given, from a child process, so Baileys never reads the file itself: a file the
+ // child cannot read sends with a zero duration and flat bars, as one Baileys could not read did.
+ const probe = await probeAudio(data, ptt);
return this.sendContent(
chatId,
// Audio carries no caption, so a mention here tags the recipient through contextInfo without
// visible @text. It is still forwarded: the route accepts `mentions` (SendAudioMessageDto
// extends SendMediaMessageDto) and whatsapp-web.js sends it, so dropping it here made the same
// request notify participants on one engine and silently not on the other.
- { audio: data, mimetype, ptt: media.ptt ?? false, ...this.withMentions(media.mentions) },
+ {
+ audio: data,
+ mimetype,
+ ptt,
+ seconds: probe.seconds ?? 0,
+ ...(ptt ? { waveform: probe.waveform ?? new Uint8Array(WAVEFORM_BARS).fill(FLAT_WAVEFORM_BAR) } : {}),
+ ...this.withMentions(media.mentions),
+ },
await this.quoteOption(media.quotedMessageId),
);
}
diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.ts
index 0e65cecc7..4364ea52f 100644
--- a/src/engine/adapters/baileys.adapter.spec.ts
+++ b/src/engine/adapters/baileys.adapter.spec.ts
@@ -8,6 +8,12 @@ jest.mock('../../common/media/load-remote-media', () => ({
loadRemoteMediaBuffer: jest.fn(),
}));
+// The probe runs a child process; its own spec drives the real one.
+jest.mock('./baileys-audio-probe', () => ({
+ WAVEFORM_BARS: 64,
+ probeAudio: jest.fn(() => Promise.resolve({})),
+}));
+
// A fake Baileys socket: an event emitter wearing the methods the adapter calls.
class FakeSock extends EventEmitter {
public ev = {
@@ -179,6 +185,7 @@ import { EngineTransportError } from '../../common/errors/engine-transport.error
import { LidNotMappedError } from '../../common/errors/lid-not-mapped.error';
import { countsTowardSendBreaker, sentNothing } from '../../modules/message/send-pacing.service';
import { loadRemoteMediaBuffer } from '../../common/media/load-remote-media';
+import { probeAudio } from './baileys-audio-probe';
import * as safeLinkPreview from './safe-link-preview';
const fakeStore = {
@@ -4197,10 +4204,31 @@ describe('BaileysAdapter media sends', () => {
it('sendAudioMessage sets ptt:false', async () => {
const adapter = await ready();
await adapter.sendAudioMessage('[email protected]', { mimetype: 'audio/mp4', data: Buffer.from([1]) });
+ // seconds is always present: an undefined one is what makes Baileys read the file itself.
expect(fakeSock.sendMessage).toHaveBeenCalledWith('[email protected]', {
audio: Buffer.from([1]),
mimetype: 'audio/mp4',
ptt: false,
+ seconds: 0,
+ });
+ });
+
+ it('sendAudioMessage hands Baileys the duration and waveform read in the child, so it reads nothing itself', async () => {
+ const waveform = Uint8Array.from({ length: 64 }, (_, bar) => bar);
+ (probeAudio as jest.Mock).mockResolvedValueOnce({ seconds: 7, waveform });
+ const adapter = await ready();
+ await adapter.sendAudioMessage('[email protected]', {
+ mimetype: 'audio/ogg; codecs=opus',
+ data: Buffer.from([1]),
+ ptt: true,
+ });
+ expect(probeAudio).toHaveBeenLastCalledWith(Buffer.from([1]), true);
+ expect(fakeSock.sendMessage).toHaveBeenCalledWith('[email protected]', {
+ audio: Buffer.from([1]),
+ mimetype: 'audio/ogg; codecs=opus',
+ ptt: true,
+ seconds: 7,
+ waveform,
});
});
@@ -4230,10 +4258,13 @@ describe('BaileysAdapter media sends', () => {
data: Buffer.from([1]),
ptt: true,
});
+ // A file the child could not read still goes out with both fields set, flat bars for a voice note.
expect(fakeSock.sendMessage).toHaveBeenCalledWith('[email protected]', {
audio: Buffer.from([1]),
mimetype: 'audio/ogg; codecs=opus',
ptt: true,
+ seconds: 0,
+ waveform: new Uint8Array(64).fill(40),
});
});