From b3df633c877a1fda090a4eabfc6e93e829434ff2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=97=B6=E7=91=BE?= <74231782+sj817@users.noreply.github.com> Date: Sat, 16 May 2026 15:07:54 +0800 Subject: [PATCH] fix(bridge): correctness pass on highway, hook, and identity service * highway/utils detectImageFormat: JPEG SOF marker check now excludes 0xC4 (DHT), 0xC8 (JPG), and 0xCC (DAC). The old `(marker & 0xFC) === 0xC0` matched DHT et al. and then read Huffman table bytes as image dimensions, producing junk width/height for many real JPEGs. * loadBinarySource takes an optional maxBytes; defaults to 1 GiB to defend against an authenticated client triggering a multi-GB load into memory. Group/private file uploads pass FILE_UPLOAD_MAX_BYTES (4 GiB) because QQ's file protocol legitimately supports that size. HTTP fetches also pick up a 60s timeout and a Content-Length precheck. * highway-client.tcpConnect detaches its error/timeout listeners after a successful connect so subsequent upload IO errors don't reject an already-resolved promise. readHttpResponseBody uses local listener refs + a central detach(), eliminating MaxListenersExceededWarning during multi-chunk uploads. * hook/qq-hook-client.nextRequestId now wraps inside uint32 explicitly and skips id 0 / in-flight ids. The previous `nextRequestId++ >>> 0` loses precision past Number.MAX_SAFE_INTEGER (postfix increment evaluates before the shift) and could collide live request ids on very long-running processes. * identity-service.rememberUidUin removes stale partner entries before inserting a new (uid, uin) pair. The old code left orphan uidByUin / uinByUid entries after a uid/uin re-bind, so the two maps could disagree. --- .../core/src/bridge/actions/group-file.ts | 10 +++- .../core/src/bridge/highway/highway-client.ts | 56 +++++++++++++++---- packages/core/src/bridge/highway/utils.ts | 43 ++++++++++++-- packages/core/src/bridge/identity-service.ts | 7 +++ packages/core/src/hook/qq-hook-client.ts | 13 ++++- .../core/tests/actions/group-file.test.ts | 1 + 6 files changed, 111 insertions(+), 19 deletions(-) diff --git a/packages/core/src/bridge/actions/group-file.ts b/packages/core/src/bridge/actions/group-file.ts index 969b13ff..06f8c123 100644 --- a/packages/core/src/bridge/actions/group-file.ts +++ b/packages/core/src/bridge/actions/group-file.ts @@ -9,7 +9,7 @@ import type { Bridge } from '../bridge'; import { protoEncode } from '../../protobuf/decode'; import { runOidb } from '../bridge-oidb'; import { fetchHighwaySession, uploadHighwayHttp } from '../highway/highway-client'; -import { computeHashes, computeMd5, loadBinarySource } from '../highway/utils'; +import { computeHashes, computeMd5, FILE_UPLOAD_MAX_BYTES, loadBinarySource } from '../highway/utils'; import { OidbGroupFileCountViewReqSchema, OidbGroupFileCountViewRespSchema, @@ -288,7 +288,9 @@ export async function uploadGroupFile( folderId = '/', uploadFile = true, ): Promise { - const loaded = await loadBinarySource(source, 'file'); + // Group/private files may legitimately be up to 4 GiB on QQ's wire, + // so override the default 1 GiB cap with the protocol ceiling. + const loaded = await loadBinarySource(source, 'file', FILE_UPLOAD_MAX_BYTES); if (!loaded.bytes.length) throw new Error('group file is empty'); const fileName = normalizeUploadFileName(name, loaded.fileName); @@ -366,7 +368,9 @@ export async function uploadPrivateFile( name = '', uploadFile = true, ): Promise { - const loaded = await loadBinarySource(source, 'file'); + // Group/private files may legitimately be up to 4 GiB on QQ's wire, + // so override the default 1 GiB cap with the protocol ceiling. + const loaded = await loadBinarySource(source, 'file', FILE_UPLOAD_MAX_BYTES); if (!loaded.bytes.length) throw new Error('private file is empty'); const targetUid = await bridge.resolveUserUid(userId); diff --git a/packages/core/src/bridge/highway/highway-client.ts b/packages/core/src/bridge/highway/highway-client.ts index dcffbd74..99d3c385 100644 --- a/packages/core/src/bridge/highway/highway-client.ts +++ b/packages/core/src/bridge/highway/highway-client.ts @@ -129,10 +129,31 @@ export function buildHighwayExtend( function tcpConnect(host: string, port: number, timeoutMs = 10000): Promise { return new Promise((resolve, reject) => { - const socket = net.createConnection({ host, port }, () => resolve(socket)); + let settled = false; + const onTimeout = () => { + if (settled) return; + settled = true; + socket.destroy(); + reject(new Error('TCP connect timeout')); + }; + const onError = (err: Error) => { + if (settled) return; + settled = true; + reject(err); + }; + const socket = net.createConnection({ host, port }, () => { + if (settled) return; + settled = true; + // Drop the connect-only listeners so subsequent IO errors / idle + // timeouts don't try to reject this already-resolved promise. + socket.setTimeout(0); + socket.removeListener('timeout', onTimeout); + socket.removeListener('error', onError); + resolve(socket); + }); socket.setTimeout(timeoutMs); - socket.on('timeout', () => { socket.destroy(); reject(new Error('TCP connect timeout')); }); - socket.on('error', reject); + socket.once('timeout', onTimeout); + socket.once('error', onError); }); } @@ -148,8 +169,21 @@ function readHttpResponseBody(socket: net.Socket): Promise { let headerEnd = -1; let contentLength = 0; let totalNeeded = 0; + let settled = false; - const checkComplete = () => { + const detach = () => { + socket.off('data', onData); + socket.off('error', onError); + socket.off('close', onClose); + }; + const finish = (fn: () => void) => { + if (settled) return; + settled = true; + detach(); + fn(); + }; + const onData = (chunk: Buffer) => { + chunks.push(chunk); const buf = Buffer.concat(chunks); if (headerEnd < 0) { const idx = buf.indexOf('\r\n\r\n'); @@ -162,19 +196,19 @@ function readHttpResponseBody(socket: net.Socket): Promise { } } if (headerEnd >= 0 && buf.length >= totalNeeded) { - socket.removeAllListeners('data'); - socket.removeAllListeners('error'); - resolve(new Uint8Array(buf.subarray(headerEnd, totalNeeded))); + finish(() => resolve(new Uint8Array(buf.subarray(headerEnd, totalNeeded)))); } }; - - socket.on('data', (chunk) => { chunks.push(chunk); checkComplete(); }); - socket.on('error', reject); - socket.on('close', () => { + const onError = (err: Error) => finish(() => reject(err)); + const onClose = () => finish(() => { const buf = Buffer.concat(chunks); if (headerEnd >= 0) resolve(new Uint8Array(buf.subarray(headerEnd))); else reject(new Error('connection closed before response')); }); + + socket.on('data', onData); + socket.on('error', onError); + socket.on('close', onClose); }); } diff --git a/packages/core/src/bridge/highway/utils.ts b/packages/core/src/bridge/highway/utils.ts index a39ceabf..1ea3e950 100644 --- a/packages/core/src/bridge/highway/utils.ts +++ b/packages/core/src/bridge/highway/utils.ts @@ -11,6 +11,17 @@ export interface LoadedBinary { fileName: string; } +/** + * Default cap on a single binary load. Image/voice/video paths use this + * to defend against an authenticated OneBot client triggering a multi-GB + * read into memory. Group/private file uploads override this cap because + * QQ's file protocol legitimately supports up to 4 GiB. + */ +const DEFAULT_MAX_BINARY_SIZE = 1024 * 1024 * 1024; // 1 GiB +/** Hard ceiling QQ's file protocol supports — used by group/private files. */ +export const FILE_UPLOAD_MAX_BYTES = 4 * 1024 * 1024 * 1024; // 4 GiB +const FETCH_TIMEOUT_MS = 60_000; + export interface ImageHashes { md5: Uint8Array; sha1: Uint8Array; @@ -54,17 +65,32 @@ export function resolveLocalFilePath(source: string): string | null { return filePath; } -export async function loadBinarySource(source: string, resourceName: string): Promise { +export async function loadBinarySource( + source: string, + resourceName: string, + maxBytes: number = DEFAULT_MAX_BINARY_SIZE, +): Promise { if (!source) throw new Error(`${resourceName} source is empty`); if (/^base64:\/\//i.test(source)) { - return { bytes: Buffer.from(source.slice(9), 'base64'), fileName: '' }; + const bytes = Buffer.from(source.slice(9), 'base64'); + if (bytes.length > maxBytes) { + throw new Error(`${resourceName} too large: ${bytes.length} > ${maxBytes}`); + } + return { bytes, fileName: '' }; } if (/^https?:\/\//i.test(source)) { - const resp = await fetch(source); + const resp = await fetch(source, { signal: AbortSignal.timeout(FETCH_TIMEOUT_MS) }); if (!resp.ok) throw new Error(`HTTP download failed: ${resp.status}`); + const declared = Number(resp.headers.get('content-length') ?? '0'); + if (Number.isFinite(declared) && declared > maxBytes) { + throw new Error(`${resourceName} too large: ${declared} > ${maxBytes}`); + } const bytes = new Uint8Array(await resp.arrayBuffer()); + if (bytes.length > maxBytes) { + throw new Error(`${resourceName} too large: ${bytes.length} > ${maxBytes}`); + } const fileName = guessFileNameFromUrl(source); return { bytes, fileName }; } @@ -72,6 +98,10 @@ export async function loadBinarySource(source: string, resourceName: string): Pr const filePath = resolveLocalFilePath(source); if (!filePath) throw new Error(`${resourceName} source is not a local file`); + const stat = fs.statSync(filePath); + if (stat.size > maxBytes) { + throw new Error(`${resourceName} too large: ${stat.size} > ${maxBytes}`); + } const bytes = fs.readFileSync(filePath); const fileName = path.basename(filePath); return { bytes, fileName }; @@ -183,7 +213,12 @@ export function detectImageFormat(bytes: Uint8Array): ImageFormat { if (offset + 4 > bytes.length) break; const segLen = readBE16(bytes, offset + 2); if (segLen < 2 || offset + 2 + segLen > bytes.length) break; - if ((marker & 0xFC) === 0xC0 && offset + 9 <= bytes.length) { + // Real SOF markers: 0xC0..0xCF EXCEPT 0xC4 (DHT), 0xC8 (JPG), 0xCC + // (DAC). The previous `(marker & 0xFC) === 0xC0` check accepted DHT + // and friends, then misread Huffman table bytes as image dimensions. + const isSof = marker >= 0xC0 && marker <= 0xCF + && marker !== 0xC4 && marker !== 0xC8 && marker !== 0xCC; + if (isSof && segLen >= 7 && offset + 9 <= bytes.length) { height = readBE16(bytes, offset + 5); width = readBE16(bytes, offset + 7); return { format: 1000, width, height }; diff --git a/packages/core/src/bridge/identity-service.ts b/packages/core/src/bridge/identity-service.ts index 3b3dfbe2..c6918284 100644 --- a/packages/core/src/bridge/identity-service.ts +++ b/packages/core/src/bridge/identity-service.ts @@ -440,6 +440,13 @@ export class IdentityService { const normalizedUid = normalizeUid(uid); const normalizedUin = normalizeUin(uin); if (!normalizedUid || normalizedUin === null) return; + // Drop any stale mapping for the old partner first; otherwise the two + // maps disagree (`findUinByUid(oldUid) -> uin` while + // `findUidByUin(uin) -> newUid`) after a uid/uin re-binding. + const oldUid = this.uidByUin.get(normalizedUin); + if (oldUid && oldUid !== normalizedUid) this.uinByUid.delete(oldUid); + const oldUin = this.uinByUid.get(normalizedUid); + if (oldUin !== undefined && oldUin !== normalizedUin) this.uidByUin.delete(oldUin); this.uinByUid.set(normalizedUid, normalizedUin); this.uidByUin.set(normalizedUin, normalizedUid); } diff --git a/packages/core/src/hook/qq-hook-client.ts b/packages/core/src/hook/qq-hook-client.ts index 9844a750..4b195f39 100644 --- a/packages/core/src/hook/qq-hook-client.ts +++ b/packages/core/src/hook/qq-hook-client.ts @@ -444,7 +444,18 @@ export class QqHookClient extends EventEmitter { }: QqHookSendOptions = {}): Promise { await this.connect(); - const requestId = this.nextRequestId++ >>> 0; + // Wrap inside uint32 explicitly. `nextRequestId++ >>> 0` would + // misbehave once the integer exceeds Number.MAX_SAFE_INTEGER (the + // postfix increment loses precision before the shift), letting two + // distinct requests collide on the same id. Skip 0 because zero is + // used as a sentinel by the wire protocol. + let requestId = this.nextRequestId; + while (this.pendingAcks.has(requestId) || this.pendingReplies.has(requestId)) { + requestId = (requestId + 1) >>> 0; + if (requestId === 0) requestId = 1; + } + this.nextRequestId = (requestId + 1) >>> 0; + if (this.nextRequestId === 0) this.nextRequestId = 1; const payload = encodeFrame({ op: PipeOp.sendRequest, requestId, diff --git a/packages/core/tests/actions/group-file.test.ts b/packages/core/tests/actions/group-file.test.ts index a6ce62cf..2f71bc76 100644 --- a/packages/core/tests/actions/group-file.test.ts +++ b/packages/core/tests/actions/group-file.test.ts @@ -16,6 +16,7 @@ vi.mock('../../src/bridge/highway/utils', () => ({ })), computeHashes: vi.fn(() => ({ md5: new Uint8Array(16), sha1: new Uint8Array(20) })), computeMd5: vi.fn(() => new Uint8Array(16)), + FILE_UPLOAD_MAX_BYTES: 4 * 1024 * 1024 * 1024, })); import * as oidb from '../../src/bridge/bridge-oidb';