From 86314539401ae2199f2f8273cb24ce64400c9093 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Sat, 23 May 2026 18:40:01 +0800 Subject: [PATCH] fix(events): self-heal group_invite cache pollution from pre-fix builds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit User report: after [a4909ec], log STILL shows 群邀请 950929451 -> [bot group(950929451)] get_stranger_info params=user_id=950929451 → 未知昵称 But NapCat and a sibling SnowLuma bot in the same group BOTH decoded the same push as `QQ:1957003260 nickname:KitaIkuyo`. So the wire is correct — our bot is just looking it up wrong. Root cause: **legacy cache pollution**. The pre-fix decoder fallback stored ` → ` in the identity DB whenever `resolveUidToUin` missed (because the fallback `ctx.fromUin` on a group-scoped push IS the group's own uin). After upgrading to [a4909ec], `decodeGroupJoinRequest` calls `resolveUidToUin` which hits the polluted in-memory map (re-populated from SQLite at boot) and returns the groupUin instead of 0. The new `needsStrangerResolve` guard only fired on `fromUin <= 0` — so the polluted value flowed straight through and re-emitted the bug. Two-part heal: 1. `needsStrangerResolve` now ALSO fires when `fromUin === groupId` — the pollution signature. A real requester's uin can never equal the group's own uin, so this is a safe override that forces the async UID-form FetchUserProfile lookup, which then `rememberUidUin`s the correct mapping on success (eviction is already wired in identity-service.ts:468-471). 2. `rememberEventIdentity` for group_invite now passes `uin: 0` when `event.fromUin === event.groupId`, so future writes don't re-pollute the cache (`rememberUidUin` short-circuits on normalized uin ≤ 0). Cache self-heals on first event after upgrade — no manual DB nuke needed. Regression test pins the legacy-pollution path: feeds an event where `fromUin === groupId` (matching the user's log), verifies the resolver fires and patches fromUin to the correct value. --- packages/bridge/src/packet-pipeline.ts | 31 +++++++++++++--- .../packet-pipeline-stranger-resolve.test.ts | 35 +++++++++++++++++++ 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/packages/bridge/src/packet-pipeline.ts b/packages/bridge/src/packet-pipeline.ts index a4fbf2b2..d7e366b4 100644 --- a/packages/bridge/src/packet-pipeline.ts +++ b/packages/bridge/src/packet-pipeline.ts @@ -115,9 +115,24 @@ export class IncomingPacketPipeline { * fire a UID-form FetchUserProfile to resolve UIN + nickname before * dispatch so the OneBot layer's `user_id` field is populated and * the consumer bot's follow-up `get_stranger_info` lookup succeeds. - * Mirrors Lagrange's `dev/Lagrange.Core/.../MessagingLogic.cs:215-224`. */ + * Mirrors Lagrange's `dev/Lagrange.Core/.../MessagingLogic.cs:215-224`. + * + * Also catches the legacy cache-pollution case: pre-fix builds + * stored ` → ` in the identity DB when + * the decoder's fallback was `ctx.fromUin` (= group's own uin on + * a group-scoped push). After upgrade, the decoder's + * `resolveUidToUin` hits that polluted mapping and returns the + * groupUin instead of 0, so the `fromUin <= 0` guard alone would + * silently skip the resolve and re-emit the bug. Treating + * `fromUin === groupId` as bogus forces the async resolve to + * overwrite the polluted entry on the next event. */ private needsStrangerResolve(event: QQEventVariant): event is Extract { - return event.kind === 'group_invite' && event.fromUin <= 0 && Boolean(event.fromUid); + if (event.kind !== 'group_invite' || !event.fromUid) return false; + if (event.fromUin <= 0) return true; + // Pollution signature — a real requester's uin would never equal + // the group's own uin. Force a re-resolve so the cache self-heals. + if (event.groupId > 0 && event.fromUin === event.groupId) return true; + return false; } private async dispatchAfterIdentityRefresh(event: Extract): Promise { @@ -265,14 +280,22 @@ export class IncomingPacketPipeline { source: 'friend_request', }); break; - case 'group_invite': + case 'group_invite': { + // Defensive: never cache a uid→uin mapping where uin equals + // the group's own uin — that's the pollution signature the + // legacy decoder fallback produced. Pass 0 so `rememberUidUin` + // skips the map write (it short-circuits on uin <= 0) but the + // user row still gets upserted with the uid + group context. + const uinForCache = event.fromUin > 0 && event.fromUin !== event.groupId + ? event.fromUin : 0; this.deps.identity.rememberRequestIdentity({ groupId: event.groupId, uid: event.fromUid, - uin: event.fromUin, + uin: uinForCache, source: 'group_request', }); break; + } default: break; } diff --git a/packages/bridge/tests/packet-pipeline-stranger-resolve.test.ts b/packages/bridge/tests/packet-pipeline-stranger-resolve.test.ts index a9bece69..d70bda67 100644 --- a/packages/bridge/tests/packet-pipeline-stranger-resolve.test.ts +++ b/packages/bridge/tests/packet-pipeline-stranger-resolve.test.ts @@ -132,6 +132,41 @@ describe('IncomingPacketPipeline / stranger resolve on group_invite', () => { expect(captured[0]).toMatchObject({ fromUin: 99999, fromUid: 'u_known' }); }); + it('forces re-resolve when fromUin === groupId (legacy cache pollution)', async () => { + // Pre-fix builds stored ` → ` because + // the decoder's fallback was `ctx.fromUin` (= group's own uin on + // a group-scoped push). After upgrade, the decoder's + // resolveUidToUin would re-read that polluted mapping and emit + // event.fromUin === groupId. The `fromUin <= 0` check alone + // wouldn't fire the async resolve, so the bug would persist. + // This guard catches that signature and forces a re-lookup, so + // the cache self-heals on the next event. + const resolveStrangerProfile = vi.fn(async () => ({ + uin: 1957003260, nickname: 'KitaIkuyo', + })); + const { pipeline, captured } = makePipeline({ resolveStrangerProfile }); + + pipeline.registerCmd('test.cmd', () => [{ + kind: 'group_invite', + time: 1, selfUin: 10001, + groupId: 950929451, + fromUin: 950929451, // ← POLLUTED: equals groupId + fromUid: 'u_kitaikuyo', + subType: 'add', message: '', flag: 'add:950929451:u_kitaikuyo', + } as QQEventVariant]); + + pipeline.process({ serviceCmd: 'test.cmd' } as PacketInfo); + await new Promise(r => setTimeout(r, 10)); + + expect(resolveStrangerProfile).toHaveBeenCalledWith('u_kitaikuyo'); + expect(captured).toHaveLength(1); + expect(captured[0]).toMatchObject({ + fromUin: 1957003260, // ← corrected by the async resolve + fromUid: 'u_kitaikuyo', + groupId: 950929451, + }); + }); + it('skips the stranger resolve when fromUid is empty (no uid to look up by)', async () => { const resolveStrangerProfile = vi.fn(async () => null); const { pipeline, captured } = makePipeline({ resolveStrangerProfile });