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

Add support for filterlist exceptions in CPM #3642

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
28 changes: 25 additions & 3 deletions DuckDuckGo/Autoconsent/AutoconsentUserScript.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,24 @@ extension AutoconsentUserScript {
}
}

func matchDomainList(domain: String?, domainsList: [String]) -> Bool {
guard let domain = domain else { return false }
let trimmedDomains = domainsList.filter { !$0.trimmingWhitespace().isEmpty }

// Break domain apart to handle www.*
var tempDomain = domain
while tempDomain.contains(".") {
if trimmedDomains.contains(tempDomain) {
return true
}

let comps = tempDomain.split(separator: ".")
tempDomain = comps.dropFirst().joined(separator: ".")
}

return false
}

@MainActor
func handleInit(message: WKScriptMessage, replyHandler: @escaping (Any?, String?) -> Void) {
guard let messageData: InitMessage = decodeMessageBody(from: message.body),
Expand Down Expand Up @@ -236,9 +254,10 @@ extension AutoconsentUserScript {
}
let remoteConfig = self.config.settings(for: .autoconsent)
let disabledCMPs = remoteConfig["disabledCMPs"] as? [String] ?? []
let enableFilterList = config.isSubfeatureEnabled(AutoconsentSubfeature.filterlist)
let filterlistExceptions = remoteConfig["filterlistExceptions"] as? [String] ?? []
let enableFilterList = config.isSubfeatureEnabled(AutoconsentSubfeature.filterlist) && !self.matchDomainList(domain: topURLDomain, domainsList: filterlistExceptions)

replyHandler([
let autoconsentConfig = [
"type": "initResp",
"rules": nil, // rules are bundled with the content script atm
"config": [
Expand All @@ -251,7 +270,10 @@ extension AutoconsentUserScript {
"isMainWorld": false,
"enableFilterList": enableFilterList
] as [String: Any?]
] as [String: Any?], nil)
] as [String: Any?]
Logger.autoconsent.debug("autoconsent config: \(String(describing: autoconsentConfig))")

replyHandler(autoconsentConfig, nil)
}

@MainActor
Expand Down
50 changes: 50 additions & 0 deletions IntegrationTests/AutoconsentIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,56 @@ class AutoconsentIntegrationTests: XCTestCase {
XCTAssertTrue(isBannerHidden == true)
}

@MainActor
func testFilterlistRule_whenFakeCookieBannerIsDisplayed_bannerIsHidden() async throws {
// enable the feature
CookiePopupProtectionPreferences.shared.isAutoconsentEnabled = true
let url = URL(string: "http://privacy-test-pages.site/features/autoconsent/filterlist.html")!
let tab = self.tabViewModel.tab
// expect `cosmetic` to be published
let cookieConsentManagedPromise = tab.privacyInfoPublisher
.compactMap {
return $0?.$cookieConsentManaged
}
.switchToLatest()
.compactMap {
return $0?.isCosmeticRuleApplied == true ? true : nil
}
.receive(on: DispatchQueue.main)
.timeout(10)
.first()
.promise()

_=await tab.setUrl(url, source: .link)?.result

do {
let cookieConsentManaged = try await cookieConsentManagedPromise.value
XCTAssertTrue(cookieConsentManaged == true)
} catch {
struct ErrorWithHTML: Error, LocalizedError, CustomDebugStringConvertible {
let originalError: Error
let html: String

var errorDescription: String? {
(originalError as CustomDebugStringConvertible).debugDescription + "\nHTML:\n\(html)"
}
var debugDescription: String {
errorDescription!
}
}
let html = try await tab.webView.evaluateJavaScript("document.documentElement.outerHTML") as String?

if let html {
throw ErrorWithHTML(originalError: error, html: html)
} else {
throw error
}
}

let isBannerHidden = try await tab.webView.evaluateJavaScript("window.getComputedStyle(banner).opacity === '0'") as Bool?
XCTAssertTrue(isBannerHidden == true)
}

}

private extension CookieConsentInfo {
Expand Down
Loading