Commit Graph

154 Commits

Author SHA1 Message Date
Erik Dubbelboer ca4600a0a3 fix: deep-copy trailer keys 2026-06-13 06:09:19 +02:00
Erik Dubbelboer f36c900902 header: match net/http CL+TE handling (#2190)
Match net/http behavior when requests or responses contain both
Content-Length and Transfer-Encoding.

Parse and validate Content-Length even when Transfer-Encoding is present, so
invalid lengths are rejected. For valid Content-Length with chunked
Transfer-Encoding, keep chunked framing as authoritative. Also apply the same
precedence when RequestHeader.DisableSpecialHeader is used.
2026-04-28 04:33:01 +02:00
Erik Dubbelboer 0b4cede30f header: reject unsupported response Transfer-Encoding (#2192)
Reject HTTP/1.1 response Transfer-Encoding values unless they are a single
chunked header, matching net/http's strict transfer parser behavior.

This prevents arbitrary or compound response Transfer-Encoding values from
being silently normalized to chunked and avoids desync/body parsing ambiguity
when parsing upstream responses.
2026-04-28 04:28:05 +02:00
Erik Dubbelboer 00410e7309 Sanitize cookie setters to prevent CRLF injection (#2185)
Prevent cookie APIs from serializing embedded CR or LF bytes into
Cookie and Set-Cookie header lines.

Route Cookie key, value, domain, and path setters, parsed cookie
fields, and RequestHeader/ResponseHeader SetCookie paths through the
existing newline sanitization. Sanitize paths after normalization so
percent-decoded CR/LF bytes cannot bypass the guard.

Thanks to @vnykmshr for reporting this issue.
2026-04-27 05:29:14 +02:00
Erik Dubbelboer b8d29bee6e header: reject pre-colon whitespace in request headers (#2187)
Reject request header field names with whitespace immediately before the
colon instead of trimming them before special-header handling.

This prevents parser differentials for malformed framing and routing
headers such as Content-Length, Transfer-Encoding, and Host when a frontend
forwards raw invalid request headers.

Keep the existing response and trailer compatibility behavior unchanged, and
add regression coverage for both header-only parsing and full request body
reads.
2026-04-27 05:28:18 +02:00
Erik Dubbelboer 52131689e9 header: reject invalid trailer names (#2188)
Validate trailer names added through AddTrailerBytes before storing them
for Trailer header serialization.

Trim OWS around comma-separated trailer names, reject names containing
bytes outside the HTTP field-name token set, and keep the existing
forbidden-trailer filtering in place. This prevents CRLF injection through
dynamic trailer names while preserving valid trailer declarations.

Add request and response regression coverage for invalid trailer names and
tab-trimmed OWS.
2026-04-27 05:28:07 +02:00
Erik Dubbelboer 3eab931bf1 Sanitize first-line header setters to prevent CRLF injection (#2182)
Prevent request and response first-line setters from serializing
embedded CR or LF bytes into the start line.

Route SetMethod, SetRequestURI, SetProtocol, and SetStatusMessage
through the existing newline sanitization used by other header-value
setters. This preserves behavior for valid inputs while preventing
header injection through malformed first-line values.

Thanks to @vnykmshr for reporting this issue.
2026-04-25 03:01:52 +02:00
Erik Dubbelboer 611132707f Strict HTTP version validation and simplified first line parsing (#2167) 2026-03-28 04:31:32 +01:00
Erik Dubbelboer eb38f5fc14 HTTP1/1 requires exactly one Host header (#2164)
Bring our parser more in line with net/http.
2026-03-28 03:10:37 +01:00
Erik Dubbelboer e2f8a255a0 Sanitize header values in all setter paths to prevent CRLF injection (#2162)
Prevents `header.Set("Key", "value\r\nEvil-Header: injected")` from
producing extra header lines in the HTTP response/request.

Thanks to @instantraaamen for reporting this issue.
2026-03-23 03:21:27 +01:00
Erik Dubbelboer c2b317d47d Go 1.26 and golangci-lint updates (#2146)
Keep Go 1.24 compatibility for now (by not using `wg.Go()`).
2026-02-21 10:28:39 +01:00
Copilot 72dccd04fb Fix extra whitespace parsing in HTTP request lines to prevent cache poisoning (#2061)
* Initial plan

* Fix FastHTTP whitespace parsing issue - reject extra spaces in request lines

Co-authored-by: erikdubbelboer <522870+erikdubbelboer@users.noreply.github.com>

* Format header.go and header_test.go with gofmt

Co-authored-by: erikdubbelboer <522870+erikdubbelboer@users.noreply.github.com>

* Fix gofumpt formatting and mirror linting issues in header_test.go

Co-authored-by: erikdubbelboer <522870+erikdubbelboer@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: erikdubbelboer <522870+erikdubbelboer@users.noreply.github.com>
2025-09-09 13:42:19 -05:00
Erik Dubbelboer f9d84d7c52 Rewrite header parsing to improve spec compliance (#2030)
This change updates header parsing to match the behavior of net/http more closely.

**Breaking change**: headers delimited by `\n` (instead of `\r\n`) are no longer supported.

Previously, fasthttp accepted `\n` as a delimiter, which is not spec compliant.
This made it difficult to correctly parse headers containing both `\n` and `\r\n`.
2025-08-12 15:49:35 +02:00
Erik Dubbelboer 81ebee8c79 Fix PeekKeys()
RequestHeader.PeekKeys() and ResponseHeader.PeekKeys() were both
implemented wrong. The tests were also wrong causing this to never be
noticed. They both never actually returned all header keys, this has
been fixed now.

While this is a backwards incompatible change, I'm still going to
release it. Anyone using these functions would have noticed they
didn't work as documented and probably would not have continued using
them.

Fixes https://github.com/valyala/fasthttp/issues/2044
2025-08-09 13:50:07 +02:00
Erik Dubbelboer 01d533ad15 Fix RequestHeader.ContentLength() if disableSpecialHeader is true (#2042) 2025-07-27 08:20:03 +02:00
Erik Dubbelboer a1c842f19e Fix trailer security (#2043)
Bad trailers weren't checked correctly.

Some bad trailers that could cause security issues weren't being
disallowed.
2025-07-27 08:19:46 +02:00
Erik Dubbelboer a3c9dab757 Add warning for deprecated newline separator (#2031)
* Add warning for deprecated newline separator

* Fix feedback, no context by default, use slog
2025-07-10 06:47:32 +02:00
Kashiwa eb1f908d97 refact: eliminate duplication in Request/Response via struct embedding (#2027)
* refact: Eliminate duplication in Request/Response headers via struct embedding

* revert: SetMultipartFormBoundaryBytes

* refact: rename the params of SetProtocol and SetProtocolBytes
2025-07-05 07:51:46 +02:00
Kashiwa a1783ffacc feat: Add iter.Seq2 iterator #2010 (#2011) 2025-05-18 13:23:04 +02:00
Kashiwa 086a114445 fix: accept invalid headers with a space (#1953)
* fix: accept invalid headers with a space #1917

Make behavior consistent with net/http by allowing header keys and trailers containing spaces without canonicalizing them

* fix: lint paramTypeCombine

* fix: https://github.com/valyala/fasthttp/pull/1953#issuecomment-2660691298

* fix: golangci-lint nestingReduce
2025-02-19 19:49:48 +09:00
Kashiwa bdcbf61348 fix: "identity" has been deprecated #1909 (#1919) 2024-12-15 05:32:21 +01:00
Erik Dubbelboer df8335f9ab A response without a body can't have trailers (#1825) 2024-08-20 23:10:04 +02:00
Kashiwa 9df444bdd2 Reduce the size of ResponseHeader and RequestHeader (#1826)
* Reduce sizeof ResponseHeader and RequestHeader

+ Reduce ResponseHeader from 320 to 312 bytes
+ Reduce RequestHeader from 360 to 352 bytes
+ In the benchmark tests, although there is no significant performance improvement, it theoretically reduces memory usage by 2.2% to 2.5%.

* Remove redundant comment
2024-08-20 23:02:30 +02:00
Juan Calderon-Perez 1fb3453165 Use Named Fields and Align Structures to Reduce Memory Usage (#1814)
* Use Named fields and Align Structures to Reduce Memory Usage

* Remove extra spaces
2024-08-02 22:26:52 +02:00
Kashiwa ed6a27a88e Fix header parser (#1808) (#1810)
* Fix RequestHeader parser (#1808)

When FastHTTP receives a header value suffixed or prefixed with tabs, they should be stripped.

* Remove redundant code

* Add test for header parser including tabs (#1808)
2024-07-28 11:16:14 +02:00
Erik Dubbelboer 7760a5b506 Validate request method
Use the same validation as net/http.

Fixes https://github.com/valyala/fasthttp/issues/1803
2024-07-14 09:43:40 +02:00
Erik Dubbelboer b4c0b2b47d Validate header values (#1796)
* Validate header values

Fixes https://github.com/valyala/fasthttp/issues/1794

* Don't allow empty header keys

And improve error handling for bad headers.
2024-07-03 10:04:04 +02:00
Erik Dubbelboer f56f9e4d38 Fix linting issues 2024-06-11 08:42:27 +02:00
Erik Dubbelboer 7b273dc590 Don't allow \r in header names (#1789)
* Don't allow \r in header names

From RFC 9112:

  A sender MUST NOT generate a bare CR (a CR character not immediately
  followed by LF) within any protocol elements other than the content.
  A recipient of such a bare CR MUST consider that element to be invalid
  or replace each bare CR with SP before processing the element or forwarding
  the message.

net/http seems to completely error on this, so let's do the same.

Fixes https://github.com/valyala/fasthttp/issues/1785

* Validate the full header field
2024-06-11 08:41:16 +02:00
Oleksandr Redko 7e1fb71854 Enable perfsprint linter; fix up lint issues (#1727) 2024-03-02 16:21:23 +01:00
Oleksandr Redko bdd459ab0e test: remove //nolint:govet comments (#1729) 2024-03-02 16:19:52 +01:00
Oleksandr Redko 190204cf1a Upgrade golangci-lint to v1.56.2; fix gocritic issues (#1722) 2024-02-21 05:51:28 +01:00
Erik Dubbelboer bce576699a Prevent request smuggling (#1719)
* Prevent request smuggling

Prevent request smuggling when fasthttp is behind a reverse proxy that
might interprets headers differently by being stricter. Should also
prevent request smuggling when fasthttp is used as the reverse proxy.

* Make header value comparison case-insensitive
2024-02-11 08:08:56 +01:00
Erik Dubbelboer 3327266342 Follow RFCs 7230 and 9112 for HTTP versions (#1710)
Require that HTTP versions match the following pattern: HTTP/[0-9]\.[0-9]
2024-02-11 07:55:31 +01:00
Erik Dubbelboer a04cd8c39f Move Fuzz tests into their own file
This is required for https://github.com/google/oss-fuzz/pull/11453
2024-01-09 13:01:31 +01:00
Oleksandr Redko 28615eba55 Change empty string checks to be more idiomatic (#1684) 2024-01-04 15:05:38 +01:00
nickajacks1 868ee455d5 feat: add function to parse HTTP header parameters (#1685)
* feat: add function to parse HTTP header parameters

The implementation is based on RFC-9110 5.6.6.

* test: add fuzz for VisitHeaderParams
2024-01-02 09:43:40 +01:00
Oleksandr Redko 4ec5c5a774 docs: fix typos in comments and tests 2023-08-30 14:13:13 +02:00
Oleksandr Redko 9aa666e814 Enable gocritic linter; fix lint issues (#1612) 2023-08-29 20:08:37 +02:00
Oleksandr Redko 064124ec85 Avoid nolint:errcheck in header tests (#1589) 2023-07-06 09:11:45 +02:00
AutumnSun 0d0bbfee5a Auto add 'Vary' header after compression (#1585)
* Auto add 'Vary' header after compression

Add config `SetAddVaryHeaderForCompression` to enable
'Vary: Accept-Encoding' header when compression is used.

* feat: always set the Vary header

* create and use `ResponseHeader.AddVaryBytes`

* not export 'AddVaryBytes'
2023-07-02 12:40:26 +02:00
Oleksandr Redko ffdf59d04c Enable gofumpt linter; format code gofumpt -w . (#1576) 2023-06-13 15:18:59 +02:00
Anthony Ter-Saakov b79233fac2 add DisableSpecialHeaders option (#1573)
* add DisableSpecialHeaders option

* polishing up disableSpecialHeader option

* forgot to uncomment

* fix silly mistakes

* dont parse special headers
2023-06-12 21:49:52 +02:00
kinggo 43cc4870e6 fix: add noDefaultContentType copy (#1538) 2023-04-14 10:38:32 +02:00
Oleksandr Redko 498a814fbf test: fix typos in function, error message, comment (#1512) 2023-03-06 16:55:21 +01:00
Oleksandr Redko b79a03aaf9 test: close response body (#1496) 2023-02-15 11:54:23 +01:00
Oleksandr Redko 5147cecec7 docs: replace links to golang.org with go.dev (#1489)
- Replace https://godoc.org with https://pkg.go.dev.
- Replace https://golang.org with https://go.dev.
- Replace https://golang.org/pkg with https://pkg.go.dev.
- Replace https://blog.golang.org with https://go.dev/blog.
- Use https://pkg.go.dev/golang.org/x/net/http2  instead of non-existing https://http2.golang.org/.
- Remove trailing slashes.
2023-02-11 19:59:44 +08:00
Oleksandr Redko c0c5f9cd38 doc,test: correct typos (#1484) 2023-02-09 09:29:28 +01:00
Erik Dubbelboer dbf457ee66 Revert "feat: support mulit/range (#1398)" (#1446)
This reverts commit a468a7dd37.
2022-11-28 11:52:30 +01:00
byene0923 a468a7dd37 feat: support mulit/range (#1398)
* feat: support mulit/range

* fix:
1. lint code
2. add SetByteRanges method

* fix: reduce the test number of testFSSingleByteRange
2022-10-30 09:48:46 +01:00