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

Ignore popup resources for top level default block #21

Merged
merged 3 commits into from
May 20, 2024
Merged
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
5 changes: 5 additions & 0 deletions Sources/TrackerRadarKit/ContentBlockerRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public struct ContentBlockerRule: Codable, Hashable {
return Trigger(urlFilter: filter, unlessDomain: urls, ifDomain: nil, resourceType: nil, loadType: loadTypes, loadContext: nil)
}

public static func trigger(urlFilter filter: String, resourceType types: [ResourceType]?,
loadTypes: [LoadType]?, loadContext: [LoadContext]?) -> Trigger {
return Trigger(urlFilter: filter, unlessDomain: nil, ifDomain: nil, resourceType: types, loadType: loadTypes, loadContext: loadContext)
}

public static func trigger(urlFilter filter: String, ifDomain domains: [String]?, resourceType types: [ResourceType]?) -> Trigger {
return Trigger(urlFilter: filter, unlessDomain: nil, ifDomain: domains, resourceType: types, loadType: [ .thirdParty ], loadContext: nil)
}
Expand Down
8 changes: 7 additions & 1 deletion Sources/TrackerRadarKit/ContentBlockerRulesBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,13 @@ public struct ContentBlockerRulesBuilder {
return [ ContentBlockerRule(trigger: .trigger(urlFilter: urlFilter,
unlessDomain: trackerData.relatedDomains(for: tracker.owner)?.wildcards(),
loadTypes: loadTypes),
action: .block()) ]
action: .block()),
ContentBlockerRule(trigger: .trigger(urlFilter: urlFilter,
resourceType: [.popup],
loadTypes: loadTypes,
loadContext: [.topFrame]),
action: .ignorePreviousRules())
]
}

private func buildRules(fromRule r: KnownTracker.Rule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ContentBlockerRulesBuilderTests: XCTestCase {
andTemporaryUnprotectedDomains: [])

// swiftlint:disable:next line_length
let domainFilter = "^(https?)?(wss?)?://([a-z0-9-]+\\.)*xvideos-cdn\\.com\\/v-c19d94e7937\\/v3\\/js\\/skins\\/min\\/default\\.header\\.static\\.js"
var domainFilter = "^(https?)?(wss?)?://([a-z0-9-]+\\.)*xvideos-cdn\\.com\\/v-c19d94e7937\\/v3\\/js\\/skins\\/min\\/default\\.header\\.static\\.js"
if let idx = rules.firstIndexOfExactFilter(filter: domainFilter) {
let nextRule = rules[idx + 1]
XCTAssertNotNil(nextRule, "Missing ignore-previous popup type rule")
Expand All @@ -62,6 +62,19 @@ class ContentBlockerRulesBuilderTests: XCTestCase {
} else {
XCTFail("Missing rule for testing")
}

// Test top level default block rule
domainFilter = "^(https?)?(wss?)?://([a-z0-9-]+\\.)*google-analytics\\.com(:?[0-9]+)?/.*"
if let idx = rules.firstIndexOfExactFilter(filter: domainFilter) {
let nextRule = rules[idx + 1]
XCTAssertNotNil(nextRule, "Missing ignore-previous popup type rule")
XCTAssert(nextRule.action == .ignorePreviousRules())
XCTAssert(nextRule.trigger.loadContext?.first == .topFrame)
XCTAssert(nextRule.trigger.resourceType?.first == .popup)
} else {
XCTFail("Missing rule for testing")
}

}

func testLoadingUnsupportedRules() throws {
Expand Down
Loading