mirror of
https://github.com/apple/swift-nio
synced 2026-11-27 05:04:22 +00:00
Merge commit from fork
- Ensure HTTP Request Uri and Method only contain allowed bytes - Ensure HTTP Response Reasons only contain allowed bytes
This commit is contained in:
1 parent
6338e9fb5e
commit
dd16365724
2 files changed
+511
-4
No files matched your search
@@ -20,7 +20,7 @@ import NIOCore
|
||||
/// are emitted on the network. If a header block is invalid, then ``NIOHTTPRequestHeadersValidator``
|
||||
/// will send a ``HTTPParserError/invalidHeaderToken``.
|
||||
///
|
||||
/// ``NIOHTTPRequestHeadersValidator`` will also valid that the HTTP trailers are within specification,
|
||||
/// ``NIOHTTPRequestHeadersValidator`` will also validate that the HTTP trailers are within specification,
|
||||
/// if they are present.
|
||||
public final class NIOHTTPRequestHeadersValidator: ChannelOutboundHandler, RemovableChannelHandler {
|
||||
public typealias OutboundIn = HTTPClientRequestPart
|
||||
@@ -31,7 +31,7 @@ public final class NIOHTTPRequestHeadersValidator: ChannelOutboundHandler, Remov
|
||||
public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
|
||||
switch NIOHTTPRequestHeadersValidator.unwrapOutboundIn(data) {
|
||||
case .head(let head):
|
||||
guard head.headers.areValidToSend else {
|
||||
guard Self.uriOnlyContainsAllowedCharacters(head.uri), head.method.isValidToSend, head.headers.areValidToSend else {
|
||||
promise?.fail(HTTPParserError.invalidHeaderToken)
|
||||
context.fireErrorCaught(HTTPParserError.invalidHeaderToken)
|
||||
return
|
||||
@@ -48,6 +48,79 @@ public final class NIOHTTPRequestHeadersValidator: ChannelOutboundHandler, Remov
|
||||
|
||||
context.write(data, promise: promise)
|
||||
}
|
||||
|
||||
static func uriOnlyContainsAllowedCharacters(_ uri: String) -> Bool {
|
||||
// The spec in [RFC 9112](https://datatracker.ietf.org/doc/html/rfc9112#section-3.2) defines the valid
|
||||
// characters for the request-target as the following:
|
||||
//
|
||||
// ```
|
||||
// request-target = origin-form / absolute-form / authority-form / asterisk-form
|
||||
//
|
||||
// origin-form = absolute-path [ "?" query ]
|
||||
// absolute-form = absolute-URI
|
||||
// authority-form = uri-host ":" port ; CONNECT only
|
||||
// asterisk-form = "*" ; OPTIONS only
|
||||
// ```
|
||||
//
|
||||
// The component grammar comes from [RFC 3986](https://datatracker.ietf.org/doc/html/rfc3986#section-3)
|
||||
// (updated by [RFC 8820](https://datatracker.ietf.org/doc/html/rfc8820), which adds best-practice
|
||||
// guidance for URI design but does not change the syntax):
|
||||
//
|
||||
// ```
|
||||
// absolute-path = 1*( "/" segment )
|
||||
// segment = *pchar
|
||||
// query = *( pchar / "/" / "?" )
|
||||
//
|
||||
// pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
|
||||
// pct-encoded = "%" HEXDIG HEXDIG
|
||||
//
|
||||
// unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
|
||||
// reserved = gen-delims / sub-delims
|
||||
// gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
|
||||
// sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
|
||||
// / "*" / "+" / "," / ";" / "="
|
||||
// ```
|
||||
//
|
||||
// In other words, the literal byte set allowed on the wire is:
|
||||
//
|
||||
// ```
|
||||
// ALPHA %x41-5A / %x61-7A ; A–Z a–z
|
||||
// DIGIT %x30-39 ; 0–9
|
||||
// unreserved "-" "." "_" "~"
|
||||
// gen-delims ":" "/" "?" "#" "[" "]" "@"
|
||||
// sub-delims "!" "$" "&" "'" "(" ")" "*" "+" "," ";" "="
|
||||
// pct-encoded "%" HEXDIG HEXDIG ; escape for anything else
|
||||
// ```
|
||||
//
|
||||
// Everything outside this set — SP, CTLs (%x00-1F / %x7F), non-ASCII (%x80-FF),
|
||||
// and `" < > \ ^ ` { | }` — MUST be percent-encoded. Bare CR, LF, or NUL in
|
||||
// the request-target MUST be rejected (request smuggling / response splitting).
|
||||
|
||||
uri.utf8.allSatisfy { byte in
|
||||
switch byte {
|
||||
case
|
||||
// unreserved
|
||||
// - ALPHA
|
||||
UInt8(ascii: "A")...UInt8(ascii: "Z"), UInt8(ascii: "a")...UInt8(ascii: "z"),
|
||||
// - DIGIT
|
||||
UInt8(ascii: "0")...UInt8(ascii: "9"),
|
||||
// - extra characters
|
||||
UInt8(ascii: "-"), UInt8(ascii: "."), UInt8(ascii: "_"), UInt8(ascii: "~"),
|
||||
// gen-delims
|
||||
UInt8(ascii: ":"), UInt8(ascii: "/"), UInt8(ascii: "?"), UInt8(ascii: "#"),
|
||||
UInt8(ascii: "["), UInt8(ascii: "]"), UInt8(ascii: "@"),
|
||||
// sub-delims
|
||||
UInt8(ascii: "!"), UInt8(ascii: "$"), UInt8(ascii: "&"), UInt8(ascii: "'"),
|
||||
UInt8(ascii: "("), UInt8(ascii: ")"), UInt8(ascii: "*"), UInt8(ascii: "+"),
|
||||
UInt8(ascii: ","), UInt8(ascii: ";"), UInt8(ascii: "="),
|
||||
// pct-encoded
|
||||
UInt8(ascii: "%"):
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A ChannelHandler to validate that outbound response headers are spec-compliant.
|
||||
@@ -57,7 +130,7 @@ public final class NIOHTTPRequestHeadersValidator: ChannelOutboundHandler, Remov
|
||||
/// are emitted on the network. If a header block is invalid, then ``NIOHTTPResponseHeadersValidator``
|
||||
/// will send a ``HTTPParserError/invalidHeaderToken``.
|
||||
///
|
||||
/// ``NIOHTTPResponseHeadersValidator`` will also valid that the HTTP trailers are within specification,
|
||||
/// ``NIOHTTPResponseHeadersValidator`` will also validate that the HTTP trailers are within specification,
|
||||
/// if they are present.
|
||||
public final class NIOHTTPResponseHeadersValidator: ChannelOutboundHandler, RemovableChannelHandler {
|
||||
public typealias OutboundIn = HTTPServerResponsePart
|
||||
@@ -79,7 +152,7 @@ public final class NIOHTTPResponseHeadersValidator: ChannelOutboundHandler, Remo
|
||||
public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
|
||||
switch (NIOHTTPResponseHeadersValidator.unwrapOutboundIn(data), self.state) {
|
||||
case (.head(let head), .validating):
|
||||
if head.headers.areValidToSend {
|
||||
if head.headers.areValidToSend, head.status.isValidToSend {
|
||||
context.write(data, promise: promise)
|
||||
} else {
|
||||
self.state = .dropping
|
||||
@@ -111,3 +184,169 @@ extension NIOHTTPRequestHeadersValidator: Sendable {}
|
||||
|
||||
@available(*, unavailable)
|
||||
extension NIOHTTPResponseHeadersValidator: Sendable {}
|
||||
|
||||
extension HTTPMethod {
|
||||
/// Whether these HTTPHeaders are valid to send on the wire.
|
||||
var isValidToSend: Bool {
|
||||
switch self {
|
||||
case .GET,
|
||||
.PUT,
|
||||
.ACL,
|
||||
.HEAD,
|
||||
.POST,
|
||||
.COPY,
|
||||
.LOCK,
|
||||
.MOVE,
|
||||
.BIND,
|
||||
.LINK,
|
||||
.PATCH,
|
||||
.TRACE,
|
||||
.MKCOL,
|
||||
.MERGE,
|
||||
.PURGE,
|
||||
.NOTIFY,
|
||||
.SEARCH,
|
||||
.UNLOCK,
|
||||
.REBIND,
|
||||
.UNBIND,
|
||||
.REPORT,
|
||||
.DELETE,
|
||||
.UNLINK,
|
||||
.CONNECT,
|
||||
.MSEARCH,
|
||||
.OPTIONS,
|
||||
.PROPFIND,
|
||||
.CHECKOUT,
|
||||
.PROPPATCH,
|
||||
.SUBSCRIBE,
|
||||
.MKCALENDAR,
|
||||
.MKACTIVITY,
|
||||
.UNSUBSCRIBE,
|
||||
.SOURCE:
|
||||
true
|
||||
|
||||
case .RAW(value: let value):
|
||||
// The spec in [RFC 9110](https://httpwg.org/specs/rfc9110.html#method.overview) defines the valid
|
||||
// characters as the following:
|
||||
//
|
||||
// ```
|
||||
// method = token
|
||||
//
|
||||
// token = 1*tchar
|
||||
//
|
||||
// tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*"
|
||||
// / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
|
||||
// / DIGIT / ALPHA
|
||||
// ; any VCHAR, except delimiters
|
||||
|
||||
value.utf8.allSatisfy { byte in
|
||||
switch byte {
|
||||
case
|
||||
// ALPHA
|
||||
UInt8(ascii: "A")...UInt8(ascii: "Z"), UInt8(ascii: "a")...UInt8(ascii: "z"),
|
||||
// DIGIT
|
||||
UInt8(ascii: "0")...UInt8(ascii: "9"),
|
||||
// token
|
||||
UInt8(ascii: "!"), UInt8(ascii: "#"), UInt8(ascii: "$"), UInt8(ascii: "%"),
|
||||
UInt8(ascii: "&"), UInt8(ascii: "'"), UInt8(ascii: "*"), UInt8(ascii: "+"),
|
||||
UInt8(ascii: "-"), UInt8(ascii: "."), UInt8(ascii: "^"), UInt8(ascii: "_"),
|
||||
UInt8(ascii: "`"), UInt8(ascii: "|"), UInt8(ascii: "~"):
|
||||
true
|
||||
default:
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension HTTPResponseStatus {
|
||||
var isValidToSend: Bool {
|
||||
switch self {
|
||||
case .continue,
|
||||
.switchingProtocols,
|
||||
.processing,
|
||||
.ok,
|
||||
.created,
|
||||
.accepted,
|
||||
.nonAuthoritativeInformation,
|
||||
.noContent,
|
||||
.resetContent,
|
||||
.partialContent,
|
||||
.multiStatus,
|
||||
.alreadyReported,
|
||||
.imUsed,
|
||||
.multipleChoices,
|
||||
.movedPermanently,
|
||||
.found,
|
||||
.seeOther,
|
||||
.notModified,
|
||||
.useProxy,
|
||||
.temporaryRedirect,
|
||||
.permanentRedirect,
|
||||
.badRequest,
|
||||
.unauthorized,
|
||||
.paymentRequired,
|
||||
.forbidden,
|
||||
.notFound,
|
||||
.methodNotAllowed,
|
||||
.notAcceptable,
|
||||
.proxyAuthenticationRequired,
|
||||
.requestTimeout,
|
||||
.conflict,
|
||||
.gone,
|
||||
.lengthRequired,
|
||||
.preconditionFailed,
|
||||
.payloadTooLarge,
|
||||
.uriTooLong,
|
||||
.unsupportedMediaType,
|
||||
.rangeNotSatisfiable,
|
||||
.expectationFailed,
|
||||
.imATeapot,
|
||||
.misdirectedRequest,
|
||||
.unprocessableEntity,
|
||||
.locked,
|
||||
.failedDependency,
|
||||
.upgradeRequired,
|
||||
.preconditionRequired,
|
||||
.tooManyRequests,
|
||||
.requestHeaderFieldsTooLarge,
|
||||
.unavailableForLegalReasons,
|
||||
.internalServerError,
|
||||
.notImplemented,
|
||||
.badGateway,
|
||||
.serviceUnavailable,
|
||||
.gatewayTimeout,
|
||||
.httpVersionNotSupported,
|
||||
.variantAlsoNegotiates,
|
||||
.insufficientStorage,
|
||||
.loopDetected,
|
||||
.notExtended,
|
||||
.networkAuthenticationRequired:
|
||||
true
|
||||
|
||||
case .custom(_, let reasonPhrase):
|
||||
// The spec in [RFC 9112](https://datatracker.ietf.org/doc/html/rfc9112#section-4) defines the valid
|
||||
// characters as the following:
|
||||
//
|
||||
// ```
|
||||
// reason-phrase = 1*( HTAB / SP / VCHAR / obs-text )
|
||||
//
|
||||
// obs-text = %x80-FF
|
||||
// ```
|
||||
|
||||
reasonPhrase.utf8.allSatisfy { byte in
|
||||
switch byte {
|
||||
case
|
||||
9, // HTAB
|
||||
32, // SP
|
||||
33...126, // VCHAR
|
||||
128...255: // obs-text
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user