|
| 1 | +/* |
| 2 | + * NoScript - a Firefox extension for whitelist driven safe JavaScript execution |
| 3 | + * |
| 4 | + * Copyright (C) 2005-2022 Giorgio Maone <https://maone.net> |
| 5 | + * |
| 6 | + * SPDX-License-Identifier: GPL-3.0-or-later |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify it under |
| 9 | + * the terms of the GNU General Public License as published by the Free Software |
| 10 | + * Foundation, either version 3 of the License, or (at your option) any later |
| 11 | + * version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, but WITHOUT |
| 14 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 15 | + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License along with |
| 18 | + * this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | + |
| 22 | +var TabGuard = (() => { |
| 23 | + (async () => { await include(["/nscl/service/TabCache.js", "/nscl/service/TabTies.js"]); })(); |
| 24 | + |
| 25 | + let allowedGroups, filteredGroups; |
| 26 | + let forget = () => { |
| 27 | + allowedGroups = {}; |
| 28 | + filteredGroups = {}; |
| 29 | + }; |
| 30 | + forget(); |
| 31 | + |
| 32 | + const AUTH_HEADERS_RX = /^(?:authorization|cookie)/i; |
| 33 | + |
| 34 | + function getDomain(u) { |
| 35 | + let {url} = Sites.parse(u); |
| 36 | + return url && url.protocol.startsWith("http") && tld.getDomain(url.hostname); |
| 37 | + } |
| 38 | + |
| 39 | + return { |
| 40 | + forget, |
| 41 | + check(request) { |
| 42 | + const mode = ns.sync.TabGuardMode; |
| 43 | + if (mode === "off" || !request.incognito && mode!== "global") return; |
| 44 | + |
| 45 | + const {tabId, type, url} = request; |
| 46 | + |
| 47 | + if (tabId < 0) return; // no tab, no party |
| 48 | + |
| 49 | + let targetDomain = getDomain(url); |
| 50 | + if (!targetDomain) return; // no domain, no cookies |
| 51 | + |
| 52 | + const mainFrame = type === "main_frame"; |
| 53 | + let tabDomain = getDomain(mainFrame ? url : TabCache.get(tabId).url); |
| 54 | + if (!tabDomain) return; // no domain, no cookies |
| 55 | + |
| 56 | + let ties = TabTies.get(tabId); |
| 57 | + if (ties.size === 0) return; // no ties, no party |
| 58 | + |
| 59 | + let legitDomains = allowedGroups[tabDomain] || new Set([tabDomain]); |
| 60 | + |
| 61 | + let otherDomains = new Set([...ties].map(id => getDomain(TabCache.get(id).url)).filter(d => !legitDomains.has(d))); |
| 62 | + if (otherDomains.size === 0) return; // no cross-site ties, no party |
| 63 | + |
| 64 | + let {requestHeaders} = request; |
| 65 | + |
| 66 | + if (!requestHeaders.some(h => AUTH_HEADERS_RX.test(h.name))) return; // no auth, no party |
| 67 | + |
| 68 | + // danger zone |
| 69 | + |
| 70 | + let filterAuth = () => { |
| 71 | + requestHeaders = requestHeaders.filter(h => !AUTH_HEADERS_RX.test(h.name)); |
| 72 | + debug("TabGuard removing auth headers from %o (%o)", request, requestHeaders); |
| 73 | + return {requestHeaders}; |
| 74 | + }; |
| 75 | + |
| 76 | + if (mainFrame) { |
| 77 | + let quietDomains = filteredGroups[tabDomain]; |
| 78 | + let mustPrompt = (!quietDomains || [...otherDomains].some(d => !quietDomains.has(d))); |
| 79 | + if (mustPrompt) { |
| 80 | + return (async () => { |
| 81 | + let options = [ |
| 82 | + {label: _("TabGuard_optAnonymize"), checked: true}, |
| 83 | + {label: _("TabGuard_optAllow")}, |
| 84 | + ]; |
| 85 | + let ret = await Prompts.prompt({ |
| 86 | + title: _("TabGuard_title"), |
| 87 | + message: _("TabGuard_message", [tabDomain, [...otherDomains].join(", ")]), |
| 88 | + options}); |
| 89 | + if (ret.button === 1) return {cancel: true}; |
| 90 | + let list = ret.option === 0 ? filteredGroups : allowedGroups; |
| 91 | + otherDomains.add(tabDomain); |
| 92 | + for (let d of otherDomains) list[d] = otherDomains; |
| 93 | + return list === filteredGroups ? filterAuth() : null; |
| 94 | + })(); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return filterAuth(); |
| 99 | + |
| 100 | + } |
| 101 | + } |
| 102 | +})(); |
0 commit comments