decodeJSONBytes is everything decodeJSONWithLimit does once the body has been read: the empty-body contract, the NUL gate, and the unmarshal. Extracted so the --repair-nul path can insert a repair between the read and the gate WITHOUT reimplementing any of the three, which is what keeps the gate th
(raw []byte, v interface{})
| 3428 | // the gate WITHOUT reimplementing any of the three, which is what keeps the |
| 3429 | // gate the single decider. |
| 3430 | func decodeJSONBytes(raw []byte, v interface{}) error { |
| 3431 | // An EMPTY (or whitespace-only) body must keep returning a wrapped |
| 3432 | // io.EOF. json.Decoder.Decode answered io.EOF there and at least one |
| 3433 | // caller depends on it — handlers_playbooks.go treats |
| 3434 | // errors.Is(err, io.EOF) as "no arguments supplied" and runs anyway — |
| 3435 | // while json.Unmarshal answers a SyntaxError instead, which that check |
| 3436 | // cannot see. Found by TestPlaybookRunAcceptsEmptyBody, which is exactly |
| 3437 | // the wiring a helper-level change is blind to. |
| 3438 | // Trim only the four bytes JSON itself calls whitespace. bytes.TrimSpace |
| 3439 | // uses unicode.IsSpace, which also strips \v, \f, U+00A0 and friends — |
| 3440 | // none of which encoding/json accepts. With TrimSpace a body of just |
| 3441 | // "\v" looked EMPTY here and returned io.EOF, so an EOF-tolerant caller |
| 3442 | // (playbook run, share links) treated a syntactically invalid body as an |
| 3443 | // ABSENT one and proceeded. Same Go-versus-spec whitespace divergence |
| 3444 | // that bites when a Go trim stands in for another grammar's definition. |
| 3445 | if len(bytes.Trim(raw, " \t\r\n")) == 0 { |
| 3446 | return fmt.Errorf("invalid JSON: %w", io.EOF) |
| 3447 | } |
| 3448 | // Refuse a decoded NUL BEFORE unmarshalling, so the value never exists |
| 3449 | // in a Go string that a handler could hand to the store. See |
| 3450 | // scanRequestBody for why the body needs its own rule and why the check |
| 3451 | // cannot be a substring search. BUG-2803. The same scan refuses a |
| 3452 | // repeated request member (BUG-2812): the typed decode below would merge |
| 3453 | // it or keep the last copy, depending on the destination field's type. |
| 3454 | found := scanRequestBody(raw, repeatFoldFor(v)) |
| 3455 | if found.nul { |
| 3456 | return errJSONBodyNUL |
| 3457 | } |
| 3458 | if found.repeat != "" { |
| 3459 | return &models.RepeatedMemberError{Member: found.repeat} |
| 3460 | } |
| 3461 | // json.Unmarshal rather than a Decoder over the buffer: it is the |
| 3462 | // cheaper of the two by ~2x in total allocation (see readBodyForDecode's |
| 3463 | // measurement), and it REFUSES trailing non-whitespace after the JSON |
| 3464 | // value where Decode silently ignores it. That second difference is a |
| 3465 | // deliberate behaviour change in the same direction as this fix — |
| 3466 | // malformed input is refused at the door rather than partly consumed — |
| 3467 | // and it is the only compatibility change in BUG-2803. Trailing |
| 3468 | // whitespace, which real clients do send, is still accepted. |
| 3469 | if err := json.Unmarshal(raw, v); err != nil { |
| 3470 | return fmt.Errorf("invalid JSON: %w", err) |
| 3471 | } |
| 3472 | return nil |
| 3473 | } |
| 3474 | |
| 3475 | // errJSONBodyNUL is returned by decodeJSON when a string in the request body |
| 3476 | // decodes to a value containing a NUL. Every decodeJSON caller already turns |