Files
swift-nio-http2/Sources/NIOHTTP2/HPACKHeaders+Validation.swift
Eirik Botten Nicolaysen bea911a3de Reject the empty HTTP/2 field name (#563)
`isValidFieldName` validates a field name with `allSatisfy` over the
tchar set. `allSatisfy` returns `true` for an empty collection, so the
empty field name passes.

The file already states the rule it is implementing. The doc comment at
line 312 quotes:

```
token          = 1*tchar
```

The derivation that follows is correct — I expanded `0x21, 0x23...0x27,
0x2a, 0x2b, 0x2d, 0x2e, 0x30...0x39, 0x5e...0x7a, 0x7c, 0x7e` byte by
byte against tchar minus `A-Z` and every one of the four rewriting steps
in lines 315-341 holds, including the `%x5E-60 / %x61-7A` → `%x5E-7A`
collapse. It is the `1*` quantifier that is missing: the code implements
`*tchar`.

### Scope

All three block types accept an empty name today, since they share
`HeaderFieldName.init`:

```
request:  empty name -> accepted
response: empty name -> accepted
trailers: empty name -> accepted
```

An empty name serialises as `: value`, and RFC 9113 § 8.2.1 lists field
names as something a recipient is required to validate and treat as
malformed when the generic syntax is violated. This is a conformance gap
in a validator that already does the rest of that section — the
uppercase check, the CR/LF/NUL value check and the connection-specific
header check are all there.

(`":"` alone is already rejected, though as `unknownPseudoHeader` rather
than a name error, since `dropFirst()` leaves an empty base name that
matches no known pseudo-header. That path is unchanged here.)

### The change

An explicit early return, mirroring the shape swift-http-types uses for
the same production in `tokenValidity`:

```swift
if buffer.isEmpty {
    return .invalid
}
```

### Tests

Added to `HTTP2FramePayloadToHTTP1CodecCRLFTests.swift`, which already
exercises `validateRequestBlock`, `validateResponseBlock` and
`validateTrailersBlock` directly for the § 8.2.1 value rules. Five
cases: the empty name in each of the three block types, plus `"a b"` and
an ordinary `x-custom-header` to keep the surrounding behaviour pinned.

### Verification

**I could not run `swift test` here**: this machine has Command Line
Tools without XCTest, so the test targets fail at `import XCTest` with
`no such module`. Pre-existing and unrelated — it reproduces on an
unmodified checkout (18 occurrences). **CI should be the authority;
treat my local run as unverified.**

`swift build` succeeds. I linked a throwaway executable against
`NIOHTTP2` to exercise the change directly:

```
PASS request:  empty name       -> rejected
PASS response: empty name       -> rejected
PASS trailers: empty name       -> rejected
PASS request:  "a b"            -> rejected
PASS request:  "Host" (uppercase) -> rejected
PASS request:  valid name       -> accepted
PASS request:  pseudo-headers only -> accepted
```

Before the change the first three were accepted.

### Conflicts

No open PR touches `HPACKHeaders+Validation.swift`. #531 and #562 touch
NIOHPACK, and #531 (`+ Fix a bug`) is about `isASCII` in
`HPACKHeader.swift` — no overlap with `isValidFieldName`.
2026-09-01 05:16:30 +00:00

438 lines
18 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2019 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIOHPACK
extension HPACKHeaders {
/// Checks that a given HPACKHeaders block is a valid request header block, meeting all of the constraints of RFC 7540.
///
/// If the header block is not valid, throws an error.
internal func validateRequestBlock(supportsExtendedConnect: Bool) throws {
try RequestBlockValidator.validateBlock(self, supportsExtendedConnect: supportsExtendedConnect)
}
/// Checks that a given HPACKHeaders block is a valid response header block, meeting all of the constraints of RFC 7540.
///
/// If the header block is not valid, throws an error.
internal func validateResponseBlock() throws {
try ResponseBlockValidator.validateBlock(self)
}
/// Checks that a given HPACKHeaders block is a valid trailer block, meeting all of the constraints of RFC 7540.
///
/// If the header block is not valid, throws an error.
internal func validateTrailersBlock() throws {
try TrailersValidator.validateBlock(self)
}
}
/// A HTTP/2 header block is divided into two sections: the leading section, containing pseudo-headers, and
/// the regular header section. Once the first regular header has been seen and we have transitioned into the
/// header section, it is an error to see a pseudo-header again in this block.
private enum BlockSection {
case pseudoHeaders
case headers
fileprivate mutating func validField(_ field: HeaderFieldName) throws {
switch (self, field.fieldType) {
case (.pseudoHeaders, .pseudoHeaderField),
(.headers, .regularHeaderField):
// Another header of the same type we're expecting. Do nothing.
break
case (.pseudoHeaders, .regularHeaderField):
// The regular header fields have begun.
self = .headers
case (.headers, .pseudoHeaderField):
// This is an error: it's not allowed to send a pseudo-header field once a regular
// header field has been sent.
throw NIOHTTP2Errors.pseudoHeaderAfterRegularHeader(":\(field.baseName)")
}
}
}
/// A `HeaderBlockValidator` is an object that can confirm that a HPACK block meets certain constraints.
private protocol HeaderBlockValidator {
init()
var allowedPseudoHeaderFields: PseudoHeaders { get }
var mandatoryPseudoHeaderFields: PseudoHeaders { get }
mutating func validateNextField(name: HeaderFieldName, value: String, pseudoHeaderType: PseudoHeaders?) throws
}
extension HeaderBlockValidator {
/// Validates that a header block meets the requirements of this `HeaderBlockValidator`.
fileprivate static func validateBlock(_ block: HPACKHeaders, supportsExtendedConnect: Bool = false) throws {
var validator = Self()
var blockSection = BlockSection.pseudoHeaders
var seenPseudoHeaders = PseudoHeaders(rawValue: 0)
for (name, value, _) in block {
let fieldName = try HeaderFieldName(name)
try blockSection.validField(fieldName)
try fieldName.legalHeaderField(value: value)
let thisPseudoHeaderFieldType = try seenPseudoHeaders.seenNewHeaderField(
fieldName,
supportsExtendedConnect: supportsExtendedConnect
)
try validator.validateNextField(name: fieldName, value: value, pseudoHeaderType: thisPseudoHeaderFieldType)
}
// We must only have seen pseudo-header fields allowed on this type of header block,
// and at least the mandatory set.
guard
validator.allowedPseudoHeaderFields.isSuperset(of: seenPseudoHeaders)
&& validator.mandatoryPseudoHeaderFields.isSubset(of: seenPseudoHeaders)
else {
throw NIOHTTP2Errors.invalidPseudoHeaders(block)
}
}
}
/// An object that can be used to validate if a given header block is a valid request header block.
private struct RequestBlockValidator {
private var isConnectRequest: Bool = false
private var containsProtocolPseudoHeader: Bool = false
}
extension RequestBlockValidator: HeaderBlockValidator {
fileprivate mutating func validateNextField(
name: HeaderFieldName,
value: String,
pseudoHeaderType: PseudoHeaders?
) throws {
// We have a wrinkle here: the set of allowed and mandatory pseudo headers for requests depends on whether this request is a CONNECT request.
// If it isn't, RFC 7540 § 8.1.2.3 rules, and says that:
//
// > All HTTP/2 requests MUST include exactly one valid value for the ":method", ":scheme", and ":path" pseudo-header fields
//
// Unfortunately, it also has an extra clause that says "unless it is a CONNECT request". That clause makes RFC 7540 § 8.3 relevant, which
// says:
//
// > The ":scheme" and ":path" pseudo-header fields MUST be omitted.
//
// Implicitly, the :authority pseudo-header field must be present here as well, as § 8.3 imposes a specific form on that header field which
// cannot make much sense if the field is optional.
//
// This is further complicated by RFC 8441 (Bootstrapping WebSockets with HTTP/2) which defines the "extended" CONNECT method. RFC 8441 § 4
// says:
//
// > A new pseudo-header field :protocol MAY be included on request HEADERS indicating the desired protocol to be spoken on the tunnel
// > created by CONNECT.
//
// > On requests that contain the :protocol pseudo-header field, the :scheme and :path pseudo-header fields of the target URI
// > MUST also be included.
//
// > On requests bearing the :protocol pseudo-header field, the :authority pseudo-header field is interpreted according to
// > Section 8.1.2.3 of [RFC7540] instead of Section 8.3 of that document.
//
// We can summarise these rules loosely by saying that:
//
// - On non-CONNECT requests or CONNECT requests with the :protocol pseudo-header, :method, :scheme, and :path are mandatory, :authority is allowed.
// - On CONNECT requests without the :protocol pseudo-header, :method and :authority are mandatory, no others are allowed.
//
// This is a bit awkward.
if let pseudoHeaderType = pseudoHeaderType {
assert(name.fieldType == .pseudoHeaderField)
switch pseudoHeaderType {
case .method:
// This is a method pseudo-header. Check if the value is CONNECT.
self.isConnectRequest = value == "CONNECT"
case .extConnectProtocol:
self.containsProtocolPseudoHeader = true
case .path:
// This is a path pseudo-header. It must not be empty.
if value.utf8.count == 0 {
throw NIOHTTP2Errors.emptyPathHeader()
}
default:
break
}
} else {
assert(name.fieldType == .regularHeaderField)
// We want to check that if the TE header field is present, it only contains "trailers".
if name.baseName == "te" && value != "trailers" {
throw NIOHTTP2Errors.forbiddenHeaderField(name: String(name.baseName), value: value)
}
}
}
var allowedPseudoHeaderFields: PseudoHeaders {
// For the logic behind this if statement, see the comment in validateNextField.
if self.isConnectRequest {
if self.containsProtocolPseudoHeader {
return .allowedExtendedConnectRequestHeaders
} else {
return .allowedConnectRequestHeaders
}
} else {
return .allowedRequestHeaders
}
}
var mandatoryPseudoHeaderFields: PseudoHeaders {
// For the logic behind this if statement, see the comment in validateNextField.
if self.isConnectRequest && !self.containsProtocolPseudoHeader {
return .mandatoryConnectRequestHeaders
} else {
return .mandatoryRequestHeaders
}
}
}
/// An object that can be used to validate if a given header block is a valid response header block.
private struct ResponseBlockValidator {
let allowedPseudoHeaderFields: PseudoHeaders = .allowedResponseHeaders
let mandatoryPseudoHeaderFields: PseudoHeaders = .mandatoryResponseHeaders
}
extension ResponseBlockValidator: HeaderBlockValidator {
fileprivate mutating func validateNextField(
name: HeaderFieldName,
value: String,
pseudoHeaderType: PseudoHeaders?
) throws {
return
}
}
/// An object that can be used to validate if a given header block is a valid trailer block.
private struct TrailersValidator {
let allowedPseudoHeaderFields: PseudoHeaders = []
let mandatoryPseudoHeaderFields: PseudoHeaders = []
}
extension TrailersValidator: HeaderBlockValidator {
fileprivate mutating func validateNextField(
name: HeaderFieldName,
value: String,
pseudoHeaderType: PseudoHeaders?
) throws {
return
}
}
/// A structure that carries the details of a specific header field name.
///
/// Used to validate the correctness of a specific header field name at a given
/// point in a header block.
private struct HeaderFieldName {
/// The type of this header-field: pseudo-header or regular.
fileprivate var fieldType: FieldType
/// The base name of this header field, which is the name with any leading colon stripped off.
fileprivate var baseName: Substring
}
extension HeaderFieldName {
/// The types of header fields in HTTP/2.
enum FieldType {
case pseudoHeaderField
case regularHeaderField
}
}
extension HeaderFieldName {
fileprivate init(_ fieldName: String) throws {
let fieldSubstring = Substring(fieldName)
let fieldBytes = fieldSubstring.utf8
let baseNameBytes: Substring.UTF8View
if fieldBytes.first == UInt8(ascii: ":") {
baseNameBytes = fieldBytes.dropFirst()
self.fieldType = .pseudoHeaderField
self.baseName = fieldSubstring.dropFirst()
} else {
baseNameBytes = fieldBytes
self.fieldType = .regularHeaderField
self.baseName = fieldSubstring
}
guard baseNameBytes.isValidFieldName else {
throw NIOHTTP2Errors.invalidHTTP2HeaderFieldName(fieldName)
}
}
func legalHeaderField(value: String) throws {
// RFC 9113 § 8.2.2 (which obsoletes RFC 7540 § 8.1.2.2) forbids all connection-specific header fields, and
// enumerates them explicitly: "Connection, Proxy-Connection, Keep-Alive, Transfer-Encoding, and Upgrade". A
// connection-specific header field technically is one that is listed in the Connection header, but the fields
// named above could also appear independently, so we forbid them all by name.
//
// There is one more wrinkle, which is that the client is allowed to send TE: trailers, and forbidden from sending TE
// with anything else. We police that separately, as TE is only defined on requests, so we can avoid checking for it
// on responses and trailers.
if self.fieldType == .pseudoHeaderField {
if !HPACKHeaders.isValidPseudoHeaderValue(value) {
throw NIOHTTP2Errors.invalidPseudoHeaderValue(name: ":\(self.baseName)", value: value)
}
return
}
switch self.baseName {
case "connection", "transfer-encoding", "proxy-connection", "keep-alive", "upgrade":
throw NIOHTTP2Errors.forbiddenHeaderField(name: String(self.baseName), value: value)
default:
// RFC 9113 § 8.2.1 forbids CR, LF, and NUL at any position in *any* field value,
// not just in pseudo-header values, and requires that a message carrying one be
// treated as malformed. HPACK field values are length-prefixed octet strings, so
// nothing in the decoder rejects these bytes for us: this is the only place on the
// inbound path where a regular field value is checked.
guard HPACKHeaders.isValidFieldValue(value) else {
throw NIOHTTP2Errors.invalidHTTP2HeaderFieldValue(name: String(self.baseName), value: value)
}
return
}
}
}
extension Substring.UTF8View {
/// Whether this is a valid HTTP/2 header field name.
fileprivate var isValidFieldName: Bool {
/// RFC 7230 defines header field names as matching the `token` ABNF, which is:
///
/// token = 1*tchar
///
/// tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*"
/// / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
/// / DIGIT / ALPHA
/// ; any VCHAR, except delimiters
///
/// DIGIT = %x30-39
/// ; 0-9
///
/// ALPHA = %x41-5A / %x61-7A ; A-Z / a-z
///
/// RFC 7540 subsequently clarifies that HTTP/2 headers must be converted to lowercase before
/// sending. This therefore excludes the range A-Z in ALPHA. If we convert tchar to the range syntax
/// used in DIGIT and ALPHA, and then collapse the ranges that are more than two elements long, we get:
///
/// tchar = %x21 / %x23-27 / %x2A / %x2B / %x2D / %x2E / %x5E-60 / %x7C / %x7E / %x30-39 /
/// / %x41-5A / %x61-7A
///
/// Now we can strip out the uppercase characters, and shuffle these so they're in ascending order:
///
/// tchar = %x21 / %x23-27 / %x2A / %x2B / %x2D / %x2E / %x30-39 / %x5E-60 / %x61-7A
/// / %x7C / %x7E
///
/// Then we can also spot that we have a pair of ranges that bump into each other and do one further level
/// of collapsing.
///
/// tchar = %x21 / %x23-27 / %x2A / %x2B / %x2D / %x2E / %x30-39 / %x5E-7A
/// / %x7C / %x7E
///
/// We can then translate this into a straightforward switch statement to check whether the code
/// units are valid.
///
/// Note the quantifier in `token = 1*tchar`: a field name must contain at least one `tchar`,
/// so the empty name is not a valid token.
if self.isEmpty {
return false
}
return self.allSatisfy { codeUnit in
switch codeUnit {
case 0x21, 0x23...0x27, 0x2a, 0x2b, 0x2d, 0x2e, 0x30...0x39,
0x5e...0x7a, 0x7c, 0x7e:
return true
default:
return false
}
}
}
}
/// A set of all pseudo-headers defined in HTTP/2.
private struct PseudoHeaders: OptionSet {
var rawValue: UInt8
static let path = PseudoHeaders(rawValue: 1 << 0)
static let method = PseudoHeaders(rawValue: 1 << 1)
static let scheme = PseudoHeaders(rawValue: 1 << 2)
static let authority = PseudoHeaders(rawValue: 1 << 3)
static let status = PseudoHeaders(rawValue: 1 << 4)
static let extConnectProtocol = PseudoHeaders(rawValue: 1 << 5)
static let mandatoryRequestHeaders: PseudoHeaders = [.path, .method, .scheme]
static let allowedRequestHeaders: PseudoHeaders = [.path, .method, .scheme, .authority]
static let allowedExtendedConnectRequestHeaders: PseudoHeaders = [
.path, .method, .scheme, .authority, .extConnectProtocol,
]
static let mandatoryConnectRequestHeaders: PseudoHeaders = [.method, .authority]
static let allowedConnectRequestHeaders: PseudoHeaders = [.method, .authority]
static let mandatoryResponseHeaders: PseudoHeaders = [.status]
static let allowedResponseHeaders: PseudoHeaders = [.status]
}
extension PseudoHeaders {
/// Obtain a PseudoHeaders optionset containing the bit for a known pseudo header. Fails if this is an unknown pseudoheader.
/// Traps if this is not a pseudo-header at all.
init?(headerFieldName name: HeaderFieldName) {
precondition(name.fieldType == .pseudoHeaderField)
switch name.baseName {
case "path":
self = .path
case "method":
self = .method
case "scheme":
self = .scheme
case "authority":
self = .authority
case "status":
self = .status
case "protocol":
self = .extConnectProtocol
default:
return nil
}
}
}
extension PseudoHeaders {
/// Updates this set of PseudoHeaders with any new pseudo headers we've seen. Also returns a PseudoHeaders that marks
/// the type of this specific header field.
mutating func seenNewHeaderField(_ name: HeaderFieldName, supportsExtendedConnect: Bool) throws -> PseudoHeaders? {
// We need to check if this is a pseudo-header field we've seen before and one we recognise.
// We only want to see a pseudo-header field once.
guard name.fieldType == .pseudoHeaderField else {
return nil
}
guard let pseudoHeaderType = PseudoHeaders(headerFieldName: name) else {
throw NIOHTTP2Errors.unknownPseudoHeader(":\(name.baseName)")
}
if pseudoHeaderType == .extConnectProtocol && !supportsExtendedConnect {
throw NIOHTTP2Errors.unsupportedPseudoHeader(":\(name.baseName)")
}
if self.contains(pseudoHeaderType) {
throw NIOHTTP2Errors.duplicatePseudoHeader(":\(name.baseName)")
}
self.formUnion(pseudoHeaderType)
return pseudoHeaderType
}
}