mirror of
https://github.com/kuhyx/signal-bot.git
synced 2026-11-10 19:17:02 +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
80 lines
3.1 KiB
Python
80 lines
3.1 KiB
Python
# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
|
|
"""The one place in this repository that starts a subprocess.
|
|
|
|
Deliberately one place. Ruff's ``ALL`` set flags every ``subprocess`` call
|
|
with ``S603`` ("check for execution of untrusted input") and there is no
|
|
code-level answer to it: exiftool is a binary, and the pure-Python EXIF
|
|
libraries handle neither HEIC nor MP4, which is most of what the album
|
|
ingests. So the rule is suppressed exactly once, for this file, in
|
|
``pyproject.toml`` -- a scoped and reviewable entry rather than an inline
|
|
per-line suppression, which this repository's own pre-commit hook blocks
|
|
outright, and which would be free to spread file by file.
|
|
|
|
Everything that needs an external tool imports ``run`` from here. If a second
|
|
file ever calls ``subprocess`` directly, the gate fails and says so, which is
|
|
the property the single entry buys.
|
|
|
|
``run`` never raises and never lets a caller mistake failure for success:
|
|
a non-zero exit, a missing binary and a timeout all come back as
|
|
``ok=False``, because the one thing this must not do is look like it worked.
|
|
An exit code that lied is what put ffmpeg in this codebase's history.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import subprocess
|
|
from dataclasses import dataclass
|
|
from typing import TYPE_CHECKING, Final
|
|
|
|
if TYPE_CHECKING:
|
|
from collections.abc import Sequence
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Long enough for exiftool to rewrite a phone video's container, short enough
|
|
# that a wedged binary cannot hold a command handler open indefinitely.
|
|
DEFAULT_TIMEOUT: Final = 60.0
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class Result:
|
|
"""What one external command did, with failure spelled out rather than raised."""
|
|
|
|
ok: bool
|
|
stdout: str
|
|
stderr: str
|
|
|
|
|
|
def run(command: Sequence[str], *, timeout: float = DEFAULT_TIMEOUT) -> Result:
|
|
"""Run one external command and report what happened.
|
|
|
|
Args:
|
|
command: The argv, already split. Never a shell string: nothing here
|
|
interpolates into a shell, and every element is built by the
|
|
caller rather than taken from a message.
|
|
timeout: Seconds before the command is killed and reported failed.
|
|
|
|
Returns:
|
|
The command's output, and whether it actually succeeded.
|
|
"""
|
|
try:
|
|
completed = subprocess.run(
|
|
list(command),
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=timeout,
|
|
check=False,
|
|
)
|
|
except (OSError, subprocess.SubprocessError) as error:
|
|
# A missing binary, a permissions problem, or a timeout. The command
|
|
# is logged, never its output: exiftool prints tag values on failure.
|
|
logger.warning("could not run %s: %s", command[0], error)
|
|
return Result(ok=False, stdout="", stderr=str(error))
|
|
if completed.returncode != 0:
|
|
logger.warning(
|
|
"%s exited %d: %s", command[0], completed.returncode, completed.stderr.strip()
|
|
)
|
|
return Result(ok=False, stdout=completed.stdout, stderr=completed.stderr)
|
|
return Result(ok=True, stdout=completed.stdout, stderr=completed.stderr)
|