Skip to content

Commit

Permalink
Ignore popup resources for top level default block (#21)
Browse files Browse the repository at this point in the history
* Ignore popup resources for top level default block

* Lint

* Add test
  • Loading branch information
SlayterDev authored May 20, 2024
1 parent 2a79006 commit c01e6a5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
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
15 changes: 14 additions & 1 deletion Tests/TrackerRadarKitTests/ContentBlockerRulesBuilderTests.swift
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

0 comments on commit c01e6a5

Please sign in to comment.