-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from hotwired/cross-origin-redirect
Detect cross-origin redirects during visits
- Loading branch information
Showing
10 changed files
with
212 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import Foundation | ||
|
||
enum RedirectHandlerError: Error { | ||
case requestFailed(Error) | ||
case responseValidationFailed(reason: ResponseValidationFailureReason) | ||
|
||
/// The underlying reason the `.responseValidationFailed` error occurred. | ||
public enum ResponseValidationFailureReason: Sendable { | ||
case missingURL | ||
case invalidResponse | ||
case unacceptableStatusCode(code: Int) | ||
} | ||
} | ||
|
||
struct RedirectHandler { | ||
enum Result { | ||
case noRedirect | ||
case sameOriginRedirect(URL) | ||
case crossOriginRedirect(URL) | ||
} | ||
|
||
func resolve(location: URL) async throws -> Result { | ||
do { | ||
let request = URLRequest(url: location) | ||
let (_, response) = try await URLSession.shared.data(for: request) | ||
let httpResponse = try validateResponse(response) | ||
|
||
guard let responseUrl = httpResponse.url else { | ||
throw RedirectHandlerError.responseValidationFailed(reason: .missingURL) | ||
} | ||
|
||
let isRedirect = location != responseUrl | ||
let redirectIsCrossOrigin = isRedirect && location.host != responseUrl.host | ||
|
||
guard isRedirect else { | ||
return .noRedirect | ||
} | ||
|
||
if redirectIsCrossOrigin { | ||
return .crossOriginRedirect(responseUrl) | ||
} | ||
|
||
return .sameOriginRedirect(responseUrl) | ||
} catch let error as RedirectHandlerError { | ||
throw error | ||
} catch { | ||
throw RedirectHandlerError.requestFailed(error) | ||
} | ||
} | ||
|
||
private func validateResponse(_ response: URLResponse) throws -> HTTPURLResponse { | ||
guard let httpResponse = response as? HTTPURLResponse else { | ||
throw RedirectHandlerError.responseValidationFailed(reason: .invalidResponse) | ||
} | ||
|
||
guard httpResponse.isSuccessful else { | ||
throw RedirectHandlerError.responseValidationFailed(reason: .unacceptableStatusCode(code: httpResponse.statusCode)) | ||
} | ||
|
||
return httpResponse | ||
} | ||
} | ||
|
||
extension HTTPURLResponse { | ||
public var isSuccessful: Bool { | ||
(200...299).contains(statusCode) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters