fix(#1217): bound what an untrusted upload can make the pull tool read

Gemini review: anyone with the public key can upload, so sender.json over 4 KB is ignored and a package that would unpack past 1 GB is refused before anything is written. A relative answer from an old git is refused rather than guessed at. Each new test failed with its guard removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
fix/1236-redact-screenshots
diffwireauto 21 hours ago committed by Strycher
parent 3ea585b4f1
commit 6702271c54

@ -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))

@ -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"

Loading…
Cancel
Save

Powered by TurnKey Linux.