Regression fix: HTTPField.Name.init(parsed:) rejects uppercased names again (#155)

### Motivation

#144 introduced a regression in `HTTPField.Name(parsed:)`.

HTTP/2 and HTTP/3 require field names to be lowercased on the wire (RFC
9113 § 8.2.1, RFC 9114 § 4.1.2). Previously we enforced this by only
accepting lowercase ASCII letters.

In #144 we replaced a specific scan with `HTTPField.isValidToken`, which
is shared in multiple token validation places and returns `valid` for
uppercased names. We checked for `isValid`, which is true for `valid` as
well as `canonical` representations. Allowing `valid` and not just
`canonical` representations is the regression.

This lead to this unwanted behavior:
- `HTTPField.Name(parsed: "Content-Type")` now succeeds, so a decoder
can no longer detect the malformed message it has to reject.
- The returned name violates the documented invariant that
`canonicalName` is lowercased, so it compares and hashes unequal to
`HTTPField.Name("Content-Type")`, which does canonicalise.

This path had no test coverage, so neither regression was caught.

### Changes

Require `.canonical` rather than merely valid token validity in
`init(parsed:)`, restoring the pre-#144 behavior, and document why. Add
`HTTPFieldNameTests` covering both initializers: valid names, pseudo
header fields, invalid characters, the mixed-case canonicalisation that
`init(_:)` performs, and the uppercase rejection that `init(parsed:)`
performs.

### Result

`init(parsed:)` rejects uppercased names again and `canonicalName` is
lowercased for every name it returns.
This commit is contained in:
Fabian Fett authored and GitHub committed 2026-09-07 09:56:43 +02:00
1 parent 46573d5db7
commit bff4b6903c
3 files changed
+83 -4

No files matched your search

+1 -1
View File
@@ -1,7 +1,7 @@
.DS_Store
.swift-version
/.build
.build
/Packages
.xcode
/*.xcodeproj
+17 -3
View File
@@ -54,6 +54,15 @@ extension HTTPField {
/// Create an HTTP field name from a string produced by HPACK or QPACK decoders used in
/// modern HTTP versions.
///
/// Returns nil if the name contains invalid characters defined in RFC 9110, or if it is not
/// already lowercased. Names arriving over HTTP/2 or HTTP/3 must be lowercased on the wire,
/// and a message carrying an uppercase field name has to be treated as malformed, so this
/// initializer rejects rather than canonicalises such a name. Use `init?(_:)` for names from
/// sources which are allowed to use mixed case.
///
/// https://www.rfc-editor.org/rfc/rfc9113.html#name-field-validity
/// https://www.rfc-editor.org/rfc/rfc9114.html#name-field-formatting-and-compre
///
/// - Warning: Do not use directly with the `HTTPFields` struct which does not allow pseudo
/// header fields.
///
@@ -68,11 +77,16 @@ extension HTTPField {
} else {
token = Substring(name)
}
guard HTTPField.isValidToken(token) else {
switch HTTPField.tokenValidity(token) {
case .canonical:
self.rawName = name
self.canonicalName = name
case .invalid, .valid:
// valid also allows uppercased characters
return nil
}
self.rawName = name
self.canonicalName = name
}
@inlinable
@@ -0,0 +1,65 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2026 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import HTTPTypes
import Testing
@Suite struct HTTPFieldNameTests {
@Test func initRejectsInvalidCharacters() {
#expect(HTTPField.Name("") == nil)
#expect(HTTPField.Name("content type") == nil)
#expect(HTTPField.Name("content:type") == nil)
#expect(HTTPField.Name("cöntent-type") == nil)
// `init(_:)` does not know about pseudo header fields, so the colon is just an illegal character.
#expect(HTTPField.Name(":method") == nil)
}
@Test func initCanonicalisesMixedCase() {
let name = HTTPField.Name("Content-Type")
#expect(name?.rawName == "Content-Type")
#expect(name?.canonicalName == "content-type")
#expect(name == HTTPField.Name("content-type"))
}
@Test func parsedAcceptsLowercasedNames() {
let name = HTTPField.Name(parsed: "content-type")
#expect(name?.rawName == "content-type")
#expect(name?.canonicalName == "content-type")
#expect(name == HTTPField.Name("content-type"))
// The symbols RFC 9110 allows in a token, plus digits.
#expect(HTTPField.Name(parsed: "!#$%&'*+-.^_`|~09az") != nil)
}
@Test func parsedAcceptsPseudoHeaderFields() {
let name = HTTPField.Name(parsed: ":method")
#expect(name?.rawName == ":method")
#expect(name?.canonicalName == ":method")
}
@Test func parsedRejectsInvalidCharacters() {
#expect(HTTPField.Name(parsed: "") == nil)
#expect(HTTPField.Name(parsed: ":") == nil)
#expect(HTTPField.Name(parsed: "content type") == nil)
#expect(HTTPField.Name(parsed: "content:type") == nil)
#expect(HTTPField.Name(parsed: "cöntent-type") == nil)
}
@Test func parsedRejectsUppercasedNames() {
#expect(HTTPField.Name(parsed: "A") == nil)
#expect(HTTPField.Name(parsed: "Content-Type") == nil)
#expect(HTTPField.Name(parsed: "content-typE") == nil)
#expect(HTTPField.Name(parsed: ":Method") == nil)
}
}