mirror of
https://github.com/apple/swift-nio-http2
synced 2026-11-22 04:30:43 +00:00
Merge commit from fork
This commit is contained in:
1 parent
81cc18264f
commit
61d1b44f6e
4 files changed
+276
-3
No files matched your search
@@ -280,8 +280,10 @@ extension HeaderFieldName {
|
||||
// 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.
|
||||
guard self.fieldType == .regularHeaderField else {
|
||||
// Pseudo-headers are never connection-specific.
|
||||
if self.fieldType == .pseudoHeaderField {
|
||||
if !HPACKHeaders.isValidPseudoHeaderValue(value) {
|
||||
throw NIOHTTP2Errors.invalidPseudoHeaderValue(name: ":\(self.baseName)", value: value)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -432,6 +432,22 @@ public enum NIOHTTP2Errors {
|
||||
ForbiddenHeaderField(name: name, value: value, file: file, line: line)
|
||||
}
|
||||
|
||||
/// Creates a ``InvalidPseudoHeaderValue`` error with appropriate source context.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - name: The pseudo-header field name (e.g. `:path`)
|
||||
/// - value: The invalid pseudo-header field value
|
||||
/// - file: Source file of the caller.
|
||||
/// - line: Source line number of the caller.
|
||||
public static func invalidPseudoHeaderValue(
|
||||
name: String,
|
||||
value: String,
|
||||
file: String = #fileID,
|
||||
line: UInt = #line
|
||||
) -> InvalidPseudoHeaderValue {
|
||||
InvalidPseudoHeaderValue(name: name, value: value, file: file, line: line)
|
||||
}
|
||||
|
||||
/// Creates a ``ContentLengthViolated`` error with appropriate source context.
|
||||
///
|
||||
/// - Parameters:
|
||||
@@ -1732,7 +1748,79 @@ public enum NIOHTTP2Errors {
|
||||
}
|
||||
}
|
||||
|
||||
/// A request or response has violated the expected content length, either exceeding or falling beneath it.
|
||||
/// A pseudo-header field value contains control characters (CR, LF, or NUL) that could enable
|
||||
/// request smuggling when translated to HTTP/1.1.
|
||||
public struct InvalidPseudoHeaderValue: NIOHTTP2Error, @unchecked Sendable {
|
||||
// @unchecked Sendable because access is controlled by getters and copy-on-write setters giving this value semantics
|
||||
|
||||
private var storage: Storage
|
||||
|
||||
private mutating func copyStorageIfNotUniquelyReferenced() {
|
||||
if !isKnownUniquelyReferenced(&self.storage) {
|
||||
self.storage = self.storage.copy()
|
||||
}
|
||||
}
|
||||
|
||||
private final class Storage: Equatable {
|
||||
var name: String
|
||||
var value: String
|
||||
var file: String
|
||||
var line: UInt
|
||||
|
||||
var location: String {
|
||||
_location(file: self.file, line: self.line)
|
||||
}
|
||||
|
||||
init(name: String, value: String, file: String, line: UInt) {
|
||||
self.name = name
|
||||
self.value = value
|
||||
self.file = file
|
||||
self.line = line
|
||||
}
|
||||
|
||||
func copy() -> Storage {
|
||||
Storage(name: self.name, value: self.value, file: self.file, line: self.line)
|
||||
}
|
||||
|
||||
static func == (lhs: Storage, rhs: Storage) -> Bool {
|
||||
lhs.name == rhs.name && lhs.value == rhs.value
|
||||
}
|
||||
}
|
||||
|
||||
/// The name of the pseudo-header field.
|
||||
public var name: String {
|
||||
get {
|
||||
self.storage.name
|
||||
}
|
||||
set {
|
||||
self.copyStorageIfNotUniquelyReferenced()
|
||||
self.storage.name = newValue
|
||||
}
|
||||
}
|
||||
|
||||
/// The invalid value of the pseudo-header field.
|
||||
public var value: String {
|
||||
get {
|
||||
self.storage.value
|
||||
}
|
||||
set {
|
||||
self.copyStorageIfNotUniquelyReferenced()
|
||||
self.storage.value = newValue
|
||||
}
|
||||
}
|
||||
|
||||
/// The file and line where the error was created.
|
||||
public var location: String {
|
||||
get {
|
||||
self.storage.location
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate init(name: String, value: String, file: String, line: UInt) {
|
||||
self.storage = Storage(name: name, value: value, file: file, line: line)
|
||||
}
|
||||
}
|
||||
|
||||
public struct ContentLengthViolated: NIOHTTP2Error, InvalidContentLengthError {
|
||||
private let file: String
|
||||
private let line: UInt
|
||||
|
||||
@@ -783,11 +783,21 @@ extension HPACKHeaders {
|
||||
}
|
||||
|
||||
if let headerValue = headerValue {
|
||||
if !Self.isValidPseudoHeaderValue(headerValue) {
|
||||
throw NIOHTTP2Errors.invalidPseudoHeaderValue(name: name, value: headerValue)
|
||||
}
|
||||
return headerValue
|
||||
} else {
|
||||
throw NIOHTTP2Errors.missingPseudoHeader(name)
|
||||
}
|
||||
}
|
||||
|
||||
static func isValidPseudoHeaderValue(_ value: String) -> Bool {
|
||||
// Pseudo-header values must not contain CR, LF, or NUL bytes. These characters could
|
||||
// enable HTTP/2-to-HTTP/1.1 request smuggling when the value is placed into an HTTP/1.1
|
||||
// message (e.g. :path becomes the request-target).
|
||||
!value.utf8.contains(where: { $0 == 0x0A || $0 == 0x0D || $0 == 0x00 })
|
||||
}
|
||||
}
|
||||
|
||||
extension HTTPHeaders {
|
||||
|
||||
@@ -0,0 +1,173 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2026 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 NIOCore
|
||||
import NIOEmbedded
|
||||
import NIOHPACK
|
||||
import NIOHTTP1
|
||||
import Testing
|
||||
|
||||
@testable import NIOHTTP2
|
||||
|
||||
struct HTTP2FramePayloadToHTTP1CodecCRLFTests {
|
||||
|
||||
// MARK: - Validation tests: pseudo-header values with control characters
|
||||
|
||||
struct PseudoHeaderInjection {
|
||||
var pseudoHeaderName: String
|
||||
var maliciousValue: String
|
||||
var label: String
|
||||
}
|
||||
|
||||
static let requestPseudoHeaderInjections: [PseudoHeaderInjection] = [
|
||||
.init(pseudoHeaderName: ":path", maliciousValue: "/legit\r\nInjected: evil", label: "CRLF"),
|
||||
.init(pseudoHeaderName: ":path", maliciousValue: "/legit\rInjected", label: "lone CR"),
|
||||
.init(pseudoHeaderName: ":path", maliciousValue: "/legit\nInjected", label: "lone LF"),
|
||||
.init(pseudoHeaderName: ":path", maliciousValue: "/legit\0Injected", label: "NUL"),
|
||||
.init(
|
||||
pseudoHeaderName: ":path",
|
||||
maliciousValue: "/legit\r\nInjected: evil\r\n\r\nGET /admin HTTP/1.1\r\nHost: internal-backend",
|
||||
label: "smuggling payload"
|
||||
),
|
||||
.init(pseudoHeaderName: ":authority", maliciousValue: "example.com\r\nInjected: evil", label: "CRLF"),
|
||||
.init(pseudoHeaderName: ":scheme", maliciousValue: "https\r\nInjected: evil", label: "CRLF"),
|
||||
.init(pseudoHeaderName: ":method", maliciousValue: "GET\r\nInjected: evil", label: "CRLF"),
|
||||
]
|
||||
|
||||
@Test(
|
||||
"invalid pseudo-headers are rejected",
|
||||
arguments: requestPseudoHeaderInjections.map(\.maliciousValue)
|
||||
) func invalidPseudoHeaderValue(value: String) {
|
||||
#expect(!HPACKHeaders.isValidPseudoHeaderValue(value))
|
||||
}
|
||||
|
||||
@Test(
|
||||
"invalid pseudo-headers are accepted",
|
||||
arguments: Self.validPaths + ["200"]
|
||||
) func validPseudoHeaderValue(value: String) {
|
||||
#expect(HPACKHeaders.isValidPseudoHeaderValue(value))
|
||||
}
|
||||
|
||||
@Test(
|
||||
"Request validation rejects control characters in pseudo-headers",
|
||||
arguments: requestPseudoHeaderInjections
|
||||
)
|
||||
func requestValidationRejectsControlCharacters(injection: PseudoHeaderInjection) {
|
||||
let headers = HPACKHeaders([
|
||||
(":method", injection.pseudoHeaderName == ":method" ? injection.maliciousValue : "GET"),
|
||||
(":path", injection.pseudoHeaderName == ":path" ? injection.maliciousValue : "/"),
|
||||
(":scheme", injection.pseudoHeaderName == ":scheme" ? injection.maliciousValue : "https"),
|
||||
(":authority", injection.pseudoHeaderName == ":authority" ? injection.maliciousValue : "example.com"),
|
||||
])
|
||||
let error = #expect(throws: NIOHTTP2Errors.InvalidPseudoHeaderValue.self) {
|
||||
try headers.validateRequestBlock(supportsExtendedConnect: false)
|
||||
}
|
||||
#expect(error?.name == injection.pseudoHeaderName && error?.value == injection.maliciousValue)
|
||||
}
|
||||
|
||||
@Test("Response validation rejects control characters in :status")
|
||||
func responseValidationRejectsCRLFInStatus() {
|
||||
let maliciousStatus = "200\r\nInjected: evil"
|
||||
let headers = HPACKHeaders([
|
||||
(":status", maliciousStatus),
|
||||
])
|
||||
#expect {
|
||||
try headers.validateResponseBlock()
|
||||
} throws: { error in
|
||||
let typedError = error as? NIOHTTP2Errors.InvalidPseudoHeaderValue
|
||||
return typedError?.name == ":status" && typedError?.value == maliciousStatus
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Validation tests: valid paths pass
|
||||
|
||||
static let validPaths = ["/", "/foo/bar", "/foo?q=1&r=2", "/foo#fragment", "*", "/foo%20bar"]
|
||||
|
||||
@Test("Normal paths pass validation", arguments: validPaths)
|
||||
func normalPathsPassValidation(path: String) throws {
|
||||
let headers = HPACKHeaders([
|
||||
(":method", "GET"),
|
||||
(":path", path),
|
||||
(":scheme", "https"),
|
||||
(":authority", "example.com"),
|
||||
])
|
||||
#expect(throws: Never.self) {
|
||||
try headers.validateRequestBlock(supportsExtendedConnect: false)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Server codec tests: rejects control characters in pseudo-headers
|
||||
|
||||
@Test(
|
||||
"Server codec rejects control characters in pseudo-headers",
|
||||
arguments: requestPseudoHeaderInjections
|
||||
)
|
||||
func serverCodecRejectsControlCharacters(injection: PseudoHeaderInjection) throws {
|
||||
let channel = EmbeddedChannel()
|
||||
try channel.pipeline.syncOperations.addHandler(HTTP2FramePayloadToHTTP1ServerCodec())
|
||||
|
||||
let requestHeaders = HPACKHeaders([
|
||||
(":method", injection.pseudoHeaderName == ":method" ? injection.maliciousValue : "GET"),
|
||||
(":path", injection.pseudoHeaderName == ":path" ? injection.maliciousValue : "/"),
|
||||
(":scheme", injection.pseudoHeaderName == ":scheme" ? injection.maliciousValue : "https"),
|
||||
(":authority", injection.pseudoHeaderName == ":authority" ? injection.maliciousValue : "example.com"),
|
||||
])
|
||||
let error = #expect(throws: NIOHTTP2Errors.InvalidPseudoHeaderValue.self) {
|
||||
try channel.writeInbound(
|
||||
HTTP2Frame.FramePayload.headers(.init(headers: requestHeaders, endStream: true))
|
||||
)
|
||||
}
|
||||
#expect(error?.name == injection.pseudoHeaderName && error?.value == injection.maliciousValue)
|
||||
}
|
||||
|
||||
// MARK: - Client codec tests: rejects control characters in :status
|
||||
|
||||
static let maliciousStatusValues: [(value: String, label: String)] = [
|
||||
("200\r\nInjected: evil", "CRLF"),
|
||||
("200\nInjected: evil", "lone LF"),
|
||||
("200\0evil", "NUL"),
|
||||
]
|
||||
|
||||
@Test(
|
||||
"Client codec rejects control characters in response :status",
|
||||
arguments: maliciousStatusValues
|
||||
)
|
||||
func clientCodecRejectsControlCharactersInStatus(
|
||||
maliciousStatus: (value: String, label: String)
|
||||
) throws {
|
||||
let handler = HTTP2FramePayloadToHTTP1ClientCodec(httpProtocol: .https)
|
||||
let channel = EmbeddedChannel(handlers: [handler])
|
||||
|
||||
// Send a valid request first so the codec is in a state to receive a response.
|
||||
let http1Head = HTTPRequestHead(
|
||||
version: .http1_1,
|
||||
method: .GET,
|
||||
uri: "/",
|
||||
headers: ["host": "example.org"]
|
||||
)
|
||||
channel.write(HTTPClientRequestPart.head(http1Head), promise: nil)
|
||||
channel.write(HTTPClientRequestPart.end(nil), promise: nil)
|
||||
channel.flush()
|
||||
|
||||
let maliciousResponseHeaders = HPACKHeaders([
|
||||
(":status", maliciousStatus.value),
|
||||
])
|
||||
let error = #expect(throws: NIOHTTP2Errors.InvalidPseudoHeaderValue.self) {
|
||||
try channel.writeInbound(
|
||||
HTTP2Frame.FramePayload.headers(.init(headers: maliciousResponseHeaders, endStream: true))
|
||||
)
|
||||
}
|
||||
#expect(error?.name == ":status" && error?.value == maliciousStatus.value)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user