From b9ee6569809346f96add79ea5dc00a434c6511fa Mon Sep 17 00:00:00 2001 From: shbernal Date: Tue, 20 Oct 2026 19:59:33 +0200 Subject: [PATCH] Pin uBlock Origin to one release per package The default addon was fetched from AMO's latest-version URL the first time it was needed, and that copy was reused for good. So two machines on the same Camoufox release could run different uBlock Origin builds, depending on the day each cache was filled, and a new uBO release reached users without passing through CI. python/src/camoufox/addons.json now names the build: its AMO file URL, version and sha256. Both packages read it, check each download against the sha256 before extracting it, and cache each version in its own directory, addons///, so a bump installs fresh instead of reusing another release's copy. The file's note says where a bump takes its values from. The install gate now checks that fetch left the pinned uBO in the cache, and no staging directories, as it already did for the GeoIP database. --- README.md | 2 +- ci/run_install.py | 7 ++++ python/src/camoufox/addons.json | 8 ++++ python/src/camoufox/addons.py | 13 ++++-- python/tests/test_addons.py | 28 +++++++++++++ typescript/scripts/golden/launch_golden.py | 6 +-- typescript/src/addons.ts | 19 ++++++--- typescript/src/paths.ts | 1 + typescript/tests/addons.test.ts | 48 +++++++++++++++++++++- typescript/tests/launch-golden.test.ts | 5 ++- typescript/tests/launch-host.ts | 5 ++- 11 files changed, 123 insertions(+), 19 deletions(-) create mode 100644 python/src/camoufox/addons.json diff --git a/README.md b/README.md index 0ba3e514..095f2369 100644 --- a/README.md +++ b/README.md @@ -512,7 +512,7 @@ claims the host's OS. Details are in | Feature | Where | |---|---| | Load extracted addons without a debug server: `addons=["/path/to/addon"]` | launcher, `camoufox-window-module` patch | -| uBlock Origin installed by default, with its own default filter lists (`exclude_addons=[DefaultAddons.UBO]` to drop it) | launcher | +| uBlock Origin installed by default, one pinned release per package (`python/src/camoufox/addons.json`), with its own default filter lists (`exclude_addons=[DefaultAddons.UBO]` to drop it) | launcher | | Addons cannot open tabs unless `allow_addon_new_tab=True` | `disable-extension-newtab` patch | | Addons run in private browsing | `all-addons-private-mode` patch | diff --git a/ci/run_install.py b/ci/run_install.py index 8b1987fb..cbce1c62 100644 --- a/ci/run_install.py +++ b/ci/run_install.py @@ -60,6 +60,7 @@ NODE_PROBE = TYPESCRIPT / "scripts" / "e2e" / "node_probe.mjs" # Every Linux identity reports these (pythonlib's base set), so each must # render from the bundle rather than fall back. ESSENTIAL_FONTS = PYTHONLIB / "src" / "camoufox" / "essential-fonts.json" +ADDON_PINS = PYTHONLIB / "src" / "camoufox" / "addons.json" LAUNCH = {"os": "linux", "headless": True} # Lowercase, so each character is one keydown: a capital adds Shift's. TYPED = "camoufox" @@ -118,6 +119,12 @@ def installed(cache: Path, tag: str) -> List[str]: leftovers = [p.name for p in cache.glob("camoufox/geoip/.download-*")] if leftovers: missing.append(f"no staging directories left in camoufox/geoip/ (found {leftovers})") + ubo = json.loads(ADDON_PINS.read_text(encoding="utf-8"))["UBO"]["version"] + if not (cache / "camoufox" / "addons" / "UBO" / ubo / "manifest.json").is_file(): + missing.append(f"the pinned uBlock Origin {ubo} under camoufox/addons/UBO/") + leftovers = [p.name for p in cache.glob("camoufox/addons/*/.*.tmp-*")] + if leftovers: + missing.append(f"no staging directories left in camoufox/addons/ (found {leftovers})") return missing diff --git a/python/src/camoufox/addons.json b/python/src/camoufox/addons.json new file mode 100644 index 00000000..c91544a7 --- /dev/null +++ b/python/src/camoufox/addons.json @@ -0,0 +1,8 @@ +{ + "UBO": { + "version": "1.75.0", + "url": "https://addons.mozilla.org/firefox/downloads/file/5034826/ublock_origin-1.75.0.xpi", + "sha256": "5b74415860456370644bd80f16125e865b0e6c356bb5dfcfb84069967eaa5287" + }, + "note": "The default addons, pinned so a package release always installs the same build. To bump uBlock Origin, take file.url and file.hash from https://addons.mozilla.org/api/v5/addons/addon/ublock-origin/versions// (the hash is prefixed sha256:). Each version is cached in its own directory, addons///." +} diff --git a/python/src/camoufox/addons.py b/python/src/camoufox/addons.py index 3211dfdd..10bd3b8a 100644 --- a/python/src/camoufox/addons.py +++ b/python/src/camoufox/addons.py @@ -1,16 +1,20 @@ +import json import os import shutil import tempfile from enum import Enum +from pathlib import Path from threading import Lock from typing import List, Optional from .exceptions import InvalidAddonPath -from .pkgman import INSTALL_DIR, unzip, webdl +from .pkgman import INSTALL_DIR, unzip, verify_sha256, webdl # Addons are stored in a shared folder, not per-browser version ADDONS_DIR = INSTALL_DIR / "addons" +PINS = json.loads((Path(__file__).parent / 'addons.json').read_text(encoding='utf-8')) + # One lock for the module: `with Lock():` on a fresh multiprocessing.Lock per call # guarded nothing, so concurrent launches in one process raced the same # check-then-extract. @@ -22,7 +26,7 @@ class DefaultAddons(Enum): Default addons to be downloaded """ - UBO = "https://addons.mozilla.org/firefox/downloads/latest/ublock-origin/latest.xpi" + UBO = PINS['UBO']['url'] def confirm_paths(paths: List[str]) -> None: @@ -63,6 +67,7 @@ def download_and_extract(url: str, extract_path: str, name: str) -> None: a manifest.json in a directory whose other files are still being written. """ buffer = webdl(url, desc=f"Downloading addon ({name})", bar=False) + verify_sha256(buffer, PINS[name]['sha256'], f"addon ({name})") parent = os.path.dirname(extract_path) tmp_dir = tempfile.mkdtemp(prefix=f".{name}.tmp-", dir=parent) try: @@ -83,9 +88,9 @@ def download_and_extract(url: str, extract_path: str, name: str) -> None: def get_addon_path(addon_name: str) -> str: """ - Returns a path to the addon in the shared addons folder. + Returns a path to the pinned version of the addon in the shared addons folder. """ - return str(ADDONS_DIR / addon_name) + return str(ADDONS_DIR / addon_name / PINS[addon_name]['version']) def maybe_download_addons( diff --git a/python/tests/test_addons.py b/python/tests/test_addons.py index 564b5f40..c8619629 100644 --- a/python/tests/test_addons.py +++ b/python/tests/test_addons.py @@ -12,13 +12,17 @@ Run with: """ import os +import re import threading import time +from io import BytesIO +from pathlib import Path import pytest from camoufox import addons as addons_mod from camoufox.addons import DefaultAddons, add_default_addons, maybe_download_addons +from camoufox.exceptions import CorruptedDownload UBO = DefaultAddons.UBO.name @@ -131,6 +135,7 @@ def test_extraction_appears_complete_or_not_at_all(tmp_path, monkeypatch): seen.append((target / "manifest.json").exists()) monkeypatch.setattr(addons_mod, "webdl", lambda *a, **k: b"") + monkeypatch.setattr(addons_mod, "verify_sha256", lambda *a, **k: None) monkeypatch.setattr(addons_mod, "unzip", fake_unzip) addons_mod.download_and_extract("url", str(target), UBO) @@ -153,6 +158,7 @@ def test_complete_addon_installed_meanwhile_is_kept(tmp_path, monkeypatch): f.write("") monkeypatch.setattr(addons_mod, "webdl", lambda *a, **k: b"") + monkeypatch.setattr(addons_mod, "verify_sha256", lambda *a, **k: None) monkeypatch.setattr(addons_mod, "unzip", fake_unzip) addons_mod.download_and_extract("url", str(target), UBO) @@ -177,3 +183,25 @@ def test_failed_download_keeps_complete_addon(addons_dir, monkeypatch): assert (path / "manifest.json").exists() assert out == [str(path)] + + +def test_default_addons_are_pinned_to_one_file(): + for addon in DefaultAddons: + pin = addons_mod.PINS[addon.name] + assert addon.value == pin["url"] + assert f"-{pin['version']}.xpi" in pin["url"] + assert re.fullmatch(r"[0-9a-f]{64}", pin["sha256"]) + + +def test_each_pinned_version_has_its_own_cache_dir(): + # A cache filled by another release must not stand in for this one's pin. + pin = addons_mod.PINS[UBO] + assert Path(addons_mod.get_addon_path(UBO)) == addons_mod.ADDONS_DIR / UBO / pin["version"] + + +def test_download_that_does_not_match_the_pin_is_rejected(addons_dir, monkeypatch): + monkeypatch.setattr(addons_mod, "webdl", lambda *a, **k: BytesIO(b"not the pinned xpi")) + + with pytest.raises(CorruptedDownload): + maybe_download_addons([DefaultAddons.UBO], []) + assert not addons_dir.exists() or not any(addons_dir.iterdir()) diff --git a/typescript/scripts/golden/launch_golden.py b/typescript/scripts/golden/launch_golden.py index 67c50426..689b51f5 100644 --- a/typescript/scripts/golden/launch_golden.py +++ b/typescript/scripts/golden/launch_golden.py @@ -91,7 +91,7 @@ import orjson # noqa: E402 import camoufox.fingerprints as fingerprints # noqa: E402 from camoufox import geolocation, locales, utils # noqa: E402 -from camoufox.addons import DefaultAddons # noqa: E402 +from camoufox.addons import DefaultAddons, get_addon_path # noqa: E402 from camoufox.fingerprints import Screen # noqa: E402 from camoufox.utils import launch_options # noqa: E402 @@ -104,8 +104,8 @@ for name in ('geoip aio by daijro-combined.mmdb', 'maxmind geolite2-ipv4.mmdb', p.write_bytes(b'') # The default addon, already "downloaded", so nothing is fetched. -(CACHE / 'addons' / 'UBO').mkdir(parents=True) -(CACHE / 'addons' / 'UBO' / 'manifest.json').write_text('{}') +Path(get_addon_path('UBO')).mkdir(parents=True) +(Path(get_addon_path('UBO')) / 'manifest.json').write_text('{}') # ------------------------------------------------------------ host probes HOST = { diff --git a/typescript/src/addons.ts b/typescript/src/addons.ts index c023f7c7..4b3c36ab 100644 --- a/typescript/src/addons.ts +++ b/typescript/src/addons.ts @@ -6,14 +6,22 @@ import * as fs from "node:fs"; import * as path from "node:path"; import { InvalidAddonPath } from "./exceptions.js"; -import { INSTALL_DIR } from "./paths.js"; -import { unzip, webdl } from "./pkgman.js"; +import { INSTALL_DIR, loadDataFile } from "./paths.js"; +import { unzip, verifySha256, webdl } from "./pkgman.js"; + +interface AddonPin { + version: string; + url: string; + sha256: string; +} + +const PINS: Record = loadDataFile("addons.json"); /** * Default addons to be downloaded. */ export const DefaultAddons = { - UBO: "https://addons.mozilla.org/firefox/downloads/latest/ublock-origin/latest.xpi", + UBO: PINS.UBO.url, } as const; export type DefaultAddon = keyof typeof DefaultAddons; @@ -72,6 +80,7 @@ export async function downloadAndExtract( name: string, ): Promise { const buffer = await webdl(url, `Downloading addon (${name})`, false); + verifySha256(buffer, PINS[name].sha256, `addon (${name})`); const tmpDir = fs.mkdtempSync( path.join(path.dirname(extractPath), `.${name}.tmp-`), ); @@ -93,10 +102,10 @@ export async function downloadAndExtract( } /** - * Returns a path to the addon in the shared addons folder. + * Returns a path to the pinned version of the addon in the shared addons folder. */ export function getAddonPath(addonName: string): string { - return path.join(ADDONS_DIR, addonName); + return path.join(ADDONS_DIR, addonName, PINS[addonName].version); } /** Seams the Python tests reach with monkeypatch. */ diff --git a/typescript/src/paths.ts b/typescript/src/paths.ts index 6cb4fa36..994865a6 100644 --- a/typescript/src/paths.ts +++ b/typescript/src/paths.ts @@ -65,6 +65,7 @@ export const INSTALL_DIR: string = userCacheDir("camoufox"); * tarball, so one seed draws one identity in either launcher. */ export const DATA_FILES: readonly string[] = [ + "addons.json", "browser-pin.json", "essential-fonts.json", "fingerprint-presets.json", diff --git a/typescript/tests/addons.test.ts b/typescript/tests/addons.test.ts index 954c2f91..cde8e978 100644 --- a/typescript/tests/addons.test.ts +++ b/typescript/tests/addons.test.ts @@ -100,6 +100,7 @@ async function withUnzip(fakeUnzip: (extractPath: string) => void) { vi.doMock("../src/pkgman.js", async (importOriginal) => ({ ...(await importOriginal()), webdl: async () => Buffer.alloc(0), + verifySha256: () => {}, unzip: (_buffer: Buffer, extractPath: string) => fakeUnzip(extractPath), })); const mod: Addons = await import("../src/addons.js"); @@ -126,7 +127,7 @@ it("extracts so the addon appears complete or not at all", async () => { "background.js", "manifest.json", ]); - expect(fs.readdirSync(path.dirname(ubo))).toEqual(["UBO"]); + expect(fs.readdirSync(path.dirname(ubo))).toEqual([path.basename(ubo)]); }); it("keeps a complete addon installed meanwhile", async () => { @@ -142,7 +143,7 @@ it("keeps a complete addon installed meanwhile", async () => { await mod.downloadAndExtract("url", ubo, "UBO"); expect(fs.readdirSync(ubo).sort()).toEqual(["manifest.json", "theirs.js"]); - expect(fs.readdirSync(path.dirname(ubo))).toEqual(["UBO"]); + expect(fs.readdirSync(path.dirname(ubo))).toEqual([path.basename(ubo)]); }); it("keeps a complete addon when a download fails", async () => { @@ -158,3 +159,46 @@ it("keeps a complete addon when a download fails", async () => { expect(fs.existsSync(path.join(ubo, "manifest.json"))).toBe(true); expect(out).toEqual([ubo]); }); + +const PINS = JSON.parse( + fs.readFileSync( + path.join(import.meta.dirname, "../../python/src/camoufox/addons.json"), + "utf-8", + ), +); + +it("pins every default addon to one file", () => { + for (const [name, url] of Object.entries(addons.DefaultAddons)) { + const pin = PINS[name]; + expect(url).toBe(pin.url); + expect(pin.url).toContain(`-${pin.version}.xpi`); + expect(pin.sha256).toMatch(/^[0-9a-f]{64}$/); + } +}); + +it("caches each pinned version in its own directory", () => { + // A cache filled by another release must not stand in for this one's pin. + expect(ubo).toBe( + path.join(tmp, "camoufox", "addons", "UBO", PINS.UBO.version), + ); +}); + +it("rejects a download that does not match the pin", async () => { + vi.doMock("../src/pkgman.js", async (importOriginal) => ({ + ...(await importOriginal()), + webdl: async () => Buffer.from("not the pinned xpi"), + })); + vi.resetModules(); + const mocked: Addons = await import("../src/addons.js"); + const { CorruptedDownload } = await import("../src/exceptions.js"); + + try { + await expect( + mocked.maybeDownloadAddons({ ...mocked.DefaultAddons }, []), + ).rejects.toBeInstanceOf(CorruptedDownload); + const dir = path.dirname(ubo); + expect(fs.existsSync(dir) ? fs.readdirSync(dir) : []).toEqual([]); + } finally { + vi.doUnmock("../src/pkgman.js"); + } +}); diff --git a/typescript/tests/launch-golden.test.ts b/typescript/tests/launch-golden.test.ts index 66de4587..1b4b8408 100644 --- a/typescript/tests/launch-golden.test.ts +++ b/typescript/tests/launch-golden.test.ts @@ -144,8 +144,9 @@ beforeAll(async () => { fs.writeFileSync(path.join(mods.geolocation.MMDB_DIR, name), ""); } // The default addon, already "downloaded", so nothing is fetched. - fs.mkdirSync(path.join(CACHE, "addons", "UBO"), { recursive: true }); - fs.writeFileSync(path.join(CACHE, "addons", "UBO", "manifest.json"), "{}"); + const { getAddonPath } = await import("../src/addons.js"); + fs.mkdirSync(getAddonPath("UBO"), { recursive: true }); + fs.writeFileSync(path.join(getAddonPath("UBO"), "manifest.json"), "{}"); const deps = mods.utils.utilsDeps; const realFix = mods.fingerprints.fixHardwareConcurrency; diff --git a/typescript/tests/launch-host.ts b/typescript/tests/launch-host.ts index ba761939..0244b23b 100644 --- a/typescript/tests/launch-host.ts +++ b/typescript/tests/launch-host.ts @@ -33,8 +33,9 @@ export const HOME = path.join(SCRATCH, "home"); fs.mkdirSync(HOME, { recursive: true }); // The default addon, already "downloaded", so nothing is fetched. -fs.mkdirSync(path.join(CACHE, "addons", "UBO"), { recursive: true }); -fs.writeFileSync(path.join(CACHE, "addons", "UBO", "manifest.json"), "{}"); +const { getAddonPath } = await import("../src/addons.js"); +fs.mkdirSync(getAddonPath("UBO"), { recursive: true }); +fs.writeFileSync(path.join(getAddonPath("UBO"), "manifest.json"), "{}"); export const utils = await import("../src/utils.js"); export const fingerprints = await import("../src/fingerprints.js");