diff --git a/tools/logs/offband_logs.py b/tools/logs/offband_logs.py index dcbcb567..3f9db5fc 100644 --- a/tools/logs/offband_logs.py +++ b/tools/logs/offband_logs.py @@ -54,6 +54,10 @@ BUCKET = "offband-log-intake" SEEN_LABEL = "seen" KEY_PATH = Path(os.environ.get("OFFBAND_LOG_KEY", r"C:\Dev\.keys\offband-log-key")) SENDER_FILE = "sender.json" +MAX_SENDER_BYTES = 4096 +# Anyone holding the public key can upload, so a package is untrusted; this +# refuses a zip bomb before anything is written. +MAX_UNPACKED_BYTES = 1024 * 1024 * 1024 UNSAFE_IN_NAME = re.compile(r'[<>:"/\\|?*\x00-\x1f]') CODE =re.compile(r"^OB-[23456789ABCDEFGHJKMNPQRSTUVWXYZ]{5}$") OBJECT = re.compile(r"`(uploads/(OB-[23456789ABCDEFGHJKMNPQRSTUVWXYZ]{5})-[0-9a-f-]{36}\.bin)`") @@ -100,6 +104,9 @@ def unpack(package: bytes, dest: Path) -> list: root = dest.resolve() with zipfile.ZipFile(io.BytesIO(package)) as zf: members = [m for m in zf.infolist() if not m.is_dir()] + total = sum(m.file_size for m in members) + if total > MAX_UNPACKED_BYTES: + raise ValueError(f"package would unpack to {total} bytes, over the {MAX_UNPACKED_BYTES} limit") for m in members: target = (root / m.filename).resolve() if root != target and root not in target.parents: @@ -126,13 +133,15 @@ def default_out_root(start: Path = Path(__file__).resolve().parent) -> Path: ).stdout.strip() except (OSError, subprocess.CalledProcessError) as e: raise SystemExit(f"Could not find the main checkout from {start} ({e}). Set OFFBAND_LOG_DIR.") + if not Path(common).is_absolute(): + raise SystemExit(f"git gave a relative path ({common}); git 2.31+ is needed. Set OFFBAND_LOG_DIR.") return Path(common).parent / "debug_logs" def sender_of(package: bytes) -> dict: """The optional name and email the sender gave, from sender.json.""" with zipfile.ZipFile(io.BytesIO(package)) as zf: - if SENDER_FILE not in zf.namelist(): + if SENDER_FILE not in zf.namelist() or zf.getinfo(SENDER_FILE).file_size > MAX_SENDER_BYTES: return {} try: data = json.loads(zf.read(SENDER_FILE)) diff --git a/tools/logs/test_offband_logs.py b/tools/logs/test_offband_logs.py index 43844a2e..0f8ce29d 100644 --- a/tools/logs/test_offband_logs.py +++ b/tools/logs/test_offband_logs.py @@ -193,6 +193,26 @@ def test_lands_in_the_main_checkout_even_from_a_worktree(tmp_path, monkeypatch): assert worktree.resolve() not in root.resolve().parents +def test_a_relative_git_answer_is_refused(tmp_path, monkeypatch): + monkeypatch.delenv("OFFBAND_LOG_DIR", raising=False) + monkeypatch.setattr(ol.subprocess, "run", + lambda *a, **k: subprocess.CompletedProcess(a, 0, stdout=".git\n")) + with pytest.raises(SystemExit, match="OFFBAND_LOG_DIR"): + ol.default_out_root(start=tmp_path) + + +def test_an_oversized_sender_file_is_ignored(): + big = json.dumps({"name": "x" * 5000}).encode() + assert ol.sender_of(zipped({"sender.json": big})) == {} + + +def test_unpack_refuses_a_zip_bomb_before_writing(tmp_path, monkeypatch): + monkeypatch.setattr(ol, "MAX_UNPACKED_BYTES", 10) + with pytest.raises(ValueError, match="over the 10 limit"): + ol.unpack(zipped({"a.log": b"x" * 11}), tmp_path / "OB-AAAAA") + assert not (tmp_path / "OB-AAAAA").exists() + + def test_offband_log_dir_overrides_the_location(tmp_path, monkeypatch): monkeypatch.setenv("OFFBAND_LOG_DIR", str(tmp_path / "elsewhere")) assert ol.default_out_root() == tmp_path / "elsewhere"