tor-browser

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

index.rst (24350B)


      1 .. _services/remotesettings:
      2 
      3 ===============
      4 Remote Settings
      5 ===============
      6 
      7 The :searchfox:`remote-settings.sys.mjs <services/settings/remote-settings.sys.mjs>` module offers the ability to fetch remote settings that are kept in sync with Mozilla servers.
      8 
      9 
     10 Usage
     11 =====
     12 
     13 The ``get()`` method returns the list of entries for a specific key. Each entry can have arbitrary attributes, and can only be modified on the server.
     14 
     15 .. code-block:: js
     16 
     17    const { RemoteSettings } = ChromeUtils.importESModule("resource://services-settings/remote-settings.sys.mjs");
     18 
     19    const data = await RemoteSettings("a-key").get();
     20 
     21    /*
     22      data == [
     23        {label: "Yahoo",  enabled: true,  weight: 10, id: "d0782d8d", last_modified: 1522764475905},
     24        {label: "Google", enabled: true,  weight: 20, id: "8883955f", last_modified: 1521539068414},
     25        {label: "Ecosia", enabled: false, weight: 5,  id: "337c865d", last_modified: 1520527480321},
     26      ]
     27    */
     28 
     29    for (const entry of data) {
     30      // Do something with entry...
     31      // await InternalAPI.load(entry.id, entry.label, entry.weight);
     32    });
     33 
     34 .. note::
     35    The ``id`` and ``last_modified`` (timestamp) attributes are assigned by the server.
     36 
     37 
     38 Empty local database
     39 --------------------
     40 
     41 On new user profiles or for recently added use-cases, the local database will be empty until a synchronization with the server happens. Synchronizations are managed internally, and can sometimes be triggered minutes after browser starts.
     42 
     43 By default, if ``.get()`` is called before the local database had the chance to be synchronized, and if no initial data was provided (:ref:`see below <services/initial-data>`), then the settings will be pulled from the server in order to avoid returning an empty list. In that case, the first call to ``.get()`` will thus take longer than the following ones.
     44 
     45 This behaviour can be disabled using the ``syncIfEmpty`` option.
     46 
     47 .. important::
     48 
     49    If the implicit synchronization fails (e.g network is not available) then errors are silent and **an empty list is returned**. :ref:`Uptake Telemetry <services/settings/uptake-telemetry>` status is sent though.
     50 
     51 
     52 Options
     53 -------
     54 
     55 * ``filters``, ``order``: The list can optionally be filtered or ordered:
     56 
     57    .. code-block:: js
     58 
     59        const subset = await RemoteSettings("a-key").get({
     60          filters: {
     61            property: "value"
     62          },
     63          order: "-weight"
     64        });
     65 
     66 * ``syncIfEmpty``: if ``true`` and no local data is present, then look for a packaged dump to load. If none, then pull records from network.
     67  Set it to ``false`` to skip loading of packaged dump and network activity. Use this only if your use-case can tolerate an empty list until the first synchronization happens (default: ``true``).
     68 
     69    .. code-block:: js
     70 
     71        await RemoteSettings("a-key").get({ syncIfEmpty: false });
     72 
     73 * ``verifySignature``: verify the content signature of the local data (default: ``false``).
     74  An error is thrown if the local data was altered. This hurts performance, but can be used if your use case needs to be secure from local tampering.
     75 
     76 * ``emptyListFallback``: return an empty list if obtaining the records fails (default: ``true``).
     77  If an error occurs during the reading of local data, or during synchronization, then an empty list is returned.
     78 
     79 
     80 Events
     81 ------
     82 
     83 The ``on()`` function registers handlers to be triggered when events occur.
     84 
     85 The ``"sync"`` event allows to be notified when the remote settings are changed on the server side. Your handler is given an ``event`` object that contains a ``data`` attribute that has information about the changes:
     86 
     87 - ``current``: current list of entries (after changes were applied);
     88 - ``created``, ``updated``, ``deleted``: list of entries that were created/updated/deleted respectively.
     89 
     90 .. code-block:: js
     91 
     92    RemoteSettings("a-key").on("sync", event => {
     93      const { data: { current } } = event;
     94      for (const entry of current) {
     95        // Do something with entry...
     96        // await InternalAPI.reload(entry.id, entry.label, entry.weight);
     97      }
     98    });
     99 
    100 .. note::
    101 
    102    Currently, the synchronization of remote settings is triggered via push notifications, and also by its own timer every 24H (see the preference ``services.settings.poll_interval`` ).
    103 
    104 
    105 File attachments
    106 ----------------
    107 
    108 When an entry has a file attached to it, it has an ``attachment`` attribute, which contains the file related information (url, hash, size, mimetype, etc.).
    109 
    110 Remote files are not downloaded automatically. In order to keep attachments in sync, the provided helper can be leveraged like this:
    111 
    112 .. code-block:: js
    113 
    114    const client = RemoteSettings("a-key");
    115 
    116    client.on("sync", async ({ data: { created, updated, deleted } }) => {
    117      const toDelete = deleted.filter(d => d.attachment);
    118      const toDownload = created
    119        .concat(updated.map(u => u.new))
    120        .filter(d => d.attachment);
    121 
    122      // Remove local files of deleted records
    123      await Promise.all(
    124        toDelete.map(record => client.attachments.deleteDownloaded(record))
    125      );
    126 
    127      // Download a bundle of all attachments if local cache is empty (see details below)
    128      client.attachments.cacheAll();
    129 
    130      // OR download new attachments individually
    131      const fileContents = await Promise.all(
    132        toDownload.map(async record => {
    133          const { buffer } = await client.attachments.download(record);
    134          return buffer;
    135        });
    136      );
    137    });
    138 
    139 The provided ``cacheAll`` helper will:
    140  - be a no-op if the local attachment cache is not empty
    141    - Use ``pruneAttachments()`` to clear or set the ``force`` parameter to true if you know you want to override this.
    142  - download and extract all attachments if bundling is enabled for the collection
    143  - return a nullable boolean to inform the calling function what happened
    144    - ``null`` if the attachment bundle download was not attempted (ex: client is offline)
    145    - ``false`` if at least one attachment failed to be extracted
    146    - ``true`` if the bundle was found and extracted without error
    147 
    148 
    149 The provided ``download`` helper will:
    150  - fetch the remote binary content
    151  - write the file in the local IndexedDB
    152  - check the file size
    153  - check the content SHA256 hash
    154  - do nothing if the attachment was already present and sound locally.
    155 
    156 .. important::
    157 
    158    The following aspects are not taken care of (yet! help welcome):
    159 
    160    - check available disk space
    161    - preserve bandwidth
    162    - resume downloads of large files
    163 
    164 .. note::
    165 
    166    The ``download()`` method supports the following options:
    167 
    168    - ``retries`` (default: ``3``): number of retries on network errors
    169    - ``fallbackToCache`` (default: ``false``): allows callers to fall back to the cached file and record, if the requested record's attachment fails to download.
    170      This enables callers to always have a valid pair of attachment and record,
    171      provided that the attachment has been retrieved at least once.
    172    - ``fallbackToDump`` (default: ``false``): activates a fallback to a dump that has been
    173      packaged with the client, when other ways to load the attachment have failed.
    174      See :ref:`services/packaging-attachments <services/packaging-attachments>` for more information.
    175    - ``cacheResult`` (default: ``true``): keeps a copy of the attachment file in the cache when true,
    176      allowing future calls for the same attachment to receive the cached result.
    177 
    178 .. note::
    179 
    180    A ``downloadAsBytes()`` method returning an ``ArrayBuffer`` is also available, if writing the attachment locally is not necessary.
    181 
    182 .. _services/initial-data:
    183 
    184 Initial data
    185 ------------
    186 
    187 It is possible to package a dump of the server records that will be loaded into the local database when no synchronization has happened yet.
    188 
    189 The JSON dump will serve as the default dataset for ``.get()``, instead of doing a round-trip to pull the latest data. It will also reduce the amount of data to be downloaded on the first synchronization.
    190 
    191 #. Place the JSON dump of the server records in the ``services/settings/dumps/main/`` folder. Using this command:
    192   ::
    193 
    194      CID="your-collection"
    195      curl "https://firefox.settings.services.mozilla.com/v1/buckets/main/collections/${CID}/changeset?_expected=0" | jq '{"data": .changes, "timestamp": .timestamp}' > services/settings/dumps/main/${CID}.json``
    196 
    197 #. Add the filename to the relevant ``FINAL_TARGET_FILES`` list in ``services/settings/dumps/main/moz.build``
    198 
    199  * Please consider the application(s) where the collection is used and only include the dump file in the relevant builds.
    200  * If it is only for Firefox desktop, i.e. ``browser/``, then add it to a build-specific browser section.
    201  * If it is for all applications, i.e. outside of ``browser/`` are other specific area, then add it to the global section.
    202 
    203 Now, when ``RemoteSettings("some-key").get()`` is called from an empty profile, the ``some-key.json`` file is going to be loaded before the results are returned.
    204 
    205 JSON dumps in the tree are periodically updated by ``taskcluster/docker/periodic-updates/scripts/periodic_file_updates.sh``.
    206 If your collection's in-tree dump should not be kept up to date by this automation, place the JSON file in ``services/settings/static-dumps/`` instead.
    207 
    208 .. note::
    209 
    210   The example above uses "main" because that's the default bucket name.
    211   If you have customized the bucket name, use the actual bucket name instead of "main".
    212 
    213 .. _services/packaging-attachments:
    214 
    215 Packaging attachments
    216 ~~~~~~~~~~~~~~~~~~~~~
    217 
    218 Attachments are not included in the JSON dumps by default. You may choose to package the attachment
    219 with the client, for example if it is important to have the data available at the first startup
    220 without requiring network activity. Or if most users would download the attachment anyway.
    221 Only package attachments if needed, since they increase the file size of the Firefox installer.
    222 
    223 To package an attachment for consumers of the `download()` method:
    224 
    225 #. Select the desired attachment record from the JSON dump of the server records, and place it at
    226   ``services/settings/dumps/<bucket name>/<collection name>/<attachment id>.meta.json``.
    227   The ``<attachment id>`` defaults to the ``id`` field of the record. If this ``id`` is not fixed,
    228   you must choose a custom ID that can be relied upon as a long-term attachment identifier. See
    229   the notes below for more details.
    230 #. Download the attachment associated with the record, and place it at
    231   ``services/settings/dumps/<bucket name>/<collection name>/<attachment id>``.
    232 #. Update ``taskcluster/docker/periodic-updates/scripts/periodic_file_updates.sh`` and add the attachment,
    233   by editing the ``compare_remote_settings_files`` function and describing the attachment.
    234   Unlike JSON dumps, attachments must explicitly be listed in that update script, because the
    235   attachment selection logic needs to be codified in a ``jq`` filter in the script.
    236   For an example, see `Bug 1636158 <https://bugzilla.mozilla.org/show_bug.cgi?id=1636158>`_.
    237 #. Register the location of the ``<attachment id>.meta.json`` and ``<attachment id>`` in the
    238   ``moz.build`` file of the collection folder, and possibly ``package-manifest.in``,
    239   as described in `the previous section about registering JSON dumps <services/initial-data>`.
    240 
    241 .. note::
    242 
    243   ``<attachment id>`` is used to derive the file names of the packaged attachment dump, and as the
    244   key for the cache where attachment updates from the network are saved.
    245   The attachment identifier is expected to be fixed across client application updates.
    246   If that expectation cannot be met, the ``attachmentId`` option of the ``download`` method of the
    247   attachment downloader should be used to override the attachment ID with a custom (stable) value.
    248   In order to keep track of the cached attachment, and prevent it from being pruned automatically,
    249   the attachment identifier will have to be explicitly listed in the ``keepAttachmentsIds = [<attachment id>]``
    250   option of the RemoteSettings client constructor.
    251 
    252 .. note::
    253 
    254   The contents of the ``.meta.json`` file is already contained within the records, but separated
    255   from the main set of records to ensure the availability of the original record with the data,
    256   independently of the packaged or downloaded records.
    257   This file may become optional in a future update, see `Bug 1640059 <https://bugzilla.mozilla.org/show_bug.cgi?id=1640059>`_.
    258 
    259 
    260 Synchronization Process
    261 =======================
    262 
    263 The synchronization process consists in pulling the recent changes, merging them with the local data, and verifying the integrity of the result.
    264 
    265 .. image:: synchronization-flow.svg
    266 
    267 .. Source of diagram
    268 .. https://mermaid-js.github.io/mermaid-live-editor/
    269 .. When using this tool, please remove xlink prefix from attributes in the resulting SVG file.
    270 .. See bug 1481470.
    271 ..
    272 .. graph TD
    273 ..     0[Sync] --> pull;
    274 ..     pull[Pull changes] --> merge[Merge with local]
    275 ..     merge --> valid{Is signature valid?};
    276 ..     valid -->|Yes| Success;
    277 ..     valid -->|No| retried{Retried?};
    278 ..     retried --> |Yes| validchanges{Valid without changes?};
    279 ..     retried --> |No| valid2{Valid without changes?};
    280 ..     validchanges -->|Yes| restoredata[Restore previous data];
    281 ..     validchanges -->|No| clear[Clear local];
    282 ..     restore --> Failure;
    283 ..     valid2 --> |No| clear2[Clear local];
    284 ..     valid2 --> |Yes| Retry;
    285 ..     Retry --> |Retry| pull;
    286 ..     clear2 --> Retry;
    287 ..     clear --> restore[Restore packaged dump];
    288 ..     restoredata --> Failure;
    289 ..     style 0 fill:#00ff00;
    290 ..     style Success fill:#00ff00;
    291 ..     style Failure fill:#ff0000;
    292 
    293 .. important::
    294 
    295    As shown above, we can end-up in situations where synchronization fails and will leave the local DB in an empty state.
    296 
    297 
    298 Targets and A/B testing
    299 =======================
    300 
    301 In order to deliver settings to subsets of the population, you can set targets on entries (platform, language, channel, version range, preferences values, samples, etc.) when editing records on the server.
    302 
    303 From the client API standpoint, this is completely transparent: the ``.get()`` method — as well as the event data — will always filter the entries on which the target matches.
    304 
    305 .. note::
    306 
    307    The remote settings targets follow the same approach as the :ref:`Normandy recipe client <components/normandy>` (ie. JEXL filter expressions).
    308 
    309 
    310 .. _services/settings/uptake-telemetry:
    311 
    312 Uptake Telemetry
    313 ================
    314 
    315 Some :ref:`uptake telemetry <telemetry/collection/uptake>` is collected in order to monitor how remote settings are propagated.
    316 
    317 It is submitted to a single :ref:`keyed histogram <histogram-type-keyed>` whose id is ``UPTAKE_REMOTE_CONTENT_RESULT_1`` and the keys are prefixed with ``main/`` (eg. ``main/a-key`` in the above example).
    318 
    319 
    320 Create new remote settings
    321 ==========================
    322 
    323 Staff members can create new kinds of remote settings, following `this documentation <https://remote-settings.readthedocs.io/en/latest/getting-started.html>`_.
    324 
    325 It basically consists in:
    326 
    327 #. Choosing a key (eg. ``search-providers``)
    328 #. Assigning collaborators to editors and reviewers groups
    329 #. (*optional*) Define a JSONSchema to validate entries
    330 #. (*optional*) Allow attachments on entries
    331 
    332 And once done:
    333 
    334 #. Create, modify or delete entries and let reviewers approve the changes
    335 #. Wait for Firefox to pick-up the changes for your settings key
    336 
    337 
    338 Global Notifications
    339 ====================
    340 
    341 The polling for changes process sends two notifications that observers can register to:
    342 
    343 * ``remote-settings:changes-poll-start``: Polling for changes is starting. triggered either by the scheduled timer or a push broadcast.
    344 * ``remote-settings:changes-poll-end``: Polling for changes has ended
    345 * ``remote-settings:sync-error``: A synchronization error occurred. Notification subject provides information about the error and affected
    346  collection in the ``wrappedJSObject`` attribute.
    347 * ``remote-settings:broken-sync-error``: Synchronization seems to be consistently failing. Profile is at risk.
    348 
    349 .. code-block:: javascript
    350 
    351    const observer = {
    352      observe(aSubject, aTopic, aData) {
    353        Services.obs.removeObserver(this, "remote-settings:changes-poll-start");
    354 
    355        const { expectedTimestamp } = JSON.parse(aData);
    356        console.log("Polling started", expectedTimestamp ? "from push broadcast" : "by scheduled trigger");
    357      },
    358    };
    359    Services.obs.addObserver(observer, "remote-settings:changes-poll-start");
    360 
    361 
    362 Advanced Options
    363 ================
    364 
    365 ``localFields``: records fields that remain local
    366 -------------------------------------------------
    367 
    368 During synchronization, the local database is compared with the server data. Any difference will be overwritten by the remote version.
    369 
    370 In some use-cases it's necessary to store some state using extra attributes on records. The ``localFields`` options allows to specify which records field names should be preserved on records during synchronization.
    371 
    372 .. code-block:: javascript
    373 
    374    const client = RemoteSettings("a-collection", {
    375      localFields: [ "userNotified", "userResponse" ],
    376    });
    377 
    378 
    379 ``filterCreator``: custom filter
    380 --------------------------------
    381 
    382 By default, the entries returned by ``.get()`` are filtered based on the JEXL expression result from the ``filter_expression`` field. The ``filterCreator`` option allows to provide a custom filter: On a call to ``.get()``, the filter creator is called to create a new filter object. Then the method `filterObject.filterEntry(entry)` is executed for every entry. This (async) method should return the record (modified or not) if the record should be kept, or a falsy value if the record should be filtered out.
    383 
    384 .. code-block:: javascript
    385 
    386    const client = RemoteSettings("a-collection", {
    387      filterCreator: async (environment, collectionName) => {
    388        // Return a filter object which filters entries based on entry.enabled.
    389        return {
    390          async filterEntry(record) {
    391            const { enabled, ...entry } = record;
    392            return enabled ? entry : null;
    393          }
    394        };
    395      }
    396    });
    397 
    398 
    399 Debugging and manual testing
    400 ============================
    401 
    402 Logging
    403 -------
    404 
    405 In order to enable verbose logging, set the log level preference to ``debug``.
    406 
    407 .. code-block:: javascript
    408 
    409    Services.prefs.setStringPref("services.settings.loglevel", "debug");
    410 
    411 Remote Settings Dev Tools
    412 -------------------------
    413 
    414 The Remote Settings Dev Tools extension provides some tooling to inspect synchronization statuses, to change the remote server or to switch to *preview* mode in order to sign-off pending changes. `More information on the dedicated repository <https://github.com/mozilla/remote-settings-devtools>`_.
    415 
    416 Preview Mode
    417 ------------
    418 
    419 Enable the preview mode in order to preview changes to be reviewed on the server. This can be achieved using the *Remote Settings Dev Tools*, or programmatically with:
    420 
    421 .. code-block:: javascript
    422 
    423    RemoteSettings.enablePreviewMode(true);
    424 
    425 In order to pull preview data **on startup**, or in order to persist it across restarts, set ``services.settings.preview_enabled`` to ``true`` in the profile preferences (ie. ``user.js``).
    426 For release and ESR, for security reasons, you would have to run the application with the ``MOZ_REMOTE_SETTINGS_DEVTOOLS=1`` environment variable for the preference to be taken into account. Note that toggling the preference won't have any effect until restart.
    427 
    428 Trigger a synchronization manually
    429 ----------------------------------
    430 
    431 The synchronization of every known remote settings clients can be triggered manually with ``pollChanges()``:
    432 
    433 .. code-block:: js
    434 
    435    await RemoteSettings.pollChanges()
    436 
    437 In order to ignore last synchronization status during polling for changes, set the ``full`` option:
    438 
    439 .. code-block:: js
    440 
    441    await RemoteSettings.pollChanges({ full: true })
    442 
    443 The synchronization of a single client can be forced with the ``.sync()`` method:
    444 
    445 .. code-block:: js
    446 
    447    await RemoteSettings("a-key").sync();
    448 
    449 .. important::
    450 
    451    The above methods are only relevant during development or debugging and should never be called in production code.
    452 
    453 
    454 Inspect local data
    455 ------------------
    456 
    457 The internal IndexedDB of Remote Settings can be accessed via the Storage Inspector in the `browser toolbox <https://developer.mozilla.org/en-US/docs/Tools/Browser_Toolbox>`_.
    458 
    459 For example, the local data of the ``"key"`` collection can be accessed in the ``remote-settings`` database at *Browser Toolbox* > *Storage* > *IndexedDB* > *chrome*, in the ``records`` store.
    460 
    461 
    462 Delete all local data
    463 ---------------------
    464 
    465 All local data, of **every collection**, including downloaded attachments, can be deleted with:
    466 
    467 .. code-block:: js
    468 
    469    await RemoteSettings.clearAll();
    470 
    471 
    472 Unit Tests
    473 ==========
    474 
    475 As a foreword, we would like to underline the fact that your tests should not test Remote Settings itself. Your tests should assume Remote Settings works, and should only run assertions on the integration part. For example, if you see yourself mocking the server responses, your tests may go over their responsibility.
    476 
    477 If your code relies on the ``"sync"`` event, you are likely to be interested in faking this event and make sure your code runs as expected. If it relies on ``.get()``, you will probably want to insert some fake local data.
    478 
    479 
    480 Simulate ``"sync"`` events
    481 --------------------------
    482 
    483 You can forge a ``payload`` that contains the events attributes as described above, and emit it :)
    484 
    485 .. code-block:: js
    486 
    487    const payload = {
    488      current: [{ id: "abc", age: 43 }],
    489      created: [],
    490      updated: [{ old: { id: "abc", age: 42 }, new: { id: "abc", age: 43 }}],
    491      deleted: [],
    492    };
    493 
    494    await RemoteSettings("a-key").emit("sync", { data: payload });
    495 
    496 
    497 Manipulate local data
    498 ---------------------
    499 
    500 A handle on the underlying database can be obtained through the ``.db`` attribute.
    501 
    502 .. code-block:: js
    503 
    504    const db = RemoteSettings("a-key").db;
    505 
    506 And records can be created manually (as if they were synchronized from the server):
    507 
    508 .. code-block:: js
    509 
    510    const record = await db.create({
    511      id: "a-custom-string-or-uuid",
    512      domain: "website.com",
    513      usernameSelector: "#login-account",
    514      passwordSelector: "#pass-signin",
    515    });
    516 
    517 If no timestamp is set, any call to ``.get()`` will trigger the load of initial data (JSON dump) if any, or a synchronization will be triggered. To avoid that, store a fake timestamp. We use ``Date.now()`` instead of an arbitrary number, to make sure it's higher than the dump's, and thus prevent its load from the test.
    518 
    519 .. code-block:: js
    520 
    521    await db.importChanges({}, Date.now());
    522 
    523 In order to bypass the potential target filtering of ``RemoteSettings("key").get()``, the low-level listing of records can be obtained with ``collection.list()``:
    524 
    525 .. code-block:: js
    526 
    527    const { data: subset } = await db.list({
    528      filters: {
    529        "property": "value"
    530      }
    531    });
    532 
    533 The local data can be flushed with ``clear()``:
    534 
    535 .. code-block:: js
    536 
    537    await db.clear()
    538 
    539 
    540 Misc
    541 ====
    542 
    543 We host more documentation on https://remote-settings.readthedocs.io/, on how to run a server locally, manage attachments, or use the REST API etc.
    544 
    545 About blocklists
    546 ----------------
    547 
    548 The security settings, as well as addons, plugins, and GFX blocklists were the first use-cases of remote settings, and thus have some specificities.
    549 
    550 For example, they leverage advanced customization options (bucket, content-signature certificate, target filtering etc.). In order to get a reference to these clients, their initialization code must be executed first.
    551 
    552 .. code-block:: js
    553 
    554    const {RemoteSecuritySettings} =
    555      ChromeUtils.importESModule("resource://gre/modules/psm/RemoteSecuritySettings.sys.mjs");
    556 
    557    RemoteSecuritySettings.init();
    558 
    559 
    560    const {BlocklistPrivate} =
    561      ChromeUtils.importESModule("resource://gre/modules/Blocklist.sys.mjs");
    562 
    563    BlocklistPrivate.ExtensionBlocklistRS._ensureInitialized();
    564    BlocklistPrivate.PluginBlocklistRS._ensureInitialized();
    565    BlocklistPrivate.GfxBlocklistRS._ensureInitialized();
    566 
    567 Then, in order to access a specific client instance, the ``bucketName`` must be specified:
    568 
    569 .. code-block:: js
    570 
    571    const client = RemoteSettings("onecrl", { bucketName: "security-state" });
    572 
    573 And in the storage inspector, the IndexedDB internal store will be prefixed with ``security-state`` instead of ``main`` (eg. ``security-state/onecrl``).