Skip to content

skip invalid fields in write_headers to prevent response splitting#2505

Merged
yhirose merged 1 commit into
yhirose:masterfrom
metsw24-max:write-headers-crlf-guard
Jul 23, 2026
Merged

skip invalid fields in write_headers to prevent response splitting#2505
yhirose merged 1 commit into
yhirose:masterfrom
metsw24-max:write-headers-crlf-guard

Conversation

@metsw24-max

Copy link
Copy Markdown
Contributor

The server serialises res.headers straight through write_headers, which formats each entry as name: value followed by CRLF without checking either part. The client side already guards this: req.headers go through check_and_write_headers, and the request line and websocket handshake run the same is_field_value check, so write_headers was the one output path left unguarded. Response::headers is a public field, and an application that reflects a request-derived value into it (a fairly common pattern) can carry a CR/LF into the response, splitting the header block and injecting further headers or a body. I noticed the gap while reading the recent chunked-trailer fix, which closed the same class for trailers but did not reach the ordinary header writer. The change skips any field whose name or value fails validation, which keeps the behaviour consistent with set_header rather than dropping the whole response. Fields the library emits itself all go through set_header and stay valid, so only directly-injected ones are affected. The regression test drives a handler that plants CR/LF in both a header name and value via res.headers and checks the split content never reaches the wire.

@yhirose
yhirose merged commit 613a41b into yhirose:master Jul 23, 2026
43 of 44 checks passed
@yhirose

yhirose commented Jul 23, 2026

Copy link
Copy Markdown
Owner

@metsw24-max thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants