Skip to content

Commit 6c87c34

Browse files
Fixed an issue where redirects to socket path-based servers from any server was always allowed.
Motivation: An arbitrary HTTP(S) server should not be able to trigger redirects, and thus activity, to a local socket-path based server, though the opposite may be a valid scenario. Currently, requests in either direction are allowed since the checks don't actually check the destination scheme. Modifications: - Refactored `hostSchemes`/`unixSchemes` to `hostRestrictedSchemes`/`allSupportedSchemes`, which better describes what they do. - Refactored `Request.supports()` to `Request.supportsRedirects(to:)` since it is only used by Redirects now. - Check the destination URL's scheme rather than the current URL's scheme when validating a redirect. Result: Closes swift-server#230
1 parent ee07ddf commit 6c87c34

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

Sources/AsyncHTTPClient/HTTPHandler.swift

+8-6
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ extension HTTPClient {
111111
/// UNIX Domain Socket HTTP request.
112112
case unixSocket(_ scheme: UnixScheme)
113113

114-
private static var hostSchemes = ["http", "https"]
115-
private static var unixSchemes = ["unix", "http+unix", "https+unix"]
114+
private static var hostRestrictedSchemes: Set = ["http", "https"]
115+
private static var allSupportedSchemes: Set = ["http", "https", "unix", "http+unix", "https+unix"]
116116

117117
init(forScheme scheme: String) throws {
118118
switch scheme {
@@ -162,12 +162,14 @@ extension HTTPClient {
162162
}
163163
}
164164

165-
func supports(scheme: String) -> Bool {
165+
func supportsRedirects(to scheme: String?) -> Bool {
166+
guard let scheme = scheme?.lowercased() else { return false }
167+
166168
switch self {
167169
case .host:
168-
return Kind.hostSchemes.contains(scheme)
170+
return Kind.hostRestrictedSchemes.contains(scheme)
169171
case .unixSocket:
170-
return Kind.unixSchemes.contains(scheme)
172+
return Kind.allSupportedSchemes.contains(scheme)
171173
}
172174
}
173175
}
@@ -1023,7 +1025,7 @@ internal struct RedirectHandler<ResponseType> {
10231025
return nil
10241026
}
10251027

1026-
guard self.request.kind.supports(scheme: self.request.scheme) else {
1028+
guard self.request.kind.supportsRedirects(to: url.scheme) else {
10271029
return nil
10281030
}
10291031

0 commit comments

Comments
 (0)