Files
dbb9a590e5 fix(engine): utf-8 encoding for all file/console I/O (Windows cp1252 crash) (#191)
Fixes #189. On Windows (and any non-UTF-8 locale) text-mode open() inherits the
code page (cp1252), so writing the engine's '->' / em-dash characters to the log,
arsenal map, or report crashed with UnicodeEncodeError, and reading them back
could mojibake.

- Pin encoding="utf-8" on every text-mode open() in engine/ (state.py, engine.py,
  memory.py, scope.py) and the subprocess-reading tests.
- Reconfigure stdout/stderr to utf-8 in engine main() so console output of the same
  characters does not crash the CLI run.
- Make Engagement.log()'s print non-fatal on a non-UTF-8 console: logging is a side
  channel and must never crash the run; the full line is already in the utf-8 log.
- Parent-side decode of child engine output in the mock subprocess tests pinned to
  utf-8 (the two tests #189 reports failing on Windows).

New engine/test_windows_encoding.py reproduces the crash deterministically on any
host by forcing an ASCII default (PYTHONUTF8=0 PYTHONCOERCECLOCALE=0 LC_ALL=C), so
the regression is caught on the existing ubuntu CI without a windows runner.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-20 21:23:22 +05:30

52 lines
2.4 KiB
Python

import os
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
ENGINE_DIR = Path(__file__).resolve().parent
class MockLedgerTests(unittest.TestCase):
def test_full_mock_run_ignores_engagement_ledger(self):
with tempfile.TemporaryDirectory() as base:
ledger = Path(base) / "ledger"
ledger.mkdir()
findings = ledger / "findings.jsonl"
original = (
'{"host":"localhost","url":"http://localhost:3002/api/account?id=5",'
'"param":"id","vuln_class":"idor","verdict_reason":"ledger sentinel"}\n'
)
findings.write_text(original, encoding="utf-8")
result = subprocess.run(
[sys.executable, str(ENGINE_DIR / "engine.py"), "--mock", "--hunt", "--use-memory",
"--scope", str(ENGINE_DIR / "engagement.example.json"), "--base", base],
env={**os.environ, "BUGHUNTER_MEMORY_DIR": str(ledger)},
capture_output=True, text=True, check=True, encoding="utf-8",
)
self.assertNotIn("skipped via ledger", result.stdout)
self.assertIn("CONFIRMED idor", result.stdout)
self.assertEqual(findings.read_text(encoding="utf-8"), original)
self.assertEqual(list(ledger.iterdir()), [findings])
def test_mock_run_does_not_read_or_write_live_engagement_files(self):
with tempfile.TemporaryDirectory() as base:
live = Path(base) / "demo-local"
live.mkdir()
state = live / "state.json"
report = live / "report.md"
state.write_text('{"phase":"recon","marker":"live"}', encoding="utf-8")
report.write_text("live report", encoding="utf-8")
command = [sys.executable, str(ENGINE_DIR / "engine.py"), "--mock", "--hunt",
"--scope", str(ENGINE_DIR / "engagement.example.json"), "--base", base]
subprocess.run(command, check=True, capture_output=True, text=True, encoding="utf-8")
self.assertEqual(state.read_text(encoding="utf-8"), '{"phase":"recon","marker":"live"}')
self.assertEqual(report.read_text(encoding="utf-8"), "live report")
self.assertFalse((live / "engine.log").exists())
mock = Path(base) / ".mock" / "demo-local"
self.assertIn("CONFIRMED", (mock / "engine.log").read_text(encoding="utf-8"))
self.assertTrue((mock / "report.md").is_file())