This repository was archived by the owner on Feb 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathAutoconsentUserScript.swift
446 lines (392 loc) · 18 KB
/
AutoconsentUserScript.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//
// AutoconsentUserScript.swift
//
// Copyright © 2021 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import WebKit
import BrowserServicesKit
import Common
import UserScript
import PrivacyDashboard
import os.log
protocol AutoconsentUserScriptDelegate: AnyObject {
func autoconsentUserScript(consentStatus: CookieConsentInfo)
}
protocol UserScriptWithAutoconsent: UserScript {
var delegate: AutoconsentUserScriptDelegate? { get set }
}
final class AutoconsentUserScript: NSObject, WKScriptMessageHandlerWithReply, UserScriptWithAutoconsent {
private struct Constants {
static let filterListCmpName = "filterList" // special CMP name used for reports from the cosmetic filterlist
}
static let newSitePopupHiddenNotification = Notification.Name("newSitePopupHidden")
var injectionTime: WKUserScriptInjectionTime { .atDocumentStart }
var forMainFrameOnly: Bool { false }
private weak var selfTestWebView: WKWebView?
private weak var selfTestFrameInfo: WKFrameInfo?
private var topUrl: URL?
private let preferences = CookiePopupProtectionPreferences.shared
private let management = AutoconsentManagement.shared
public var messageNames: [String] { MessageName.allCases.map(\.rawValue) }
let source: String
private let config: PrivacyConfiguration
weak var delegate: AutoconsentUserScriptDelegate?
init(scriptSource: ScriptSourceProviding, config: PrivacyConfiguration) {
Logger.autoconsent.debug("Initialising autoconsent userscript")
source = Self.loadJS("autoconsent-bundle", from: .main, withReplacements: [:])
self.config = config
}
func userContentController(_ userContentController: WKUserContentController,
didReceive message: WKScriptMessage) {
// this is never used because macOS <11 is not supported by autoconsent
}
@MainActor
func refreshDashboardState(consentManaged: Bool, cosmetic: Bool?, optoutFailed: Bool?, selftestFailed: Bool?) {
let consentStatus = CookieConsentInfo(
consentManaged: consentManaged, cosmetic: cosmetic, optoutFailed: optoutFailed, selftestFailed: selftestFailed
)
Logger.autoconsent.debug("Refreshing dashboard state: \(String(describing: consentStatus))")
self.delegate?.autoconsentUserScript(consentStatus: consentStatus)
}
@MainActor
func userContentController(_ userContentController: WKUserContentController,
didReceive message: WKScriptMessage,
replyHandler: @escaping (Any?, String?) -> Void) {
Logger.autoconsent.debug("Message received: \(String(describing: message.body))")
return handleMessage(replyHandler: replyHandler, message: message)
}
}
extension AutoconsentUserScript {
enum MessageName: String, CaseIterable {
case `init`
case cmpDetected
case eval
case popupFound
case optOutResult
case optInResult
case selfTestResult
case autoconsentDone
case autoconsentError
}
struct InitMessage: Codable {
let type: String
let url: String
}
struct CmpDetectedMessage: Codable {
let type: String
let cmp: String
let url: String
}
struct EvalMessage: Codable {
let type: String
let id: String
let code: String
}
struct PopupFoundMessage: Codable {
let type: String
let cmp: String // name of the Autoconsent rule that matched
let url: String
}
struct OptOutResultMessage: Codable {
let type: String
let cmp: String // name of the Autoconsent rule that matched
let result: Bool
let scheduleSelfTest: Bool
let url: String
}
struct OptInResultMessage: Codable {
let type: String
let cmp: String // name of the Autoconsent rule that matched
let result: Bool
let scheduleSelfTest: Bool
let url: String
}
struct SelfTestResultMessage: Codable {
let type: String
let cmp: String // name of the Autoconsent rule that matched
let result: Bool
let url: String
}
struct AutoconsentDoneMessage: Codable {
let type: String
let cmp: String // name of the Autoconsent rule that matched
let url: String
let isCosmetic: Bool
}
func decodeMessageBody<Input: Any, Target: Codable>(from message: Input) -> Target? {
do {
let json = try JSONSerialization.data(withJSONObject: message)
return try JSONDecoder().decode(Target.self, from: json)
} catch {
Logger.autoconsent.error("Error decoding message body: \(error.localizedDescription, privacy: .public)")
return nil
}
}
}
extension AutoconsentUserScript {
@MainActor
func handleMessage(replyHandler: @escaping (Any?, String?) -> Void,
message: WKScriptMessage) {
guard let messageName = MessageName(rawValue: message.name) else {
replyHandler(nil, "Unknown message type")
return
}
switch messageName {
case MessageName.`init`:
handleInit(message: message, replyHandler: replyHandler)
case MessageName.eval:
handleEval(message: message, replyHandler: replyHandler)
case MessageName.popupFound:
handlePopupFound(message: message, replyHandler: replyHandler)
case MessageName.optOutResult:
handleOptOutResult(message: message, replyHandler: replyHandler)
case MessageName.optInResult:
// this is not supported in browser
Logger.autoconsent.debug("ignoring optInResult: \(String(describing: message.body))")
replyHandler(nil, "opt-in is not supported")
case MessageName.cmpDetected:
// no need to do anything here
replyHandler([ "type": "ok" ], nil) // this is just to prevent a Promise rejection
case MessageName.selfTestResult:
handleSelfTestResult(message: message, replyHandler: replyHandler)
case MessageName.autoconsentDone:
handleAutoconsentDone(message: message, replyHandler: replyHandler)
case MessageName.autoconsentError:
Logger.autoconsent.error("Autoconsent error: \(String(describing: message.body))")
replyHandler([ "type": "ok" ], nil) // this is just to prevent a Promise rejection
}
}
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),
let url = URL(string: messageData.url) else {
assertionFailure("Received a malformed message from autoconsent")
replyHandler(nil, "cannot decode message")
return
}
guard url.navigationalScheme?.isHypertextScheme == true else {
// ignore special schemes
Logger.autoconsent.debug("Ignoring special URL scheme: \(messageData.url)")
replyHandler([ "type": "ok" ], nil) // this is just to prevent a Promise rejection
return
}
if preferences.isAutoconsentEnabled == false {
// this will only happen if the user has just declined a prompt in this tab
replyHandler([ "type": "ok" ], nil) // this is just to prevent a Promise rejection
return
}
let topURLDomain = message.webView?.url?.host
guard config.isFeature(.autoconsent, enabledForDomain: topURLDomain) else {
Logger.autoconsent.info("disabled for site: \(String(describing: url.absoluteString))")
replyHandler([ "type": "ok" ], nil) // this is just to prevent a Promise rejection
return
}
if message.frameInfo.isMainFrame {
topUrl = url
// reset dashboard state
refreshDashboardState(
consentManaged: management.sitesNotifiedCache.contains(url.host ?? ""),
cosmetic: nil,
optoutFailed: nil,
selftestFailed: nil
)
}
let remoteConfig = self.config.settings(for: .autoconsent)
let disabledCMPs = remoteConfig["disabledCMPs"] as? [String] ?? []
let filterlistExceptions = remoteConfig["filterlistExceptions"] as? [String] ?? []
#if DEBUG
// The `filterList` feature flag being disabled causes the integration test suite to fail - this is a temporary change to hardcode the
// flag to true when integration tests are running. In all other cases, continue to use the flag as usual.
let enableFilterList: Bool
if [.integrationTests].contains(NSApp.runType) {
enableFilterList = true
} else {
enableFilterList = config.isSubfeatureEnabled(AutoconsentSubfeature.filterlist) && !self.matchDomainList(domain: topURLDomain, domainsList: filterlistExceptions)
}
#else
let enableFilterList = config.isSubfeatureEnabled(AutoconsentSubfeature.filterlist) && !self.matchDomainList(domain: topURLDomain, domainsList: filterlistExceptions)
#endif
let autoconsentConfig = [
"type": "initResp",
"rules": nil, // rules are bundled with the content script atm
"config": [
"enabled": true,
"autoAction": preferences.isAutoconsentEnabled == true ? "optOut" : nil,
"disabledCmps": disabledCMPs,
"enablePrehide": true,
"enableCosmeticRules": true,
"detectRetries": 20,
"isMainWorld": false,
"enableFilterList": enableFilterList
] as [String: Any?]
] as [String: Any?]
Logger.autoconsent.debug("autoconsent config: \(String(describing: autoconsentConfig))")
replyHandler(autoconsentConfig, nil)
}
@MainActor
func handleEval(message: WKScriptMessage, replyHandler: @escaping (Any?, String?) -> Void) {
guard let messageData: EvalMessage = decodeMessageBody(from: message.body) else {
assertionFailure("Received a malformed message from autoconsent")
replyHandler(nil, "cannot decode message")
return
}
let script = """
(() => {
try {
return !!(\(messageData.code));
} catch (e) {
// ignore any errors
return;
}
})();
"""
if let webview = message.webView {
webview.evaluateJavaScript(script, in: message.frameInfo, in: WKContentWorld.page, completionHandler: { (result) in
switch result {
case.failure(let error):
replyHandler(nil, "Error snippet: \(error)")
case.success(let value):
replyHandler(
[
"type": "evalResp",
"id": messageData.id,
"result": value
],
nil
)
}
})
} else {
replyHandler(nil, "missing frame target")
}
}
@MainActor
func handlePopupFound(message: WKScriptMessage, replyHandler: @escaping (Any?, String?) -> Void) {
guard let messageData: PopupFoundMessage = decodeMessageBody(from: message.body),
let url = URL(string: messageData.url),
let host = url.host else {
assertionFailure("Received a malformed message from autoconsent")
replyHandler(nil, "cannot decode message")
return
}
Logger.autoconsent.debug("Cookie popup found: \(String(describing: messageData))")
// if popupFound is sent with "filterList", it indicates that cosmetic filterlist matched in the prehide stage,
// but a real opt-out may still follow. See https://github.com/duckduckgo/autoconsent/blob/main/api.md#messaging-api
if messageData.cmp == Constants.filterListCmpName {
refreshDashboardState(consentManaged: true, cosmetic: true, optoutFailed: false, selftestFailed: nil)
// trigger animation, but do not cache it because it can still be overridden
if !management.sitesNotifiedCache.contains(host) {
Logger.autoconsent.debug("Starting animation for cosmetic filters")
// post popover notification
NotificationCenter.default.post(name: Self.newSitePopupHiddenNotification, object: self, userInfo: [
"topUrl": self.topUrl ?? url,
"isCosmetic": true
])
}
}
replyHandler([ "type": "ok" ], nil) // this is just to prevent a Promise rejection
}
@MainActor
func handleOptOutResult(message: WKScriptMessage, replyHandler: @escaping (Any?, String?) -> Void) {
guard let messageData: OptOutResultMessage = decodeMessageBody(from: message.body) else {
assertionFailure("Received a malformed message from autoconsent")
replyHandler(nil, "cannot decode message")
return
}
Logger.autoconsent.debug("opt-out result: \(String(describing: messageData))")
if !messageData.result {
refreshDashboardState(consentManaged: true, cosmetic: nil, optoutFailed: true, selftestFailed: nil)
} else if messageData.scheduleSelfTest {
// save a reference to the webview and frame for self-test
selfTestWebView = message.webView
selfTestFrameInfo = message.frameInfo
}
replyHandler([ "type": "ok" ], nil) // this is just to prevent a Promise rejection
}
@MainActor
func handleAutoconsentDone(message: WKScriptMessage, replyHandler: @escaping (Any?, String?) -> Void) {
// report a managed popup
guard let messageData: AutoconsentDoneMessage = decodeMessageBody(from: message.body),
let url = URL(string: messageData.url),
let host = url.host else {
assertionFailure("Received a malformed message from autoconsent")
replyHandler(nil, "cannot decode message")
return
}
Logger.autoconsent.debug("opt-out successful: \(String(describing: messageData))")
refreshDashboardState(consentManaged: true, cosmetic: messageData.isCosmetic, optoutFailed: false, selftestFailed: nil)
// trigger popup once per domain
if !management.sitesNotifiedCache.contains(host) {
management.sitesNotifiedCache.insert(host)
if messageData.cmp != Constants.filterListCmpName { // filterlist animation should have been triggered already (see handlePopupFound)
Logger.autoconsent.debug("Starting animation for the handled cookie popup")
// post popover notification
NotificationCenter.default.post(name: Self.newSitePopupHiddenNotification, object: self, userInfo: [
"topUrl": self.topUrl ?? url,
"isCosmetic": messageData.isCosmetic
])
}
}
replyHandler([ "type": "ok" ], nil) // this is just to prevent a Promise rejection
if let selfTestWebView = selfTestWebView,
let selfTestFrameInfo = selfTestFrameInfo {
Logger.autoconsent.debug("requesting self-test in: \(messageData.url)")
selfTestWebView.evaluateJavaScript(
"window.autoconsentMessageCallback({ type: 'selfTest' })",
in: selfTestFrameInfo,
in: WKContentWorld.defaultClient,
completionHandler: { (result) in
switch result {
case.failure(let error):
Logger.autoconsent.error("Error running self-test: \(error.localizedDescription, privacy: .public)")
case.success:
Logger.autoconsent.debug("self-test requested")
}
}
)
} else {
Logger.autoconsent.error("no self-test scheduled in this tab")
}
selfTestWebView = nil
selfTestFrameInfo = nil
}
@MainActor
func handleSelfTestResult(message: WKScriptMessage, replyHandler: @escaping (Any?, String?) -> Void) {
guard let messageData: SelfTestResultMessage = decodeMessageBody(from: message.body),
let url = URL(string: messageData.url) else {
assertionFailure("Received a malformed message from autoconsent")
replyHandler(nil, "cannot decode message")
return
}
// store self-test result
Logger.autoconsent.debug("self-test result: \(String(describing: messageData))")
refreshDashboardState(consentManaged: true, cosmetic: nil, optoutFailed: false, selftestFailed: messageData.result)
replyHandler([ "type": "ok" ], nil) // this is just to prevent a Promise rejection
}
}