Files
watermarks-remover/tests/test_binary_guard.py
256d90d1b1 Refuse binary input in the text-only tools (#24)
* Refuse binary input in the text-only tools

inspect_text.py, clean_text.py and rewrite_text.py accept any path and decode
it with errors="surrogateescape". Pointed at a .docx - a zip - they walk
deflate-compressed bytes and report whatever codepoints fall out of them. The
counts look like findings but track the compression, not the content: in one
sample set a document with nothing hidden in its text reported 12 "suspicious"
characters, while another with 54 real no-break spaces reported 11, none of
which were the no-break spaces.

clean_text.py is worse than misleading. It writes the mangled decode back, so
`clean_text.py report.docx` reports "removed=1" and silently corrupts the
document - the output still passes zipfile.is_zipfile() because the end-of-
central-directory record survives, but reading a member raises.

common.looks_binary() now sniffs magic numbers plus a control-byte ratio, and
guard_binary() refuses with a message naming the tool that does handle the
format. The ratio test is deliberately conservative so text in encodings other
than UTF-8 keeps working, and every entry point takes --force-text to override.
clean_file.py gets the same check on the branch where classify() falls back to
"text" for unrecognised bytes.

Adds tests covering magic-number and heuristic detection, the override, refusal
without writing or backing up, and that clean_file.py still routes a .docx to
the container path.

* Address review: backup ordering, stdin sniff, router advice

Three fixes from the review on #24.

clean_file.py sniffed after --in-place had already taken the backup, so
`clean_file.py --in-place mystery.bin` left a mystery.bin.bak sidecar behind
before exiting 2 — for a file the run never touches, and exactly what
clean_text.py avoids. The sniff now runs before backup_path(). The same hole
applied to `--as text` on a .docx, which bypasses classify() entirely.

The stdin path decoded before sniffing, which made detection depend on the
console codec. It was worse than codec drift: the text layer also translates
newlines, so PNG's `\x89PNG\r\n\x1a\n` arrived as `\x89PNG\n\x1a\n` and the
magic number never matched — the file was refused by the NUL-byte heuristic
instead, and would have sailed through had it lacked NULs. _read_stdin_capped
now reads sys.stdin.buffer and guards the raw octets, matching the file path,
with a text fallback for a replaced stdin.

guard_binary always advised "Use inspect_file.py / clean_file.py", which is
circular when the caller is one of them and classify() has already ruled out
every known container. The advice is now a parameter: the text-only scripts
keep the pointer to the routers, and the routers say the bytes match no
supported format and point at --force-text / --as.

Adds tests for the backup ordering (both --in-place paths), the advice split,
and stdin magic that is not ASCII, across default, cp1252 and latin-1 stdio
codecs — the previous stdin test piped a ZIP, whose "PK" header is ASCII and
survives any of them.

---------

Co-authored-by: Guillaume Meyer (The Opinionated Man) <1385518+guillaumemeyer@users.noreply.github.com>
2026-08-13 13:35:00 -07:00

254 lines
8.2 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Tests for the binary-input guard on the text-only tools."""
from __future__ import annotations
import io
import os
import subprocess
import sys
import zipfile
from pathlib import Path
import pytest
ROOT = Path(__file__).resolve().parents[1]
SCRIPTS = ROOT / "skills" / "remove-ai-marks" / "scripts"
sys.path.insert(0, str(SCRIPTS))
from common import guard_binary, looks_binary # noqa: E402
DOCX_XML = (
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
'<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">'
"<w:body><w:p><w:r><w:t>Table 1 holds the results.</w:t></w:r></w:p></w:body>"
"</w:document>"
)
def make_docx(path: Path) -> Path:
with zipfile.ZipFile(path, "w", zipfile.ZIP_DEFLATED) as zf:
zf.writestr("[Content_Types].xml", "<Types/>")
zf.writestr("word/document.xml", DOCX_XML)
return path
def run(script: str, *args: str) -> subprocess.CompletedProcess:
return subprocess.run(
[sys.executable, str(SCRIPTS / script), *args],
capture_output=True,
text=True,
timeout=60,
)
# --- looks_binary ----------------------------------------------------------
@pytest.mark.parametrize(
"data,expected_fragment",
[
(b"PK\x03\x04rest", "ZIP"),
(b"%PDF-1.7\n", "PDF"),
(b"\x89PNG\r\n\x1a\nrest", "PNG"),
(b"\xff\xd8\xff\xe0rest", "JPEG"),
(b"\x7fELF\x02\x01", "ELF"),
(b"SQLite format 3\x00", "SQLite"),
(b"plain text\x00with a nul", "NUL"),
],
)
def test_flags_binary(data, expected_fragment):
kind = looks_binary(data)
assert kind is not None
assert expected_fragment.lower() in kind.lower()
@pytest.mark.parametrize(
"data",
[
b"",
b"Just some prose.\n",
b"# Markdown\n\n- bullet\n",
"Accented prose: naïve café résumé\n".encode("utf-8"),
"Zero width and nbsp here\n".encode("utf-8"),
b"Latin-1 bytes: caf\xe9 na\xefve\n", # not UTF-8, still text
b"a\tb\r\nc\x0cd\x1b[0m\n", # tabs, CRLF, form feed, ANSI escape
],
)
def test_allows_text(data):
assert looks_binary(data) is None
def test_compressed_bytes_are_flagged(tmp_path):
data = make_docx(tmp_path / "x.docx").read_bytes()
assert looks_binary(data) is not None
def test_guard_binary_can_be_overridden():
guard_binary(b"PK\x03\x04", "x.docx", allow_binary=True) # must not raise
with pytest.raises(SystemExit) as exc:
guard_binary(b"PK\x03\x04", "x.docx")
assert exc.value.code == 2
# --- CLI behaviour ---------------------------------------------------------
def test_inspect_text_refuses_docx(tmp_path):
docx = make_docx(tmp_path / "doc.docx")
r = run("inspect_text.py", str(docx))
assert r.returncode == 2
assert "looks like" in r.stderr
assert "inspect_file.py" in r.stderr
assert "Suspicious:" not in r.stdout
def test_inspect_text_force_text_still_works(tmp_path):
docx = make_docx(tmp_path / "doc.docx")
r = run("inspect_text.py", str(docx), "--force-text")
assert r.returncode in (0, 1)
assert "Length:" in r.stdout
def test_clean_text_refuses_docx_and_writes_nothing(tmp_path):
docx = make_docx(tmp_path / "doc.docx")
before = docx.read_bytes()
out = tmp_path / "doc.cleaned.docx"
r = run("clean_text.py", str(docx), "-o", str(out))
assert r.returncode == 2
assert not out.exists()
assert docx.read_bytes() == before
def test_clean_text_in_place_leaves_docx_intact(tmp_path):
docx = make_docx(tmp_path / "doc.docx")
before = docx.read_bytes()
r = run("clean_text.py", str(docx), "--in-place")
assert r.returncode == 2
assert docx.read_bytes() == before
assert not (tmp_path / "doc.docx.bak").exists()
def test_clean_file_still_routes_docx_to_container(tmp_path):
docx = make_docx(tmp_path / "doc.docx")
out = tmp_path / "out.docx"
r = run("clean_file.py", str(docx), "-o", str(out), "--json")
assert r.returncode == 0, r.stderr
assert out.exists()
with zipfile.ZipFile(out) as zf:
assert zf.testzip() is None
assert "word/document.xml" in zf.namelist()
def test_clean_file_refuses_unknown_binary(tmp_path):
blob = tmp_path / "mystery.bin"
blob.write_bytes(b"\x00\x01\x02\x03" * 64)
out = tmp_path / "out.bin"
r = run("clean_file.py", str(blob), "-o", str(out))
assert r.returncode == 2
assert not out.exists()
def test_clean_file_in_place_refuses_before_taking_a_backup(tmp_path):
"""The refusal must land before backup_path(), not after.
Sniffing after the backup left a .bak sidecar for a file the run never
touches, which is exactly what clean_text.py avoids.
"""
blob = tmp_path / "mystery.bin"
blob.write_bytes(b"\x00\x01\x02\x03" * 64)
before = blob.read_bytes()
r = run("clean_file.py", str(blob), "--in-place")
assert r.returncode == 2
assert blob.read_bytes() == before
assert not (tmp_path / "mystery.bin.bak").exists()
assert list(tmp_path.iterdir()) == [blob]
def test_clean_file_in_place_as_text_on_docx_leaves_no_backup(tmp_path):
"""--as text bypasses classify(), so the guard is the only thing left."""
docx = make_docx(tmp_path / "doc.docx")
before = docx.read_bytes()
r = run("clean_file.py", str(docx), "--in-place", "--as", "text")
assert r.returncode == 2
assert docx.read_bytes() == before
assert not (tmp_path / "doc.docx.bak").exists()
def test_router_advice_is_not_circular(tmp_path):
"""clean_file.py / inspect_file.py must not point back at themselves."""
blob = tmp_path / "mystery.bin"
blob.write_bytes(b"\x00\x01\x02\x03" * 64)
for script in ("clean_file.py", "inspect_file.py"):
r = run(script, str(blob))
assert r.returncode == 2, script
assert "no supported text, image or container format" in r.stderr, script
assert "Use inspect_file.py / clean_file.py" not in r.stderr, script
assert "--force-text" in r.stderr, script
def test_text_only_scripts_keep_the_pointer_to_the_routers(tmp_path):
docx = make_docx(tmp_path / "doc.docx")
r = run("clean_text.py", str(docx))
assert r.returncode == 2
assert "Use inspect_file.py / clean_file.py" in r.stderr
def test_text_files_are_unaffected(tmp_path):
src = tmp_path / "note.txt"
src.write_text("Hiddenmark here.\n", encoding="utf-8")
out = tmp_path / "note.cleaned.txt"
r = run("clean_text.py", str(src), "-o", str(out))
assert r.returncode == 0, r.stderr
assert out.read_text(encoding="utf-8") == "Hiddenmark here.\n"
def test_stdin_binary_is_refused():
docx = io.BytesIO()
with zipfile.ZipFile(docx, "w") as zf:
zf.writestr("word/document.xml", DOCX_XML)
r = subprocess.run(
[sys.executable, str(SCRIPTS / "inspect_text.py")],
input=docx.getvalue(),
capture_output=True,
timeout=60,
)
assert r.returncode == 2
assert b"looks like" in r.stderr
PNG_HEADER = b"\x89PNG\r\n\x1a\n" + b"\x00\x00\x00\rIHDR" + b"\x00" * 32
JPEG_HEADER = b"\xff\xd8\xff\xe0\x00\x10JFIF\x00" + b"A" * 64
@pytest.mark.parametrize("data,label", [(PNG_HEADER, "PNG"), (JPEG_HEADER, "JPEG")])
@pytest.mark.parametrize("io_encoding", [None, "cp1252", "latin-1"])
def test_stdin_non_ascii_magic_is_refused_whatever_the_codec(data, label, io_encoding):
"""The ZIP test alone could not catch this: 'PK' is ASCII.
PNG's 0x89 and JPEG's 0xff only survive to the sniff if stdin is read as
bytes. Decoding first makes detection depend on the console codec.
"""
env = dict(os.environ)
if io_encoding is None:
env.pop("PYTHONIOENCODING", None)
else:
env["PYTHONIOENCODING"] = io_encoding
r = subprocess.run(
[sys.executable, str(SCRIPTS / "inspect_text.py")],
input=data,
capture_output=True,
timeout=60,
env=env,
)
assert r.returncode == 2, (label, io_encoding, r.stderr)
assert label.encode() in r.stderr, (label, io_encoding, r.stderr)
def test_stdin_text_still_flows_through():
r = subprocess.run(
[sys.executable, str(SCRIPTS / "clean_text.py")],
input="plaintext\n".encode("utf-8"),
capture_output=True,
timeout=60,
)
assert r.returncode == 0, r.stderr
assert r.stdout.replace(b"\r\n", b"\n") == b"plaintext\n"