CryptoSafetyChild.sys.mjs (1599B)
1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ 2 /* Copyright (c) 2020, The Tor Project, Inc. 3 * 4 * This Source Code Form is subject to the terms of the Mozilla Public 5 * License, v. 2.0. If a copy of the MPL was not distributed with this 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 7 8 import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs"; 9 10 const lazy = {}; 11 12 ChromeUtils.defineESModuleGetters(lazy, { 13 setTimeout: "resource://gre/modules/Timer.sys.mjs", 14 }); 15 16 XPCOMUtils.defineLazyPreferenceGetter( 17 lazy, 18 "isCryptoSafetyEnabled", 19 "security.cryptoSafety", 20 true // Defaults to true. 21 ); 22 23 export class CryptoSafetyChild extends JSWindowActorChild { 24 handleEvent(event) { 25 if ( 26 !lazy.isCryptoSafetyEnabled || 27 // Ignore non-HTTP addresses. 28 // We do this before reading the host property since this is not available 29 // for about: pages. 30 !this.document.documentURIObject.schemeIs("http") || 31 // Ignore onion addresses. 32 this.document.documentURIObject.host.endsWith(".onion") || 33 (event.type !== "copy" && event.type !== "cut") 34 ) { 35 return; 36 } 37 38 // We send a message to the parent to inspect the clipboard content. 39 // NOTE: We wait until next cycle to allow the event to propagate and fill 40 // the clipboard before being read. 41 // NOTE: Using navigator.clipboard.readText fails with Wayland. See 42 // tor-browser#42702. 43 lazy.setTimeout(() => { 44 this.sendAsyncMessage("CryptoSafety:CopiedText", { 45 host: this.document.documentURIObject.host, 46 }); 47 }); 48 } 49 }