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

Update Link Tracking Protection to preserve headers #600

Merged
merged 6 commits into from
Dec 22, 2023
Merged
Changes from 3 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
27 changes: 17 additions & 10 deletions Sources/BrowserServicesKit/LinkProtection/LinkProtection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public struct LinkProtection {
errorReporting: errorReporting)
}

private func getNewRequest(changingUrl url: URL, inRequest request: URLRequest) -> URLRequest {
SlayterDev marked this conversation as resolved.
Show resolved Hide resolved
var newRequest = request
newRequest.url = url
return newRequest
}

public mutating func setMainFrameUrl(_ url: URL?) {
mainFrameUrl = url
}
Expand Down Expand Up @@ -77,11 +83,12 @@ public struct LinkProtection {

// swiftlint:disable function_parameter_count
public func requestTrackingLinkRewrite(initiatingURL: URL?,
destinationURL: URL,
destinationRequest: URLRequest,
onStartExtracting: () -> Void,
onFinishExtracting: @escaping () -> Void,
onLinkRewrite: @escaping (URL) -> Void,
onLinkRewrite: @escaping (URLRequest) -> Void,
policyDecisionHandler: @escaping (Bool) -> Void) -> Bool {
let destinationURL = destinationRequest.url
if let mainFrameUrl = mainFrameUrl, destinationURL != mainFrameUrl {
// If mainFrameUrl is set and is different from destinationURL we will assume this is a redirect
// We do not rewrite redirects due to breakage concerns
Expand All @@ -91,7 +98,7 @@ public struct LinkProtection {
var didRewriteLink = false
if let newURL = linkCleaner.extractCanonicalFromAMPLink(initiator: initiatingURL, destination: destinationURL) {
policyDecisionHandler(false)
onLinkRewrite(newURL)
onLinkRewrite(getNewRequest(changingUrl: newURL, inRequest: destinationRequest))
didRewriteLink = true
} else if ampExtractor.urlContainsAMPKeyword(destinationURL) {
onStartExtracting()
Expand All @@ -103,13 +110,13 @@ public struct LinkProtection {
}

policyDecisionHandler(false)
onLinkRewrite(canonical)
onLinkRewrite(getNewRequest(changingUrl: canonical, inRequest: destinationRequest))
}
didRewriteLink = true
} else if let newURL = linkCleaner.cleanTrackingParameters(initiator: initiatingURL, url: destinationURL) {
if newURL != destinationURL {
policyDecisionHandler(false)
onLinkRewrite(newURL)
onLinkRewrite(getNewRequest(changingUrl: newURL, inRequest: destinationRequest))
didRewriteLink = true
}
}
Expand All @@ -121,10 +128,10 @@ public struct LinkProtection {
navigationAction: WKNavigationAction,
onStartExtracting: () -> Void,
onFinishExtracting: @escaping () -> Void,
onLinkRewrite: @escaping (URL, WKNavigationAction) -> Void,
onLinkRewrite: @escaping (URLRequest, WKNavigationAction) -> Void,
policyDecisionHandler: @escaping (WKNavigationActionPolicy) -> Void) -> Bool {
requestTrackingLinkRewrite(initiatingURL: initiatingURL,
destinationURL: navigationAction.request.url!,
destinationRequest: navigationAction.request,
onStartExtracting: onStartExtracting,
onFinishExtracting: onFinishExtracting,
onLinkRewrite: { onLinkRewrite($0, navigationAction) },
Expand All @@ -134,13 +141,13 @@ public struct LinkProtection {

@MainActor
public func requestTrackingLinkRewrite(initiatingURL: URL?,
destinationURL: URL,
destinationRequest: URLRequest,
onStartExtracting: () -> Void,
onFinishExtracting: @escaping () -> Void,
onLinkRewrite: @escaping (URL) -> Void) async -> Bool? {
onLinkRewrite: @escaping (URLRequest) -> Void) async -> Bool? {
await withCheckedContinuation { continuation in
let didRewriteLink = requestTrackingLinkRewrite(initiatingURL: initiatingURL,
destinationURL: destinationURL,
destinationRequest: destinationRequest,
onStartExtracting: onStartExtracting,
onFinishExtracting: onFinishExtracting,
onLinkRewrite: onLinkRewrite) { navigationActionPolicy in
Expand Down
Loading