diff --git a/Sources/NIOHTTP2/HTTP2ToHTTP1Codec.swift b/Sources/NIOHTTP2/HTTP2ToHTTP1Codec.swift index b1a7375..ba4a2d1 100644 --- a/Sources/NIOHTTP2/HTTP2ToHTTP1Codec.swift +++ b/Sources/NIOHTTP2/HTTP2ToHTTP1Codec.swift @@ -796,7 +796,28 @@ extension HPACKHeaders { // 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 }) + // + // We reject the entire CTL range (0x00-0x1F), SP (0x20), and DEL (0x7F), rather than + // only CR/LF/NUL, for two reasons: + // + // 1. `:path` becomes the HTTP/1.1 request-target, which is serialized into the request + // line `METHOD SP request-target SP HTTP-version CRLF`. A bare SP inside `:path` + // therefore produces an ambiguous request line (e.g. `GET /a HTTP/1.1 HTTP/1.1`), + // which HTTP/1.1 parsers may split differently depending on whether they take the + // first or last SP-delimited token as the version. RFC 9112 § 3.2 requires any SP + // in a request-target to be percent-encoded. Bare HTAB and the other CTLs are + // likewise not permitted in a request-target. + // + // 2. No pseudo-header defined for HTTP/2 has a grammar admitting SP or a CTL: + // `:method` and `:protocol` are tokens (RFC 9110 § 5.6.2), `:scheme` is a URI scheme + // (RFC 3986 § 3.1), `:authority` is host[:port], `:path` is a request-target, and + // `:status` is 3DIGIT. Rejecting these bytes is therefore conformant and does not + // reject any well-formed value. + // + // Bytes >= 0x80 are deliberately still permitted: they are not delimiters in an + // HTTP/1.1 request line or header block, and rejecting them would break peers that + // send unencoded UTF-8 in `:path`. + !value.utf8.contains(where: { $0 <= 0x20 || $0 == 0x7F }) } /// Whether this is a valid value for a regular (non-pseudo) HTTP/2 header field. diff --git a/Tests/NIOHTTP2Tests/HTTP2FramePayloadToHTTP1CodecCRLFTests.swift b/Tests/NIOHTTP2Tests/HTTP2FramePayloadToHTTP1CodecCRLFTests.swift index 93b35d5..56bcad1 100644 --- a/Tests/NIOHTTP2Tests/HTTP2FramePayloadToHTTP1CodecCRLFTests.swift +++ b/Tests/NIOHTTP2Tests/HTTP2FramePayloadToHTTP1CodecCRLFTests.swift @@ -315,4 +315,68 @@ struct HTTP2FramePayloadToHTTP1CodecCRLFTests { // `appendRegularHeaders(from:)` without inspecting them. Regular field values are policed by // the connection state machine, which calls `validateRequestBlock` before the codec ever sees // the block, so a codec-only `EmbeddedChannel` has no validation in it to exercise. + + // MARK: - Validation tests: SP and other CTLs in pseudo-header values + + // `:path` becomes the HTTP/1.1 request-target in the request line + // `METHOD SP request-target SP HTTP-version CRLF`. A bare SP inside `:path` makes that line + // ambiguous (`GET /a HTTP/1.1 HTTP/1.1`), so RFC 9112 § 3.2 requires it to be + // percent-encoded. No HTTP/2 pseudo-header has a grammar admitting SP or a CTL. + static let pseudoHeaderDelimiterInjections: [PseudoHeaderInjection] = [ + .init(pseudoHeaderName: ":path", maliciousValue: "/a HTTP/1.1", label: "SP request-line split"), + .init(pseudoHeaderName: ":path", maliciousValue: "/a\tb", label: "HTAB"), + .init(pseudoHeaderName: ":path", maliciousValue: "/a\u{0B}b", label: "vertical tab"), + .init(pseudoHeaderName: ":path", maliciousValue: "/a\u{0C}b", label: "form feed"), + .init(pseudoHeaderName: ":path", maliciousValue: "/a\u{7F}b", label: "DEL"), + .init(pseudoHeaderName: ":path", maliciousValue: "/a\u{01}b", label: "SOH"), + .init(pseudoHeaderName: ":authority", maliciousValue: "example.com evil.com", label: "SP"), + .init(pseudoHeaderName: ":method", maliciousValue: "GET /admin HTTP/1.1", label: "SP"), + .init(pseudoHeaderName: ":scheme", maliciousValue: "https evil", label: "SP"), + ] + + @Test( + "pseudo-header values containing SP or CTLs are rejected", + arguments: pseudoHeaderDelimiterInjections.map(\.maliciousValue) + ) + func invalidPseudoHeaderDelimiterValue(value: String) { + #expect(!HPACKHeaders.isValidPseudoHeaderValue(value)) + } + + @Test( + "Request validation rejects SP and CTLs in pseudo-headers", + arguments: pseudoHeaderDelimiterInjections + ) + func requestValidationRejectsDelimiters(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( + "Server codec rejects SP and CTLs in pseudo-headers", + arguments: pseudoHeaderDelimiterInjections + ) + func serverCodecRejectsDelimiters(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"), + ]) + #expect(throws: NIOHTTP2Errors.InvalidPseudoHeaderValue.self) { + try channel.writeInbound( + HTTP2Frame.FramePayload.headers(.init(headers: requestHeaders, endStream: true)) + ) + } + } }