mirror of
https://github.com/kuhyx/signal-bot.git
synced 2026-11-10 19:07:19 +01:00
Some checks are pending
CI / check (push) Waiting to run
Send a photo captioned "!add_photo alfa wakacje" in one of the album's own groups and it lands in the pack tagged with both, retrievable through !alfa and the new !losowe immediately -- no restart, and no tagger session on the PC. A label matching nobody becomes a custom tag, and !losowe is the only door to one, which is what keeps a live swap a *data* swap: it can never register a trigger or widen a command's groups. No typed label travels. Labels become pseudonym keys in a new registry arg_resolver, at the dispatcher, before the line that logs the command name -- so no handler, journal entry, command_uses row or reply body ever holds a word somebody typed. The reply is counts and pseudonyms: "dodano · p1: 4 · t1: 1". Stripping is exiftool with a *positive* check on the output, never an exit code. ffmpeg was tried first and fails silently on photos: given a JPEG with a real APP1 Exif segment it returned a byte-identical file, GPS intact, exit 0. The surviving-tag allowlist is per tag *name* rather than per group, because measurement showed a group-level "must be empty" rule rejects every format except JPEG -- a valid PNG always reports its IHDR, an MP4 some fifty QuickTime tags that are the container itself. Codec configuration (CompressorName, VendorID, the HEVC parameter set) is on the list by decision: CompressorName lives in the avc1 sample description and exiftool cannot write it at all, so the alternative was refusing every video and every iPhone HEIC. Location and identity tags are still refused, and the e2e run re-reads the stored files to prove a QuickTime geotag did not survive. .webm and .svg stay on the format allowlist and are stored *unstripped*: exiftool 13.55 can write neither, and its SVG reader does not even surface <metadata>. That is a deliberate, documented hole, not an oversight. Two things this had to defuse along the way: - personal_data_sources now reads custom.json, so "!add_photo kot <photo>" would have outlawed the word "kot" and refused every future commit, fixable only by hand-editing an album the committer cannot see. Minting now refuses a word that is already a command, and the gate subtracts the repo's own alias vocabulary from custom labels only -- curated people.json names are never excused. - signal_bot/external.py is now the single subprocess site in the repository, so ruff's S603 needs exactly one scoped per-file-ignore instead of one per module that shells out. A test asserts nothing else imports subprocess. A failed add changes nothing: media and the three manifests are written, the album is reloaded, and only a load that succeeds is published; a load that fails rolls all four back and keeps serving the pack that was already working, without raising -- Restart=always would turn a ConfigError into a loop. 977 tests, 100% branch coverage, no omits and no suppressions. scripts/e2e.sh gains 8 scenarios driven against the real exiftool on media generated at run time, and asserts the invariants a reply cannot: that a refused upload is not stored, that a message with no attachment leaves custom.json byte-identical, and that signal-cli's own copy of every ingested attachment is gone. Closes TODO-add-photo.md. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JqqE22jp94B1ypwxppdr1o
126 lines
4.5 KiB
Python
126 lines
4.5 KiB
Python
# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
|
|
"""Where the personal-data gate learns its forbidden words from.
|
|
|
|
Worth its own test because ``!add_photo`` made one of those sources
|
|
*writable from a group chat*, and the failure mode is silent and permanent:
|
|
a label that is also a word in this repository would put that word on the
|
|
forbidden list and refuse every commit from then on, fixable only by
|
|
hand-editing an album the committer cannot see.
|
|
|
|
Driven entirely through ``forbidden()``, which is what the gate itself calls.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import importlib.util
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
import pytest
|
|
|
|
if TYPE_CHECKING:
|
|
from types import ModuleType
|
|
|
|
REPO = Path(__file__).resolve().parent.parent
|
|
|
|
|
|
@pytest.fixture
|
|
def sources() -> ModuleType:
|
|
"""Import scripts/personal_data_sources.py, which is not a package."""
|
|
path = REPO / "scripts" / "personal_data_sources.py"
|
|
spec = importlib.util.spec_from_file_location("personal_data_sources", path)
|
|
assert spec is not None
|
|
assert spec.loader is not None
|
|
module = importlib.util.module_from_spec(spec)
|
|
sys.modules[spec.name] = module
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
|
|
def _album(
|
|
root: Path,
|
|
*,
|
|
triggers: list[str] | None = None,
|
|
custom: dict[str, Any] | None = None,
|
|
corrupt_custom: bool = False,
|
|
) -> Path:
|
|
root.mkdir(parents=True, exist_ok=True)
|
|
people = [{"key": "p1", "triggers": triggers or ["!zenon"]}]
|
|
(root / "people.json").write_text(
|
|
json.dumps({"groups": ["g"], "people": people}), encoding="utf-8"
|
|
)
|
|
if corrupt_custom:
|
|
(root / "custom.json").write_text("{ not json", encoding="utf-8")
|
|
elif custom is not None:
|
|
(root / "custom.json").write_text(json.dumps(custom), encoding="utf-8")
|
|
return root
|
|
|
|
|
|
def _words(sources: ModuleType, root: Path, monkeypatch: pytest.MonkeyPatch) -> set[str]:
|
|
monkeypatch.setenv("PHOTO_PACK_DIR", str(root))
|
|
words, _ = sources.forbidden(set())
|
|
return set(words)
|
|
|
|
|
|
def test_a_people_trigger_is_forbidden(
|
|
sources: ModuleType, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
assert "zenon" in _words(sources, _album(tmp_path / "pack"), monkeypatch)
|
|
|
|
|
|
def test_a_custom_label_is_forbidden_too(
|
|
sources: ModuleType, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
"""A label can be a name -- "ciocia_hania" is one however it got there."""
|
|
root = _album(tmp_path / "pack", custom={"t1": ["ciocia_hania"]})
|
|
assert "ciocia_hania" in _words(sources, root, monkeypatch)
|
|
|
|
|
|
def test_a_label_that_is_a_command_word_is_not_forbidden(
|
|
sources: ModuleType, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
"""The wedge: "!add_photo kot" must not outlaw "kot" in aliases.json."""
|
|
root = _album(tmp_path / "pack", custom={"t1": ["kot"], "t2": ["ciocia_hania"]})
|
|
words = _words(sources, root, monkeypatch)
|
|
assert "kot" not in words
|
|
assert "ciocia_hania" in words
|
|
|
|
|
|
@pytest.mark.parametrize("label", ["losowe", "papiez", "add_photo", "staty"])
|
|
def test_no_command_word_can_be_outlawed_by_a_label(
|
|
sources: ModuleType, tmp_path: Path, monkeypatch: pytest.MonkeyPatch, label: str
|
|
) -> None:
|
|
"""Generalised: every alias in the table, not just the one that bit."""
|
|
root = _album(tmp_path / "pack", custom={"t1": [label]})
|
|
assert label not in _words(sources, root, monkeypatch)
|
|
|
|
|
|
def test_a_people_trigger_is_never_excused_by_the_vocabulary(
|
|
sources: ModuleType, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
"""Only custom labels get the subtraction.
|
|
|
|
A curated real name turning up in this repository is precisely the leak
|
|
the gate exists to fail on, so it stays forbidden even when it collides.
|
|
"""
|
|
root = _album(tmp_path / "pack", triggers=["!kot"])
|
|
assert "kot" in _words(sources, root, monkeypatch)
|
|
|
|
|
|
def test_an_album_with_no_custom_file_still_works(
|
|
sources: ModuleType, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
"""Other sources (the database, .env) contribute too, so this is membership."""
|
|
words = _words(sources, _album(tmp_path / "pack"), monkeypatch)
|
|
assert "zenon" in words
|
|
|
|
|
|
def test_a_corrupt_custom_file_is_ignored_rather_than_fatal(
|
|
sources: ModuleType, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
"""Refusing every commit because an album file got truncated is backwards."""
|
|
root = _album(tmp_path / "pack", corrupt_custom=True)
|
|
assert "zenon" in _words(sources, root, monkeypatch)
|