Skip to content

Commit 6b2e29b

Browse files
authored
Merge pull request from GHSA-v3r5-pjpm-mwgq
Motivation Allowing arbitrary data in outbound header field values allows for the possibility that users of AHC will accidentally pass untrusted data into those values. That untrusted data can substantially alter the parsing and content of the HTTP requests, which is extremely dangerous. The result of this is vulnerability to CRLF injection. Modifications Add validation of outbound header field values. Result No longer vulnerable to CRLF injection (cherry picked from commit 3034835a213babfcda19031e80c0b7c9780475e9)
1 parent 03b3e7b commit 6b2e29b

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed

Sources/AsyncHTTPClient/HTTPClient.swift

+5
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,7 @@ public struct HTTPClientError: Error, Equatable, CustomStringConvertible {
10241024
case uncleanShutdown
10251025
case traceRequestWithBody
10261026
case invalidHeaderFieldNames([String])
1027+
case invalidHeaderFieldValues([String])
10271028
case bodyLengthMismatch
10281029
case writeAfterRequestSent
10291030
@available(*, deprecated, message: "AsyncHTTPClient now silently corrects invalid headers.")
@@ -1092,6 +1093,8 @@ public struct HTTPClientError: Error, Equatable, CustomStringConvertible {
10921093
return "Trace request with body"
10931094
case .invalidHeaderFieldNames:
10941095
return "Invalid header field names"
1096+
case .invalidHeaderFieldValues:
1097+
return "Invalid header field values"
10951098
case .bodyLengthMismatch:
10961099
return "Body length mismatch"
10971100
case .writeAfterRequestSent:
@@ -1158,6 +1161,8 @@ public struct HTTPClientError: Error, Equatable, CustomStringConvertible {
11581161
public static let traceRequestWithBody = HTTPClientError(code: .traceRequestWithBody)
11591162
/// Header field names contain invalid characters.
11601163
public static func invalidHeaderFieldNames(_ names: [String]) -> HTTPClientError { return HTTPClientError(code: .invalidHeaderFieldNames(names)) }
1164+
/// Header field values contain invalid characters.
1165+
public static func invalidHeaderFieldValues(_ values: [String]) -> HTTPClientError { return HTTPClientError(code: .invalidHeaderFieldValues(values)) }
11611166
/// Body length is not equal to `Content-Length`.
11621167
public static let bodyLengthMismatch = HTTPClientError(code: .bodyLengthMismatch)
11631168
/// Body part was written after request was fully sent.

Sources/AsyncHTTPClient/RequestValidation.swift

+51
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ extension HTTPHeaders {
2121
bodyLength: RequestBodyLength
2222
) throws -> RequestFramingMetadata {
2323
try self.validateFieldNames()
24+
try self.validateFieldValues()
2425

2526
if case .TRACE = method {
2627
switch bodyLength {
@@ -80,6 +81,56 @@ extension HTTPHeaders {
8081
}
8182
}
8283

84+
private func validateFieldValues() throws {
85+
let invalidValues = self.compactMap { _, value -> String? in
86+
let satisfy = value.utf8.allSatisfy { char -> Bool in
87+
/// Validates a byte of a given header field value against the definition in RFC 9110.
88+
///
89+
/// The spec in [RFC 9110](https://httpwg.org/specs/rfc9110.html#fields.values) defines the valid
90+
/// characters as the following:
91+
///
92+
/// ```
93+
/// field-value = *field-content
94+
/// field-content = field-vchar
95+
/// [ 1*( SP / HTAB / field-vchar ) field-vchar ]
96+
/// field-vchar = VCHAR / obs-text
97+
/// obs-text = %x80-FF
98+
/// ```
99+
///
100+
/// Additionally, it makes the following note:
101+
///
102+
/// "Field values containing CR, LF, or NUL characters are invalid and dangerous, due to the
103+
/// varying ways that implementations might parse and interpret those characters; a recipient
104+
/// of CR, LF, or NUL within a field value MUST either reject the message or replace each of
105+
/// those characters with SP before further processing or forwarding of that message. Field
106+
/// values containing other CTL characters are also invalid; however, recipients MAY retain
107+
/// such characters for the sake of robustness when they appear within a safe context (e.g.,
108+
/// an application-specific quoted string that will not be processed by any downstream HTTP
109+
/// parser)."
110+
///
111+
/// As we cannot guarantee the context is safe, this code will reject all ASCII control characters
112+
/// directly _except_ for HTAB, which is explicitly allowed.
113+
switch char {
114+
case UInt8(ascii: "\t"):
115+
// HTAB, explicitly allowed.
116+
return true
117+
case 0...0x1f, 0x7F:
118+
// ASCII control character, forbidden.
119+
return false
120+
default:
121+
// Printable or non-ASCII, allowed.
122+
return true
123+
}
124+
}
125+
126+
return satisfy ? nil : value
127+
}
128+
129+
guard invalidValues.count == 0 else {
130+
throw HTTPClientError.invalidHeaderFieldValues(invalidValues)
131+
}
132+
}
133+
83134
private mutating func setTransportFraming(
84135
method: HTTPMethod,
85136
bodyLength: RequestBodyLength

Tests/AsyncHTTPClientTests/HTTPClientTests+XCTest.swift

+4
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ extension HTTPClientTests {
144144
("testRequestWithHeaderTransferEncodingIdentityDoesNotFail", testRequestWithHeaderTransferEncodingIdentityDoesNotFail),
145145
("testMassiveDownload", testMassiveDownload),
146146
("testShutdownWithFutures", testShutdownWithFutures),
147+
("testRejectsInvalidCharactersInHeaderFieldNames_http1", testRejectsInvalidCharactersInHeaderFieldNames_http1),
148+
("testRejectsInvalidCharactersInHeaderFieldNames_http2", testRejectsInvalidCharactersInHeaderFieldNames_http2),
149+
("testRejectsInvalidCharactersInHeaderFieldValues_http1", testRejectsInvalidCharactersInHeaderFieldValues_http1),
150+
("testRejectsInvalidCharactersInHeaderFieldValues_http2", testRejectsInvalidCharactersInHeaderFieldValues_http2),
147151
]
148152
}
149153
}

Tests/AsyncHTTPClientTests/HTTPClientTests.swift

+109
Original file line numberDiff line numberDiff line change
@@ -3468,4 +3468,113 @@ class HTTPClientTests: XCTestCase {
34683468
let httpClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup))
34693469
XCTAssertNoThrow(try httpClient.shutdown().wait())
34703470
}
3471+
3472+
func testRejectsInvalidCharactersInHeaderFieldNames_http1() throws {
3473+
try self._rejectsInvalidCharactersInHeaderFieldNames(mode: .http1_1(ssl: true))
3474+
}
3475+
3476+
func testRejectsInvalidCharactersInHeaderFieldNames_http2() throws {
3477+
try self._rejectsInvalidCharactersInHeaderFieldNames(mode: .http2(compress: false))
3478+
}
3479+
3480+
private func _rejectsInvalidCharactersInHeaderFieldNames(mode: HTTPBin<HTTPBinHandler>.Mode) throws {
3481+
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
3482+
defer { XCTAssertNoThrow(try group.syncShutdownGracefully()) }
3483+
let client = HTTPClient(eventLoopGroupProvider: .shared(group))
3484+
defer { XCTAssertNoThrow(try client.syncShutdown()) }
3485+
let bin = HTTPBin(mode)
3486+
defer { XCTAssertNoThrow(try bin.shutdown()) }
3487+
3488+
// The spec in [RFC 9110](https://httpwg.org/specs/rfc9110.html#fields.values) defines the valid
3489+
// characters as the following:
3490+
//
3491+
// ```
3492+
// field-name = token
3493+
//
3494+
// token = 1*tchar
3495+
//
3496+
// tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*"
3497+
// / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
3498+
// / DIGIT / ALPHA
3499+
// ; any VCHAR, except delimiters
3500+
let weirdAllowedFieldName = "!#$%&'*+-.^_`|~0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
3501+
3502+
var request = try Request(url: "\(self.defaultHTTPBinURLPrefix)get")
3503+
request.headers.add(name: weirdAllowedFieldName, value: "present")
3504+
3505+
// This should work fine.
3506+
let response = try client.execute(request: request).wait()
3507+
XCTAssertEqual(response.status, .ok)
3508+
3509+
// Now, let's confirm all other bytes are rejected. We want to stay within the ASCII space as the HTTPHeaders type will forbid anything else.
3510+
for byte in UInt8(0)...UInt8(127) {
3511+
// Skip bytes that we already believe are allowed.
3512+
if weirdAllowedFieldName.utf8.contains(byte) {
3513+
continue
3514+
}
3515+
let forbiddenFieldName = weirdAllowedFieldName + String(decoding: [byte], as: UTF8.self)
3516+
3517+
var request = try Request(url: "\(self.defaultHTTPBinURLPrefix)get")
3518+
request.headers.add(name: forbiddenFieldName, value: "present")
3519+
3520+
XCTAssertThrowsError(try client.execute(request: request).wait()) { error in
3521+
XCTAssertEqual(error as? HTTPClientError, .invalidHeaderFieldNames([forbiddenFieldName]))
3522+
}
3523+
}
3524+
}
3525+
3526+
func testRejectsInvalidCharactersInHeaderFieldValues_http1() throws {
3527+
try self._rejectsInvalidCharactersInHeaderFieldValues(mode: .http1_1(ssl: true))
3528+
}
3529+
3530+
func testRejectsInvalidCharactersInHeaderFieldValues_http2() throws {
3531+
try self._rejectsInvalidCharactersInHeaderFieldValues(mode: .http2(compress: false))
3532+
}
3533+
3534+
private func _rejectsInvalidCharactersInHeaderFieldValues(mode: HTTPBin<HTTPBinHandler>.Mode) throws {
3535+
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
3536+
defer { XCTAssertNoThrow(try group.syncShutdownGracefully()) }
3537+
let client = HTTPClient(eventLoopGroupProvider: .shared(group))
3538+
defer { XCTAssertNoThrow(try client.syncShutdown()) }
3539+
let bin = HTTPBin(mode)
3540+
defer { XCTAssertNoThrow(try bin.shutdown()) }
3541+
3542+
// We reject all ASCII control characters except HTAB and tolerate everything else.
3543+
let weirdAllowedFieldValue = "!\" \t#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
3544+
3545+
var request = try Request(url: "\(self.defaultHTTPBinURLPrefix)get")
3546+
request.headers.add(name: "Weird-Value", value: weirdAllowedFieldValue)
3547+
3548+
// This should work fine.
3549+
let response = try client.execute(request: request).wait()
3550+
XCTAssertEqual(response.status, .ok)
3551+
3552+
// Now, let's confirm all other bytes in the ASCII range ar rejected
3553+
for byte in UInt8(0)...UInt8(127) {
3554+
// Skip bytes that we already believe are allowed.
3555+
if weirdAllowedFieldValue.utf8.contains(byte) {
3556+
continue
3557+
}
3558+
let forbiddenFieldValue = weirdAllowedFieldValue + String(decoding: [byte], as: UTF8.self)
3559+
3560+
var request = try Request(url: "\(self.defaultHTTPBinURLPrefix)get")
3561+
request.headers.add(name: "Weird-Value", value: forbiddenFieldValue)
3562+
3563+
XCTAssertThrowsError(try client.execute(request: request).wait()) { error in
3564+
XCTAssertEqual(error as? HTTPClientError, .invalidHeaderFieldValues([forbiddenFieldValue]))
3565+
}
3566+
}
3567+
3568+
// All the bytes outside the ASCII range are fine though.
3569+
for byte in UInt8(128)...UInt8(255) {
3570+
let evenWeirderAllowedValue = weirdAllowedFieldValue + String(decoding: [byte], as: UTF8.self)
3571+
3572+
var request = try Request(url: "\(self.defaultHTTPBinURLPrefix)get")
3573+
request.headers.add(name: "Weird-Value", value: evenWeirderAllowedValue)
3574+
3575+
// This should work fine.
3576+
let response = try client.execute(request: request).wait()
3577+
XCTAssertEqual(response.status, .ok)
3578+
}
3579+
}
34713580
}

0 commit comments

Comments
 (0)