-
Notifications
You must be signed in to change notification settings - Fork 432
feat(paywalls): web_view navigation/origin policy (3/7) #7229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+195
−2
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d331b34
feat(paywalls): web_view navigation policy + content-rule isolation (…
cursoragent 902e2d0
Merge branch 'main' into webview/3-isolation-navigation
ajpallares a849668
Address review: tighten web_view content rules and expand tests
ajpallares d48cb77
Merge remote-tracking branch 'origin/main' into webview/3-isolation-n…
ajpallares b3e0f35
Enforce origin check on sub-frames and split navigation policy tests
ajpallares e981bf1
Log web_view content-rule compile failures at error level
ajpallares a004956
Canonicalize expectedOrigin in navigation policy
ajpallares b6c281a
Merge branch 'main' into webview/3-isolation-navigation
ajpallares 2af9e9a
other(paywalls): drop web_view content-blocking rules in favor of CSP
ajpallares 93c5770
other(paywalls): drop unused isMainFrame param from navigation policy
ajpallares 255cff9
other(paywalls): match Android web_view navigation policy
ajpallares d95382a
Merge branch 'main' into webview/3-isolation-navigation
ajpallares b2f76f9
Merge branch 'main' of https://github.com/RevenueCat/purchases-ios in…
ajpallares 2475742
Merge commit 'd95382ac6' into webview/3-isolation-navigation
ajpallares File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
58 changes: 58 additions & 0 deletions
58
RevenueCatUI/Templates/V2/Components/WebView/WebViewOriginPolicy.swift
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import Foundation | ||
|
|
||
| #if canImport(WebKit) | ||
| import WebKit | ||
| #endif | ||
|
|
||
| #if !os(tvOS) && canImport(WebKit) // For Paywalls V2 | ||
|
|
||
| @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) | ||
| enum WebViewOrigin { | ||
|
|
||
| nonisolated static func origin(of url: URL) -> String? { | ||
| guard let scheme = url.scheme?.lowercased(), | ||
| let host = url.host?.lowercased(), | ||
| !host.isEmpty else { | ||
| return nil | ||
| } | ||
|
|
||
| let port = url.port | ||
| let suffix: String | ||
| if let port, !Self.isDefaultPort(port, scheme: scheme) { | ||
| suffix = ":\(port)" | ||
| } else { | ||
| suffix = "" | ||
| } | ||
| return "\(scheme)://\(host)\(suffix)" | ||
| } | ||
|
|
||
| nonisolated private static func isDefaultPort(_ port: Int, scheme: String) -> Bool { | ||
| (scheme == "https" && port == 443) || (scheme == "http" && port == 80) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) | ||
| enum WebViewNavigationPolicy { | ||
|
|
||
| // Non-https navigation is blocked on any frame. Cross-origin navigation is additionally blocked | ||
| // on the main frame (same-origin different-path navigation stays allowed), which makes | ||
| // cross-origin message races structurally impossible. Cross-origin sub-frame loads are not | ||
| // blocked here; isolation for those is left to the server-provided CSP (`frame-src` falls back | ||
| // to `default-src 'self'`). | ||
| static func policy(for url: URL?, isMainFrame: Bool, expectedOrigin: String) -> WKNavigationActionPolicy { | ||
| guard let url, | ||
| let origin = WebViewOrigin.origin(of: url), | ||
| origin.hasPrefix("https://") else { | ||
| return .cancel | ||
| } | ||
| guard isMainFrame else { | ||
| return .allow | ||
| } | ||
| let expected = URL(string: expectedOrigin).flatMap(WebViewOrigin.origin(of:)) | ||
| return origin == expected ? .allow : .cancel | ||
| } | ||
|
|
||
| } | ||
|
|
||
| #endif | ||
129 changes: 129 additions & 0 deletions
129
Tests/RevenueCatUITests/PaywallsV2/WebViewNavigationPolicyTests.swift
|
ajpallares marked this conversation as resolved.
|
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| // | ||
| // Copyright RevenueCat Inc. All Rights Reserved. | ||
| // | ||
|
|
||
| @testable import RevenueCatUI | ||
| import XCTest | ||
|
|
||
| #if !os(tvOS) && canImport(WebKit) | ||
| import WebKit | ||
|
|
||
| @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) | ||
| final class WebViewNavigationPolicyTests: TestCase { | ||
|
|
||
| func testSameOriginMainFrameDifferentPathIsAllowed() { | ||
| XCTAssertEqual( | ||
| WebViewNavigationPolicy.policy( | ||
| for: URL(string: "https://example.com/promo/step-two.html")!, | ||
| isMainFrame: true, | ||
| expectedOrigin: "https://example.com" | ||
| ), | ||
| .allow | ||
| ) | ||
| } | ||
|
|
||
| func testCrossOriginMainFrameIsCancelled() { | ||
| XCTAssertEqual( | ||
| WebViewNavigationPolicy.policy( | ||
| for: URL(string: "https://evil.example/phish.html")!, | ||
| isMainFrame: true, | ||
| expectedOrigin: "https://example.com" | ||
| ), | ||
| .cancel | ||
| ) | ||
| } | ||
|
|
||
| func testMainFramePortMismatchIsCancelled() { | ||
| XCTAssertEqual( | ||
| WebViewNavigationPolicy.policy( | ||
| for: URL(string: "https://example.com:8443/next")!, | ||
| isMainFrame: true, | ||
| expectedOrigin: "https://example.com" | ||
| ), | ||
| .cancel | ||
| ) | ||
| } | ||
|
|
||
| func testNonHttpsIsCancelledOnAnyFrame() { | ||
| XCTAssertEqual( | ||
| WebViewNavigationPolicy.policy( | ||
| for: URL(string: "http://example.com/promo/index.html")!, | ||
| isMainFrame: false, | ||
| expectedOrigin: "https://example.com" | ||
| ), | ||
| .cancel | ||
| ) | ||
| XCTAssertEqual( | ||
| WebViewNavigationPolicy.policy( | ||
| for: URL(string: "custom://example.com/")!, | ||
| isMainFrame: true, | ||
| expectedOrigin: "https://example.com" | ||
| ), | ||
| .cancel | ||
| ) | ||
| } | ||
|
|
||
| func testCrossOriginHttpsSubFrameIsAllowed() { | ||
| // Sub-frame isolation is expected from the server-provided CSP, not this navigation policy. | ||
| XCTAssertEqual( | ||
| WebViewNavigationPolicy.policy( | ||
| for: URL(string: "https://other.example.com/frame.html")!, | ||
| isMainFrame: false, | ||
| expectedOrigin: "https://example.com" | ||
| ), | ||
| .allow | ||
| ) | ||
| } | ||
|
|
||
| func testNilURLMainFrameIsCancelled() { | ||
| XCTAssertEqual( | ||
| WebViewNavigationPolicy.policy( | ||
| for: nil, | ||
| isMainFrame: true, | ||
| expectedOrigin: "https://example.com" | ||
| ), | ||
| .cancel | ||
| ) | ||
| } | ||
|
|
||
| func testHostlessExpectedOriginMainFrameIsCancelled() { | ||
| XCTAssertEqual( | ||
| WebViewNavigationPolicy.policy( | ||
| for: URL(string: "https://example.com/next")!, | ||
| isMainFrame: true, | ||
| expectedOrigin: "https:///no-host" | ||
| ), | ||
| .cancel | ||
| ) | ||
| } | ||
|
|
||
| func testNonCanonicalExpectedOriginCaseAndDefaultPortIsAllowed() { | ||
| XCTAssertEqual( | ||
| WebViewNavigationPolicy.policy( | ||
| for: URL(string: "https://example.com/promo/index.html")!, | ||
| isMainFrame: true, | ||
| expectedOrigin: "https://Example.COM:443" | ||
| ), | ||
| .allow | ||
| ) | ||
| } | ||
|
|
||
| func testOriginStripsDefaultPortKeepsNonDefaultAndNormalizesCase() { | ||
| XCTAssertEqual( | ||
| WebViewOrigin.origin(of: URL(string: "https://Example.COM:443/path")!), | ||
| "https://example.com" | ||
| ) | ||
| XCTAssertEqual( | ||
| WebViewOrigin.origin(of: URL(string: "http://Example.COM:80/path")!), | ||
| "http://example.com" | ||
| ) | ||
| XCTAssertEqual( | ||
| WebViewOrigin.origin(of: URL(string: "HTTPS://Example.COM:8443/path")!), | ||
| "https://example.com:8443" | ||
| ) | ||
| XCTAssertNil(WebViewOrigin.origin(of: URL(string: "https:///no-host")!)) | ||
| } | ||
|
ajpallares marked this conversation as resolved.
|
||
|
|
||
| } | ||
|
|
||
| #endif | ||
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.
Uh oh!
There was an error while loading. Please reload this page.