mirror of
https://github.com/apple/swift-http-types
synced 2026-11-22 04:30:40 +00:00
### 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.
66 lines
2.4 KiB
Swift
66 lines
2.4 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// 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)
|
|
}
|
|
}
|