Safe-cast URLResponse in ProxyHTTPHandler#203
Merged
Conversation
URLSession.data(for:) returning a non-HTTPURLResponse (custom URL protocols, file://, some error paths) previously trapped on force-unwrap, tearing down the server actor. Replace the force cast with a guard that throws URLError(.badServerResponse). Also adds a test using a URLProtocol that returns a plain URLResponse to lock in the behavior. Closes TVT-284 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #203 +/- ##
==========================================
+ Coverage 92.40% 93.21% +0.80%
==========================================
Files 69 69
Lines 3543 4806 +1263
==========================================
+ Hits 3274 4480 +1206
- Misses 269 326 +57 ☔ View full report in Codecov by Sentry. |
Exercises the makeResponse(...) branch of handleRequest using a URLProtocol fake that returns a valid HTTPURLResponse. Together with the existing non-HTTP-response test, both sides of the guard added for TVT-284 are now covered. Also normalizes the existing test function name to match the project convention (no underscore between "Error" and "When"). Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ProxyHTTPHandler.handleRequestforce-unwrapped theURLResponsereturned byURLSession.data(for:)as anHTTPURLResponse. If the session is configured with a customURLProtocol, afile://URL, or hits certain error paths, the response isn't guaranteed to be anHTTPURLResponse— the force cast traps and crashes the server actor.Change
FlyingFox/Sources/Handlers/ProxyHTTPHandler.swift:public func handleRequest(_ request: HTTPRequest) async throws -> HTTPResponse { let req = try await makeURLRequest(for: request) let (data, response) = try await session.data(for: req) - return makeResponse(for: response as! HTTPURLResponse, data: data) + guard let httpResponse = response as? HTTPURLResponse else { + throw URLError(.badServerResponse) + } + return makeResponse(for: httpResponse, data: data) }Test
New test in
FlyingFox/Tests/Handlers/HTTPHandlerTests.swiftuses aURLProtocolsubclass that returns a plain (non-HTTP)URLResponseand asserts the handler throwsURLErrorrather than crashing.Test plan
swift buildclean.proxyHandler_ThrowsError_WhenResponseIsNotHTTPURLResponsepasses.swift test— 405 tests across 49 suites pass (up from 404).🤖 Generated with Claude Code