Commit Graph

249 Commits

Author SHA1 Message Date
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
Oleksandr Redko 9aa666e814 Enable gocritic linter; fix lint issues (#1612) 2023-08-29 20:08:37 +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
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
cui fliter ac631aa4eb fix functiom name (#1505)
Signed-off-by: cui fliter <imcusg@gmail.com>
2023-03-03 17:43:44 +01:00
Oleksandr Redko e15a810a98 docs: fix grammar issues and typos in comments (#1492) 2023-02-13 10:43:44 +08:00
Oleksandr Redko 934f04e330 Refactor golangci-lint config and remove redundant nolints (#1486)
* Refactor golangci-lint config

- Use golangci-lint-action for GitHub workflow.
- Add additional golangci-lint run options.
- Remove unused nolint directives.

* Revert exclude-use-default option
2023-02-11 15:35:15 +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
kinggo 7b3bf58850 style: modify typo and remove repeated type conversions (#1437) 2022-11-18 08:13:18 +02: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
kinggo 3963a79a64 feat: add PeekKeys and PeekTrailerKeys (#1405)
* feat: add PeekKeys and PeekTrailerKeys

* Improve warning

Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>
2022-10-29 18:57:40 +02:00
byene0923 eca86de6f6 fix: (#1410)
1. the length of trailer key should > 0
2. fix http_test words typo
2022-10-29 18:32:18 +02:00