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/<name>/<version>/, 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.
This commit is contained in:
shbernal
2026-10-24 09:57:46 +02:00
parent 026ec90ea4
commit b9ee656980
11 changed files with 123 additions and 19 deletions
+8
View File
@@ -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/<version>/ (the hash is prefixed sha256:). Each version is cached in its own directory, addons/<name>/<version>/."
}
+9 -4
View File
@@ -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(
+28
View File
@@ -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())