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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
|
/**
* GNU LibreJS - A browser add-on to block nonfree nontrivial JavaScript.
* *
* Copyright (C) 2011, 2012, 2013, 2014 Loic J. Duros
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
*
* dom_handler.js
*
* After the HTML DOM has been parsed, domHandler finds all the scripts
* on a page (including inline, on-page, and external files), and triggers the
* JavaScript analysis for each of them.
*
*/
var {Cc, Ci, Cu, Cm, Cr} = require("chrome");
var scriptProperties =
require("html_script_finder/dom_handler/script_properties");
const scriptTypes = scriptProperties.scriptTypes;
const statusTypes = scriptProperties.statusTypes;
const reasons = scriptProperties.reasons;
var urlHandler = require("url_handler/url_handler");
var WebLabelFinder =
require("html_script_finder/web_labels/js_web_labels").WebLabelFinder;
// object model for script entries.
var scriptObject = require("html_script_finder/dom_handler/script_object");
var privacyChecker = require("js_checker/privacy_checker").privacyCheck;
var jsChecker = require("js_checker/js_checker");
const types = require("js_checker/constant_types");
var checkTypes = types.checkTypes;
var stripCDATAOpen = /<\!\[CDATA\[/gi;
var stripCDATAClose = /]]>/g;
var isDryRun = require("addon_management/prefchange").isDryRun;
var allowedRef = require('http_observer/allowed_referrers').allowedReferrers;
var attributeHelpers = require("html_script_finder/dom_handler/attributes");
// javascript:*
var jsInAttrRe = attributeHelpers.jsInAttrRe;
// the list of all available event attributes
var intrinsicEvents = attributeHelpers.intrinsicEvents;
var domGatherer =
require("html_script_finder/dom_handler/dom_gatherer").domGatherer;
var domChecker =
require("html_script_finder/dom_handler/dom_checker").domChecker;
/**
* The DomHandler object takes a whole document,
* finds script elements within that DOM, analyzes them
* using the js_checker module and finally returns a cleaned
* DOM depending on the result.
*/
var DomHandler = function() {
// external object with methods used
// in DomHandler
this.domGatherer = null;
// external object with methods used
// in DomHandler
this.domChecker = null;
this.dom = null;
this.pageURL = null;
// fragment found in url.
this.fragment = null;
// array containing all scripts on a page.
this.domScripts = [];
// array containing all scripts on a page,
// data related to them, such as parse tree, ...
this.inlineScripts = [];
this.externalScripts = [];
// all scripts.
this.scripts = [];
// keeps track of the number of scripts.
this.numScripts = 0;
// store the reference to the callback method
// presumably from htmlParser.
this.callback = function() {};
// boolean set to true if external scripts are loaded
// from the html page.
this.loadsHtmlExternalScripts = false;
this.jsCheckString = null;
/* object containing boolean property set to false if trivialness
is not allowed anymore (if another script defines ajax requests,
...) */
this.allowTrivial = null;
// boolean set to true if inline JavaScript
// is found to be free.
this.inlineJsFree = null;
// boolean set to true when at least one script
// has been removed.
this.hasRemovedScripts = null;
// boolean to check if scripts were removed
// prevents removeAllJs from running multiple times.
this.removedAllScripts = null;
// will eventually contain an array of data
// for the js web labels licenses.
this.licenseList = [];
// the response status for the page (200, 404, ...)
this.responseStatus = null;
// number of scripts fully tested.
this.scriptsTested = 0;
// number of external scripts to be tested.
this.numExternalScripts = null;
// number of inline/inattribute scripts
this.numInlineScripts = null;
};
/**
* Initialize properties of the object
*
* @param {domObject} obj A reference of the DOM object being
* analyzed.
*
* @param {pageURL} string The formatted URL (with fragment
* removed) of the corresponding page for this DOM
*
* @param {fragment} the #fragment from the url if applicable.
*
* @param {callback} the callback function.
*
*/
DomHandler.prototype.init = function(
domObject, pageURL, fragment, responseStatus, callback
) {
// initialize object properties.
console.debug('init', pageURL);
var that = this;
this.reset();
// arguments passed.
this.dom = domObject;
this.pageURL = pageURL;
this.fragment = fragment;
this.responseStatus = responseStatus;
console.debug('in dom handler, responseStatus is', this.responseStatus);
// make callback function available
// for the entire object.
this.callback = function (dom) {
callback(dom);
that.destroy();
};
};
DomHandler.prototype.reset = function () {
this.dom = null;
// arrays.
this.onEventElement = [];
this.scriptStatus = [];
this.inlineScripts = [];
this.externalScripts = [];
this.scripts = [];
// booleans
this.allowTrivial = true;
this.inlineJsFree = false;
this.hasRemovedScripts = false;
this.removedAllScripts = false;
// we start with 0, and will increment in
// dom_checker.
this.numExternalScripts = 0;
this.numInlineScripts = 0;
this.scriptsTested = 0;
};
DomHandler.prototype.destroy = function () {
this.domGatherer = null;
this.domChecker = null;
/* destroy callback so that it can't be called multiple times. */
this.callback = function() {};
//this.reset();
};
DomHandler.prototype.scriptHasBeenTested = function() {
this.scriptsTested++;
console.debug('incremented DomHandler.scriptsTested to',
this.scriptsTested);
};
/**
* scriptHasJsWebLabel
*
* Checks if a script was found earlier in a Js License Web Label
* table. See http://www.gnu.org/licenses/javascript-labels.html
* for more information.
*
*/
DomHandler.prototype.scriptHasJsWebLabel = function(script) {
if (this.licenseList) {
var url = urlHandler.resolve(this.pageURL, script.src),
i = 0,
len = this.licenseList.length;
console.debug('looking for web label');
for (; i < len; i++) {
if (this.licenseList[i].fileUrl === url &&
this.licenseList[i].free === true
) {
console.debug('found something true');
console.debug(
this.licenseList[i].fileUrl, ' is found');
return true;
}
}
}
return false;
};
/**
* processScripts.
* Starts by looking for a js web labels page
* then calls the complete function, which runs
* the rest of the check.
*/
DomHandler.prototype.processScripts = function () {
var that = this;
// check for the existence of the
// js web labels first.
this.lookForJsWebLabels(function () {
// gather and check all script elements on
// page.
console.debug("Calling checkAllScripts");
that.checkAllScripts();
});
};
/**
* jsWebLabelsComplete
*
*/
DomHandler.prototype.checkAllScripts = function () {
try {
console.debug(
'found in', this.pageURL, JSON.stringify(this.licenseList));
console.debug('checkAllScripts triggered async');
// use domGatherer to gather scripts.
this.domGatherer.findScripts();
this.domGatherer.gatherScriptsContent();
this.domGatherer.gatherIntrinsicEvents();
console.debug('fragment is', this.fragment);
if (
this.fragment === undefined ||
this.fragment === null ||
this.fragment.indexOf('librejs=true') < 0
) {
try {
// use domChecker to check scripts.
console.debug("Calling checkAllInlineScripts");
this.domChecker.checkAllInlineScripts();
} catch (x) {
console.debug('error in domChecker:', x, x.lineNumber);
this.removeAllJs();
}
} else {
console.debug('This is a pageworker, removing all js');
// this is the Page Worker to find contact link
// just remove all the JS since we don't need it.
console.debug('fragment found, remove js');
this.removeAllJs();
}
} catch (x) {
console.debug('error', x, x.lineNumber, x.fileName);
}
};
/**
* lookForJsWebLabels
*
* Checks if a link to a js web label table exists.
* If it does, return an array of objects with the data
* gathered (script name, path, license name, url, ...)
*
*/
DomHandler.prototype.lookForJsWebLabels = function (completed) {
var that = this;
console.debug("calling lookForJsWebLabels");
if (this.fragment !== '#librejs=true') {
var webLabelFinder = new WebLabelFinder();
webLabelFinder.init(
this.dom,
this.pageURL,
function (licenseList) {
// assign array returned to property.
that.licenseList = licenseList;
console.debug("calling completed");
completed();
});
} else {
completed();
}
};
DomHandler.prototype.checkScriptForJsWebLabels = function(script) {
var scriptEntry;
if (this.hasSrc(script) && this.scriptHasJsWebLabel(script)) {
// This script is in the list of allowed scripts (through web labels)
scriptEntry = scriptObject.Script({
'type': scriptTypes.EXTERNAL,
'status': statusTypes.ACCEPTED,
'element': script,
'url': urlHandler.resolve(this.pageURL, script.src)
});
scriptEntry.tagAsAccepted(this.pageURL, reasons.FREE);
return true;
}
};
/**
* hasSrc
* Check the given script has an src attribute.
* @param script obj The script element.
* @return a string with the value of the src attribute.
*/
DomHandler.prototype.hasSrc = function(script) {
if (script.src) {
return script.src;
}
return false;
};
/**
* Uses relationChecker to guess whether the script only uses
* predefined functions/variables or interacts with other scripts
* (this is still very experimental and needs improvement.)
*
*/
DomHandler.prototype.removeScriptIfDependent = function (script) {
var nonWindowProps = script.tree.relationChecker.nonWindowProperties;
for (var entry in nonWindowProps) {
if (nonWindowProps[entry]) {
console.debug('script has non window properties.');
this.removeGivenJs(script, reasons.TRIVIAL_NOT_ALLOWED);
return true;
}
}
};
/**
* removeGivenJs
* Remove a single script from the DOM.
* @param script Obj The script element to be removed from the
* DOM.
*
*/
DomHandler.prototype.removeGivenJs = function (script, reason, singleton, hash) {
var commentedOut;
var isAllowed = allowedRef.urlInAllowedReferrers(this.pageURL);
console.debug("removing given js hash", hash);
if (script.status != statusTypes.REJECTED &&
script.status != statusTypes.JSWEBLABEL
) {
console.debug('removing a', script.type);
if (script.type === scriptTypes.ATTRIBUTE &&
!isAllowed
) {
this.removeGivenAttribute(script, reason);
return;
}
if (!isAllowed) {
// set invalid type if dry run off.
script.element.setAttribute('type', 'librejs/blocked');
// add entry as removed.
console.debug('removeGivenJs hash is', hash);
script.tagAsRemoved(this.pageURL, reason, hash);
} else {
script.element.setAttribute(
'data-librejs-dryrun', 'librejs/blocked');
script.tagAsDryRun(this.pageURL, reason, hash);
}
if (singleton === true) {
// flag singletons.
script.element.setAttribute('data-singleton', 'true');
}
// remove src if dry run off.
if (script.element.getAttribute('src') !== undefined) {
script.element.setAttribute(
'data-librejs-blocked-src',
script.element.getAttribute('src')
);
if (!isAllowed) {
script.element.removeAttribute('src');
}
}
if (isAllowed) {
comment_str = 'LibreJS: Script should be blocked, but page is whitelisted.';
script.status = statusTypes.ACCEPTED;
} else {
comment_str = 'LibreJS: script blocked.';
script.status = statusTypes.REJECTED;
}
commentedOut = this.dom.createComment(comment_str);
// add a comment for curious source readers.
script.element.parentNode.appendChild(commentedOut);
script.element.parentNode.insertBefore(commentedOut, script.element);
this.hasRemovedScripts = true;
}
};
DomHandler.prototype.removeGivenAttribute = function (script, reason) {
var i = 0,
le = script.jsAttributes.length;
console.debug('removing given attribute', script, reason);
script.element.setAttribute('data-librejs-blocked-event',
JSON.stringify(script.jsAttributes));
script.tagAsRemoved(this.pageURL, reason, script.hash || script.tree.hash);
// might need to be removed.
script.element.setAttribute('data-librejs-blocked-value', '');
if (!allowedRef.urlInAllowedReferrers(this.pageURL)) {
// only run if not in dry run mode.
for (; i < le; i++) {
console.debug('removing attribute', JSON.stringify(script.jsAttributes));
script.element.removeAttribute(script.jsAttributes[i].attribute);
}
} else {
}
this.hasRemovedScripts = true;
};
/**
* removeAllJs
* Loop through all scripts from top to bottom and add a type
* attribute 'librejs/blocked' to prevent their interpretation
* by the browser.
*
*/
DomHandler.prototype.removeAllJs = function (reason) {
// remove all js is useless from now on.
console.debug('removeAllJs');
this.hasRemovedScripts = true;
// removeAllJs needs not be run next time.
this.removedAllScripts = true;
try {
this.removeAllArray(this.scripts, reason);
this.callback(this.dom);
} catch (x) {
console.debug(
'in removeAllJs method: ',
x,
'number of scripts is',
this.numScripts
);
this.callback(this.dom);
}
};
DomHandler.prototype.removeAllArray = function(scriptArray, reason) {
var script, i = 0, le;
console.debug('removeAllArray');
try {
le = scriptArray.length;
// loop through all scripts.
for (; i < le; i++) {
script = scriptArray[i];
if (script.type === scriptTypes.INLINE ||
script.type === scriptTypes.EXTERNAL
) {
this.removeGivenJs(script, reason);
}
else if (script.type === scriptTypes.ATTRIBUTE) {
this.removeGivenAttribute(script, reason);
}
}
} catch (e) {
this.callback("");
}
};
exports.DomHandler = DomHandler;
/**
* exports.domHandler
* Instantiates a DomHandler and checks the DOM
* @param dom obj The given dom for analysis.
* @param pageURL string the URL for the page.
* @param callback function callback when all the work has been performed.
*/
exports.domHandler = function(
dom, pageURL, fragment, responseStatus, callback) {
console.debug("Creating domHandler");
var domHandler = new DomHandler();
domHandler.init(dom, pageURL, fragment, responseStatus, callback);
// use domGatherer methods.
domHandler.domGatherer = domGatherer(domHandler);
// use domChecker methods.
domHandler.domChecker = domChecker(domHandler);
// launch the whole process.
console.debug("Calling processScripts");
domHandler.processScripts();
};
|