{"record":{"id":"baa424073d5496a3","repo":"valyala/fasthttp","slug":"cannot-find-crlf-at-the-end-of-chunk","errorCode":null,"errorMessage":"cannot find crlf at the end of chunk","messagePattern":"cannot find crlf at the end of chunk","errorType":"exception","errorClass":"ErrBrokenChunk","httpStatus":null,"severity":"error","filePath":"http.go","lineNumber":2961,"sourceCode":"\tstrCRLFLen := len(strCRLF)\n\tfor {\n\t\tchunkSize, err := parseChunkSize(r)\n\t\tif err != nil {\n\t\t\treturn dst, err\n\t\t}\n\t\tif chunkSize == 0 {\n\t\t\treturn dst, err\n\t\t}\n\t\tif maxBodySize > 0 && len(dst)+chunkSize > maxBodySize {\n\t\t\treturn dst, ErrBodyTooLarge\n\t\t}\n\t\tdst, err = appendBodyFixedSize(r, dst, chunkSize+strCRLFLen)\n\t\tif err != nil {\n\t\t\treturn dst, err\n\t\t}\n\t\tif !bytes.Equal(dst[len(dst)-strCRLFLen:], strCRLF) {\n\t\t\treturn dst, ErrBrokenChunk{\n\t\t\t\terror: errors.New(\"cannot find crlf at the end of chunk\"),\n\t\t\t}\n\t\t}\n\t\tdst = dst[:len(dst)-strCRLFLen]\n\t}\n}\n\nfunc parseChunkSize(r *bufio.Reader) (int, error) {\n\tn, err := readHexInt(r)\n\tif err != nil {\n\t\treturn -1, err\n\t}\n\tinExt := false\n\tafterSizeOWS := false\n\tfor {\n\t\tc, err := r.ReadByte()\n\t\tif err != nil {\n\t\t\treturn -1, ErrBrokenChunk{\n\t\t\t\terror: fmt.Errorf(\"cannot read '\\\\r' char at the end of chunk size: %w\", err),","sourceCodeStart":2943,"sourceCodeEnd":2979,"githubUrl":"https://github.com/valyala/fasthttp/blob/c96f600972c6f4a7a30d664257b340ebe9d60124/http.go#L2943-L2979","documentation":"While decoding a chunked transfer-encoding body, fasthttp expects each chunk's data to be terminated by CRLF. If the bytes at the expected end-of-chunk position are not \\r\\n, it wraps this error in ErrBrokenChunk, signaling malformed or truncated chunked data.","triggerScenarios":"A peer sends chunked data where chunk-size and the CRLF after the chunk payload do not match; the connection delivers a truncated chunk; a non-HTTP client speaks on a chunked endpoint; chunkSize computed from a corrupted size header misaligns the stream.","commonSituations":"Buggy proxies/servers emitting malformed chunked responses; man-in-the-middle or connection corruption; request-smuggling probes (fasthttp deliberately rejects this); reading a stream that ends mid-chunk.","solutions":["Verify the sender produces standards-compliant chunked encoding (each chunk followed by CRLF).","Check intermediaries (proxies, gateways) for re-chunking bugs; bypass or upgrade them.","Inspect the peer's output and network for truncation/corruption; retry the request on a fresh connection."],"exampleFix":"// before (malformed peer output)\n5\\r\\nhelloX\\r\\n // missing trailing CRLF -> ErrBrokenChunk\n// after (valid chunked framing)\n5\\r\\nhello\\r\\n0\\r\\n\\r\\n","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"var broken fasthttp.ErrBrokenChunk\nerr := client.Do(req, resp)\nif errors.As(err, &broken) && strings.Contains(broken.Error(), \"cannot find crlf\") {\n    // discard response, retry on a fresh connection\n    req.ConnectionClose = true\n    err = client.Do(req, resp)\n}","preventionTips":["Keep intermediaries (proxies/LBs) current; avoid custom re-chunking.","Set ConnectionClose on suspicious peers.","Monitor ErrBrokenChunk rates as a signal of broken upstreams."],"tags":["http","chunked-encoding","protocol-violation"],"backgroundTag":"broken-chunk-encoding","analyzedSha":"c96f600972c6f4a7a30d664257b340ebe9d60124","analyzedAt":"2026-08-31T22:48:28.265Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}