{"record":{"id":"d709517f93fb1671","repo":"valyala/fasthttp","slug":"error-when-reading-s-headers-w-buffer-size-d-d70951","errorCode":null,"errorMessage":"error when reading %s headers: %w: buffer size=%d, contents: %s","messagePattern":"error when reading (.+?) headers: %w: buffer size=(.+?), contents: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"header.go","lineNumber":2303,"sourceCode":"\t// Buggy servers may leave trailing CRLFs after http body.\n\t// Treat this case as EOF.\n\tif isOnlyCRLF(b) {\n\t\treturn io.EOF\n\t}\n\n\tif err != bufio.ErrBufferFull {\n\t\treturn headerErrorMsg(typ, err, b, secureErrorLogMessage)\n\t}\n\treturn &ErrSmallBuffer{\n\t\terror: headerErrorMsg(typ, ErrSmallReadBuffer, b, secureErrorLogMessage),\n\t}\n}\n\nfunc headerErrorMsg(typ string, err error, b []byte, secureErrorLogMessage bool) error {\n\tif secureErrorLogMessage {\n\t\treturn fmt.Errorf(\"error when reading %s headers: %w: buffer size=%d\", typ, err, len(b))\n\t}\n\treturn fmt.Errorf(\"error when reading %s headers: %w: buffer size=%d, contents: %s\", typ, err, len(b), bufferSnippet(b))\n}\n\n// Read reads request header from r.\n//\n// io.EOF is returned if r is closed before reading the first header byte.\nfunc (h *RequestHeader) Read(r *bufio.Reader) error {\n\treturn h.readLoop(r, true)\n}\n\n// readLoop reads request header from r optionally loops until it has enough data.\n//\n// io.EOF is returned if r is closed before reading the first header byte.\nfunc (h *RequestHeader) readLoop(r *bufio.Reader, waitForMore bool) error {\n\tn := 1\n\tfor {\n\t\terr := h.tryRead(r, n)\n\t\tif err == nil {\n\t\t\treturn nil","sourceCodeStart":2285,"sourceCodeEnd":2321,"githubUrl":"https://github.com/valyala/fasthttp/blob/c96f600972c6f4a7a30d664257b340ebe9d60124/header.go#L2285-L2321","documentation":"This error wraps any failure encountered while reading HTTP headers off the wire, including the buffer size and (unless secure error logging is enabled) a snippet of the raw bytes received. fasthttp throws it when header parsing fails so the developer can see exactly what malformed data arrived and how much of it fit in the buffer.","triggerScenarios":"RequestHeader.Read or ResponseHeader.Read fails during parsing (via headerError) when the header block is malformed, too large, or the underlying bufio.Reader errors — and secureErrorLogMessage is false.","commonSituations":"Clients sending non-HTTP garbage to a fasthttp server; headers exceeding the configured ReadBufferSize; proxies forwarding truncated or corrupted header blocks; fuzzing or port scanners hitting the port.","solutions":["Inspect the 'contents' snippet in the error to identify the malformed bytes the peer sent","Increase Server.ReadBufferSize (or Client.ReadBufferSize) if headers are legitimately large","Enable Server.SecureErrorLogMessage in production to avoid echoing raw request bytes to logs","Reject or rate-limit the offending client; the data is malformed and cannot be parsed"],"exampleFix":"// before\nsrv := &fasthttp.Server{Handler: h}\n// after\nsrv := &fasthttp.Server{Handler: h, ReadBufferSize: 16384, SecureErrorLogMessage: true}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"func isHeaderReadError(err error) bool {\n    return err != nil && strings.Contains(err.Error(), \"error when reading\")\n}","tryCatchPattern":"err := fasthttp.Do(req, resp)\nif err != nil && strings.Contains(err.Error(), \"error when reading\") {\n    log.Warn(\"malformed headers from peer\", \"err\", err)\n    return errPeerMalformedResponse\n}","preventionTips":["Set ReadBufferSize generously on both server and client","Enable SecureErrorLogMessage in production","Sanitize client input before proxying through fasthttp servers","Monitor logs for repeated offenders and rate-limit them"],"tags":["http","header-parsing","fasthttp"],"backgroundTag":"malformed-http-header","analyzedSha":"c96f600972c6f4a7a30d664257b340ebe9d60124","analyzedAt":"2026-08-31T22:48:28.265Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}