Commit Graph

262 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 a95a1ad11c Only normalize pre-colon whitespace for HTTP headers (#2172)
Keep headerScanner strict so malformed MIME header lines are still rejected.

Move trimming before ':' into the HTTP header handling paths that
intentionally normalize header names, and add a fuzz seed for the
regression case.
2026-04-04 04:24:04 +02:00
RW c4569c5fbb feat: enhance performance (#2135)
* feat: enhance performance

* fix: improve request URI parsing condition

* feat: validate HTTP date parsing and optimize status code length calculation

* Address parsing and lint issues

* chore: update Go version to 1.24.x in CI configuration

* feat: enhance HTTP date parsing and request URI handling

* refactor: optimize month and day name parsing using bitwise operations

* refactor: replace cookie token comparison with case insensitive function and streamline request URI parsing

* refactor: streamline request body handling and simplify request URI assignment

* chore: update Go version to 1.25.x in CI configuration

* feat: add fuzz testing for HTTP date parsing to improve robustness

* refactor: avoid unused return values in HTTP date parsing benchmarks

* refactor: update HTTP date parsing to use http.TimeFormat for consistency
2026-04-01 16:19:26 +09: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 7d90713bda Validate request URI format during header parsing to reject malformed requests (#2168) 2026-03-28 03:10:23 +01:00
Erik Dubbelboer 3c43293b0c Prevent chunk extension request smuggling (#2165) 2026-03-28 03:10:11 +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
pj 21dbfe80ce return prev values (#2123) 2026-01-07 18:13:11 -05:00
Erik Dubbelboer 7cf1fb7967 Add documentation that modifying during iteration can panic (#2122)
Fixes https://github.com/valyala/fasthttp/issues/2120
2026-01-05 15:10:34 +01:00
RW 2a82a57b9d Expose header parsing error variables (#2096) 2025-11-12 02:31:36 +01:00
Erik Dubbelboer 1962450629 Fix copyTrailer
Don't use it in PeekTrailerKeys and have it reuse the byte slices it might already
have.
2025-10-06 03:45:12 +02:00
Erik Dubbelboer 4d25421ae5 Drop Go 1.23 support (#2065)
The Go team doesn't support this version anymore. This is required to
update our golang.org/x/... dependencies.
2025-09-10 22:16:01 +02: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
Erik Dubbelboer 4891fc5304 Update golangci-lint to v2 (#1980) 2025-03-25 06:40:55 +01:00
Erik Dubbelboer b8969ed8dc Fix normalizeHeaderValue (#1963)
The fuzzer found some cases where it would panic.

The output of normalizeHeaderValue doesn't need to affect s.b and s.hLen
because the length of the normalized header will never be bigger, so it
can just be normalize in place without affecting the rest of the buffer.
2025-02-22 08:33:57 +01: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 b59f47e3ee Refactor: split delAllArgs into delAllArgs and delAllArgsStable (#1945)
- Renamed the original `delAllArgs` method to `delAllArgsStable` to maintain stable behavior.
- Added a new `delAllArgs` method for non-stable functionality, improving runtime efficiency.
2025-02-19 19:48:10 +09:00
Kashiwa c908d9c1ee Refactor trailer Field for Improved Memory Efficiency and Performance (#1928)
* refact: represent trailer filed by [][]byte instead of []argsKV

* fix: address golangci-lint warnings
2025-01-29 05:53:12 +01:00
Kashiwa bdcbf61348 fix: "identity" has been deprecated #1909 (#1919) 2024-12-15 05:32:21 +01:00
Sigmund Xia 夏天睿 441750d6f5 add a note to clarify header.Set behavior on cookies (#1864) 2024-09-25 20:57:24 +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
Kashiwa 86c7e844f4 Optimize struct field order to reduce memory usage (#1809)
1. Reduce RequestHeader from 368 bytes to 360 bytes
2. Reduce Request from 816 bytes to 800 bytes
3. Reduce Response from 432 bytes to 416 bytes
4. Reduce Client from 312 bytes to 288 bytes
5. Reduce HostClient from 416 bytes to 392 bytes
6. Reduce PipelineClient from 176 bytes to 168 bytes
7. Reduce pipelineConnClient from 216 bytes to 208 bytes
8. Reduce Cookie from 232 bytes to 224 bytes
9. Reduce FS from 184 bytes to 160 bytes
10. Reduce fsHandler from 168 bytes to 160 bytes
11. Reduce ResponseHeader from 328 bytes to 320 bytes
12. Reduce headerScanner from 128 bytes to 120 bytes
13. Reduce TCPDialer from 104 bytes to 96 bytes
14. Reduce workerPool from 152 btyes to 144 btyes
2024-07-20 19:20:26 +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 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 3166afd835 Enable few gocritic checks; fix up issues (#1728) 2024-03-02 16:19:05 +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
nickajacks1 2accefa5cb refactor: move manually created tchar table to bytesconv_table_gen (#1689) 2024-01-07 08:27:45 +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 9d6b470260 chore: Add missing dots at the end of comments (#1677) 2023-12-13 13:56:24 +08:00
xiluoxi 497922a21e reset request disableSpecialHeader (#1626) 2023-09-22 14:21:18 +02:00