{"record":{"id":"9e331922f1272a29","repo":"crowdsecurity/crowdsec","slug":"while-encoding-request-w","errorCode":null,"errorMessage":"while encoding request: %w","messagePattern":"while encoding request: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/appsec/request.go","lineNumber":288,"sourceCode":"\tfd, err := os.CreateTemp(\"\", \"crowdsec_req_dump_*.json\")\n\tif err != nil {\n\t\treturn fmt.Errorf(\"while creating temp file: %w\", err)\n\t}\n\tdefer fd.Close()\n\tenc := json.NewEncoder(fd)\n\tenc.SetIndent(\"\", \"  \")\n\n\treq := r.GetFilteredRequest()\n\n\tlog.Tracef(\"dumping : %+v\", req)\n\n\tif err := enc.Encode(req); err != nil {\n\t\t//Don't clobber the temp directory with empty files\n\t\terr2 := os.Remove(fd.Name())\n\t\tif err2 != nil {\n\t\t\tlog.Errorf(\"while removing temp file %s: %s\", fd.Name(), err)\n\t\t}\n\t\treturn fmt.Errorf(\"while encoding request: %w\", err)\n\t}\n\tlog.Infof(\"request dumped to %s\", fd.Name())\n\treturn nil\n}\n\n// forwardedHeaders are the X-Crowdsec-Appsec-* headers the bouncer supplies to the WAF.\n// They carry the original request's metadata and must be stripped before handing the request\n// off to Coraza so they aren't mistaken for client-supplied headers.\nvar forwardedHeaders = []string{\n\tIPHeaderName,\n\tHostHeaderName,\n\tURIHeaderName,\n\tVerbHeaderName,\n\tUserAgentHeaderName,\n\tAPIKeyHeaderName,\n\tHTTPVersionHeaderName,\n\tTransactionIDHeaderName,\n}","sourceCodeStart":270,"sourceCodeEnd":306,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/appsec/request.go#L270-L306","documentation":"ReqDumpFilter.ToJSON encodes the filtered request to the temp file with a json.Encoder. This error wraps json.Encode failing on the request structure (e.g. an unsupported or cyclic value), and the partially written temp file is deleted to avoid empty dumps.","triggerScenarios":"Calling ToJSON when enc.Encode(req) returns an error — typically an unsupported type inside the request dump (marshal failure), or an I/O write error to the temp file handle.","commonSituations":"Unusual request payloads containing values that cannot be JSON-marshaled by the dump structures, or a temp file write failing mid-dump (disk full, file closed). A secondary logged error here shows the remove failure reason, not the encode one — a common misread.","solutions":["Read the wrapped error: if it is a json marshal error, look for unsupported types in custom request fields/headers being dumped","If it is a write error, check disk space and writability of the temp dir","Upgrade crowdsec — dumps of exotic requests have been hardened over time","If you only need the dump for debugging, retry the request reproduction with a simpler payload to isolate the offending field"],"exampleFix":"// note: the code logs err2 with the wrong variable (err, not err2)\n// after patch\nerr2 := os.Remove(fd.Name())\nif err2 != nil {\n    log.Errorf(\"while removing temp file %s: %s\", fd.Name(), err2)\n}","handlingStrategy":"try-catch","validationCode":"null","typeGuard":null,"tryCatchPattern":"if err := dump.ToJSON(); err != nil {\n    log.Errorf(\"request dump failed: %v\", err)\n    // non-fatal: skip dump and continue processing the request\n}","preventionTips":["Treat dumps as best-effort debugging output","Watch disk space; encode failures can be write errors","Report marshal failures with the offending request shape upstream"],"tags":["json","serialization","debug","appsec"],"backgroundTag":"json-marshal-failed","analyzedSha":"909b5157986a2b2c2163300fdaef5ed01289f7d2","analyzedAt":"2026-09-06T12:27:26.012Z","contentChangedAt":"2026-09-06T12:27:26.012Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}