-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathTrackingProtectionPageStats.swift
228 lines (191 loc) · 8.74 KB
/
TrackingProtectionPageStats.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// This file is largely verbatim from Focus iOS (Blockzilla/Lib/TrackingProtection).
// The preload and postload js files are unmodified from Focus.
import Shared
import Deferred
struct TPPageStats {
let adCount: Int
let analyticCount: Int
let contentCount: Int
let socialCount: Int
var total: Int { return adCount + socialCount + analyticCount + contentCount }
init() {
adCount = 0
analyticCount = 0
contentCount = 0
socialCount = 0
}
private init(adCount: Int, analyticCount: Int, contentCount: Int, socialCount: Int) {
self.adCount = adCount
self.analyticCount = analyticCount
self.contentCount = contentCount
self.socialCount = socialCount
}
func create(byAddingListItem listItem: BlocklistName) -> TPPageStats {
switch listItem {
case .advertising: return TPPageStats(adCount: adCount + 1, analyticCount: analyticCount, contentCount: contentCount, socialCount: socialCount)
case .analytics: return TPPageStats(adCount: adCount, analyticCount: analyticCount + 1, contentCount: contentCount, socialCount: socialCount)
case .content: return TPPageStats(adCount: adCount, analyticCount: analyticCount, contentCount: contentCount + 1, socialCount: socialCount)
case .social: return TPPageStats(adCount: adCount, analyticCount: analyticCount, contentCount: contentCount, socialCount: socialCount + 1)
}
}
}
@available(iOS 11, *)
class TPStatsBlocklistChecker {
static let shared = TPStatsBlocklistChecker()
private var blockLists: TPStatsBlocklists?
func isBlocked(url: URL, isStrictMode: Bool) -> Deferred<BlocklistName?> {
let deferred = Deferred<BlocklistName?>()
guard let blockLists = blockLists, let host = url.host, !host.isEmpty else {
// TP Stats init isn't complete yet
deferred.fill(nil)
return deferred
}
// Make a copy on the main thread
let whitelistRegex = ContentBlockerHelper.whitelistedDomains.domainRegex
DispatchQueue.global().async {
let enabledLists = BlocklistName.forStrictMode(isOn: isStrictMode)
deferred.fill(blockLists.urlIsInList(url, whitelistedDomains: whitelistRegex).flatMap { return enabledLists.contains($0) ? $0 : nil })
}
return deferred
}
func startup() {
DispatchQueue.global().async {
let parser = TPStatsBlocklists()
parser.load()
DispatchQueue.main.async {
self.blockLists = parser
}
}
}
}
// The 'unless-domain' and 'if-domain' rules use wildcard expressions, convert this to regex.
func wildcardContentBlockerDomainToRegex(domain: String) -> NSRegularExpression? {
struct Memo { static var domains = [String: NSRegularExpression]() }
if let memoized = Memo.domains[domain] {
return memoized
}
// Convert the domain exceptions into regular expressions.
var regex = domain + "$"
if regex.first == "*" {
regex = "." + regex
}
regex = regex.replacingOccurrences(of: ".", with: "\\.")
do {
let result = try NSRegularExpression(pattern: regex, options: [])
Memo.domains[domain] = result
return result
} catch {
assertionFailure("Blocklists: \(error.localizedDescription)")
return nil
}
}
@available(iOS 11, *)
fileprivate class TPStatsBlocklists {
class Rule {
let regex: NSRegularExpression
let loadType: LoadType
let resourceType: ResourceType
let domainExceptions: [NSRegularExpression]?
let list: BlocklistName
init(regex: NSRegularExpression, loadType: LoadType, resourceType: ResourceType, domainExceptions: [NSRegularExpression]?, list: BlocklistName) {
self.regex = regex
self.loadType = loadType
self.resourceType = resourceType
self.domainExceptions = domainExceptions
self.list = list
}
}
private var blockRules = [String: [Rule]]()
enum LoadType {
case all
case thirdParty
}
enum ResourceType {
case all
case font
}
func load() {
// All rules have this prefix on the domain to match.
let standardPrefix = "^https?://([^/]+\\.)?"
for blockList in BlocklistName.all {
let list: [[String: AnyObject]]
do {
guard let path = Bundle.main.path(forResource: blockList.filename, ofType: "json") else {
assertionFailure("Blocklists: bad file path.")
return
}
let json = try Data(contentsOf: URL(fileURLWithPath: path))
guard let data = try JSONSerialization.jsonObject(with: json, options: []) as? [[String: AnyObject]] else {
assertionFailure("Blocklists: bad JSON cast.")
return
}
list = data
} catch {
assertionFailure("Blocklists: \(error.localizedDescription)")
return
}
for rule in list {
guard let trigger = rule["trigger"] as? [String: AnyObject],
let filter = trigger["url-filter"] as? String,
let filterRegex = try? NSRegularExpression(pattern: filter, options: []) else {
assertionFailure("Blocklists error: Rule has unexpected format.")
continue
}
guard let loc = filter.range(of: standardPrefix) else {
assert(false, "url-filter code needs updating for new list format")
return
}
let baseDomain = filter.substring(from: loc.upperBound).replacingOccurrences(of: "\\.", with: ".")
assert(!baseDomain.isEmpty)
// Sanity check for the lists.
["*", "?", "+"].forEach { x in
// This will only happen on debug
assert(!baseDomain.contains(x), "No wildcards allowed in baseDomain")
}
let domainExceptionsRegex = (trigger["unless-domain"] as? [String])?.flatMap { domain in
return wildcardContentBlockerDomainToRegex(domain: domain)
}
// Only "third-party" is supported; other types are not used in our block lists.
let loadTypes = trigger["load-type"] as? [String] ?? []
let loadType = loadTypes.contains("third-party") ? LoadType.thirdParty : .all
// Only "font" is supported; other types are not used in our block lists.
let resourceTypes = trigger["resource-type"] as? [String] ?? []
let resourceType = resourceTypes.contains("font") ? ResourceType.font : .all
let rule = Rule(regex: filterRegex, loadType: loadType, resourceType: resourceType, domainExceptions: domainExceptionsRegex, list: blockList)
blockRules[baseDomain] = (blockRules[baseDomain] ?? []) + [rule]
}
}
}
func urlIsInList(_ url: URL, whitelistedDomains: [NSRegularExpression]) -> BlocklistName? {
let resourceString = url.absoluteString
let resourceRange = NSRange(location: 0, length: resourceString.count)
guard let baseDomain = url.baseDomain, let rules = blockRules[baseDomain] else {
return nil
}
domainSearch: for rule in rules {
// First, test the top-level filters to see if this URL might be blocked.
if rule.regex.firstMatch(in: resourceString, options: .anchored, range: resourceRange) != nil {
// Check the domain exceptions. If a domain exception matches, this filter does not apply.
for domainRegex in (rule.domainExceptions ?? []) {
if domainRegex.firstMatch(in: resourceString, options: [], range: resourceRange) != nil {
continue domainSearch
}
}
// Check the whitelist.
if let baseDomain = url.baseDomain, !whitelistedDomains.isEmpty {
let range = NSRange(location: 0, length: baseDomain.count)
for ignoreDomain in whitelistedDomains {
if ignoreDomain.firstMatch(in: baseDomain, options: [], range: range) != nil {
return nil
}
}
}
return rule.list
}
}
return nil
}
}