codearts 0.3.20: a body too large to sign is a 413, not an expired sign-in (#1121)

APIG signs a request body of 12 MB at most. A larger one (apple-small's
long chat with 21 images, ~20 MB) came back after ~30s as 401 APIG.0301
"calc ak sk signature fail:body too large". The plugin read every such 401
as a stale credential: it renewed, then answered 401 with
X-Magpie-Sign-In: expired, so magpie showed the account signed out and
signing in again changed nothing.

Now that answer, on chats and other requests, is a 413 saying the size and
what to do (new chat, or drop the large images), with no sign-in mark and
no renewal. A body over 12 MiB is refused the same way before it is signed
or sent, so it doesn't wait the gateway's ~30s first.

Not changed: images are not dropped or rewritten; that would change what
the user sent. OfficeAce sends Basic auth, no AK/SK body signature, so it
is left as is.

Run: wsl-run bun test in packages/codearts, 213 pass. Broken by hand:
isBodyTooLarge always false -> the 413 test red; no pre-send size check ->
the 12 MB test red. scripts/check.mjs codearts ok.
Not tried: a real mainland account with a 20 MB chat; the gateway's answer
is the reporter's log, used byte for byte in the test.
This commit is contained in:
yetone
2026-11-14 11:49:01 +08:00
parent 001a1f9fbf
commit 9bca17b5d2
3 changed files with 67 additions and 1 deletions
+20
View File
@@ -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
+46
View File
@@ -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"}'
+1 -1
View File
@@ -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",