aboutsummaryrefslogtreecommitdiffstats
path: root/lib/html_script_finder/dom_handler/dom_checker.js
blob: 1a0f30e7507fe8de30f50f58e71943060b015cd1 (plain)
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
/**
 * 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_checker.js
 * 
 * checks scripts for nonfree/nontrivial.
 * 
 */

var {Cc, Ci, Cu, Cm, Cr} = require("chrome");
var timer = require("sdk/timers");

var scriptProperties = require("html_script_finder/dom_handler/script_properties");
const scriptTypes = scriptProperties.scriptTypes;
const statusTypes = scriptProperties.statusTypes;
const reasons = scriptProperties.reasons;

// ensure xhr won't create an infinite loop
// with html content.
var urlTester = require("html_script_finder/url_seen_tester").urlSeenTester;
var urlHandler = require("url_handler/url_handler");

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;

const getHash = require("script_entries/scripts_cache").scriptsCached.getHash;

var DomChecker = function() {
    // reference to domHandler instance
    // using this object.
    this.d = null;
};

/**
 * init
 * 
 * assign a reference domHandler object
 * to access/updates its properties.
 * 
 */
DomChecker.prototype.init = function(domHandler) {
    "use strict";

    this.d = domHandler;
};

DomChecker.prototype.destroy = function() {
    "use strict";

    this.d = null;
};

/**
 * checkAllInlineScripts
 * 
 * Sends all the inline/onpage scripts as a whole for a check and
 * removes all scripts if nonfree nontrivial is found.
 *
 */
DomChecker.prototype.checkAllInlineScripts = function() {
    "use strict";

    try {
        var i = 0, len, script;

        if (typeof this.d.inlineScripts !== 'undefined' &&
                this.d.inlineScripts.length > 0
        ) {
            script = this.d.inlineScripts.shift();
            console.debug("checking script for page",
                    this.d.pageURL
                    /*, JSON.stringify(script)*/);
            if (this.d.removedAllScripts) {
                // all js has already been removed.
                // stop check.
                console.debug("removed all");
                return;
            }

            if (this.d.inlineJsFree === true) {
                // add entry as accepted.
                try {
                    hash = getHash(script.text);
                    script.tagAsAccepted(this.d.pageURL, reasons.FREE, hash);
                } catch (e) {
                    console.debug(e);
                }
            }

            // even if page is free we need to check for allow trivial.
            if (script.type === scriptTypes.INLINE) {
                console.debug("analyzing script", script);
                this.analyzeJs(script,
                        script.text,
                        this.checkSingleInlineScript.bind(this));
            } else if (script.type === scriptTypes.ATTRIBUTE) {
                console.debug("analyzing inline script", script);
                this.analyzeJs(script,
                        this.concatAttributes(script),
                        this.checkSingleElementAttributes.bind(this));
            }
        } else {
            // no more inline scripts. Switch to external scripts.
            this.readyForExternal();
        }
    } catch (x) {
        console.debug('checkAllInlineScripts error',
                x, x.lineNumber, x.fileName);
        this.readyForExternal();
    }
};

DomChecker.prototype.concatAttributes = function(script) {
    "use strict";
    var i = 0,
        le = script.jsAttributes.length,
        text = "";

    // we concatenate all js in multiple attributes.
    // because it's too much of a hassle to keep track
    // otherwise.
    for (; i < le; i++) {
        text += script.jsAttributes[i].value + '\n';
    }

    return text;
};

/**
 *
 * check a single element with attributes
 */
DomChecker.prototype.checkSingleElementAttributes = function(
        script, loadedScript, checker) {
    "use strict";
    var check, value, 
        i = 0, 
        le = script.jsAttributes.length,
        text = "";

    try {
        check = checker.parseTree.freeTrivialCheck;
        script.tree = checker;
        script.result = check;		
        script.status = statusTypes.CHECKED;
    } catch (e) {
        console.debug('problem checking inline scripts', e, e.lineNumber);
        this.d.removeGivenJs(script);
    }

    this.processInlineCheckResult(script, check, checker);
};

DomChecker.prototype.processInlineCheckResult = function(
        script, check, checker) {
    "use strict";
    console.debug("check.reason is", check.reason, "and type", check.type);
    var hash = checker.hash;

    if (this.d.inlineJsFree === true) {
        console.debug('tagging', script.text, 'as accepted', "with reason", check.reason);
        script.tagAsAccepted(this.d.pageURL, this.d.freeReason + " -- " + check.reason, hash);
    }

    // process the result.
    if (check.type === checkTypes.FREE) {
        // this is free.
        console.debug('tagging', script.text, 'as accepted with reason', check.reason);
        this.d.inlineJsFree = true;
        this.d.freeReason = check.reason;
        // add entry as accepted.
        script.tagAsAccepted(this.d.pageURL, check.reason, hash);
    } else if (check.type === checkTypes.FREE_SINGLE_ITEM) {
        // accept this script.
        console.debug("free single item, ", check.reason);
        script.tagAsAccepted(this.d.pageURL, check.reason, hash);
    } else if (check.type === checkTypes.NONTRIVIAL) {
        console.debug("nontrivial hash is", hash);
        if (this.d.inlineJsFree) {
            // inline is free. So accept.
            console.debug('tagging', script.text, 'as accepted');
            script.tagAsAccepted(
                    this.d.pageURL,
                    this.d.freeReason + ' -- ' + check.reason,
                    hash);
        } else {
            console.debug('tagging', script.text, 'as removed');
            this.d.removeGivenJs(script, check.reason, false, hash);
        }
    } else if (!this.d.inlineJsFree &&
        this.d.loadsHtmlExternalScripts &&
        check.type === checkTypes.TRIVIAL_DEFINES_FUNCTION
    ) {
        // nontrivial, because defines function and loads
        // external scripts
        console.debug('tagging', script.text, 'as removed');
        this.d.removeGivenJs(script, reasons.FUNCTIONS_INLINE, false, hash);
    } else if (!this.d.loadsHtmlExternalScripts &&
            check === checkTypes.TRIVIAL_DEFINES_FUNCTION
    ) {
        console.debug("Tag as accepted doesn't load another external script");
        script.tagAsAccepted(this.d.pageURL, check.reason, hash);
    } else if (check.type === checkTypes.TRIVIAL || 
            check.type === checkTypes.TRIVIAL_DEFINES_FUNCTION ||
            check.type === checkTypes.WHITELISTED
    ) {
        // add entry as accepted.
        console.debug("Trivial accepted");
        script.tagAsAccepted(this.d.pageURL, check.reason, hash);
    } 

    // next inline script, if applicable.
    this.checkAllInlineScripts();
};

DomChecker.prototype.readyForExternal = function() {
    "use strict";

    console.debug('DomChecker.readyForExternal');
    // done with those inline scripts, continue with
    // the rest.
    this.checkExternalScripts();
};

/**
 * check a single inline script. 
 */
DomChecker.prototype.checkSingleInlineScript = function(
        script, loadedScript, checker) {
    "use strict";
    var check, text;

    console.debug('DomChecker.checkSingleInlineScript');

    try {

        check = checker.parseTree.freeTrivialCheck;

        // update status.
        script.tree = checker;
        script.result = check;
        console.debug("script result is", check.type);
        script.status = statusTypes.CHECKED;

    } catch (e) {
        console.debug('problem checking inline scripts', e, e.lineNumber);
        this.d.removeGivenJs(script, '', false, checker.hash);
    }

    this.processInlineCheckResult(script, check, checker);

};

/**
 * checkExternalScripts
 * Loop through series of external scripts,
 * perform xhr to get their data, and check them
 * to see whether they are free/nontrivial
 *
 */
DomChecker.prototype.checkExternalScripts = function() {
    "use strict";

    console.debug('DomChecker.checkExternalScripts');

    var i = 0;
    var len = this.d.externalScripts.length;
    var that = this;

    console.debug("externalScripts length", len);
    if (this.d.removedAllScripts || len === 0) {
        // all js has already been removed.
        // stop check.
        this.wrapUpBeforeLeaving();
        return;
    }

    for (; i < len; i++) {
        this.xhr(
            this.d.externalScripts[i],
            function(script, scriptText) {
                console.debug("In xhr callback for script url:", script.url);
                if (scriptText === false) {
                    that.d.removeGivenJs(script);
                    that.d.scriptHasBeenTested();
                    that.externalCheckIsDone();
                    return;
                }

                console.debug('about to analyzeJS for script:', script.url);
                that.analyzeJs(
                    script,
                    scriptText,
                    that.checkSingleExternalScript.bind(that));
            }
        );
    }
};

DomChecker.prototype.wrapUpBeforeLeaving = function() {
    "use strict";

    console.debug("wrap up before leaving triggered");
    console.debug('wrapping up');
    this.d.callback(this.d.dom);

};

DomChecker.prototype.analyzeJs = function(script, scriptText, callback) {
    "use strict";
    console.debug('DomChecker.analyzeJs for script:', script.url);
    try {
        var checker = jsChecker.jsChecker();
        var url = "";
        if (typeof script.url !== "undefined") {
            url = script.url;
        } else {
            url = this.pageURL;
        }
        checker.searchJs(scriptText, function() {
            console.debug("Analyze JS"/*, JSON.stringify(checker)*/);
            timer.setTimeout(function() {
                callback(script, scriptText, checker);
            }, 0);
        }, url);
    } catch (x) {
        console.debug('error', x, x.lineNumber, x.fileName);
    }
};

/**
 * Check a single external script.
 */
DomChecker.prototype.checkSingleExternalScript = function(
    script, loadedScript, checker
) {
    "use strict";
    var check;

    console.debug('DomChecker.checkSingleExternalScript()');
    try {
        check = checker.parseTree.freeTrivialCheck;

        script.tree = checker;
        script.result = check;
        console.debug('in checkSingleExternalScript, checker.hash is',
                      checker.hash);
        if (script.status != statusTypes.JSWEBLABEL) {
            script.status = statusTypes.CHECKED;		
        } 

        if (check.type === checkTypes.FREE ||
                check.type === checkTypes.FREE_SINGLE_ITEM
        ) {
            // add entry as accepted.
            script.tagAsAccepted(this.d.pageURL, check.reason, checker.hash);
        } 

        else if (check.type === checkTypes.NONTRIVIAL) {
            console.debug("Removing given js", check.reason);
            this.d.removeGivenJs(script, check.reason, false, checker.hash);
        }

        else if (check.type === checkTypes.TRIVIAL ||
                check.type === checkTypes.WHITELISTED
        ) {
            // if it's accepted, allow.
            script.tagAsAccepted(this.d.pageURL, check.reason, checker.hash);
        } else {
            // anything else is nontrivial. Including TRIVIAL_DEFINES_FUNCTION.
            console.debug("checker hash for remove is ", checker.hash);
            this.d.removeGivenJs(
                script, reasons.FUNCTIONS_EXTERNAL, false, checker.hash);
        } 

    } catch (e) {
        console.debug('error in checkExternalScript',
                      e, e.lineNumber, 'for script', script.url);

        this.d.removeAllJs();
        this.destroy();
        return;
    }
    console.debug('script url is', script.url, 'result is', script.result);
    this.d.scriptHasBeenTested();
    this.externalCheckIsDone();
};

DomChecker.prototype.externalCheckIsDone = function() {
    "use strict";
    console.debug('DomChecker.externalCheckIsDone');

    console.debug('scriptsTested is', this.d.scriptsTested);
    console.debug('num external', this.d.numExternalScripts);

    if (this.d.scriptsTested  >= this.d.numExternalScripts) {
        console.debug('wrapping up external');
        this.wrapUpBeforeLeaving();
    } else {
        var scriptsToCheck = this.d.numExternalScripts - this.d.scriptsTested;
        console.debug('Not wrapping up! Waiting to check ' + scriptsToCheck +
                      ' more script(s)');

        if (this.d.externalScripts[0]) {
            console.debug('script 0 is', this.d.externalScripts[0]);
        }
        if (this.d.externalScripts[1]) {
            console.debug('script 1 is', this.d.externalScripts[1]);
        }
    }
};

/**
 * xhr
 * Perform a XMLHttpRequest on the url given.
 * @param url string A URL.
 * @return The response text.
 */
DomChecker.prototype.xhr = function(script, responseCallback) {
    "use strict";

    var regex = /^text\/html/i;	
    var url = script.url;

    try {
        // add url to whitelist.
        urlTester.addUrl(url);

        // request module. Compatible with Https-Everywhere.
        require('html_script_finder/dom_handler/request')
            .request(script, responseCallback).request();
    }  catch (x) {
        console.debug('error', x, x.lineNumber, x.fileName);
        responseCallback(script, false);
    }
};

/**
 * exports.domChecker
 * Instantiate a brand new clone of the domChecker.
 * @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.domChecker = function(domHandler) {
    "use strict";

    var domChecker = new DomChecker();

    domChecker.init(domHandler);

    return domChecker;
};

exports.xhr = new DomChecker().xhr;