tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

RecommendedPreferences.sys.mjs (17963B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
      6 
      7 const lazy = {};
      8 
      9 ChromeUtils.defineESModuleGetters(lazy, {
     10  Log: "chrome://remote/content/shared/Log.sys.mjs",
     11 });
     12 
     13 XPCOMUtils.defineLazyPreferenceGetter(
     14  lazy,
     15  "useRecommendedPrefs",
     16  "remote.prefs.recommended",
     17  false
     18 );
     19 
     20 ChromeUtils.defineLazyGetter(lazy, "logger", () => lazy.Log.get());
     21 
     22 // Ensure we are in the parent process.
     23 if (Services.appinfo.processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT) {
     24  throw new Error(
     25    "RecommendedPreferences should only be loaded in the parent process"
     26  );
     27 }
     28 
     29 // ALL CHANGES TO THIS LIST MUST HAVE REVIEW FROM A WEBDRIVER PEER!
     30 //
     31 // Preferences are set for automation on startup, unless
     32 // remote.prefs.recommended has been set to false.
     33 //
     34 // Note: Clients do not always use the latest version of the application. As
     35 // such backward compatibility has to be ensured at least for the last three
     36 // releases.
     37 
     38 // INSTRUCTIONS TO ADD A NEW PREFERENCE
     39 //
     40 // Preferences for remote control and automation can be set from several entry
     41 // points:
     42 // - remote/shared/RecommendedPreferences.sys.mjs
     43 // - remote/test/puppeteer/packages/browsers/src/browser-data/firefox.ts
     44 // - testing/geckodriver/src/prefs.rs
     45 // - testing/marionette/client/marionette_driver/geckoinstance.py
     46 // - testing/profiles/
     47 //
     48 // The preferences in `firefox.ts`, `prefs.rs` and `geckoinstance.py`
     49 // will be applied before the application starts, and should typically be used
     50 // for preferences which cannot be updated during the lifetime of the application.
     51 //
     52 // The preferences in `RecommendedPreferences.sys.mjs` are applied after
     53 // the application has started, which means that the application must apply this
     54 // change dynamically and behave correctly. Note that you can also define
     55 // protocol specific preferences (WebDriver, ...) which are merged with the
     56 // COMMON_PREFERENCES from `RecommendedPreferences.sys.mjs`.
     57 //
     58 // Additionally, users relying on the Marionette Python client (ie. using
     59 // geckoinstance.py) set `remote.prefs.recommended = false`. This means that
     60 // preferences from `RecommendedPreferences.sys.mjs` are not applied and have to
     61 // be added to the list of preferences in that Python file. Note that there are
     62 // several lists of preferences, either common or specific to a given application
     63 // (Firefox Desktop, Fennec, Thunderbird).
     64 //
     65 // Some test types may disable recommended preferences. Search for
     66 // `"remote.prefs.recommended", false` in `/testing/profiles` to find
     67 // `user.js` files that do so.
     68 //
     69 // Depending on how users interact with the Remote Agent, they will use different
     70 // combinations of preferences. So it's important to update the preferences files
     71 // so that all users have the proper preferences.
     72 //
     73 // When adding a new preference, follow this guide to decide where to add it:
     74 // - Add the preference to `geckoinstance.py`
     75 // - If the preference has to be set before startup:
     76 //   - Add the preference to `prefs.rs`
     77 //   - Add the preference `browser-data/firefox.ts` in the puppeteer folder
     78 //   - Create a PR to upstream the change on `browser-data/firefox.ts` to puppeteer
     79 // - Otherwise, if the preference can be set after startup:
     80 //   - Add the preference to `RecommendedPreferences.sys.mjs`
     81 // - If a `user.js` file in `/testing/profiles` disables recommended preferences,
     82 //   identify affected test suites and set the new preference where needed.
     83 const COMMON_PREFERENCES = new Map([
     84  // Make sure Shield doesn't hit the network.
     85  ["app.normandy.api_url", ""],
     86 
     87  // Disable automatically upgrading Firefox
     88  //
     89  // Note: This preference should have already been set by the client when
     90  // creating the profile. But if not and to absolutely make sure that updates
     91  // of Firefox aren't downloaded and applied, enforce its presence.
     92  ["app.update.disabledForTesting", true],
     93 
     94  // Increase the APZ content response timeout in tests to 1 minute.
     95  // This is to accommodate the fact that test environments tends to be
     96  // slower than production environments (with the b2g emulator being
     97  // the slowest of them all), resulting in the production timeout value
     98  // sometimes being exceeded and causing false-positive test failures.
     99  //
    100  // (bug 1176798, bug 1177018, bug 1210465)
    101  ["apz.content_response_timeout", 60000],
    102 
    103  // Disable the profile backup service.
    104  ["browser.backup.enabled", false],
    105 
    106  // Don't show the content blocking introduction panel.
    107  // We use a larger number than the default 22 to have some buffer
    108  // This can be removed once Firefox 69 and 68 ESR and are no longer supported.
    109  ["browser.contentblocking.introCount", 99],
    110 
    111  // Disable extension discovery
    112  ["browser.discovery.enabled", false],
    113 
    114  // Set global `dump` function to log strings to `stdout` for release builds as well.
    115  ["browser.dom.window.dump.enabled", true],
    116 
    117  // Indicate that the download panel has been shown once so that
    118  // whichever download test runs first doesn't show the popup
    119  // inconsistently.
    120  ["browser.download.panel.shown", true],
    121 
    122  // Make sure error page is not shown for blank pages with 4xx or 5xx response code
    123  ["browser.http.blank_page_with_error_response.enabled", true],
    124 
    125  // Disable all machine learning features by default
    126  ["browser.ml.enable", false],
    127 
    128  // Disable CFR features for automated tests.
    129  ["browser.newtabpage.activity-stream.asrouter.userprefs.cfr.features", false],
    130 
    131  // Do not initialize any activitystream features
    132  ["browser.newtabpage.activity-stream.testing.shouldInitializeFeeds", false],
    133 
    134  // Make sure Topsites doesn't hit the network to retrieve sponsored tiles.
    135  ["browser.newtabpage.activity-stream.showSponsoredTopSites", false],
    136 
    137  // Background thumbnails in particular cause grief, and disabling
    138  // thumbnails in general cannot hurt
    139  ["browser.pagethumbnails.capturing_disabled", true],
    140 
    141  // Disable geolocation ping(#1)
    142  ["browser.region.network.url", ""],
    143 
    144  // Disable safebrowsing components.
    145  //
    146  // These should also be set in the profile prior to starting Firefox,
    147  // as it is picked up at runtime.
    148  ["browser.safebrowsing.blockedURIs.enabled", false],
    149  ["browser.safebrowsing.downloads.enabled", false],
    150  ["browser.safebrowsing.malware.enabled", false],
    151  ["browser.safebrowsing.phishing.enabled", false],
    152 
    153  // Disable updates to search engines.
    154  //
    155  // Should be set in profile.
    156  ["browser.search.update", false],
    157 
    158  // Do not restore the last open set of tabs if the browser has crashed
    159  ["browser.sessionstore.resume_from_crash", false],
    160 
    161  // Don't check for the default web browser during startup.
    162  //
    163  // These should also be set in the profile prior to starting Firefox,
    164  // as it is picked up at runtime.
    165  ["browser.shell.checkDefaultBrowser", false],
    166 
    167  // Disable session restore infobar
    168  ["browser.startup.couldRestoreSession.count", -1],
    169 
    170  // Do not redirect user when a milstone upgrade of Firefox is detected
    171  ["browser.startup.homepage_override.mstone", "ignore"],
    172 
    173  // Unload the previously selected tab immediately
    174  ["browser.tabs.remote.unloadDelayMs", 0],
    175 
    176  // Don't unload tabs when available memory is running low
    177  ["browser.tabs.unloadOnLowMemory", false],
    178 
    179  // Do not warn when closing all open tabs
    180  ["browser.tabs.warnOnClose", false],
    181 
    182  // Do not warn when closing all other open tabs
    183  ["browser.tabs.warnOnCloseOtherTabs", false],
    184 
    185  // Do not warn when multiple tabs will be opened
    186  ["browser.tabs.warnOnOpen", false],
    187 
    188  // Don't show the Bookmarks Toolbar on any tab (the above pref that
    189  // disables the New Tab Page ends up showing the toolbar on about:blank).
    190  ["browser.toolbars.bookmarks.visibility", "never"],
    191 
    192  // Make sure Topsites doesn't hit the network to retrieve tiles from Contile.
    193  ["browser.topsites.contile.enabled", false],
    194 
    195  // Disable translations
    196  ["browser.translations.enable", false],
    197 
    198  // Disable first run splash page on Windows 10
    199  ["browser.usedOnWindows10.introURL", ""],
    200 
    201  // Turn off Merino suggestions in the location bar so as not to trigger
    202  // network connections.
    203  ["browser.urlbar.merino.endpointURL", ""],
    204 
    205  // Turn off search suggestions in the location bar so as not to trigger
    206  // network connections.
    207  ["browser.urlbar.suggest.searches", false],
    208 
    209  // Do not warn on quitting Firefox
    210  ["browser.warnOnQuit", false],
    211 
    212  // Do not show datareporting policy notifications which can
    213  // interfere with tests
    214  [
    215    "datareporting.healthreport.documentServerURI",
    216    "http://%(server)s/dummy/healthreport/",
    217  ],
    218  ["datareporting.healthreport.logging.consoleEnabled", false],
    219  ["datareporting.healthreport.service.enabled", false],
    220  ["datareporting.healthreport.service.firstRun", false],
    221  ["datareporting.healthreport.uploadEnabled", false],
    222  ["datareporting.policy.dataSubmissionEnabled", false],
    223  ["datareporting.policy.dataSubmissionPolicyAccepted", false],
    224  ["datareporting.policy.dataSubmissionPolicyBypassNotification", true],
    225  ["datareporting.usage.uploadEnabled", false],
    226  ["telemetry.fog.test.localhost_port", -1],
    227 
    228  // Disable popup-blocker
    229  ["dom.disable_open_during_load", false],
    230 
    231  // Enabling the support for File object creation in the content process
    232  ["dom.file.createInChild", true],
    233 
    234  // Disable delayed user input event handling
    235  ["dom.input_events.security.minNumTicks", 0],
    236  ["dom.input_events.security.minTimeElapsedInMS", 0],
    237 
    238  // Disable the ProcessHangMonitor
    239  ["dom.ipc.reportProcessHangs", false],
    240 
    241  // Disable the QoS manager on MacOS and the priority manager on all other
    242  // platforms to not cause stalled processes in background tabs when the
    243  // overall CPU load on the machine is high.
    244  //
    245  // TODO: Should be considered to get removed once bug 1960741 is fixed.
    246  ["threads.lower_mainthread_priority_in_background.enabled", false],
    247  ["dom.ipc.processPriorityManager.enabled", false],
    248 
    249  // Disable slow script dialogues
    250  ["dom.max_chrome_script_run_time", 0],
    251  ["dom.max_script_run_time", 0],
    252 
    253  // Disable location change rate limitation
    254  ["dom.navigation.navigationRateLimit.count", 0],
    255 
    256  // DOM Push
    257  ["dom.push.connection.enabled", false],
    258 
    259  // Screen Orientation API
    260  ["dom.screenorientation.allow-lock", true],
    261 
    262  // Disable dialog abuse if alerts are triggered too quickly.
    263  ["dom.successive_dialog_time_limit", 0],
    264 
    265  // Only load extensions from the application and user profile
    266  // AddonManager.SCOPE_PROFILE + AddonManager.SCOPE_APPLICATION
    267  //
    268  // Should be set in profile.
    269  ["extensions.autoDisableScopes", 0],
    270  ["extensions.enabledScopes", 5],
    271 
    272  // Disable form autofill for extensions and credit cards
    273  ["extensions.formautofill.addresses.enabled", false],
    274  ["extensions.formautofill.creditCards.enabled", false],
    275 
    276  // Disable metadata caching for installed add-ons by default
    277  ["extensions.getAddons.cache.enabled", false],
    278 
    279  // Disable installing any distribution extensions or add-ons.
    280  // Should be set in profile.
    281  ["extensions.installDistroAddons", false],
    282 
    283  // Turn off extension updates so they do not bother tests
    284  ["extensions.update.enabled", false],
    285  ["extensions.update.notifyUser", false],
    286 
    287  // Make sure opening about:addons will not hit the network
    288  ["extensions.getAddons.discovery.api_url", "data:, "],
    289 
    290  // Redirect various extension update URLs
    291  [
    292    "extensions.blocklist.detailsURL",
    293    "http://%(server)s/extensions-dummy/blocklistDetailsURL",
    294  ],
    295  [
    296    "extensions.blocklist.itemURL",
    297    "http://%(server)s/extensions-dummy/blocklistItemURL",
    298  ],
    299  ["extensions.hotfix.url", "http://%(server)s/extensions-dummy/hotfixURL"],
    300  ["extensions.systemAddon.update.enabled", false],
    301  [
    302    "extensions.update.background.url",
    303    "http://%(server)s/extensions-dummy/updateBackgroundURL",
    304  ],
    305  ["extensions.update.url", "http://%(server)s/extensions-dummy/updateURL"],
    306 
    307  // Make sure opening about: addons won't hit the network
    308  ["extensions.getAddons.discovery.api_url", "data:, "],
    309  [
    310    "extensions.getAddons.get.url",
    311    "http://%(server)s/extensions-dummy/repositoryGetURL",
    312  ],
    313  [
    314    "extensions.getAddons.search.browseURL",
    315    "http://%(server)s/extensions-dummy/repositoryBrowseURL",
    316  ],
    317 
    318  // Allow the application to have focus even it runs in the background
    319  ["focusmanager.testmode", true],
    320 
    321  // Disable useragent updates
    322  ["general.useragent.updates.enabled", false],
    323 
    324  // Disable geolocation ping(#2)
    325  ["geo.provider.network.url", ""],
    326 
    327  // Always use network provider for geolocation tests so we bypass the
    328  // macOS dialog raised by the corelocation provider
    329  ["geo.provider.testing", true],
    330 
    331  // Do not scan Wifi
    332  ["geo.wifi.scan", false],
    333 
    334  // Disable Firefox accounts ping
    335  ["identity.fxaccounts.auth.uri", "https://{server}/dummy/fxa"],
    336 
    337  // Disable connectivity service pings
    338  ["network.connectivity-service.enabled", false],
    339 
    340  // Do not prompt for temporary redirects
    341  ["network.http.prompt-temp-redirect", false],
    342 
    343  // Do not automatically switch between offline and online
    344  ["network.manage-offline-status", false],
    345 
    346  // Make sure SNTP requests do not hit the network
    347  ["network.sntp.pools", "%(server)s"],
    348 
    349  // Turn off semantic history search as it triggers network connections to
    350  // download ML models.
    351  ["places.semanticHistory.featureGate", false],
    352 
    353  // Privacy and Tracking Protection
    354  ["privacy.trackingprotection.enabled", false],
    355  ["privacy.trackingprotection.pbmode.enabled", false],
    356 
    357  // Used to check if recommended preferences are applied
    358  ["remote.prefs.recommended.applied", true],
    359 
    360  // Don't do network connections for mitm priming
    361  ["security.certerrors.mitm.priming.enabled", false],
    362 
    363  // Local documents have access to all other local documents,
    364  // including directory listings
    365  ["security.fileuri.strict_origin_policy", false],
    366 
    367  // Tests do not wait for the notification button security delay
    368  ["security.notification_enable_delay", 0],
    369 
    370  // Do not download intermediate certificates
    371  ["security.remote_settings.intermediates.enabled", false],
    372 
    373  // Disable logging for remote settings
    374  ["services.settings.loglevel", "off"],
    375 
    376  // Ensure remote settings do not hit the network
    377  ["services.settings.server", "data:,#remote-settings-dummy/v1"],
    378 
    379  // Do not automatically fill sign-in forms with known usernames and
    380  // passwords
    381  ["signon.autofillForms", false],
    382 
    383  // Disable alerts for credential issues
    384  ["signon.management.page.breach-alerts.enabled", false],
    385  ["signon.management.page.vulnerable-passwords.enabled", false],
    386 
    387  // Disable password capture, so that tests that include forms are not
    388  // influenced by the presence of the persistent doorhanger notification
    389  ["signon.rememberSignons", false],
    390 
    391  // Disable first-run welcome page
    392  ["startup.homepage_welcome_url", "about:blank"],
    393  ["startup.homepage_welcome_url.additional", ""],
    394 
    395  // Do not show TOU new user modal which can interfere with tests
    396  ["termsofuse.bypassNotification", true],
    397 
    398  // Prevent starting into safe mode after application crashes
    399  ["toolkit.startup.max_resumed_crashes", -1],
    400 
    401  // Disable all telemetry pings
    402  ["toolkit.telemetry.server", "https://%(server)s/telemetry-dummy/"],
    403 
    404  // Disable window occlusion on Windows, which can prevent webdriver commands
    405  // such as WebDriver:FindElements from working properly (Bug 1802473).
    406  ["widget.windows.window_occlusion_tracking.enabled", false],
    407 ]);
    408 
    409 export const RecommendedPreferences = {
    410  alteredPrefs: new Set(),
    411 
    412  isInitialized: false,
    413 
    414  /**
    415   * Apply the provided map of preferences.
    416   *
    417   * Note, that they will be automatically reset on application shutdown.
    418   *
    419   * @param {Map<string, object>=} preferences
    420   *     Map of preference name to preference value.
    421   */
    422  applyPreferences(preferences = new Map()) {
    423    if (!lazy.useRecommendedPrefs) {
    424      // If remote.prefs.recommended is set to false, do not set any preference
    425      // here. Needed for our Firefox CI.
    426      return;
    427    }
    428 
    429    // Only apply common recommended preferences on first call to
    430    // applyPreferences.
    431    if (!this.isInitialized) {
    432      // Merge common preferences and optionally provided preferences in a
    433      // single map. Hereby the extra preferences have higher priority.
    434      preferences = new Map([...COMMON_PREFERENCES, ...preferences]);
    435 
    436      Services.obs.addObserver(this, "quit-application");
    437      this.isInitialized = true;
    438    }
    439 
    440    for (const [k, v] of preferences) {
    441      if (!Services.prefs.prefHasUserValue(k)) {
    442        lazy.logger.debug(`Setting recommended pref ${k} to ${v}`);
    443 
    444        switch (typeof v) {
    445          case "string":
    446            Services.prefs.setStringPref(k, v);
    447            break;
    448          case "boolean":
    449            Services.prefs.setBoolPref(k, v);
    450            break;
    451          case "number":
    452            Services.prefs.setIntPref(k, v);
    453            break;
    454          default:
    455            throw new TypeError(`Invalid preference type: ${typeof v}`);
    456        }
    457 
    458        // Keep track all the altered preferences to restore them on
    459        // quit-application.
    460        this.alteredPrefs.add(k);
    461      }
    462    }
    463  },
    464 
    465  observe(subject, topic) {
    466    if (topic === "quit-application") {
    467      this.restoreAllPreferences();
    468    }
    469  },
    470 
    471  /**
    472   * Restore all the altered preferences.
    473   */
    474  restoreAllPreferences() {
    475    this.restorePreferences(this.alteredPrefs);
    476    if (this.isInitialized) {
    477      Services.obs.removeObserver(this, "quit-application");
    478    }
    479    this.isInitialized = false;
    480  },
    481 
    482  /**
    483   * Restore provided preferences.
    484   *
    485   * @param {Map} preferences
    486   *     Map of preferences that should be restored.
    487   */
    488  restorePreferences(preferences) {
    489    for (const k of preferences.keys()) {
    490      lazy.logger.debug(`Resetting recommended pref ${k}`);
    491      Services.prefs.clearUserPref(k);
    492      this.alteredPrefs.delete(k);
    493    }
    494  },
    495 };