summaryrefslogtreecommitdiffstats
path: root/src/js/redirect-engine.js
blob: 2f58066039a8dc0e7f4b05cfbbca2f1a974b1121 (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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*******************************************************************************

    uBlock Origin - a comprehensive, efficient content blocker
    Copyright (C) 2015-present Raymond Hill

    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/}.

    Home: https://github.com/gorhill/uBlock
*/

'use strict';

/******************************************************************************/

import redirectableResources from './redirect-resources.js';

import {
    LineIterator,
    orphanizeString,
} from './text-utils.js';

/******************************************************************************/

const extToMimeMap = new Map([
    [  'css', 'text/css' ],
    [   'fn', 'fn/javascript' ], // invented mime type for internal use
    [  'gif', 'image/gif' ],
    [ 'html', 'text/html' ],
    [   'js', 'text/javascript' ],
    [ 'json', 'application/json' ],
    [  'mp3', 'audio/mp3' ],
    [  'mp4', 'video/mp4' ],
    [  'png', 'image/png' ],
    [  'txt', 'text/plain' ],
    [  'xml', 'text/xml' ],
]);

const typeToMimeMap = new Map([
    [     'main_frame', 'text/html' ],
    [          'other', 'text/plain' ],
    [         'script', 'text/javascript' ],
    [     'stylesheet', 'text/css' ],
    [      'sub_frame', 'text/html' ],
    [ 'xmlhttprequest', 'text/plain' ],
]);

const validMimes = new Set(extToMimeMap.values());

const mimeFromName = name => {
    const match = /\.([^.]+)$/.exec(name);
    if ( match === null ) { return ''; }
    return extToMimeMap.get(match[1]);
};

const removeTopCommentBlock = text => {
    return text.replace(/^\/\*[\S\s]+?\n\*\/\s*/, '');
};

// vAPI.warSecret is optional, it could be absent in some environments,
// i.e. nodejs for example. Probably the best approach is to have the
// "web_accessible_resources secret" added outside by the client of this
// module, but for now I just want to remove an obstacle to modularization.
const warSecret = typeof vAPI === 'object' && vAPI !== null
    ? vAPI.warSecret.short
    : ( ) => '';

const RESOURCES_SELFIE_VERSION = 7;
const RESOURCES_SELFIE_NAME = 'compiled/redirectEngine/resources';

/******************************************************************************/
/******************************************************************************/

class RedirectEntry {
    constructor() {
        this.mime = '';
        this.data = '';
        this.warURL = undefined;
        this.params = undefined;
        this.requiresTrust = false;
        this.world = 'MAIN';
        this.dependencies = [];
    }

    // Prevent redirection to web accessible resources when the request is
    // of type 'xmlhttprequest', because XMLHttpRequest.responseURL would
    // cause leakage of extension id. See:
    // - https://stackoverflow.com/a/8056313
    // - https://bugzilla.mozilla.org/show_bug.cgi?id=998076
    // https://www.reddit.com/r/uBlockOrigin/comments/cpxm1v/
    //   User-supplied resources may already be base64 encoded.

    toURL(fctxt, asDataURI = false) {
        if (
            this.warURL !== undefined &&
            asDataURI !== true &&
            fctxt instanceof Object &&
            fctxt.type !== 'xmlhttprequest'
        ) {
            const params = [];
            const secret = warSecret();
            if ( secret !== '' ) { params.push(`secret=${secret}`); }
            if ( this.params !== undefined ) {
                for ( const name of this.params ) {
                    const value = fctxt[name];
                    if ( value === undefined ) { continue; }
                    params.push(`${name}=${encodeURIComponent(value)}`);
                }
            }
            let url = `${this.warURL}`;
            if ( params.length !== 0 ) {
                url += `?${params.join('&')}`;
            }
            return url;
        }
        if ( this.data === undefined ) { return; }
        // https://github.com/uBlockOrigin/uBlock-issues/issues/701
        if ( this.data === '' ) {
            const mime = typeToMimeMap.get(fctxt.type);
            if ( mime === '' ) { return; }
            return `data:${mime},`;
        }
        if ( this.data.startsWith('data:') === false ) {
            if ( this.mime.indexOf(';') === -1 ) {
                this.data = `data:${this.mime};base64,${btoa(this.data)}`;
            } else {
                this.data = `data:${this.mime},${this.data}`;
            }
        }
        return this.data;
    }

    toContent() {
        if ( this.data.startsWith('data:') ) {
            const pos = this.data.indexOf(',');
            const base64 = this.data.endsWith(';base64', pos);
            this.data = this.data.slice(pos + 1);
            if ( base64 ) {
                this.data = atob(this.data);
            }
        }
        return this.data;
    }

    static fromDetails(details) {
        const r = new RedirectEntry();
        Object.assign(r, details);
        return r;
    }
}

/******************************************************************************/
/******************************************************************************/

class RedirectEngine {
    constructor() {
        this.aliases = new Map();
        this.resources = new Map();
        this.reset();
        this.modifyTime = Date.now();
    }

    reset() {
    }

    freeze() {
    }

    tokenToURL(
        fctxt,
        token,
        asDataURI = false
    ) {
        const entry = this.resources.get(this.aliases.get(token) || token);
        if ( entry === undefined ) { return; }
        return entry.toURL(fctxt, asDataURI);
    }

    tokenToDNR(token) {
        const entry = this.resources.get(this.aliases.get(token) || token);
        if ( entry === undefined ) { return; }
        if ( entry.warURL === undefined ) { return; }
        return entry.warURL;
    }

    hasToken(token) {
        if ( token === 'none' ) { return true; }
        const asDataURI = token.charCodeAt(0) === 0x25 /* '%' */;
        if ( asDataURI ) {
            token = token.slice(1);
        }
        return this.resources.get(this.aliases.get(token) || token) !== undefined;
    }

    tokenRequiresTrust(token) {
        const entry = this.resources.get(this.aliases.get(token) || token);
        return entry && entry.requiresTrust === true || false;
    }

    async toSelfie() {
    }

    async fromSelfie() {
        return true;
    }

    contentFromName(name, mime = '') {
        const entry = this.resources.get(this.aliases.get(name) || name);
        if ( entry === undefined ) { return; }
        if ( entry.mime.startsWith(mime) === false ) { return; }
        return {
            js: entry.toContent(),
            world: entry.world,
            dependencies: entry.dependencies.slice(),
        };
    }

    // https://github.com/uBlockOrigin/uAssets/commit/deefe8755511
    //   Consider 'none' a reserved keyword, to be used to disable redirection.
    // https://github.com/uBlockOrigin/uBlock-issues/issues/1419
    //   Append newlines to raw text to ensure processing of trailing resource.

    resourcesFromString(text) {
        const lineIter = new LineIterator(
            removeTopCommentBlock(text) + '\n\n'
        );
        const reNonEmptyLine = /\S/;
        let fields, encoded, details;

        while ( lineIter.eot() === false ) {
            const line = lineIter.next();
            if ( line.startsWith('#') ) { continue; }
            if ( line.startsWith('// ') ) { continue; }

            if ( fields === undefined ) {
                if ( line === '' ) { continue; }
                // Modern parser
                if ( line.startsWith('/// ') ) {
                    const name = line.slice(4).trim();
                    fields = [ name, mimeFromName(name) ];
                    continue;
                }
                // Legacy parser
                const head = line.trim().split(/\s+/);
                if ( head.length !== 2 ) { continue; }
                if ( head[0] === 'none' ) { continue; }
                let pos = head[1].indexOf(';');
                if ( pos === -1 ) { pos = head[1].length; }
                if ( validMimes.has(head[1].slice(0, pos)) === false ) {
                    continue;
                }
                encoded = head[1].indexOf(';') !== -1;
                fields = head;
                continue;
            }

            if ( line.startsWith('/// ') ) {
                if ( details === undefined ) {
                    details = [];
                }
                const [ prop, value ] = line.slice(4).trim().split(/\s+/);
                if ( value !== undefined ) {
                    details.push({ prop, value });
                }
                continue;
            }

            if ( reNonEmptyLine.test(line) ) {
                fields.push(encoded ? line.trim() : line);
                continue;
            }

            // No more data, add the resource.
            const name = this.aliases.get(fields[0]) || fields[0];
            const mime = fields[1];
            const data = orphanizeString(
                fields.slice(2).join(encoded ? '' : '\n')
            );
            this.resources.set(name, RedirectEntry.fromDetails({ mime, data }));
            if ( Array.isArray(details) ) {
                const resource = this.resources.get(name);
                for ( const { prop, value } of details ) {
                    switch ( prop ) {
                    case 'alias':
                        this.aliases.set(value, name);
                        break;
                    case 'world':
                        if ( /^isolated$/i.test(value) === false ) { break; }
                        resource.world = 'ISOLATED';
                        break;
                    case 'dependency':
                        if ( this.resources.has(value) === false ) { break; }
                        resource.dependencies.push(value);
                        break;
                    default:
                        break;
                    }
                }
            }

            fields = undefined;
            details = undefined;
        }

        this.modifyTime = Date.now();
    }

    loadBuiltinResources(fetcher) {
        this.resources = new Map();
        this.aliases = new Map();

        const fetches = [
            import('/assets/resources/scriptlets.js').then(module => {
                for ( const scriptlet of module.builtinScriptlets ) {
                    const details = {};
                    details.mime = mimeFromName(scriptlet.name);
                    details.data = scriptlet.fn.toString();
                    for ( const [ k, v ] of Object.entries(scriptlet) ) {
                        if ( k === 'fn' ) { continue; }
                        details[k] = v;
                    }
                    const entry = RedirectEntry.fromDetails(details);
                    this.resources.set(details.name, entry);
                    if ( Array.isArray(details.aliases) === false ) { continue; }
                    for ( const alias of details.aliases ) {
                        this.aliases.set(alias, details.name);
                    }
                }
                this.modifyTime = Date.now();
            }),
        ];

        const store = (name, data = undefined) => {
            const details = redirectableResources.get(name);
            const entry = RedirectEntry.fromDetails({
                mime: mimeFromName(name),
                data,
                warURL: `/web_accessible_resources/${name}`,
                params: details.params,
            });
            this.resources.set(name, entry);
            if ( details.alias === undefined ) { return; }
            if ( Array.isArray(details.alias) ) {
                for ( const alias of details.alias ) {
                    this.aliases.set(alias, name);
                }
            } else {
                this.aliases.set(details.alias, name);
            }
        };

        const processBlob = (name, blob) => {
            return new Promise(resolve => {
                const reader = new FileReader();
                reader.onload = ( ) => {
                    store(name, reader.result);
                    resolve();
                };
                reader.onabort = reader.onerror = ( ) => {
                    resolve();
                };
                reader.readAsDataURL(blob);
            });
        };

        const processText = (name, text) => {
            store(name, removeTopCommentBlock(text));
        };

        const process = result => {
            const match = /^\/web_accessible_resources\/([^?]+)/.exec(result.url);
            if ( match === null ) { return; }
            const name = match[1];
            return result.content instanceof Blob
                ? processBlob(name, result.content)
                : processText(name, result.content);
        };

        for ( const [ name, details ] of redirectableResources ) {
            if ( typeof details.data !== 'string' ) {
                store(name);
                continue;
            }
            fetches.push(
                fetcher(`/web_accessible_resources/${name}`, {
                    responseType: details.data
                }).then(
                    result => process(result)
                )
            );
        }

        return Promise.all(fetches);
    }

    getResourceDetails() {
        const out = new Map([
            [ 'none', { canInject: false, canRedirect: true, aliasOf: '' } ],
        ]);
        for ( const [ name, entry ] of this.resources ) {
            out.set(name, {
                canInject: typeof entry.data === 'string',
                canRedirect: entry.warURL !== undefined,
                aliasOf: '',
                extensionPath: entry.warURL,
            });
        }
        for ( const [ alias, name ] of this.aliases ) {
            const original = out.get(name);
            if ( original === undefined ) { continue; }
            const aliased = Object.assign({}, original);
            aliased.aliasOf = name;
            out.set(alias, aliased);
        }
        return Array.from(out).sort((a, b) => {
            return a[0].localeCompare(b[0]);
        });
    }

    getTrustedScriptletTokens() {
        const out = [];
        const isTrustedScriptlet = entry => {
            if ( entry.requiresTrust !== true ) { return false; }
            if ( entry.warURL !== undefined ) { return false; }
            if ( typeof entry.data !== 'string' ) { return false; }
            if ( entry.name.endsWith('.js') === false ) { return false; }
            return true;
        };
        for ( const [ name, entry ] of this.resources ) {
            if ( isTrustedScriptlet(entry) === false ) { continue; }
            out.push(name.slice(0, -3));
        }
        for ( const [ alias, name ] of this.aliases ) {
            if ( out.includes(name.slice(0, -3)) === false ) { continue; }
            out.push(alias.slice(0, -3));
        }
        return out;
    }

    selfieFromResources(storage) {
        storage.put(
            RESOURCES_SELFIE_NAME,
            JSON.stringify({
                version: RESOURCES_SELFIE_VERSION,
                aliases: Array.from(this.aliases),
                resources: Array.from(this.resources),
            })
        );
    }

    async resourcesFromSelfie(storage) {
        const result = await storage.get(RESOURCES_SELFIE_NAME);
        let selfie;
        try {
            selfie = JSON.parse(result.content);
        } catch(ex) {
        }
        if (
            selfie instanceof Object === false ||
            selfie.version !== RESOURCES_SELFIE_VERSION ||
            Array.isArray(selfie.resources) === false
        ) {
            return false;
        }
        this.aliases = new Map(selfie.aliases);
        this.resources = new Map();
        for ( const [ token, entry ] of selfie.resources ) {
            this.resources.set(token, RedirectEntry.fromDetails(entry));
        }
        return true;
    }

    invalidateResourcesSelfie(storage) {
        storage.remove(RESOURCES_SELFIE_NAME);
    }
}

/******************************************************************************/

const redirectEngine = new RedirectEngine();

export { redirectEngine };

/******************************************************************************/