Commit Graph

198 Commits

Author SHA1 Message Date
Cory Benfield 116b6d1cc6 Merge pull request from GHSA-v3r5-pjpm-mwgq
Motivation

Allowing arbitrary data in outbound header field values allows for the
possibility that users of AHC will accidentally pass untrusted data into
those values. That untrusted data can substantially alter the parsing
and content of the HTTP requests, which is extremely dangerous. The
result of this is vulnerability to CRLF injection.

Modifications

Add validation of outbound header field values.

Result

No longer vulnerable to CRLF injection

(cherry picked from commit 3034835a213babfcda19031e80c0b7c9780475e9)
2023-01-17 11:06:09 +00:00
Fabian Fett 5844a6b4ee Crash fix: HTTP2 can handle requests are cancelled (#555)
Co-authored-by: George Barnett <gbarnett@apple.com>
2022-02-10 10:42:27 +01:00
David Nadoba 06b9f98989 Make async/await API public (#552) 2022-02-08 11:48:58 +01:00
David Nadoba f3830d11ec Add async shutdown method to HTTPClient (#551) 2022-02-07 11:36:53 +01:00
David Nadoba 14c95bf6c9 Disable logging by default and add timeout API (#549) 2022-02-01 17:32:33 +01:00
Fabian Fett ce3958ff92 Fix race between connection close and scheduling new request (#546) 2022-01-24 12:07:41 +00:00
Fabian Fett 2497a68427 Print invalid state, if hitting precondition (#545) 2022-01-24 12:26:08 +01:00
Karl 609b4368b8 Used vendored base64 encoding implementation rather than Foundation.Data (#539) 2022-01-21 15:28:26 +01:00
David Nadoba c2805dfa4e Prepare async/await API for public release (#531) 2022-01-14 14:35:17 +01:00
Karl 6474d8d2b2 Add HTTPClient.Body.bytes as an alternative to .data (#534) 2022-01-13 14:19:48 +00:00
Karl 972bcddedc Redo HTTP cookie parsing (#510)
* Redo HTTP cookie parsing using strptime

* Make String(utf8Slice:from:) less ugly

* Adjust cookie component parsing to better match RFC-6562
2022-01-06 16:40:55 +00:00
David Nadoba 19e83a35df Set host on new request correctly (#536)
### Motivation
If we follow a redirect which changes the origin e.g. from `127.0.0.1` to `localhost` we didn't change the `Host` header to the appropriate new origin and port combination.
### Changes
Use the original request which does not include the host instead of the prepared request to form a new request to the redirect URL.

### Alternatives
If the user defines a `Host` header themselves on the original `HTTPClientRequest` we currently never touch it, even in the redirect case. Maybe we should change our strategy and do one of the following:
1. We could always override the user defined `Host` header
2. We could only remove the user defined `Host` header on redirect and set it to the new origin and port combination
2021-12-24 12:31:24 +01:00
David Nadoba e531961906 remove dot from HTTP1.1 folder (#535)
Swift tools version 5.3 and higher (the version that is specified at very top of a Package.swift file) excludes folders with a dot in the name by default. It luckily produces a warning "found 1 file(s) which are unhandled; explicitly declare them as resources or exclude from the target". However, this issue is buried under a lot of missing types Errors because of the 3 excluded files.
I run into this issue and it took me some time to figure out what the actual problem was. As we will eventually move from 5.2 to 5.3 we can already save the next person some time by resolving this issue now.
2021-12-23 00:09:38 +01:00
David Nadoba d372bdc213 Make async/await available on older Apple Platforms (#527)
### Motivation
With Xcode 13.2, and therefore Swift 5.5.2, Swift Concurrecy is supported on older Apple OSs. async/await suport will no longer be available on Swift before `5.5.2` but this isn't a breaking change because we have not yet made anything of it public.

### Changes
- replace all `#if compiler(>=5.5) && canImport(_Concurrency)` with `#if compiler(>=5.5.2) && canImport(_Concurrency)`
- replace all `available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)` with `available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)`
2021-12-17 16:08:24 +01:00
David Nadoba e4b11eb547 Fix HTTP1 to HTTP2 migration while shutdown is in progress (#530)
* Fix HTTP1 to HTTP2 migration while shutdown is in progress

### Motivation
Calling `HTTPClient.shutdown()` may never return if connections are still starting and one new established connection results in a state migration (i.e. from HTTP1 to HTTP2 or vice versa). We forgot to migrate the shutdown state. This could result in a large dealy until `.shutdown()` returns because we wait until connections are closed because of idle timeout. Worse, it could also never return if more requests are queued because the connections would not be idle and therefore not close itself.
###Changes
- Mirgrate shutdown state too
- add tests for this specific case

* simplify testMigrationFromHTTP1ToHTTP2WhileShuttingDown
* add http2 to http1 migration test
2021-12-17 14:44:11 +01:00
David Nadoba d95277640f Respect deadline on new HTTPClient.execute for async/await (#529)
* Schedule deadline timeout
* Add state machine tests and enable skipped test for http1
2021-12-17 09:36:41 +01:00
David Nadoba b1c0c3c77b replace EventLoopGroup.next() with .any() (#526) 2021-12-14 21:59:54 +01:00
David Nadoba 5db7719a27 async/await execute (#524)
* async/await execute

* remove default length for `HTTPClientRequest.Body`

* make redirect logic iterative

* move Task creation into `TransactionCancelHandler`
2021-12-14 20:24:45 +01:00
David Nadoba bf5668c37b Use the current EventLoop as the requests preferred EventLoop (#515)
* Use the current `EventLoop` as the requests prefered `EventLoop` if possible

* use released version of swift-nio
2021-12-14 20:10:31 +01:00
Fabian Fett 24b0a14e9c Add async Transaction (#518)
This introduces an async Transaction object. The object is the translation layer between the user facing async API and the NIO channel handler.
2021-12-09 18:04:01 +01:00
David Nadoba c4feafd813 Refactor redirect logic to be reusable for async/await (#522)
* refactor RedirectHandler
- `redirectState` is no longer a property of `HTTPClient.Request`. RedirectHandler now stores this state directly and therefore no longer optional.
- we no longer count the number of allowed redirects down. Instead the number of redirects is dervied from `self.visited.count` and we compare it to the maxRedirect to check if we git the limit.

* `HTTPClient.Configuration.RedirectConfiguration.Configuration` is now called `HTTPClient.Configuration.RedirectConfiguration.Mode`
only two `Configuration`s left in the type name

* add redirect logger test
2021-12-08 19:16:06 +01:00
Fabian Fett 474b23d677 Typo: Replace HTTPExecutingRequest with HTTPExecutableRequest (#514) 2021-12-03 17:10:55 +01:00
David Nadoba 5ce7377a8a Add HTTPClientReuqest.Prepared (#511)
* add HTTPClientReuqest.Prepared

* make `prepared()` an init of `Prepared`

* make all stored properties of `Prepared` `var`s
2021-12-03 09:54:01 +01:00
David Nadoba a956e7b448 Add HTTPClientRequest (#509)
* HTTPClientRequest

* move into single file

* fix review comments

* remove overload
2021-12-02 14:25:58 +01:00
David Nadoba 99bd384b38 Refactor deconstructURL and scheme parsing (#504)
* make `Scheme` a type

* introduce new Endpoint type

* use endpoint as storage in `HTTPClient.Request`

* fix merge conflicts

* rename Endpoint to DeconstructedURL

* swift-format

* make `DeconstructedURL` properties `var`'s

* move scheme into global namespace

- rename `useTLS` to `usesTLS` where posible without breaking public API
- only import Foundation.URL

* fix review comments
2021-12-01 13:03:09 +01:00
David Nadoba 591aa445d9 fix nits from #501 (#503) 2021-11-30 18:47:13 +01:00
Karl f1a91872c5 Introduce a ConnectionTarget enum (#501)
* Add a ConnectionPool.Host enum

* Move Host out as a top-level ConnectionTarget type, and use it in Request.
2021-11-30 18:29:38 +01:00
Karl f2c2f73fb9 Refactor URL component extraction (#485)
* Refactor URL component extraction

* Remove superfluous test message

Co-authored-by: Fabian Fett <fabianfett@apple.com>
Co-authored-by: David Nadoba <d_nadoba@apple.com>
2021-11-26 12:49:43 +01:00
David Nadoba 9eaecbbbce SwiftFormat --ifdef no-indent (#494)
* SwiftFormat --ifdef no-indent

* update `generate_linux_tests.rb` to use new indention rule
2021-11-25 17:09:19 +01:00
Fabian Fett e5022468bb Update swiftformat to 0.48.8 (#491)
### Motivation

Our current swiftformat version does not support async/await. Since we want to add support for async/await we must update swiftformat or disable it. I tried my very best to keep the number of changes as small as possible. I assume we want to stick with the new 0.48.8 for some time.

### Changes

- Update swiftformat to 0.48.8

### Result

We can land async/await code.
2021-11-25 10:15:36 +01:00
David Nadoba 4fd1150184 Fix bodyLengthMissmatch error handling (#490) 2021-11-24 15:46:51 +01:00
David Nadoba 0ed00b8b36 Always overwrite Transport-Encoding and Content-Length headers (#479) 2021-11-23 10:22:57 +01:00
Karl 8c48625d0f Remove a couple of unnecessary imports (#484)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2021-11-22 12:31:36 +00:00
Cory Benfield 8713f952cd Two missing selfs got past soundness. (#489)
Co-authored-by: Fabian Fett <fabianfett@apple.com>
2021-11-22 11:55:35 +00:00
David Nadoba b479afe4c8 Fix bug in migration from HTTP1 to HTTP2 and back to HTTP1 (#486) 2021-11-22 12:33:57 +01:00
Fabian Fett 2fe3f42fee Crash fix: HTTP2Connections emit events after the pool has closed them. (#481) 2021-11-19 12:33:08 +01:00
David Nadoba 1f3f141038 Remove redundant RequestFramingMetadata.Body.none case (#480) 2021-11-18 17:13:38 +01:00
Fabian Fett ec2e080d70 Only crash in debug mode, if HTTPClient was not shutdown (#478)
### Motivation

Generally we want to inform users that they need to shutdown their HTTPClient. Until `1.6.0` we did this with an assert in HTTPClient's deinit. With `1.6.0` this behavior was raised to a precondition. Because of this adopters might suddenly crash in production where they didn't before.

### Changes

- This pr reverts the current behavior back to something pre `1.6.0`

### Result

- HTTPClient doesn't crash in production anymore.
2021-11-17 14:22:55 +01:00
David Nadoba 3e44e5154e [HTTP2] Make HTTPVersion public and set to .automatic by default (#473) 2021-11-11 14:41:11 +01:00
Fabian Fett 7617c35db3 Handle NIOSSLError.uncleanShutdown correctly (#472)
### Motivation

Fixes #238 and #231.

### Changes

- Extracted the unclean shutdown test from `HTTPClientTests` into their own file `HTTPClientUncleanSSLConnectionShutdownTests`
- Copy and pasted @weissi great explanation from #238 into the test file
- Removed property `ignoreUncleanSSLShutdown` everywhere

### Result

`ignoreUncleanSSLShutdown` on `HTTPClient.Configuration` is deprecated and ignored.

Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2021-11-11 11:02:54 +01:00
Fabian Fett 170fd536f9 Support informational response heads (#469) 2021-11-10 13:33:38 +01:00
David Nadoba 7d1ed4cca5 [HTTP2] Forward HTTP2 events to StateMachine (#466) 2021-11-04 10:48:12 +00:00
David Nadoba 18a58bb874 [HTTP2] Improve performance of backoff timer done event (#464) 2021-11-03 14:29:24 +00:00
David Nadoba 60fef53db7 [HTTP2] Internal HTTPVersion configuration (#463) 2021-11-02 18:46:29 +00:00
David Nadoba 149b8d2656 [HTTP2] Integrate HTTP2StateMachine into HTTPConnectionPool.StateMachine (#462) 2021-11-02 11:51:18 +00:00
David Nadoba 4147fd647d [HTTP2] Create new connections during migration if needed (#459) 2021-10-27 18:28:51 +02:00
Sergei Fundaev 1361eccfb2 Double quoted cookie values are supported now. (#460)
Motivation:

In according to RFC 6265 a cookie value may be placed between double quotes.

Modifications:

HTTPClient.Cookie ignores now the double quotes at the beginning and the ending of a cookie value.
New unit test is added to check it.

Result:

Quoted cookie values are parsed properly now.
2021-10-20 11:08:29 +01:00
David Nadoba c1a60d8aa4 [HTTP2] Prepare migration actions (#456) 2021-10-13 17:11:04 +02:00
Cory Benfield 1081b0b054 Don't crash when hitting long backoffs. (#458)
Motivation:

If we backoff sufficiently far we can overflow Int64, which will cause
us to crash.

Modifications:

Clamp the backoff value before we convert to Int64.

Results:

No crashes!
2021-10-13 13:09:54 +01:00
David Nadoba d5bd8d6526 Always clear read idle timeout at the end of a request (#455) 2021-10-08 16:29:28 +02:00