Repository navigation
Keep the signer webview on the signer page #223
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
Open
tomas-martins-crossmint
wants to merge
2
commits into
main
Choose a base branch
from
tomas/wal-13316-check-that-signer-webview-messages-come-from-the-signer-page
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,117 @@ | ||
| // | ||
| // SignerPageFixture.swift | ||
| // CrossmintSDK | ||
| // | ||
| // Created by Tomas Martins on 30/10/26. | ||
| // | ||
|
|
||
| import Foundation | ||
| import WebKit | ||
| @testable import Web | ||
|
|
||
| @MainActor | ||
| final class SignerPageFixture { | ||
| static let SIGNER_URL = "crossmint-test://signer/" | ||
| static let SIGNER_SECOND_PAGE_URL = "crossmint-test://signer/second" | ||
| static let FOREIGN_URL = "crossmint-test://foreign/" | ||
|
|
||
| private static let PAGES: [String: String] = [ | ||
| SIGNER_URL: page(posting: "from.signer"), | ||
| SIGNER_SECOND_PAGE_URL: page(posting: "from.signer.second"), | ||
| "crossmint-test://signer/frame": """ | ||
| <script> | ||
| \(post("from.iframe")) | ||
| parent.postMessage("posted", "*"); | ||
| </script> | ||
| """, | ||
| FOREIGN_URL: """ | ||
| <script> | ||
| window.addEventListener("message", () => { | ||
| \(post("from.foreign")) | ||
| window.webkit.messageHandlers.probe.postMessage("foreign.done"); | ||
| }); | ||
| </script> | ||
| <iframe src="crossmint-test://signer/frame"></iframe> | ||
| """ | ||
| ] | ||
|
|
||
| let proxy = DefaultWebViewCommunicationProxy() | ||
| let webView: WKWebView | ||
| let recorder = MessageRecorder() | ||
| private let schemeHandler = PageSchemeHandler(pages: PAGES) | ||
|
|
||
| init() { | ||
| let configuration = WKWebViewConfiguration() | ||
| configuration.websiteDataStore = .nonPersistent() | ||
| configuration.setURLSchemeHandler(schemeHandler, forURLScheme: "crossmint-test") | ||
|
|
||
| let userContentController = WKUserContentController() | ||
| userContentController.add(webViewCommunicationProxy: proxy) | ||
| userContentController.add(recorder, name: "probe") | ||
| configuration.userContentController = userContentController | ||
|
|
||
| webView = WKWebView(frame: .zero, configuration: configuration) | ||
| webView.navigationDelegate = proxy | ||
| proxy.webView = webView | ||
| proxy.onUnknownMessage = { [recorder] type, _ in recorder.record(type) } | ||
| } | ||
|
|
||
| private static func page(posting type: String) -> String { | ||
| "<script>\(post(type))</script>" | ||
| } | ||
|
|
||
| private static func post(_ type: String) -> String { | ||
| "window.webkit.messageHandlers.crossmintMessageHandler.postMessage(JSON.stringify({ type: \"\(type)\" }));" | ||
| } | ||
| } | ||
|
|
||
| @MainActor | ||
| final class MessageRecorder: NSObject, WKScriptMessageHandler { | ||
| private(set) var types: [String] = [] | ||
| private var waiters: [String: CheckedContinuation<Void, Never>] = [:] | ||
|
|
||
| func record(_ type: String) { | ||
| types.append(type) | ||
| waiters.removeValue(forKey: type)?.resume() | ||
| } | ||
|
|
||
| func wait(for type: String) async { | ||
| guard !types.contains(type) else { return } | ||
| await withCheckedContinuation { waiters[type] = $0 } | ||
| } | ||
|
|
||
| func userContentController( | ||
| _ userContentController: WKUserContentController, | ||
| didReceive message: WKScriptMessage | ||
| ) { | ||
| guard let type = message.body as? String else { return } | ||
| record(type) | ||
| } | ||
| } | ||
|
|
||
| final class PageSchemeHandler: NSObject, WKURLSchemeHandler { | ||
| private let pages: [String: String] | ||
|
|
||
| init(pages: [String: String]) { | ||
| self.pages = pages | ||
| } | ||
|
|
||
| func webView(_ webView: WKWebView, start urlSchemeTask: any WKURLSchemeTask) { | ||
| guard let url = urlSchemeTask.request.url, let html = pages[url.absoluteString] else { | ||
| urlSchemeTask.didFailWithError(URLError(.fileDoesNotExist)) | ||
| return | ||
| } | ||
|
|
||
| let data = Data(html.utf8) | ||
| urlSchemeTask.didReceive(URLResponse( | ||
| url: url, | ||
| mimeType: "text/html", | ||
| expectedContentLength: data.count, | ||
| textEncodingName: "utf-8" | ||
| )) | ||
| urlSchemeTask.didReceive(data) | ||
| urlSchemeTask.didFinish() | ||
| } | ||
|
|
||
| func webView(_ webView: WKWebView, stop urlSchemeTask: any WKURLSchemeTask) {} | ||
| } | ||
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,70 @@ | ||
| // | ||
| // WebViewCommunicationProxyOriginTests.swift | ||
| // CrossmintSDK | ||
| // | ||
| // Created by Tomas Martins on 30/10/26. | ||
| // | ||
|
|
||
| import Foundation | ||
| import Testing | ||
| import WebKit | ||
| @testable import Web | ||
|
|
||
| @Suite("Signer page origin", .tags(.unit), .timeLimit(.minutes(1))) | ||
| @MainActor | ||
| struct WebViewCommunicationProxyOriginTests { | ||
| @Test func deliversMainFrameMessagesFromLoadedOrigin() async throws { | ||
| let fixture = SignerPageFixture() | ||
|
|
||
| try await fixture.proxy.loadURL(#require(URL(string: SignerPageFixture.SIGNER_URL))) | ||
| await fixture.recorder.wait(for: "from.signer") | ||
|
|
||
| #expect(fixture.recorder.types == ["from.signer"]) | ||
| } | ||
|
|
||
| @Test func dropsMessagesFromForeignPageAndItsIframe() async throws { | ||
| let fixture = SignerPageFixture() | ||
| try await fixture.proxy.loadURL(#require(URL(string: SignerPageFixture.SIGNER_URL))) | ||
| await fixture.recorder.wait(for: "from.signer") | ||
|
|
||
| fixture.webView.navigationDelegate = nil | ||
| try fixture.webView.load(URLRequest(url: #require(URL(string: SignerPageFixture.FOREIGN_URL)))) | ||
| await fixture.recorder.wait(for: "foreign.done") | ||
|
|
||
| fixture.webView.navigationDelegate = fixture.proxy | ||
| try fixture.webView.load(URLRequest(url: #require(URL(string: SignerPageFixture.SIGNER_SECOND_PAGE_URL)))) | ||
| await fixture.recorder.wait(for: "from.signer.second") | ||
|
|
||
| #expect(fixture.recorder.types == ["from.signer", "foreign.done", "from.signer.second"]) | ||
| } | ||
|
|
||
| struct NavigationCase: Sendable, CustomTestStringConvertible { | ||
| let url: String | ||
| let mainFrame: Bool | ||
| let allowed: Bool | ||
|
|
||
| var testDescription: String { "\(url) in \(mainFrame ? "main frame" : "subframe")" } | ||
| } | ||
|
|
||
| @Test(arguments: [ | ||
| NavigationCase(url: "crossmint-test://signer/second", mainFrame: true, allowed: true), | ||
| NavigationCase(url: "crossmint-test://foreign/", mainFrame: true, allowed: false), | ||
| NavigationCase(url: "https://signer/", mainFrame: true, allowed: false), | ||
| NavigationCase(url: "crossmint-test://signer:8080/", mainFrame: true, allowed: false), | ||
| NavigationCase(url: "crossmint-test://foreign/", mainFrame: false, allowed: true) | ||
| ]) | ||
| func allowsMainFrameNavigationOnlyWithinLoadedOrigin(_ navigation: NavigationCase) async throws { | ||
| let fixture = SignerPageFixture() | ||
| try await fixture.proxy.loadURL(#require(URL(string: SignerPageFixture.SIGNER_URL))) | ||
| let url = try #require(URL(string: navigation.url)) | ||
|
|
||
| #expect(fixture.proxy.allowsNavigation(to: url, inMainFrame: navigation.mainFrame) == navigation.allowed) | ||
|
tomas-martins-crossmint marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| @Test func blocksMainFrameNavigationBeforeAnyPageLoads() throws { | ||
| let fixture = SignerPageFixture() | ||
| let url = try #require(URL(string: SignerPageFixture.SIGNER_URL)) | ||
|
|
||
| #expect(!fixture.proxy.allowsNavigation(to: url, inMainFrame: true)) | ||
| } | ||
| } | ||
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.