fix(bridge): harden local transport handling

Strengthen validation and buffering for local process communication while preserving the established compatibility boundary.\n\nAdd regression coverage for malformed input, boundary values, fragmented delivery, combined traffic, and normal request behavior.
This commit is contained in:
DrSmoothl committed 2026-08-01 17:37:16 +08:00
1 parent ae2e894f14
commit abe4ea80c8
2 files changed
+276 -26

No files matched your search

+103 -26
View File
@@ -14,6 +14,12 @@ import path from 'path';
export const PIPE_MAGIC = 0x31504851;
export const PIPE_VERSION = 1;
export const HEADER_SIZE = 40;
// Keep these byte limits in lockstep with nnphook's pipe_protocol.hpp. The
// native readers already enforce them; mirroring them here prevents the JS
// endpoint from buffering frames that the peer can never accept.
export const MAX_PIPE_CMD_BYTES = 4096;
export const MAX_PIPE_MSG_BYTES = 65536;
export const MAX_PIPE_BODY_BYTES = 16 * 1024 * 1024;
export const DEFAULT_ACK_TIMEOUT_MS = 5000;
export const DEFAULT_REPLY_TIMEOUT_MS = 30000;
const DEFAULT_PIPE_PROBE_TIMEOUT_MS = 250;
@@ -303,6 +309,7 @@ function encodeFrame({
const cmdBuf = Buffer.from(cmd, 'utf8');
const msgBuf = Buffer.from(msg, 'utf8');
const bodyBuf = toBuffer(body);
validatePipeFrameLengths(cmdBuf.length, msgBuf.length, bodyBuf.length);
const header = Buffer.alloc(HEADER_SIZE);
header.writeUInt32LE(PIPE_MAGIC, 0);
header.writeUInt16LE(PIPE_VERSION, 4);
@@ -317,45 +324,115 @@ function encodeFrame({
return Buffer.concat([header, cmdBuf, msgBuf, bodyBuf]);
}
function validatePipeFrameLengths(cmdLen: number, msgLen: number, bodyLen: number): void {
const lengths = [
['command', cmdLen, MAX_PIPE_CMD_BYTES],
['message', msgLen, MAX_PIPE_MSG_BYTES],
['body', bodyLen, MAX_PIPE_BODY_BYTES],
] as const;
for (const [name, length, limit] of lengths) {
if (length > limit) {
throw new Error(`pipe frame ${name} length ${length} exceeds limit ${limit}`);
}
}
}
interface PendingPipeFrame {
op: number;
requestId: number;
status: number;
flags: number;
value0: bigint;
cmdLen: number;
msgLen: number;
bodyLen: number;
payload: Buffer;
received: number;
}
class FrameReader {
private buffer = Buffer.alloc(0);
private readonly header = Buffer.alloc(HEADER_SIZE);
private headerBytes = 0;
private pending: PendingPipeFrame | null = null;
constructor(private readonly onFrame: (frame: PipeFrame) => void) { }
push(chunk: Buffer): void {
this.buffer = Buffer.concat([this.buffer, chunk]);
while (this.buffer.length >= HEADER_SIZE) {
const magic = this.buffer.readUInt32LE(0);
const version = this.buffer.readUInt16LE(4);
if (magic !== PIPE_MAGIC || version !== PIPE_VERSION) {
throw new Error(`bad frame header magic=0x${magic.toString(16)} version=${version}`);
let chunkOffset = 0;
while (chunkOffset < chunk.length) {
if (!this.pending) {
const headerBytes = Math.min(HEADER_SIZE - this.headerBytes, chunk.length - chunkOffset);
chunk.copy(
this.header,
this.headerBytes,
chunkOffset,
chunkOffset + headerBytes,
);
this.headerBytes += headerBytes;
chunkOffset += headerBytes;
if (this.headerBytes < HEADER_SIZE) return;
this.headerBytes = 0;
const magic = this.header.readUInt32LE(0);
const version = this.header.readUInt16LE(4);
if (magic !== PIPE_MAGIC || version !== PIPE_VERSION) {
throw new Error(`bad frame header magic=0x${magic.toString(16)} version=${version}`);
}
const cmdLen = this.header.readUInt32LE(20);
const msgLen = this.header.readUInt32LE(24);
const bodyLen = this.header.readUInt32LE(28);
validatePipeFrameLengths(cmdLen, msgLen, bodyLen);
const payloadLength = cmdLen + msgLen + bodyLen;
this.pending = {
op: this.header.readUInt16LE(6),
requestId: this.header.readUInt32LE(8),
status: this.header.readInt32LE(12),
flags: this.header.readUInt32LE(16),
value0: this.header.readBigUInt64LE(32),
cmdLen,
msgLen,
bodyLen,
payload: payloadLength === 0 ? Buffer.alloc(0) : Buffer.allocUnsafe(payloadLength),
received: 0,
};
}
const cmdLen = this.buffer.readUInt32LE(20);
const msgLen = this.buffer.readUInt32LE(24);
const bodyLen = this.buffer.readUInt32LE(28);
const total = HEADER_SIZE + cmdLen + msgLen + bodyLen;
if (this.buffer.length < total) return;
const pending = this.pending;
const payloadBytes = Math.min(
pending.payload.length - pending.received,
chunk.length - chunkOffset,
);
if (payloadBytes > 0) {
chunk.copy(
pending.payload,
pending.received,
chunkOffset,
chunkOffset + payloadBytes,
);
pending.received += payloadBytes;
chunkOffset += payloadBytes;
}
if (pending.received < pending.payload.length) return;
this.pending = null;
const frame: PipeFrame = {
op: this.buffer.readUInt16LE(6),
requestId: this.buffer.readUInt32LE(8),
status: this.buffer.readInt32LE(12),
flags: this.buffer.readUInt32LE(16),
value0: this.buffer.readBigUInt64LE(32),
op: pending.op,
requestId: pending.requestId,
status: pending.status,
flags: pending.flags,
value0: pending.value0,
cmd: '',
msg: '',
body: Buffer.alloc(0),
};
let offset = HEADER_SIZE;
frame.cmd = this.buffer.subarray(offset, offset + cmdLen).toString('utf8');
offset += cmdLen;
frame.msg = this.buffer.subarray(offset, offset + msgLen).toString('utf8');
offset += msgLen;
frame.body = Buffer.from(this.buffer.subarray(offset, offset + bodyLen));
this.buffer = this.buffer.subarray(total);
let offset = 0;
frame.cmd = pending.payload.subarray(offset, offset + pending.cmdLen).toString('utf8');
offset += pending.cmdLen;
frame.msg = pending.payload.subarray(offset, offset + pending.msgLen).toString('utf8');
offset += pending.msgLen;
frame.body = Buffer.from(pending.payload.subarray(offset, offset + pending.bodyLen));
this.onFrame(frame);
}
}
@@ -830,7 +907,7 @@ export class QqHookClient extends EventEmitter {
reader.push(chunk);
} catch (error) {
this.emit('error', error);
socket.destroy(error instanceof Error ? error : undefined);
socket.destroy();
}
});
socket.on('error', error => {
@@ -15,6 +15,9 @@ import { afterEach, describe, expect, it, vi } from 'vitest';
import {
HEADER_SIZE,
listLiveLinuxPipePids,
MAX_PIPE_BODY_BYTES,
MAX_PIPE_CMD_BYTES,
MAX_PIPE_MSG_BYTES,
PIPE_MAGIC,
PIPE_VERSION,
PipeOp,
@@ -147,6 +150,176 @@ describe('listLiveLinuxPipePids', () => {
});
});
describe('QqHookClient — pipe frame limits', () => {
it.each([
['command', 20, MAX_PIPE_CMD_BYTES],
['message', 24, MAX_PIPE_MSG_BYTES],
['body', 28, MAX_PIPE_BODY_BYTES],
] as const)(
'rejects an oversized inbound %s before waiting for its payload',
async (name, lengthOffset, limit) => {
const pid = 778830;
let socket: net.Socket | null = null;
const createConnection = vi.spyOn(net, 'createConnection').mockImplementation((() => {
socket = new Duplex({ read() { /* peer data is injected with emit('data') */ } }) as net.Socket;
queueMicrotask(() => {
socket!.emit('connect');
const header = Buffer.alloc(HEADER_SIZE);
header.writeUInt32LE(PIPE_MAGIC, 0);
header.writeUInt16LE(PIPE_VERSION, 4);
header.writeUInt16LE(PipeOp.hello, 6);
header.writeUInt32LE(limit + 1, lengthOffset);
socket!.emit('data', header);
});
return socket;
}) as typeof net.createConnection);
const client = new QqHookClient(pid);
const connecting = client.connect().catch(() => undefined);
try {
const error = await new Promise<Error>((resolve, reject) => {
const timer = setTimeout(
() => reject(new Error('oversized frame was not rejected')),
100,
);
client.once('error', (reason: unknown) => {
clearTimeout(timer);
resolve(reason instanceof Error ? reason : new Error(String(reason)));
});
});
expect(error.message).toBe(
`pipe frame ${name} length ${limit + 1} exceeds limit ${limit}`,
);
expect(socket?.destroyed).toBe(true);
} finally {
client.close();
await connecting;
createConnection.mockRestore();
}
});
it('rejects an oversized outbound command before writing to the pipe', async () => {
const pid = 778831;
const onWrite = vi.fn();
const connection = mockControlPipe(pid, onWrite);
const client = new QqHookClient(pid);
try {
await client.connect();
await expect(client.sendNoReply('x'.repeat(MAX_PIPE_CMD_BYTES + 1), Buffer.alloc(0), {
ackTimeoutMs: 25,
})).rejects.toThrow(
`pipe frame command length ${MAX_PIPE_CMD_BYTES + 1} exceeds limit ${MAX_PIPE_CMD_BYTES}`,
);
expect(onWrite).not.toHaveBeenCalled();
} finally {
client.close();
connection.mockRestore();
}
});
it('rejects an oversized outbound body before writing to the pipe', async () => {
const pid = 778832;
const onWrite = vi.fn();
const connection = mockControlPipe(pid, onWrite);
const client = new QqHookClient(pid);
try {
await client.connect();
await expect(client.sendNoReply(
'test.command',
Buffer.alloc(MAX_PIPE_BODY_BYTES + 1),
)).rejects.toThrow(
`pipe frame body length ${MAX_PIPE_BODY_BYTES + 1} exceeds limit ${MAX_PIPE_BODY_BYTES}`,
);
expect(onWrite).not.toHaveBeenCalled();
} finally {
client.close();
connection.mockRestore();
}
});
it('accepts values exactly at the command limit', async () => {
const pid = 778833;
const connection = mockControlPipe(pid, (socket, payload) => {
const requestId = payload.readUInt32LE(8);
queueMicrotask(() => socket.emit('data', pipeFrame({
op: PipeOp.sendAck,
requestId,
})));
});
const client = new QqHookClient(pid);
try {
await expect(client.sendNoReply('x'.repeat(MAX_PIPE_CMD_BYTES), Buffer.alloc(0)))
.resolves.toMatchObject({ requestId: 1 });
} finally {
client.close();
connection.mockRestore();
}
});
it('parses fragmented and coalesced frames without losing boundaries', async () => {
const pid = 778834;
const states: string[] = [];
const wire = Buffer.concat([
pipeFrame({ op: PipeOp.hello, value0: BigInt(pid), message: 'control' }),
pipeFrame({ op: PipeOp.loginState, status: 1, value0: 10001n, message: '10001' }),
pipeFrame({ op: PipeOp.loginState, status: 1, value0: 10002n, message: '10002' }),
]);
const createConnection = vi.spyOn(net, 'createConnection').mockImplementation((() => {
const socket = new Duplex({ read() { /* peer data is injected with emit('data') */ } }) as net.Socket;
queueMicrotask(() => {
socket.emit('connect');
queueMicrotask(() => {
const cuts = [1, 7, 39, 40, 44, 83, wire.length - 1, wire.length];
let offset = 0;
for (const end of cuts) {
if (end > offset) socket.emit('data', wire.subarray(offset, end));
offset = end;
}
});
});
return socket;
}) as typeof net.createConnection);
const client = new QqHookClient(pid);
client.on('loginState', state => states.push(state.uin));
try {
const hello = await client.connect();
expect(hello).toMatchObject({ pipeName: 'control', pid });
expect(states).toEqual(['10001', '10002']);
} finally {
client.close();
createConnection.mockRestore();
}
});
it('accepts an inbound message exactly at the native limit', async () => {
const pid = 778835;
const message = 'x'.repeat(MAX_PIPE_MSG_BYTES);
const frame = pipeFrame({ op: PipeOp.hello, value0: BigInt(pid), message });
const createConnection = vi.spyOn(net, 'createConnection').mockImplementation((() => {
const socket = new Duplex({ read() { /* peer data is injected with emit('data') */ } }) as net.Socket;
queueMicrotask(() => {
socket.emit('connect');
queueMicrotask(() => {
for (let offset = 0; offset < frame.length; offset += 1024) {
socket.emit('data', frame.subarray(offset, offset + 1024));
}
});
});
return socket;
}) as typeof net.createConnection);
const client = new QqHookClient(pid);
try {
await expect(client.connect()).resolves.toMatchObject({ pipeName: message, pid });
} finally {
client.close();
createConnection.mockRestore();
}
});
});
describe('QqHookClient — runtime TRACE', () => {
it('records control and receive pipe hello lifecycles under the parent request', async () => {
const pid = 778820;