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
|
/*
* This file is part of Privacy Badger <https://www.eff.org/privacybadger>
* Copyright (C) 2015 Electronic Frontier Foundation
*
* Derived from Chameleon <https://github.com/ghostwords/chameleon>
* Copyright (C) 2015 ghostwords
*
* Privacy Badger is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* Privacy Badger is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Privacy Badger. If not, see <http://www.gnu.org/licenses/>.
*/
function getFpPageScript() {
// code below is not a content script: no chrome.* APIs /////////////////////
// return a string
return "(" + function (DOCUMENT, dispatchEvent, CUSTOM_EVENT, ERROR, DATE, setTimeout, OBJECT) {
const V8_STACK_TRACE_API = !!(ERROR && ERROR.captureStackTrace);
if (V8_STACK_TRACE_API) {
ERROR.stackTraceLimit = Infinity; // collect all frames
} else {
// from https://github.com/csnover/TraceKit/blob/b76ad786f84ed0c94701c83d8963458a8da54d57/tracekit.js#L641
var geckoCallSiteRe = /^\s*(.*?)(?:\((.*?)\))?@?((?:file|https?|chrome):.*?):(\d+)(?::(\d+))?\s*$/i;
}
var event_id = DOCUMENT.currentScript.getAttribute('data-event-id');
// from Underscore v1.6.0
function debounce(func, wait, immediate) {
var timeout, args, context, timestamp, result;
var later = function () {
var last = DATE.now() - timestamp;
if (last < wait) {
timeout = setTimeout(later, wait - last);
} else {
timeout = null;
if (!immediate) {
result = func.apply(context, args);
context = args = null;
}
}
};
return function () {
context = this; // eslint-disable-line consistent-this
args = arguments;
timestamp = DATE.now();
var callNow = immediate && !timeout;
if (!timeout) {
timeout = setTimeout(later, wait);
}
if (callNow) {
result = func.apply(context, args);
context = args = null;
}
return result;
};
}
// messages the injected script
var send = (function () {
var messages = [];
// debounce sending queued messages
var _send = debounce(function () {
dispatchEvent.call(DOCUMENT, new CUSTOM_EVENT(event_id, {
detail: messages
}));
// clear the queue
messages = [];
}, 100);
return function (msg) {
// queue the message
messages.push(msg);
_send();
};
}());
/**
* Gets the stack trace by throwing and catching an exception.
* @returns {*} Returns the stack trace
*/
function getStackTraceFirefox() {
let stack;
try {
throw new ERROR();
} catch (err) {
stack = err.stack;
}
return stack.split('\n');
}
/**
* Gets the stack trace using the V8 stack trace API:
* https://github.com/v8/v8/wiki/Stack-Trace-API
* @returns {*} Returns the stack trace
*/
function getStackTrace() {
let err = {},
origFormatter,
stack;
origFormatter = ERROR.prepareStackTrace;
ERROR.prepareStackTrace = function (_, structuredStackTrace) {
return structuredStackTrace;
};
ERROR.captureStackTrace(err, getStackTrace);
stack = err.stack;
ERROR.prepareStackTrace = origFormatter;
return stack;
}
/**
* Strip away the line and column number (from stack trace urls)
* @param script_url The stack trace url to strip
* @returns {String} the pure URL
*/
function stripLineAndColumnNumbers(script_url) {
return script_url.replace(/:\d+:\d+$/, '');
}
/**
* Parses the stack trace for the originating script URL
* without using the V8 stack trace API.
* @returns {String} The URL of the originating script
*/
function getOriginatingScriptUrlFirefox() {
let trace = getStackTraceFirefox();
if (trace.length < 4) {
return '';
}
// this script is at 0, 1 and 2
let callSite = trace[3];
let scriptUrlMatches = callSite.match(geckoCallSiteRe);
return scriptUrlMatches && scriptUrlMatches[3] || '';
}
/**
* Parses the stack trace for the originating script URL.
* @returns {String} The URL of the originating script
*/
function getOriginatingScriptUrl() {
let trace = getStackTrace();
if (OBJECT.prototype.toString.call(trace) == '[object String]') {
// we failed to get a structured stack trace
trace = trace.split('\n');
// this script is at 0, 1, 2 and 3
let script_url_matches = trace[4].match(/\((http.*:\d+:\d+)/);
// TODO do we need stripLineAndColumnNumbers (in both places) here?
return script_url_matches && stripLineAndColumnNumbers(script_url_matches[1]) || stripLineAndColumnNumbers(trace[4]);
}
if (trace.length < 2) {
return '';
}
// this script is at 0 and 1
let callSite = trace[2];
if (callSite.isEval()) {
// argh, getEvalOrigin returns a string ...
let eval_origin = callSite.getEvalOrigin(),
script_url_matches = eval_origin.match(/\((http.*:\d+:\d+)/);
// TODO do we need stripLineAndColumnNumbers (in both places) here?
return script_url_matches && stripLineAndColumnNumbers(script_url_matches[1]) || stripLineAndColumnNumbers(eval_origin);
} else {
return callSite.getFileName();
}
}
/**
* Monitor the writes in a canvas instance
* @param item special item objects
*/
function trapInstanceMethod(item) {
var is_canvas_write = (
item.propName == 'fillText' || item.propName == 'strokeText'
);
item.obj[item.propName] = (function (orig) {
// set to true after the first write, if the method is not
// restorable. Happens if another library also overwrites
// this method.
var skip_monitoring = false;
function wrapped() {
var args = arguments;
if (is_canvas_write) {
// to avoid false positives,
// bail if the text being written is too short,
// of if we've already sent a monitoring payload
if (skip_monitoring || !args[0] || args[0].length < 5) {
return orig.apply(this, args);
}
}
var script_url = (
V8_STACK_TRACE_API ?
getOriginatingScriptUrl() :
getOriginatingScriptUrlFirefox()
),
msg = {
obj: item.objName,
prop: item.propName,
scriptUrl: script_url
};
if (item.hasOwnProperty('extra')) {
msg.extra = item.extra.apply(this, args);
}
send(msg);
if (is_canvas_write) {
// optimization: one canvas write is enough,
// restore original write method
// to this CanvasRenderingContext2D object instance
// Careful! Only restorable if we haven't already been replaced
// by another lib, such as the hidpi polyfill
if (this[item.propName] === wrapped) {
this[item.propName] = orig;
} else {
skip_monitoring = true;
}
}
return orig.apply(this, args);
}
OBJECT.defineProperty(wrapped, "name", { value: orig.name });
OBJECT.defineProperty(wrapped, "length", { value: orig.length });
OBJECT.defineProperty(wrapped, "toString", { value: orig.toString.bind(orig) });
return wrapped;
}(item.obj[item.propName]));
}
var methods = [];
['getImageData', 'fillText', 'strokeText'].forEach(function (method) {
var item = {
objName: 'CanvasRenderingContext2D.prototype',
propName: method,
obj: CanvasRenderingContext2D.prototype,
extra: function () {
return {
canvas: true
};
}
};
if (method == 'getImageData') {
item.extra = function () {
var args = arguments,
width = args[2],
height = args[3];
// "this" is a CanvasRenderingContext2D object
if (width === undefined) {
width = this.canvas.width;
}
if (height === undefined) {
height = this.canvas.height;
}
return {
canvas: true,
width: width,
height: height
};
};
}
methods.push(item);
});
methods.push({
objName: 'HTMLCanvasElement.prototype',
propName: 'toDataURL',
obj: HTMLCanvasElement.prototype,
extra: function () {
// "this" is a canvas element
return {
canvas: true,
width: this.width,
height: this.height
};
}
});
methods.forEach(trapInstanceMethod);
// save locally to keep from getting overwritten by site code
} + "(document, document.dispatchEvent, CustomEvent, Error, Date, setTimeout, Object));";
// code above is not a content script: no chrome.* APIs /////////////////////
}
// END FUNCTION DEFINITIONS ///////////////////////////////////////////////////
(function () {
// don't inject into non-HTML documents (such as XML documents)
// but do inject into XHTML documents
if (document instanceof HTMLDocument === false && (
document instanceof XMLDocument === false ||
document.createElement('div') instanceof HTMLDivElement === false
)) {
return;
}
// TODO race condition; fix waiting on https://crbug.com/478183
chrome.runtime.sendMessage({
type: "detectFingerprinting"
}, function (enabled) {
if (!enabled) {
return;
}
/**
* Communicating to webrequest.js
*/
var event_id = Math.random();
// listen for messages from the script we are about to insert
document.addEventListener(event_id, function (e) {
// pass these on to the background page
chrome.runtime.sendMessage({
type: "fpReport",
data: e.detail
});
});
window.injectScript(getFpPageScript(), {
event_id: event_id
});
});
}());
|