From bff4b6903cdc99dda49649dd52f46c11cfd3ed50 Mon Sep 17 00:00:00 2001 From: Fabian Fett Date: Mon, 7 Sep 2026 09:56:43 +0200 Subject: [PATCH] Regression fix: `HTTPField.Name.init(parsed:)` rejects uppercased names again (#155) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### 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. --- .gitignore | 2 +- Sources/HTTPTypes/HTTPFieldName.swift | 20 +++++- Tests/HTTPTypesTests/HTTPFieldNameTests.swift | 65 +++++++++++++++++++ 3 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 Tests/HTTPTypesTests/HTTPFieldNameTests.swift diff --git a/.gitignore b/.gitignore index a6b3e03..9534807 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ .DS_Store .swift-version -/.build +.build /Packages .xcode /*.xcodeproj diff --git a/Sources/HTTPTypes/HTTPFieldName.swift b/Sources/HTTPTypes/HTTPFieldName.swift index cb73532..9f0f1d8 100644 --- a/Sources/HTTPTypes/HTTPFieldName.swift +++ b/Sources/HTTPTypes/HTTPFieldName.swift @@ -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 diff --git a/Tests/HTTPTypesTests/HTTPFieldNameTests.swift b/Tests/HTTPTypesTests/HTTPFieldNameTests.swift new file mode 100644 index 0000000..67f88f0 --- /dev/null +++ b/Tests/HTTPTypesTests/HTTPFieldNameTests.swift @@ -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) + } +}