MCPcopy Create free account
hub / github.com/PerpetualSoftware/pad / decodeJSONBytes

Function decodeJSONBytes

internal/server/server.go:3488–3531  ·  view source on GitHub ↗

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{})

Source from the content-addressed store, hash-verified

3486// the gate WITHOUT reimplementing any of the three, which is what keeps the
3487// gate the single decider.
3488func decodeJSONBytes(raw []byte, v interface{}) error {
3489 // An EMPTY (or whitespace-only) body must keep returning a wrapped
3490 // io.EOF. json.Decoder.Decode answered io.EOF there and at least one
3491 // caller depends on it — handlers_playbooks.go treats
3492 // errors.Is(err, io.EOF) as "no arguments supplied" and runs anyway —
3493 // while json.Unmarshal answers a SyntaxError instead, which that check
3494 // cannot see. Found by TestPlaybookRunAcceptsEmptyBody, which is exactly
3495 // the wiring a helper-level change is blind to.
3496 // Trim only the four bytes JSON itself calls whitespace. bytes.TrimSpace
3497 // uses unicode.IsSpace, which also strips \v, \f, U+00A0 and friends —
3498 // none of which encoding/json accepts. With TrimSpace a body of just
3499 // "\v" looked EMPTY here and returned io.EOF, so an EOF-tolerant caller
3500 // (playbook run, share links) treated a syntactically invalid body as an
3501 // ABSENT one and proceeded. Same Go-versus-spec whitespace divergence
3502 // that bites when a Go trim stands in for another grammar's definition.
3503 if len(bytes.Trim(raw, " \t\r\n")) == 0 {
3504 return fmt.Errorf("invalid JSON: %w", io.EOF)
3505 }
3506 // Refuse a decoded NUL BEFORE unmarshalling, so the value never exists
3507 // in a Go string that a handler could hand to the store. See
3508 // scanRequestBody for why the body needs its own rule and why the check
3509 // cannot be a substring search. BUG-2803. The same scan refuses a
3510 // repeated request member (BUG-2812): the typed decode below would merge
3511 // it or keep the last copy, depending on the destination field's type.
3512 found := scanRequestBody(raw, repeatFoldFor(v))
3513 if found.nul {
3514 return errJSONBodyNUL
3515 }
3516 if found.repeat != "" {
3517 return &models.RepeatedMemberError{Member: found.repeat}
3518 }
3519 // json.Unmarshal rather than a Decoder over the buffer: it is the
3520 // cheaper of the two by ~2x in total allocation (see readBodyForDecode's
3521 // measurement), and it REFUSES trailing non-whitespace after the JSON
3522 // value where Decode silently ignores it. That second difference is a
3523 // deliberate behaviour change in the same direction as this fix —
3524 // malformed input is refused at the door rather than partly consumed —
3525 // and it is the only compatibility change in BUG-2803. Trailing
3526 // whitespace, which real clients do send, is still accepted.
3527 if err := json.Unmarshal(raw, v); err != nil {
3528 return fmt.Errorf("invalid JSON: %w", err)
3529 }
3530 return nil
3531}
3532
3533// errJSONBodyNUL is returned by decodeJSON when a string in the request body
3534// decodes to a value containing a NUL. Every decodeJSON caller already turns

Callers 5

decodeJSONWithLimitFunction · 0.85
decodeAppJSONFunction · 0.85
strictBodyBytesFunction · 0.85
BenchmarkPerf2812Function · 0.85

Calls 2

scanRequestBodyFunction · 0.85
repeatFoldForFunction · 0.85

Tested by 2

BenchmarkPerf2812Function · 0.68