Skip to content
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

Fix: external application requests via redirect URLs shows wrong origin. #1900

Merged
merged 7 commits into from
Dec 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions DuckDuckGo/Tab/Navigation/ExternalAppSchemeHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ final class ExternalAppSchemeHandler {

extension ExternalAppSchemeHandler: NavigationResponder {

@MainActor
func decidePolicy(for navigationAction: NavigationAction, preferences: inout NavigationPreferences) async -> NavigationActionPolicy? {
// swiftlint:disable:next function_body_length
@MainActor func decidePolicy(for navigationAction: NavigationAction, preferences: inout NavigationPreferences) async -> NavigationActionPolicy? {
let externalUrl = navigationAction.url
// only proceed with non-external-scheme navigations
guard externalUrl.isExternalSchemeLink, let scheme = externalUrl.scheme else {
Expand Down Expand Up @@ -114,19 +114,18 @@ extension ExternalAppSchemeHandler: NavigationResponder {
}

let permissionType = PermissionType.externalScheme(scheme: scheme)
// use domain from the url for user-entered app schemes, otherwise use current website domain
let domain = navigationAction.isUserEnteredUrl ? navigationAction.url.host ?? "" : navigationAction.sourceFrame.securityOrigin.host
permissionModel.permissions([permissionType], requestedForDomain: domain, url: externalUrl) { [workspace, weak self] isGranted in
// Check for cross-origin redirects first, then use domain from the url for user-entered app schemes, then use current website domain
let redirectDomain = navigationAction.redirectHistory?.reversed().first(where: { $0.url.host != navigationAction.url.host })?.url.host
let domain = redirectDomain ?? (navigationAction.isUserEnteredUrl ? navigationAction.url.host ?? "" : navigationAction.sourceFrame.securityOrigin.host)
permissionModel.permissions([permissionType], requestedForDomain: domain, url: externalUrl) { [workspace] isGranted in
if isGranted {
workspace.open(externalUrl)

// if "Always allow" is set and this is the only navigation in the tab: close the tab
if self?.shouldCloseTabOnExternalAppOpen == true, let webView = navigationAction.targetFrame?.webView {
if self.shouldCloseTabOnExternalAppOpen == true, let webView = navigationAction.targetFrame?.webView {
webView.close()
}
}
}

return .cancel
}

Expand Down
Loading