{"record":{"id":"3c669d6a6671bd65","repo":"JuliusBrussee/caveman","slug":"proposalrun-detail-contains-trailing-data-w","errorCode":null,"errorMessage":"proposalrun: detail contains trailing data: %w","messagePattern":"proposalrun: detail contains trailing data: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shared/platform/proposalrun/proposalrun.go","lineNumber":71,"sourceCode":"// 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.\n// maxJSONBNumberExponent is an additional allocation guard. PostgreSQL can\n// accept some zero literals with a larger positive exponent after normalizing","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/proposalrun/proposalrun.go#L53-L89","documentation":"After successfully decoding one JSON value, CanonicalDetail performs a second Decode to confirm nothing follows; if that second Decode returns a non-EOF error (rather than a second value), the trailing data was unparseable garbage and this wrapped error is returned. Like the trailing-JSON error, it guards the determinism required for RowHash.","triggerScenarios":"Calling CanonicalDetail/RowHash with a valid JSON value followed by malformed bytes, e.g. '{\"a\":1}garbage' or a truncated second fragment.","commonSituations":"A partially-written append corrupted the payload after the first JSON value; byte-level truncation during transfer/storage; binary data concatenated onto a JSON document.","solutions":["Locate and remove the trailing garbage after the first JSON value in the detail payload","Fix the writer responsible for the truncated/corrupt append","Check storage/transfer paths (column size limits, streaming truncation) that may cut payloads mid-write","Validate payloads with json.Decoder EOF checks before hashing"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"var v json.RawMessage\nif err := json.Unmarshal(detail, &v); err != nil { return err }\nif len(bytes.TrimSpace(detail)) != len(bytes.TrimSpace(v)) { return errors.New(\"detail has trailing data\") }","typeGuard":null,"tryCatchPattern":"if _, err := CanonicalDetail(detail); err != nil {\n    if strings.Contains(err.Error(), \"trailing data\") {\n        // payload corrupted after first JSON value: recover from source system or re-derive\n    }\n    return err\n}","preventionTips":["Write detail payloads atomically (single UPDATE) rather than appending bytes","Check for truncation on transfer/storage paths (column limits, streaming cut-offs)","Validate stored details periodically with a decoder EOF check"],"tags":["json","hashing","data-integrity","truncated-data"],"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"}