diff --git a/packages/codearts/index.mjs b/packages/codearts/index.mjs index f85697e..73dcdd9 100644 --- a/packages/codearts/index.mjs +++ b/packages/codearts/index.mjs @@ -1361,6 +1361,22 @@ function errorMessageOf(text) { } } +/** + * APIG signs a body of 12 MB at most. A larger one comes back as a 401 + * `APIG.0301` "calc ak sk signature fail:body too large" after ~30s (#1121): + * the request's size, not the sign-in, so it is never renewed or marked. + */ +const MAX_SIGNED_BODY = 12 * 1024 * 1024 + +function isBodyTooLarge(text) { + return /body too large/i.test(String(text ?? "")) +} + +function bodyTooLargeResponse(bytes, code = "") { + const mb = (bytes / (1024 * 1024)).toFixed(1) + return errorResponse(413, code, `CodeArts 网关只接受 12 MB 以内的请求,这次请求有 ${mb} MB(通常是对话里的图片)。请新开会话,或去掉较大的图片后再试`) +} + /** A 401/400 that only means the temporary AK/SK/security-token went stale. */ function isStaleCredential(text) { return /APIG\.0(301|307|602)|security token has expired|verify ak sk signature fail/i.test(String(text ?? "")) @@ -1891,10 +1907,13 @@ async function signedFetch(cred, input, init, context) { // A sign-in known to be beyond renewal never reaches the network again. if (context.signInDead?.()) return signInExpiredResponse() + // A body APIG can't sign is refused here, not after its ~30s wait. + if (body.length > MAX_SIGNED_BODY) return bodyTooLargeResponse(body.length) if (!chat) { const res = await send() if (res.status !== 401 || !context.renewCredential) return withSignInMark(res) const text = await res.text().catch(() => "") + if (isBodyTooLarge(text)) return bodyTooLargeResponse(body.length, errorCodeOf(text)) const next = await context.renewCredential(active) if (!next) return unrenewedResponse(context, text) active = next @@ -1944,6 +1963,7 @@ async function signedFetch(cred, input, init, context) { code = errorCodeOf(text) } + if (isBodyTooLarge(text)) return bodyTooLargeResponse(body.length, code) if (res.status === 401 || isStaleCredential(text)) { if (!renewed && context.renewCredential) { renewed = true diff --git a/packages/codearts/index.test.mjs b/packages/codearts/index.test.mjs index 0a541a6..c3c79ef 100644 --- a/packages/codearts/index.test.mjs +++ b/packages/codearts/index.test.mjs @@ -1701,6 +1701,52 @@ test("a spent refresh token marks the sign-in and stops calling the vendor", asy assert.equal(chats, 1) }) + +test("a body APIG can't sign is a 413 that keeps the sign-in, never a renewal (#1121)", async () => { + const cred = await makeCred() + const tooLarge = '{"error_msg":"Incorrect IAM authentication information: calc ak sk signature fail:body too large","error_code":"APIG.0301","request_id":"r"}' + let chats = 0 + let renewals = 0 + const fetcher = async (url) => { + if (String(url) === x.STS_TOKEN_ENDPOINT) { + renewals += 1 + return new Response("{}", { status: 500 }) + } + chats += 1 + return new Response(tooLarge, { status: 401 }) + } + const plugin = await CodeArtsAuthPlugin({ client: {} }, { fetch: fetcher }) + const loaded = await plugin.auth.loader(async () => makeAuth(cred)) + // under the 12 MB the plugin checks, the gateway's own answer is read + const big = "x".repeat(2 * 1024 * 1024) + for (const send of [ + () => loaded.fetch(CHAT_URL, chatInit({ model: "GLM-5.2", messages: [{ role: "user", content: big }] })), + () => loaded.fetch("https://snap-access.cn-north-4.myhuaweicloud.com/api/v1/embeddings", { method: "POST", body: JSON.stringify({ input: big }) }), + ]) { + const res = await send() + assert.equal(res.status, 413) + assert.equal(res.headers.get("x-magpie-sign-in"), null) + assert.match(await res.text(), /12 MB/) + } + assert.equal(chats, 2) + assert.equal(renewals, 0, "a body too large is not a stale credential") + // the sign-in is still usable: the next request goes out + await loaded.fetch(CHAT_URL, chatInit({ model: "GLM-5.2", messages: [] })) + assert.equal(chats, 3) +}) + +test("a body over 12 MB is refused before it is signed or sent (#1121)", async () => { + const cred = await makeCred() + let calls = 0 + const plugin = await CodeArtsAuthPlugin({ client: {} }, { fetch: async () => { calls += 1; return new Response("{}") } }) + const loaded = await plugin.auth.loader(async () => makeAuth(cred)) + const image = "data:image/png;base64," + "A".repeat(13 * 1024 * 1024) + const res = await loaded.fetch(CHAT_URL, chatInit({ model: "GLM-5.2", messages: [{ role: "user", content: [{ type: "image_url", image_url: { url: image } }] }] })) + assert.equal(res.status, 413) + assert.equal(res.headers.get("x-magpie-sign-in"), null) + assert.match(await res.text(), /13\.0 MB/) + assert.equal(calls, 0) +}) test("a 403 is a sign-in the account no longer has, on chats and other requests alike", async () => { const cred = await makeCred() const denied = '{"error_code":"APIG.0305","error_msg":"access denied"}' diff --git a/packages/codearts/package.json b/packages/codearts/package.json index 0332b25..5abe9cd 100644 --- a/packages/codearts/package.json +++ b/packages/codearts/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-codearts-auth", - "version": "0.3.19", + "version": "0.3.20", "description": "Huawei Cloud in OpenCode and magpie: CodeArts (码道) and OfficeAce (AgentArts), each from its own Huawei Cloud OAuth sign-in — models, credit rates, credit usage and the daily check-in (unofficial)", "type": "module", "main": "./opencode.mjs",