-
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 8 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
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
63 changes: 63 additions & 0 deletions
63
RevenueCatUI/Templates/V2/Components/WebView/WebViewIsolation.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,63 @@ | ||
| import Foundation | ||
| @_spi(Internal) import RevenueCat | ||
|
|
||
| #if !os(tvOS) && canImport(WebKit) // For Paywalls V2 | ||
| import WebKit | ||
|
|
||
| @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) | ||
| enum WebViewIsolation { | ||
|
|
||
| static let contentRuleListIdentifier = "rc-webview-v2-isolation" | ||
|
|
||
| static let contentBlockingRules = """ | ||
| [ | ||
| { | ||
| "trigger": { | ||
| "url-filter": ".*", | ||
| "resource-type": ["image", "script", "font", "raw", "style-sheet", "media", "document"], | ||
| "load-type": ["third-party"] | ||
| }, | ||
| "action": {"type": "block"} | ||
| } | ||
| ] | ||
| """ | ||
|
|
||
| @MainActor | ||
| static var compileRuleList: @MainActor (String, String) async -> WKContentRuleList? = { identifier, rules in | ||
| await withCheckedContinuation { continuation in | ||
| WKContentRuleListStore.default().compileContentRuleList( | ||
| forIdentifier: identifier, | ||
| encodedContentRuleList: rules | ||
| ) { ruleList, error in | ||
| if let error { | ||
| Logger.error(Strings.paywall_web_view_content_rules_failed(String(describing: error))) | ||
| } | ||
| continuation.resume(returning: ruleList) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @MainActor | ||
| private static var ruleListTask: Task<WKContentRuleList?, Never>? | ||
|
|
||
| @MainActor | ||
| static func ruleList() async -> WKContentRuleList? { | ||
| if let ruleListTask { | ||
| return await ruleListTask.value | ||
| } | ||
|
|
||
| let task = Task<WKContentRuleList?, Never> { @MainActor in | ||
| await Self.compileRuleList(Self.contentRuleListIdentifier, Self.contentBlockingRules) | ||
| } | ||
| self.ruleListTask = task | ||
| return await task.value | ||
| } | ||
|
|
||
| @MainActor | ||
| static func resetRuleListCacheForTests() { | ||
| self.ruleListTask = nil | ||
| } | ||
|
|
||
| } | ||
|
|
||
| #endif | ||
55 changes: 55 additions & 0 deletions
55
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,55 @@ | ||
| 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 { | ||
|
|
||
| // The origin check is enforced on every frame, including sub-frames, so cross-origin iframes | ||
| // cannot navigate freely even if a third-party document load slips past the content rules. | ||
| // `isMainFrame` is kept for call-site context and potential future differentiation. | ||
| // Both sides are canonicalized so a non-canonical `expectedOrigin` (mixed case, explicit | ||
| // default port) doesn't cause legitimate same-origin navigations to be cancelled. | ||
| static func policy(for url: URL?, isMainFrame: Bool, expectedOrigin: String) -> WKNavigationActionPolicy { | ||
| guard let url, | ||
| let origin = WebViewOrigin.origin(of: url), | ||
| let expected = URL(string: expectedOrigin).flatMap(WebViewOrigin.origin(of:)), | ||
| origin == expected else { | ||
| return .cancel | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| } | ||
| return .allow | ||
| } | ||
|
|
||
| } | ||
|
|
||
| #endif | ||
77 changes: 77 additions & 0 deletions
77
Tests/RevenueCatUITests/PaywallsV2/WebViewIsolationTests.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,77 @@ | ||
| // | ||
| // 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, *) | ||
| @MainActor | ||
| final class WebViewIsolationTests: TestCase { | ||
|
|
||
| func testContentRulesShape() throws { | ||
|
ajpallares marked this conversation as resolved.
Outdated
|
||
| XCTAssertEqual(WebViewIsolation.contentRuleListIdentifier, "rc-webview-v2-isolation") | ||
| let rules = WebViewIsolation.contentBlockingRules | ||
| let data = try XCTUnwrap(rules.data(using: .utf8)) | ||
| let object = try XCTUnwrap(JSONSerialization.jsonObject(with: data) as? [[String: Any]]) | ||
|
|
||
| XCTAssertEqual(object.count, 1) | ||
| let trigger = try XCTUnwrap(object[0]["trigger"] as? [String: Any]) | ||
| XCTAssertEqual( | ||
| trigger["resource-type"] as? [String], | ||
| ["image", "script", "font", "raw", "style-sheet", "media", "document"] | ||
| ) | ||
| XCTAssertEqual(trigger["load-type"] as? [String], ["third-party"]) | ||
| let action = try XCTUnwrap(object[0]["action"] as? [String: Any]) | ||
| XCTAssertEqual(action["type"] as? String, "block") | ||
| XCTAssertFalse(rules.contains(#""url-filter": "data:"#)) | ||
| } | ||
|
|
||
| func testContentBlockingRulesCompileThroughWebKit() async throws { | ||
| let identifier = "\(WebViewIsolation.contentRuleListIdentifier)-integration-test" | ||
|
|
||
| let ruleList: WKContentRuleList? = await withCheckedContinuation { continuation in | ||
| WKContentRuleListStore.default().compileContentRuleList( | ||
| forIdentifier: identifier, | ||
| encodedContentRuleList: WebViewIsolation.contentBlockingRules | ||
| ) { compiled, _ in | ||
| continuation.resume(returning: compiled) | ||
| } | ||
| } | ||
|
|
||
| XCTAssertNotNil(ruleList) | ||
|
|
||
| await withCheckedContinuation { (continuation: CheckedContinuation<Void, Never>) in | ||
| WKContentRuleListStore.default().removeContentRuleList(forIdentifier: identifier) { _ in | ||
| continuation.resume() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func testCompileFailureReturnsNilAndCaches() async { | ||
| WebViewIsolation.resetRuleListCacheForTests() | ||
| var calls = 0 | ||
| let original = WebViewIsolation.compileRuleList | ||
| WebViewIsolation.compileRuleList = { _, _ in | ||
| calls += 1 | ||
| return nil | ||
| } | ||
| defer { | ||
| WebViewIsolation.compileRuleList = original | ||
| WebViewIsolation.resetRuleListCacheForTests() | ||
| } | ||
|
|
||
| let first = await WebViewIsolation.ruleList() | ||
| let second = await WebViewIsolation.ruleList() | ||
|
|
||
| XCTAssertNil(first) | ||
| XCTAssertNil(second) | ||
| XCTAssertEqual(calls, 1) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| #endif | ||
Oops, something went wrong.
Oops, something went wrong.
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.