### Motivation:
`CertificateValidation.swift` imports Foundation but uses nothing from
it. On Linux this makes every program that uses NIOCertificateHelpers
link the full Foundation and FoundationInternationalization, with ICU.
Its dependencies, X509 and SwiftASN1, already import
FoundationEssentials when it's available.
### Modifications:
Remove `import Foundation` from `CertificateValidation.swift`.
### Result:
On Linux, a program that uses NIOCertificateHelpers no longer links
`libFoundation.so` or `libFoundationInternationalization.so`; only
`libFoundationEssentials.so` is left. Checked with `readelf -d` on a
small executable (Swift 6.4.1, Ubuntu 24.04), before and after. `swift
build -Xswiftc -warnings-as-errors
--explicit-target-dependency-import-check error` and
`NIOCertificateHelpersTests` pass on Linux and macOS.
Replace repo-level CODE_OF_CONDUCT file with the inherited org-wide
[Code of
Conduct](https://github.com/apple/.github/blob/main/CODE_OF_CONDUCT.md)
in .github repo.
Co-authored-by: apple-ospo[bot] <326325757+apple-ospo[bot]@users.noreply.github.com>
Motivation:
swift-http-types 1.7.0 adds an HTTP2ErrorCode type which is ambiguous
with the type of the same name from swift-nio-http2. This causes
compilation failures when users depend on the swift-nio-extras.
Modifications:
- Disambiguate the type
Result:
Build compiles
This should enable NIOExtras to work on Windows, except for modules that
depend on NIOSSL.
---------
Co-authored-by: Cory Benfield <lukasa@apple.com>
Co-authored-by: George Barnett <gbarnett@apple.com>
### Motivation:
zlib stores the address of `z_stream` in its internal state and expects
that
address to remain stable across calls. Storing `z_stream` inline in
movable
Swift value types does not provide that guarantee and can cause
`Z_STREAM_ERROR` during cleanup.
Fixes#308.
### Modifications:
- Make the internal compressor and decompressor reference owners of
separately
allocated `z_stream` storage.
- Pass the stable pointer to every zlib initialization, processing, and
cleanup
call.
- Keep lifecycle state with the pointer owner and make conditional
cleanup
idempotent.
- Add regression tests covering failed initialization recovery, multiple
compression and decompression calls, repeated lifecycles, and
deallocation
cleanup.
### Result:
zlib observes the same `z_stream` address for each complete lifecycle,
while
stream cleanup remains single-owner and safe against repeated shutdown.
Fix `HTTPResumableUploadHandler` silently dropping all responses after
the first request on a connection.
### Motivation:
`HTTPResumableUploadHandler` forwards all outbound operations through
its stored `ChannelHandlerContext`, which is set only in `handlerAdded`
and cleared by `detach()` whenever an upload releases the handler. When
a second request arrives on the same channel — the normal case for
HTTP/1.1 with keep-alive — `channelRead` calls `resetUpload`, which ends
the previous upload. That synchronously detaches the handler and clears
its context, and nothing restores it before the new upload is attached.
Every response write for the second and subsequent requests then hits
`self.context?.write(...)` as a no-op: the response is silently dropped,
write promises never complete, and `read()` forwarding to the parent
channel stops, so the client hangs until it times out.
The existing tests never caught this because each test sends one request
per `EmbeddedChannel`; resumption is only exercised across separate
channels, and per-stream channels (HTTP/2) recreate the handler per
request, which also masks the bug.
### Modifications:
- Restore `self.context` in `resetUpload(context:)` after ending the
previous upload.
- Add `testNonUploadKeepAlive` and `testResumableUploadKeepAlive`, which
send a second request on the same `EmbeddedChannel` for pass-through
traffic and after a completed upload creation respectively. Both fail
without the fix.
### Result:
Connections handled by `HTTPResumableUploadHandler` can serve more than
one request: responses to the second and subsequent requests on a
keep-alive connection are delivered instead of being silently dropped.
Fixes#318
Co-authored-by: Cory Benfield <lukasa@apple.com>
### Motivation:
The readTCPIPv4 helper subtracted 20 from the IPv4 total length field in
UInt16 space before validating it. A total length below the header size
wraps around and traps with an arithmetic overflow.
A TCP/IPv4 packet is always at least 40 bytes (a 20 byte IPv4 header
plus a 20 byte TCP header), so a smaller total length is an illegal
packet that no further data can complete. Validate the total length
alongside the version and protocol checks and throw when it is too
small, then slice the payload. A regression test covers the lengths that
previously trapped.
These helpers only ever parse pcaps that WritePCAPHandler wrote, which
are always well formed, so the bug was dormant here. It becomes a real
crash when the helpers are reused to parse arbitrary captures.
### Modifications:
- Implement the check
### Result:
- More correct code, even though it's just a test helper
### Motivation:
`NIOHTTPRequestDecompressor` / `NIOHTTPResponseDecompressor` keep their
state in a
stored `var decompressor` (a `struct`) and forwarded downstream
(`fireChannelRead` /
`fireErrorCaught`) from inside that struct's `mutating` `channelRead`,
i.e. while an
exclusive (modify) access to the stored property was still open. If
firing downstream
synchronously re-enters the handler's `channelRead`, a second exclusive
access is
opened and the program traps with `Fatal access conflict detected`
(SIGABRT).
This reproduces in production with AsyncHTTPClient over HTTP/2 and
`decompression = .enabled`: a compressed response is mid-decompress when
a
decompression error closes the stream, and the buffered read
re-delivered on the
stream-close path re-enters `channelRead` on the same decompressor.
### Modifications:
- The body decompression is now expressed as a
`NIOSingleStepByteToMessageDecoder`,
driven by a `NIOSingleStepByteToMessageProcessor`. The handler pulls one
decompressed
buffer at a time and forwards it only after `decode` has returned, so
the decoder
relinquishes its exclusive access before anything is fired downstream.
- The shared driver is now a `class`, so the head/end forwarding paths
are re-entrancy
safe for the same reason.
- The zlib stream is torn down in the decoder's `deinit`, which
additionally fixes a
leak when a message ends without a final `.end`.
- No public API changes.
### Result:
Synchronous re-entry of `channelRead` no longer traps. Adds regression
tests covering
the forward and error re-entry paths on both decompressors; all existing
decompression
tests (limits, trailing data, truncation, multi-message reuse) continue
to pass.
Fixes#314
---------
Co-authored-by: George Barnett <gbarnett@apple.com>
Motivation:
The HTTPType to HTTP2 conversion functions can trip up on invalid psuedo
header fields. Prior to this change they'll loop indefinitely, instead
they should be rejected per RFC 9113 § 8.3.
Modifications:
- Throw on unrecognised psuedo header fields
- Add tests
Result:
- More spec compliant
### Motivation:
The swift-crypto beta can only be used if all packages in the dependency
tree accept it.
### Modifications:
Widen upper bound of swift-crypto dependency requirements to accept beta
releases when `SWIFT_NIO_EXTRAS_ALLOW_SWIFT_CRYPTO_BETA` is in the
environment.
### Result:
Enable use of swift-nio-extras with swift-crypto beta releases.
Note: Export `SWIFT_CERTIFICATES_ALLOW_SWIFT_CRYPTO_BETA` to enable
swift-certificates (in the dependency tree) to do the same.
Motivation:
Certain malformed q-values can trigger a crash because they don't
validate that a string split results in the expected number of parts.
Modifications:
- validate that the number of split parts is as expected, return a
q-value of zero otherwise (i.e. "not acceptable")
Result:
Safer code
### Motivation
We want to maintain less code.
### Changes
- Unifi the request and response decompression by extracting the decompression code into a `Decompressor` struct
- `NIOHTTPRequestDecompressor` calls out to `Decompressor` internally
- `NIOHTTPResponseDecompressor` calls out to `Decompressor` internally
### Result
- Less code to maintain
- Less subtle bugs
Motivation
* The shared `macos_tests.yml` workflow no longer accepts
`xcode_16_2_build_arguments_override` and the `xcode_16_3` input is a
no-op. Passing the removed input causes CI to fail.
* Older Xcodes are being removed from the runners; Swift version inputs
are the replacement.
Modifications
* Replace `xcode_16_2/16_3_build_arguments_override` with
`swift_6_1/6_2/6_3_build_arguments_override` carrying the same
`-Xswiftc -Xfrontend -Xswiftc -require-explicit-sendable` value.
Result
* macOS CI passes again using the Swift version inputs.
Motivation:
Swift 6.0 is no longer supported, we should bump the tools version and
remove it from our CI.
Modifications:
* Bump the Swift tools version to Swift 6.1
* Remove Swift 6.0 jobs where appropriate in main.yml, pull_request.yml
Result:
Code reflects our support window.
---------
Co-authored-by: George Barnett <gbarnett@apple.com>
Motivation:
Swift 6.3 has been released, we should add it to our CI coverage.
Modifications:
Add additional Swift 6.3 jobs where appropriate in main.yml,
pull_request.yml
Result:
Improved test coverage.
Motivation
* Following [GitHub's
announcement](https://github.blog/changelog/2025-09-19-deprecation-of-node-20-on-github-actions-runners/),
Node 20 is being deprecated and runners will begin using Node 24 by
default starting March 4th, 2026. Node 20 will reach end-of-life in
April 2026.
Modifications
* Upgrade GitHub Actions to their latest versions to ensure
compatibility with Node 24.
* Replace `actions/checkout@v4` with `actions/checkout@v6` in all
workflow files.
Result
* Workflow files reference `actions/checkout@v6`.
### Motivation:
Relying on the system zlib causes issues if that library (or its
headers) doesn't ship with the system.
### Modifications:
Vendor it.
### Result:
- Fewer dependencies.
- Binary size increase negligible. The _sum of the debug builds_ of all
`.o` files is less than 200kB. A linked release build will make this
even smaller:
```
$ du -k .build/arm64-apple-macosx/debug/CNIOExtrasZlib.build/*.o
12 .build/arm64-apple-macosx/debug/CNIOExtrasZlib.build/adler32.c.o
12 .build/arm64-apple-macosx/debug/CNIOExtrasZlib.build/crc32.c.o
60 .build/arm64-apple-macosx/debug/CNIOExtrasZlib.build/deflate.c.o
16 .build/arm64-apple-macosx/debug/CNIOExtrasZlib.build/inffast.c.o
40 .build/arm64-apple-macosx/debug/CNIOExtrasZlib.build/inflate.c.o
16 .build/arm64-apple-macosx/debug/CNIOExtrasZlib.build/inftrees.c.o
40 .build/arm64-apple-macosx/debug/CNIOExtrasZlib.build/trees.c.o
8 .build/arm64-apple-macosx/debug/CNIOExtrasZlib.build/zutil.c.o
```
Co-authored-by: Johannes Weiss <johannes@jweiss.io>
Motivation
* `@main` references to `swiftlang/github-workflows` are deprecated.
Modifications
* Replace `@main` with `@0.0.7` in all workflow files.
Result
* Workflow files reference a pinned version of
`swiftlang/github-workflows`.
### Motivation:
Servers using `HTTPResumableUploadHandler` can currently crash during
channel creation if the parent channel is closed.
During channel creation, the handler will inherit the `autoRead` option
from the parent channel:
```
try! parent.syncOptions!.getOption(ChannelOptions.autoRead)
```
The force-unwrap of `syncOptions` is justified because this handler can
only be used with channels that support sync options, but the force-try
can trigger a crash with supported channels when the connection is
interrupted.
NIO's `BaseSocketChannel` will throw an error if the channel is closed,
and this presents a race window and we've observed crashes with the
following output:
```
HTTPResumableUploadChannel.swift:81: Fatal error: 'try!' expression unexpectedly raised an error: I/O on closed channel
```
### Modifications:
Replace the force-try with `try?` to prevent the crash. Given it looks
like the only reason this would throw is if the connection is closed,
the channel is not long for this world, and so the default value used is
moot. However, I've defaulted this to `false` as it's the most
conservative option.
I also wrote a test that uses new `EmbeddedChannel` API that was added
to support this fix. The PR has been structured to add the test first,
showing it failing, and then add the fix commit, which then shows the
test passing.
### Result:
The server will no longer crash when parent channels close during upload
operations.
### Motivation:
Swift Certificates offers the ability to write complex verification
logic. We should be able to make use of that in a NIO application.
### Modifications:
Add a helper function to validate a swift nio chain using a Verifier.
This function could be called within a custom verification callback on a
NIOSSLServerHandler, for example.
Add `context.fireChannelActive()` to
`SOCKSClientHandler.channelActive()`
### Motivation:
Handler should propagate the channel active to handlers further down
Motivation:
The HTTPResumableUpload type is non-sendable and bound to a particular
event loop. It holds a ref to a HTTPResumableUploadHandler which is also
non-sendable and bound to a particular event loop which may be different
to the event loop of the upload. This should not be possible: if the
event loops aren't guaranteed to be the same then the upload can't
possibly hold a handler as it must necessarily cross an isolation
domain.
The sendable view of the uploads are also held in a dictionary protected
by a mutex but also provide access to the loop bound holding the upload.
This makes it a little too easy to accidentally use the loop bound value
from the wrong event loop which will lead to a runtime crash.
Modifications:
- Add a sendable view to the HTTPResumableUploadHandler which dispatches
work to the correct event loop
- Replace occurrences of HTTPResumableUploadHandler with its sendable
view in the HTTPResumableUpload
- Extend the sendable view of HTTPResumableUpload and audit calls using
the loop bound value
- Add a test for multi-thread multi-chunk uploads
Result:
Can do multi-chunk multi-thread uploads without runtime crashes
Workaround a compiler issue in Swift 6.1 with non-copyable types and
exclusivity checking.
### Motivation:
Code introduced in #298, led to a runtime crash when compiling with
`-Xswiftc -enforce-exclusivity=unchecked`. This fixes the issue by
removing non-copyable types from the implementation, whilst retaining a
fix for copy-on-write behaviour.
### Modifications:
- Change LifecycleStateMachine to not use non-copyable types
- Use a `.modifying` state instead to prevent copy-on-write
### Result:
No runtime crash when compiling with exclusivity checks disabled on
swift 6.1, behaviour otherwise unchanged, apart from possible
performance loss from not using non-copyable types.
Fix a copy on write issue in QuiescingHelper that led to significant
performance problems when the number of channels became large, or
connection churn.
### Motivation:
When using QuiescingHelper, every channel holds onto a reference to
ChannelCollector, which leads to copy on write behaviour for
`openChannels[]`. This has performance issues when a large number of
channels are open (e.g. 25k channels can be hundreds of KB), and every
time a channel is opened, or closed this is copied. As accepting
channels happens on a single NIO thread, this work is all done on that
thread, and can lead to saturation, and no more channels being accepted.
### Modifications:
Move the state into a non copyable LifecycleStateMachine, to avoid the
copies.
### Result:
Overhead from ChannelCollector disappears (as seen in traces from
http://github.com/apple/swift-profile-recorder).
Motivation
The dripping download handler should aim to be as efficient as possible.
Modifications
Avoid creating a second HTTPFields object when we are sending
content-length in the response.
Avoid an extra flush on the response head.
Results
Better performance behaviour.
Handle absolute-form requests when converting from/to HTTPTypes.
### Motivation:
HTTPTypes represent modern HTTP/2 / HTTP/3 semantics which do not
support absolute-form requests.
### Modifications:
When converting an absolute-form request to an HTTPRequest, the request
target is converted to its path. When converting an HTTPRequest back,
the user needs to specify which form to convert to.
### Result:
HTTPRequest no longer contains invalid paths when working with cleartext
HTTP/1 proxies.
Motivation:
* More secure GitHub Actions workflows
Modifications:
Add explicit 'contents: read' permissions to workflows that did not have
explicit permissions defined. This follows GitHub Actions security best
practices by limiting the default GITHUB_TOKEN permissions.
Result:
An extra layer of security.
### Motivation:
Swift 5.10 is no longer supported, we should bump the swift-tools
version.
### Modifications:
* Update Swift tools version to 6.0
* Remove unnecessary compiler directives
### Result:
Code reflects our support window.
---------
Co-authored-by: Cory Benfield <lukasa@apple.com>
Motivation:
Swift 6.2 has been released, we should add it to our CI coverage.
Modifications:
Add additional Swift 6.2 jobs where appropriate in main.yml,
pull_request.yml
Result:
Improved test coverage.
### Motivation:
Currently docs on SPI don't include NIOCertificateReloading.
### Modifications:
Added NIOCertificateReloading to the list of targets to build docs for.
### Result:
Docs on SPI.
### Motivation:
Some errors do not show up in debug builds. Enabling release mode builds
improves the CI coverage.
### Modifications:
Enable release mode builds for pull requests and scheduled builds on
main.
### Result:
Improved CI coverage.
Callback for certificate reload failures
### Motivation:
Users might want to emit a metric or log when rotation fails.
### Modifications:
Add a new callback which is called whenever a reload fails. This
callback is given an instance of CertificateLoadFailure which contains
the error. The CertificateLoadFailure type allows us to evolve this in
future
In #281 we added a Configuration object. Now it's possible to create a
TimedCertificateReloader with a single configuration parameter. The
makeReloaderValidatingSources function should be updated to also take
this parameter
Add callback in TimedCertificateReloader to listen for reloads
### Motivation:
Adopter might want to know when a certificate was reloaded, e.g. to emit
a log or metric
### Modifications:
- Add a new callback in the reloader which is called whenever a reload
happens. The closure is passed a struct which tells us which cert and
key were read, and whether or not theyre 'new'
- Change the initializer to take a config struct to make this more
extensible going forward
Motivation:
`RequestResponseHandler` and `RequestResponseWithIDHandler` previously
allowed non-`Sendable` Requests to move across event loops by wrapping
it in `UnsafeTransfer`, which disabled the compiler's sendability
checking. Requiring every Response to be `Sendable` would solve the
safety issue but it limits how the handlers can be used.
Modifications:
* Extracted the common logic of both handlers into a single state
machine.
* Modified the original implementations to have Request conform to
`Sendable`
* Introduced two new variants that don't care if the Request is
`Sendable` as promises moving to different event loops would be failed
with an error.
* Removed the UnsafeTransfer type as it is not used anymore.
* Added unit tests to confirm that the Sendable variants allow for
promises for different event loops and non-Sendable variants fail the
promises with an Error.
Result:
* Compiler's sendability checks are restored for the handlers.
---------
Co-authored-by: George Barnett <gbarnett@apple.com>
### Motivation:
The SimpleResponsivenessRequestMux knows how to handle a few different
requests. For those it doesn’t know how to handle, it responds with a
404.
It would be helpful if the handler had a mode where it optionally
forwards requests it doesn’t know how to handle to a fallback channel
handler so that users can respond to these requests.
### Modifications:
The mux has a new initializer that can configure it to accept unexpected
requests, i.e., those not defined by itself. In this case it will
forward requests down the pipeline. However, its default behavior
remains the same: respond with a 404.
### Result:
When setting up the mux, users can add additional handler to the
pipeline and configure the mux to forward its requests.
…eing loaded
Fix certificate check and make sure all code paths throw an error.
### Motivation:
TimedCertificateReloader currently has an invalid check that
accidecanlly enforces self-signed certificates. Additionally some code
paths of `reload()` did not throw an error which made it hard for the
call site to judge if the reloader was initialized successfully or not.
Further, the missing errors also meant that the `run()` method would
never be able to log these.
### Modifications:
* Updated the certificate check to validate the the public key of the
private key matches the cert's public key (instead of checking the
signature).
* Added three new error cases.
### Result:
* A caller can now be sure that their certificate and private key pair
are valid after calling `reload()` the first time.
* Errors in future reloads will be surfaced using the provided logger
and do not go unnoticed.
* You can actually use the reloader with normal certificates.
## Motivation
The new `TimedCertificateReloader` currently only supports loading PEM
files with a single cert. It is often the case that a single PEM file
contains an entire certificate chain.
## Modification
Modifies the code to support loading an entire certificate chain from a
single PEM file.
## Result
PEM based certificate chain support in our new
`TimedCertificateReloader`
This PR adds a `CertificateReloader` protocol and a
`TimedCertificateReloader` implementation.
A `CertificateReloader` allows for certificate-key pairs to be observed
and updated. A `TimedCertificateReloader` does this every set interval
of time.
This PR also provides an extension on `TLSConfiguration` to enable this
reloading ability in NIO applications, so that updated certificates and
keys can be used during SSL handshakes; as well as conformance of the
`TimedCertificateReloader` to `swift-service-lifecycle`'s `Service`
protocol, for easy composition.
Motivation:
Swift 5.9 is no longer supported, we should bump the tools version and
remove it from our CI.
Modifications:
* Bump the Swift tools version to Swift 5.10
* Remove Swift 5.9 jobs where appropriate in main.yml, pull_request.yml
Result:
Code reflects our support window.
Motivation:
Swift 6.1 has been released, we should add it to our CI coverage.
Modifications:
Add additional Swift 6.1 jobs where appropriate in main.yml,
pull_request.yml
Result:
Improved test coverage.