Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: contain malformed hosted microphone payloads (#11274)

Adam Eivy64ff4117d77c
diff --git a/server/sockets/fableLoomHosted.js b/server/sockets/fableLoomHosted.js
index d8cb99baef..744074e438 100644
--- a/server/sockets/fableLoomHosted.js
+++ b/server/sockets/fableLoomHosted.js
@@ -28,6 +28,25 @@ import {
// 8 MB is ~4 minutes of 16 kHz mono audio for a single turn.
const MAX_MIC_FRAME_BYTES = 64 * 1024;
const MAX_UTTERANCE_BYTES = 8 * 1024 * 1024;
+const AUDIO_TOO_LARGE = Symbol('audio-too-large');
+
+// Socket payloads are untrusted: never let Buffer.from coerce arbitrary objects
+// or allocate from a declared array-like length. Views represent raw audio bytes,
+// including their offsets, rather than an array of numeric sample values.
+function boundedAudioBuffer(chunk, maxBytes) {
+ try {
+ if (!(chunk instanceof ArrayBuffer) && !ArrayBuffer.isView(chunk)) return null;
+ if (chunk.byteLength > maxBytes) return AUDIO_TOO_LARGE;
+ if (Buffer.isBuffer(chunk)) return chunk;
+ return chunk instanceof ArrayBuffer
+ ? Buffer.from(chunk)
+ : Buffer.from(chunk.buffer, chunk.byteOffset, chunk.byteLength);
+ } catch {
+ // A detached buffer (or otherwise unusable binary view) is a dropped frame,
+ // never an exception escaping the Socket.IO event boundary.
+ return null;
+ }
+}
let hostedNsInstance = null;
@@ -209,8 +228,8 @@ export function registerFableLoomHostedNamespace(io) {
// Already refused this turn — keep dropping until the next mic:start.
if (micOverflow || !chunk) return;
- const frame = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
- if (frame.length > MAX_MIC_FRAME_BYTES) return;
+ const frame = boundedAudioBuffer(chunk, MAX_MIC_FRAME_BYTES);
+ if (!Buffer.isBuffer(frame)) return;
if (micBytes + frame.length > MAX_UTTERANCE_BYTES) {
micOverflow = true;
@@ -234,14 +253,13 @@ export function registerFableLoomHostedNamespace(io) {
const overflowed = micOverflow;
let finalAudio = null;
- if (!overflowed && completeBuffer
- && (Buffer.isBuffer(completeBuffer) || ArrayBuffer.isView(completeBuffer))) {
- const complete = Buffer.isBuffer(completeBuffer) ? completeBuffer : Buffer.from(completeBuffer);
- if (complete.length > MAX_UTTERANCE_BYTES) {
+ if (!overflowed && completeBuffer != null) {
+ const complete = boundedAudioBuffer(completeBuffer, MAX_UTTERANCE_BYTES);
+ if (!Buffer.isBuffer(complete)) {
resetMicBuffer();
socket.emit('hosted:error', {
- code: 'UTTERANCE_TOO_LARGE',
- message: `audio too large (${complete.length} > ${MAX_UTTERANCE_BYTES} bytes)`,
+ code: complete === AUDIO_TOO_LARGE ? 'UTTERANCE_TOO_LARGE' : 'INVALID_AUDIO_FRAME',
+ message: 'audio must be a binary buffer within the utterance limit',
});
return;
}
diff --git a/server/sockets/fableLoomHosted.test.js b/server/sockets/fableLoomHosted.test.js
index 30101162e5..d061e8693b 100644
--- a/server/sockets/fableLoomHosted.test.js
+++ b/server/sockets/fableLoomHosted.test.js
@@ -480,6 +480,37 @@ describe('fableLoomHosted Socket.IO namespace', () => {
expect(stt.transcribe).toHaveBeenCalledWith(Buffer.from('aabb'), expect.anything());
});
+ it('drops malformed frames without coercion and keeps the guest session usable', async () => {
+ // Complete the real admission middleware before exercising the listener.
+ audience.handshake = { auth: { role: 'audience', sessionId: session.id, token } };
+ const next = vi.fn();
+ await middleware(audience, next);
+ expect(next).toHaveBeenCalledWith();
+ await audience.listeners['hosted:mic:start']();
+ const declaredLength = vi.fn(() => { throw new Error('array-like length was read'); });
+ const arrayLike = { get length() { return declaredLength(); } };
+ const detached = new ArrayBuffer(8);
+ structuredClone(detached, { transfer: [detached] });
+ for (const payload of [{}, 42, 'audio', [1, 2], { type: 'Buffer', data: [1] }, arrayLike, detached]) {
+ expect(() => audience.listeners['hosted:mic:frame'](payload)).not.toThrow();
+ }
+ expect(declaredLength).not.toHaveBeenCalled();
+ audience.listeners['hosted:mic:frame'](Buffer.from('still usable'));
+ await audience.listeners['hosted:mic:stop']();
+ expect(stt.transcribe).toHaveBeenCalledWith(Buffer.from('still usable'), expect.anything());
+ });
+
+ it('preserves raw bytes and slice offsets of binary microphone frames', async () => {
+ await audience.listeners['hosted:mic:start']();
+ const bytes = Uint8Array.from([0xff, 0x61, 0x62, 0xff]);
+ audience.listeners['hosted:mic:frame'](bytes.subarray(1, 3));
+ audience.listeners['hosted:mic:frame'](new DataView(bytes.buffer, 1, 2));
+ audience.listeners['hosted:mic:frame'](Uint8Array.from([0x63]).buffer);
+ audience.listeners['hosted:mic:frame'](new Uint16Array(bytes.buffer, 0, 1));
+ await audience.listeners['hosted:mic:stop']();
+ expect(stt.transcribe).toHaveBeenCalledWith(Buffer.from([0x61, 0x62, 0x61, 0x62, 0x63, 0xff, 0x61]), expect.anything());
+ });
+
it('stops buffering mic frames once the session expires mid-listen', async () => {
await audience.listeners['hosted:mic:start']();
@@ -503,6 +534,8 @@ describe('fableLoomHosted Socket.IO namespace', () => {
it('drops a single mic frame larger than the per-frame cap', async () => {
await audience.listeners['hosted:mic:start']();
audience.listeners['hosted:mic:frame'](Buffer.alloc(MAX_MIC_FRAME_BYTES + 1, 0x7a));
+ audience.listeners['hosted:mic:frame'](new ArrayBuffer(MAX_MIC_FRAME_BYTES + 1));
+ audience.listeners['hosted:mic:frame'](new Uint16Array(MAX_MIC_FRAME_BYTES / 2 + 1));
audience.listeners['hosted:mic:frame'](Buffer.from('aa'));
await audience.listeners['hosted:mic:stop']();
@@ -540,6 +573,18 @@ describe('fableLoomHosted Socket.IO namespace', () => {
.toMatchObject({ code: 'UTTERANCE_TOO_LARGE' });
});
+ it('rejects a malformed complete buffer and accepts a subsequent sliced binary utterance', async () => {
+ await audience.listeners['hosted:mic:start']();
+ await audience.listeners['hosted:mic:stop']({ length: 2 ** 32 });
+ expect(stt.transcribe).not.toHaveBeenCalled();
+ expect(audience.emitted.find((e) => e.event === 'hosted:error')?.data)
+ .toMatchObject({ code: 'INVALID_AUDIO_FRAME' });
+ await audience.listeners['hosted:mic:start']();
+ const bytes = Uint8Array.from([0xff, 0x61, 0x62, 0xff]);
+ await audience.listeners['hosted:mic:stop'](new DataView(bytes.buffer, 1, 2));
+ expect(stt.transcribe).toHaveBeenCalledWith(Buffer.from('ab'), expect.anything());
+ });
+
it('ignores hosted:mic:stop when the session is not listening', async () => {
await audience.listeners['hosted:mic:stop'](Buffer.from('aa'));