{"record":{"id":"3df6dc2f7a27b68c","repo":"JuliusBrussee/caveman","slug":"proposalrun-detail-contains-trailing-json","errorCode":null,"errorMessage":"proposalrun: detail contains trailing JSON","messagePattern":"proposalrun: detail contains trailing JSON","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shared/platform/proposalrun/proposalrun.go","lineNumber":69,"sourceCode":"// hash identically, while adjacent arbitrary-precision integers remain\n// distinct. The bounds mirror PostgreSQL's numeric-backed jsonb limits and\n// reject values that could not be persisted there before allocating output.\n// The decoder enforces the JSON number grammar (including rejecting NaN/Inf),\n// and a second decode below rejects any trailing value or non-whitespace data.\nfunc CanonicalDetail(detail json.RawMessage) ([]byte, error) {\n\tif len(detail) == 0 {\n\t\treturn []byte(\"{}\"), nil\n\t}\n\tvar v any\n\tdec := json.NewDecoder(bytes.NewReader(detail))\n\tdec.UseNumber()\n\tif err := dec.Decode(&v); err != nil {\n\t\treturn nil, fmt.Errorf(\"proposalrun: detail is not valid JSON: %w\", err)\n\t}\n\tvar trailing any\n\tif err := dec.Decode(&trailing); err != io.EOF {\n\t\tif err == nil {\n\t\t\treturn nil, fmt.Errorf(\"proposalrun: detail contains trailing JSON\")\n\t\t}\n\t\treturn nil, fmt.Errorf(\"proposalrun: detail contains trailing data: %w\", err)\n\t}\n\tv, err := canonicalizeJSONValue(v)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"proposalrun: canonicalize detail: %w\", err)\n\t}\n\tout, err := json.Marshal(v)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"proposalrun: marshal canonical detail: %w\", err)\n\t}\n\treturn out, nil\n}\n\n// PostgreSQL jsonb stores JSON numbers using its numeric type. These are the\n// documented numeric bounds (131072 digits before and 16383 after the decimal\n// point); rejecting at the canonicalization boundary keeps write and read-back\n// hashing deterministic and avoids unbounded strings from hostile exponents.","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/proposalrun/proposalrun.go#L51-L87","documentation":"CanonicalDetail requires the detail to contain exactly one JSON value. After decoding the first value it decodes again expecting io.EOF; if a second value decodes successfully it throws this error. Trailing JSON (e.g. two concatenated objects) would make canonicalization and hashing ambiguous, so it is rejected.","triggerScenarios":"Calling CanonicalDetail/RowHash with detail bytes like '{\"a\":1}{\"b\":2}' or '[1][2]' — a valid first JSON value followed by another complete JSON value.","commonSituations":"Two JSON payloads concatenated by a buggy writer or stream joiner; log-style NDJSON accidentally stored in a single detail field; a producer appending a second record to an existing detail.","solutions":["Inspect the detail bytes and split/reject records that contain more than one JSON value","Fix the producer to write exactly one JSON document per detail field","If NDJSON is intended, parse per-line and process each record separately rather than passing the whole blob","Pre-validate with a single-value JSON parser before hashing"],"exampleFix":"// before\ndetail := []byte(`{\"run\":1}{\"extra\":2}`) // CanonicalDetail -> trailing JSON\n// after\ndetail := []byte(`{\"run\":1,\"extra\":2}`) // merge into a single JSON object","handlingStrategy":"validation","validationCode":"dec := json.NewDecoder(bytes.NewReader(detail))\nif err := dec.Decode(&struct{}{}); err != nil { return err }\nif dec.More() { return errors.New(\"detail holds more than one JSON value\") }","typeGuard":null,"tryCatchPattern":"if _, err := CanonicalDetail(detail); err != nil {\n    if strings.Contains(err.Error(), \"trailing JSON\") {\n        // split concatenated documents and process each individually\n    }\n    return err\n}","preventionTips":["Write exactly one JSON document per detail field; use NDJSON endpoints only for line-oriented logs","Never append to an existing JSON payload; rewrite the whole field","Round-trip detail through marshal on write so the stored form is a single canonical document"],"tags":["json","hashing","data-integrity","canonicalization"],"backgroundTag":"json-parse-error","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}