x402-pos-tag 的 labels 輸入在 GET 與 POST 下產生不同回答
The array that changed the answer instead of the status code
作者發現,x402-pos-tag 的 GET 查詢字串與 POST JSON 對 labels 的處理不同,令模型收到不同提示詞,兩者卻都返回 200 並完成收費。同一寫法出現在14個處理器,當中13個在即時 402 中把 labels 標示為字串形式;作者為14個處理器改用共享 asText(),並以兩種入口逐字比對提示詞驗證修正。
This content reflects minia2a as of its publication date and is kept for the record. Current model: x402 pay-per-call in USDC on Base only, 5 free trial calls per signed wallet, no credits and no top-up rail.
The array that changed the answer instead of the status code
2026-11-23 · thirteen endpoints, two doors, one value that arrived as two different things
The last two posts in this series were about 400s and wrong answers at the transport layer. This one is quieter: nothing fails, nothing warns, the status is 200, the envelope says ok:true, and the buyer is charged. The answer is simply different depending on which door the request came through.
Here is the whole mechanism, in two lines of JavaScript that nobody writes deliberately:
String(['a']) === 'a' // array of one string, coerced
// the same value after a round trip through a query string:
const labels = '["a"]'; // five characters, a string
Our 402 advertised labels: ["a"]. HTTP has no array type, so the query-string door carries the JSON text. A handler that writes ${labels}\n\n${text} is doing exactly what its author intended and is still wrong, because the two doors hand it two different values: a real array on one, a five-character string on the other. There is no exception to catch. The prompt went out, the model answered, the buyer paid.
Measured, on one paid endpoint, twice per door
I took one fresh trial wallet and called x402-pos-tag four times: twice through the documented GET form, twice with a JSON body. Both doors were deterministic. They did not agree.
| Door | Value the handler received | Prompt sent to the model | Tokens returned |
|---|---|---|---|
| GET (the advertised form) | '["a"]' |
["a"]\n\nhello world example |
hello, world, example — no a
|
| POST (JSON body) | ['a'] |
a\n\nhello world example |
a, hello, world, example — with a tagged as a determiner |
The brackets leaked into the model's input. The label the caller asked to apply was not applied on the documented door, and was applied on the other one. Same service, same advertised example, same price, two answers.
This was not one endpoint. After finding it I grepped the deployed handlers for the exact line — input?.labels || input?.label || '' concatenated into a prompt — and found it in fourteen. Thirteen of those fourteen currently advertise labels as the five-character string ["a"] in their live 402. Every one of them advertised the door that produced the worse prompt.
The tell in a 402 is not the word "array". It is a JSON array printed inside a string —
"labels": "[\"a\"]". That is the shape a GET door can carry. Any seller whose schema says "array" and whose example says "string containing a JSON array" has two products, whether or not they wrote two.
Why the existing tests were green
Every one of these handlers had a test. Each test called the exported handler with a real array and asserted the prompt. That test is honest, it passes, and it is a test of the POST door. The GET door was never in the test at all, because the conversion between "JSON in a body" and "JSON in a query string" does not live in the handler — it lives in the URL parser, one layer above the code being tested.
Our in-process acceptance census had the same blind spot for the same reason: it pushes examples through handlers in-process, so it never stringifies anything, so the string branch never runs. A guard can be at 100% and still be measuring one door.
The fix: one coercion point, not fourteen patches
We already had asArray() and asMap() in a shared handler library for the two earlier posts in this series. The label case needed the sibling that turns any of these shapes into the text a prompt can carry:
// compat/lib/input-coerce.js
function asText(v) {
if (Array.isArray(v)) return v.join(',');
if (v === null || v === undefined) return '';
const s = String(v);
const t = s.trim();
if (t.startsWith('[')) {
try { const p = JSON.parse(t); return Array.isArray(p) ? p.join(',') : s; }
catch (e) { return s; }
}
return s;
}
Three cases matter. A real array joins. A string that parses as an array is joined the same way, so both doors produce a. Anything else — including a string that starts with [ but is not JSON — passes through unchanged, so a caller's genuine input is never swallowed by a parse that was only a guess.
All fourteen handlers now read const label = asText(input?.labels || input?.label || '');. The POST door's output is unchanged: asText(['a']) is a, which is exactly what String(['a']) already produced. The GET door now agrees with it.
Verification, in three layers
-
In-process: a stub
fetchcaptures the prompt the handler builds. Both doors now assert the prompt byte-for-byte:a\n\nhello world example. -
Live: the same parity probe that found the bug now reports
SAMEforx402-pos-tag, twice per door, on a fresh wallet. -
Regression guards: the recorded-fix presence guard still finds all 658 recorded fixes in the deployed files, the handler-freshness guard confirms the running sidecar loaded the new bytes, and the undeclared-lib-reference guard confirms every handler that calls
asTextalso requires it.
The pattern across all three posts is the same, and it is worth stating plainly: our advertised example is a promise about a value, and a value has no type over HTTP. Whatever a seller prints in a 402, a buyer will send it through the door the 402 itself documents. If the handler treats that door's string as a different type than the body door's JSON, the seller has shipped two products at one price.
What a seller should take from this
- Any field you concatenate into a prompt, a URL, a CSV, or any other string context needs an explicit coercion function — not a default and not implicit
toString. - Test the door your 402 documents. If your example shows a query string, your unit test should call the handler with the string the query parser produces, not with the object you wish it produced.
- Prefer a loud failure to a silent difference — but know that this class cannot fail loudly by itself. Nothing threw because nothing was illegal. The only detector is comparing the two doors.
The probe that found this is the same one that will keep finding it: for every service with an advertised example, call it on both doors with one fresh wallet and diff the answers. It is a single command in the same repository as the other guards. The outbound population has now been swept — every id either judged on both doors or recorded as unreachable by a trial — and the in-process census covers the whole advertised-example corpus, skipping the entries whose upstream call cannot be reached from our own box.
Originally published at minia2a.uk.
來源:DEV Community · dev.to