Fix security, robustness, and config-drift issues

- Close printer handles (contextlib.closing) in CLI and TUI
- Whitelist 4chan image extensions + numeric tim (path traversal)
- Cap downloads at 16 MB
- clean: unlink instead of rmtree, cover legacy dir, add -y flag
- TUI: escape captured output as rich Text; propagate failure exit code
- Pass --ip/--port through to connect_receipt.sh via env
- Fix double-space textln calls
- CI matrix 3.10/3.12/3.13
- pytest pythonpath so tests run without editable install
- Print retry narrowed to OSError/escpos errors

Co-Authored-By: Claude Code <[email protected]>
This commit is contained in:
2026-09-22 22:20:51 +01:00
co-authored by Claude Code
parent 71686e8ad4
commit e333d6729f
7 changed files with 110 additions and 41 deletions
+20 -9
View File
@@ -6,9 +6,11 @@ from __future__ import annotations
import argparse
import contextlib
import io
import os
import subprocess
from pathlib import Path
from rich.text import Text
from textual import work
from textual.app import App, ComposeResult
from textual.containers import Horizontal, Vertical
@@ -39,10 +41,13 @@ ACTIONS = [
]
def setup_network_quiet() -> None:
def setup_network_quiet(args: argparse.Namespace) -> None:
"""Run the connect script with output captured so it can't corrupt the TUI."""
env = os.environ.copy()
env["RECEIPT_PRINTER_IP"] = args.ip
env["RECEIPT_PRINTER_PORT"] = str(args.port)
result = subprocess.run(
["bash", str(receipt.CONNECT_SCRIPT)], capture_output=True, text=True
["bash", str(receipt.CONNECT_SCRIPT)], env=env, capture_output=True, text=True
)
if result.stdout:
print(result.stdout, end="")
@@ -72,13 +77,13 @@ def run_action(command: str, base: argparse.Namespace, **params) -> tuple[int, s
def _dispatch(command: str, args: argparse.Namespace) -> int:
if command == "list":
return receipt.cmd_list()
# No TTY in the worker thread: the button press is the confirmation.
if command == "clean":
return receipt.cmd_clean()
return receipt.cmd_clean(yes=True)
if command == "status":
return receipt.cmd_status(args)
if not args.usb:
setup_network_quiet()
p = receipt.get_printer(args)
setup_network_quiet(args)
handlers = {
"image": receipt.cmd_image,
"note": receipt.cmd_note,
@@ -86,7 +91,8 @@ def _dispatch(command: str, args: argparse.Namespace) -> int:
"qr": receipt.cmd_qr,
"reprint": receipt.cmd_reprint,
}
return handlers[command](p, args)
with contextlib.closing(receipt.get_printer(args)) as p:
return handlers[command](p, args)
class ReceiptApp(App):
@@ -130,6 +136,7 @@ class ReceiptApp(App):
def __init__(self, base_args: argparse.Namespace):
super().__init__()
self.base_args = base_args
self.last_failure = 0 # exit code of the most recent failed action
def compose(self) -> ComposeResult:
yield Header()
@@ -257,15 +264,19 @@ class ReceiptApp(App):
def report(self, command: str, rc: int, output: str) -> None:
if output:
self.log_panel.write(output)
# Captured output is arbitrary text; Text() stops stray [..] parsing as markup.
self.log_panel.write(Text(output))
if rc == 0:
self.log_panel.write("[green]done[/green]")
else:
self.log_panel.write(f"[red]failed (exit {rc})[/red]")
if rc != 0:
self.last_failure = rc
if command in ("image", "clean"):
self.refresh_cache_list()
def run_tui(args: argparse.Namespace) -> int:
ReceiptApp(args).run()
return 0
app = ReceiptApp(args)
app.run()
return app.last_failure