From 90d3f7c0d6fc829e1e38c8460b1f583b683c82a2 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Thu, 18 Jun 2026 00:40:26 +0800 Subject: [PATCH] test(protocol): real byte-oracle for ocr_image request wire MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adversarial Wave 3 pass: the prior 'round-trip locks pb tags' test was a fake lock — symmetric encode/decode via the same proto would not catch a tag swap. Replace with a fixed hex byte-oracle (hand-decoded in the comment) that goes red on any tag change, plus an empty-ocr-body deserialize test. Note in the action that a passed-in URL is used verbatim (not rkey re-signed). --- packages/onebot/src/actions/extended.ts | 3 +++ .../oidb-services/misc/image-ocr.test.ts | 20 +++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/packages/onebot/src/actions/extended.ts b/packages/onebot/src/actions/extended.ts index c47ee7a4..0214e2cc 100644 --- a/packages/onebot/src/actions/extended.ts +++ b/packages/onebot/src/actions/extended.ts @@ -1258,6 +1258,9 @@ export const actions = [ readOnly: true, params: { image: f.string({ allowEmpty: false }) }, run: async (p, ctx) => { + // A passed-in http(s) URL is used verbatim (NOT re-signed) — if it is a + // stale CDN URL with an expired rkey the server fetch fails and surfaces + // the server's retCode. The file_id path below re-signs via getImageInfo. let url = /^https?:\/\//i.test(p.image) ? p.image : ''; if (!url) { const info = await ctx.getImageInfo(p.image); diff --git a/packages/protocol/tests/oidb-services/misc/image-ocr.test.ts b/packages/protocol/tests/oidb-services/misc/image-ocr.test.ts index 27dc0747..acdf61e4 100644 --- a/packages/protocol/tests/oidb-services/misc/image-ocr.test.ts +++ b/packages/protocol/tests/oidb-services/misc/image-ocr.test.ts @@ -43,21 +43,29 @@ describe('ImageOcr namespace', () => { expect(out.ocrReqBody).toMatchObject({ imageUrl: 'https://x/a.jpg', isCut: false }); }); - it('routes to OidbSvcTrpcTcp.0xe07_0 and round-trips the request body (locks pb tags)', async () => { + it('routes to OidbSvcTrpcTcp.0xe07_0 and encodes the exact wire bytes (fixed byte-oracle locks pb tags)', async () => { const sender = makeSender(); await ImageOcr.invoke(sender, { imageUrl: 'https://x/a.jpg' }); const [cmd, bytes] = sender.sendRawPacket.mock.calls[0]!; expect(cmd).toBe('OidbSvcTrpcTcp.0xe07_0'); + // Independent byte-oracle: a fixed hex constant, NOT re-derived from the + // proto, so a tag change actually goes red (a symmetric encode→decode + // round-trip would not). Hand-decoded: 08 871c = field1 command varint + // 0xE07; 22 17 = field4 body (23B); body: 08 01 version=1, 18 01 + // entrance=1 (client=0 omitted), 52 11 = field10 ocrReqBody (17B): + // 0a 0f = field1 imageUrl + "https://x/a.jpg". + expect(Buffer.from(bytes).toString('hex')) + .toBe('08871c22170801180152110a0f68747470733a2f2f782f612e6a7067'); + // And the structure decodes back as expected. const env = protobuf_decode>(bytes); expect(env.command).toBe(0xE07); - // subCommand 0 is a protobuf default → omitted on the wire (decodes to - // null); the '0xe07_0' cmd string above already pins it. version/entrance - // are non-zero so survive; client=0 is likewise omitted (serialize test - // covers the pre-encode shape). - expect(env.body).toMatchObject({ version: 1, entrance: 1 }); expect(env.body.ocrReqBody?.imageUrl).toBe('https://x/a.jpg'); }); + it('deserializes an empty ocr body to {texts:[], language:""}', () => { + expect(ImageOcr.deserialize({} as any, { retCode: 0 })).toEqual({ texts: [], language: '' }); + }); + it('deserializes text detections + coordinates + language', async () => { const result = await ImageOcr.invoke(makeSender(), { imageUrl: 'https://x/a.jpg' }); expect(result.language).toBe('zh');