diff --git a/python/src/camoufox/fpgen_model.py b/python/src/camoufox/fpgen_model.py index f4bdbd82..f7cb3bee 100644 --- a/python/src/camoufox/fpgen_model.py +++ b/python/src/camoufox/fpgen_model.py @@ -40,9 +40,10 @@ DECOMPRESSED_FILES = ('fingerprint-network.json', 'values.json', 'values.dat') # and CAMOUFOX_FPGEN_DATA may point it at this directory. The pin carries its # hash, so a values.dat from the pinned model is kept and any other is dropped. -# 2100-01-01T00:00:00Z. fpgen refetches files whose mtime is older than this -# window (pkgman.files_are_recent). -PINNED_MTIME = 4102444800 +# 2038-01-01T00:00:00Z. fpgen refetches files whose mtime is older than this +# window (pkgman.files_are_recent). It fits in signed 32 bits: AWS Lambda keeps +# file times that way, and reads a later date back as one in the past. +PINNED_MTIME = 2145916800 FPGEN_MAX_AGE_S = 5 * 7 * 24 * 3600 # fpgen fetches a model from this URL instead of GitHub. A caller who set it @@ -152,16 +153,16 @@ def _drop_foreign_decompressed(data_dir: Path) -> None: def _stamp(data_dir: Path) -> None: - """Date the files past fpgen's refresh and write the stamp. Files seeded by - another user (a root-built image) may be left as they are while fpgen still - considers them current.""" + """Date the files past fpgen's refresh and write the stamp. Files that + cannot be re-dated (seeded by another user, or on a read-only filesystem) + may be left as they are while fpgen still considers them current.""" for name in PIN['file_sha256']: path = data_dir / name if int(path.stat().st_mtime) == PINNED_MTIME: continue try: os.utime(path, (PINNED_MTIME, PINNED_MTIME)) - except PermissionError: + except OSError: if path.stat().st_mtime < time.time() - FPGEN_MAX_AGE_S: raise stamp = data_dir / STAMP diff --git a/python/tests/test_fpgen_model.py b/python/tests/test_fpgen_model.py index 53b85f16..44d591d4 100644 --- a/python/tests/test_fpgen_model.py +++ b/python/tests/test_fpgen_model.py @@ -7,6 +7,7 @@ webgl.py. python/scripts/pin-fpgen-model.py pinned the model for CI only. """ import ast +import errno import hashlib import io import json @@ -290,6 +291,50 @@ def test_read_only_current_pinned_model_is_used(read_only, pinned): assert pinned == [] +@pytest.fixture +def read_only_fs(monkeypatch): + """A filesystem that cannot be written, such as AWS Lambda's outside /tmp.""" + + def refuse(path, *args, **kwargs): + raise OSError(errno.EROFS, os.strerror(errno.EROFS), str(path)) + + return lambda: monkeypatch.setattr(os, "utime", refuse) + + +def _as_int32(seconds): + return (seconds + 2**31) % 2**32 - 2**31 + + +@pytest.mark.parametrize( + "mtime", + [ + # Lambda keeps file times as signed 32-bit seconds, so a stamp past + # 2038 reads back as a date in the past. + pytest.param(_as_int32(PINNED_MTIME), id="pinned-on-32-bit-times"), + pytest.param(time.time(), id="current"), + ], +) +def test_read_only_filesystem_model_fpgen_accepts_is_used(tmp_path, pinned, read_only_fs, mtime): + data_dir = tmp_path / "data" + _write(data_dir, MEMBERS, mtime=mtime, stamp=fpgen_model.PIN["sha256"]) + read_only_fs() + + ensure_fpgen_model(data_dir) + + assert pinned == [] + + +def test_read_only_filesystem_stale_model_fails_loudly(tmp_path, pinned, read_only_fs): + """fpgen would fetch its own model over files this old.""" + data_dir = tmp_path / "data" + _write(data_dir, MEMBERS, mtime=STALE, stamp=fpgen_model.PIN["sha256"]) + read_only_fs() + + with pytest.raises(OSError) as raised: + ensure_fpgen_model(data_dir) + assert raised.value.errno == errno.EROFS + + def test_install_never_stages_in_a_private_temporary_directory(tmp_path, pinned, monkeypatch): # Since Python 3.13, tempfile.mkdtemp() makes an owner-only directory on # Windows, and a file moved out of it keeps that ACL: a model installed from