Skip to content

Commit 79190aa

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 e8dc0de commit 79190aa

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
@@ -107,8 +107,8 @@ extension HTTPClient {
107107
/// UNIX Domain Socket HTTP request.
108108
case unixSocket(_ scheme: UnixScheme)
109109

110-
private static var hostSchemes = ["http", "https"]
111-
private static var unixSchemes = ["unix", "http+unix", "https+unix"]
110+
private static var hostRestrictedSchemes: Set = ["http", "https"]
111+
private static var allSupportedSchemes: Set = ["http", "https", "unix", "http+unix", "https+unix"]
112112

113113
init(forScheme scheme: String) throws {
114114
switch scheme {
@@ -158,12 +158,14 @@ extension HTTPClient {
158158
}
159159
}
160160

161-
func supports(scheme: String) -> Bool {
161+
func supportsRedirects(to scheme: String?) -> Bool {
162+
guard let scheme = scheme?.lowercased() else { return false }
163+
162164
switch self {
163165
case .host:
164-
return Kind.hostSchemes.contains(scheme)
166+
return Kind.hostRestrictedSchemes.contains(scheme)
165167
case .unixSocket:
166-
return Kind.unixSchemes.contains(scheme)
168+
return Kind.allSupportedSchemes.contains(scheme)
167169
}
168170
}
169171
}
@@ -1019,7 +1021,7 @@ internal struct RedirectHandler<ResponseType> {
10191021
return nil
10201022
}
10211023

1022-
guard self.request.kind.supports(scheme: self.request.scheme) else {
1024+
guard self.request.kind.supportsRedirects(to: url.scheme) else {
10231025
return nil
10241026
}
10251027

0 commit comments

Comments
 (0)