Capture traffic from sessions that own their configuration - #4
Merged
Conversation
URLProtocol.registerClass only reaches URLSession.shared. Any URLSession built with its own configuration resolves its handler chain from that configuration's protocolClasses, snapshotted at init, and never consults the global registry — so Ktor's Darwin engine (PTEShared), and any SDK that builds its own session, was invisible to the inspector. Swizzle the protocolClasses getter so InspectorURLProtocol lands in the snapshot as each configuration is read during session construction. Because those sessions hold a direct class reference, unregister() no longer stops them, so gate shouldIntercept on an isCapturing flag to keep disable() effective. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
🟡 Changes recommended
The swizzle installation is not thread-safe (can be undone by concurrent calls), and the newly added shared capture state further increases the risk of data races from URL loading threads.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR extends NetworkInspector’s traffic capture beyond URLSession.shared by ensuring InspectorURLProtocol is included in URLSessionConfiguration.protocolClasses snapshots taken during URLSession construction, and preserves the ability to “disable” capture by gating interception at runtime.
Changes:
- Adds a
URLSessionConfigurationswizzle to injectInspectorURLProtocolintoprotocolClasses. - Introduces an
isCapturinggate so sessions that already hold a protocol class reference stop intercepting afterdisable()/unregister().
File summaries
| File | Description |
|---|---|
| Sources/NetworkInspector/URLSessionConfigurationSwizzle.swift | Adds protocolClasses getter swizzle to ensure the inspector protocol is present in per-session snapshots. |
| Sources/NetworkInspector/NetworkInterceptor.swift | Adds isCapturing flag and gates shouldIntercept to keep disable/unregister effective with swizzled sessions. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 2
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+24
to
+40
| static func installInspectorSwizzle() { | ||
| guard !isSwizzled else { return } | ||
|
|
||
| guard | ||
| let original = class_getInstanceMethod( | ||
| URLSessionConfiguration.self, | ||
| #selector(getter: URLSessionConfiguration.protocolClasses) | ||
| ), | ||
| let replacement = class_getInstanceMethod( | ||
| URLSessionConfiguration.self, | ||
| #selector(getter: URLSessionConfiguration.inspector_protocolClasses) | ||
| ) | ||
| else { return } | ||
|
|
||
| method_exchangeImplementations(original, replacement) | ||
| isSwizzled = true | ||
| } |
Comment on lines
13
to
15
| private static var allowedBaseURLs: [String] = [] | ||
| private static var isCapturing = false | ||
|
|
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.
URLProtocol.registerClass only reaches URLSession.shared. Any URLSession built with its own configuration resolves its handler chain from that configuration's protocolClasses, snapshotted at init, and never consults the global registry — so Ktor's Darwin engine (PTEShared), and any SDK that builds its own session, was invisible to the inspector.
Swizzle the protocolClasses getter so InspectorURLProtocol lands in the snapshot as each configuration is read during session construction.
Because those sessions hold a direct class reference, unregister() no longer stops them, so gate shouldIntercept on an isCapturing flag to keep disable() effective.