ckb/tests/unit/test_exporters.py
Michał Kopeć cd7ae4dd44 Template 1.28.2: backport 2.0.1 fixes to the 1.x line
Python 3.8 compatibility, routines.py --mark YAML spacing and comments,
OKF --out and Starlight image paths confined to their folders, script
counts in AGENTS.md/READMEs/manuals, and the tests/unit/ suite.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 14:27:15 +01:00

69 lines
3.2 KiB
Python

"""The exporters delete and rewrite their output folder; they must never
reach outside it, whatever the page text or the --out flag says."""
import shutil
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
FIX = ROOT / "tests/fixtures/minimal-kb"
OKF = ROOT / ".agents/skills/ckb-export-okf/scripts/export_okf.py"
STARLIGHT = ROOT / ".agents/skills/ckb-export-starlight/scripts/export_starlight.py"
def run(cwd, script, *args):
return subprocess.run([sys.executable, str(script), *args], cwd=str(cwd),
capture_output=True, text=True, encoding="utf-8", errors="replace")
class ExportContainment(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.outer = Path(self.tmp.name)
self.kb = self.outer / "kb"
shutil.copytree(str(FIX), str(self.kb))
def tearDown(self):
self.tmp.cleanup()
def test_okf_refuses_out_outside_outputs(self):
for bad in (".", "wiki", "outputs", "..", str(self.outer)):
r = run(self.kb, OKF, "--out", bad)
self.assertEqual(r.returncode, 2, f"--out {bad}: {r.stdout}{r.stderr}")
self.assertTrue((self.kb / "wiki" / "index.md").is_file(), f"--out {bad} deleted the wiki")
self.assertTrue((self.kb / "ckb.yaml").is_file(), f"--out {bad} deleted the KB")
def test_okf_default_and_nested_out_work(self):
self.assertIn(run(self.kb, OKF).returncode, (0, 1))
self.assertTrue((self.kb / "outputs/okf").is_dir())
self.assertIn(run(self.kb, OKF, "--out", "outputs/okf-test").returncode, (0, 1))
self.assertTrue((self.kb / "outputs/okf-test").is_dir())
def test_okf_check_writes_nothing(self):
before = sorted(p.relative_to(self.kb) for p in self.kb.rglob("*"))
self.assertIn(run(self.kb, OKF, "--check").returncode, (0, 1))
self.assertEqual(sorted(p.relative_to(self.kb) for p in self.kb.rglob("*")), before)
def test_starlight_image_traversal_is_not_copied(self):
(self.outer / "secret.txt").write_text("do not export", encoding="utf-8")
assets = self.kb / "wiki" / "assets"
assets.mkdir(parents=True, exist_ok=True)
(assets / "ok.png").write_bytes(b"\x89PNG")
page = self.kb / "wiki" / "entities" / "pic.md"
page.parent.mkdir(parents=True, exist_ok=True)
page.write_text("---\ntype: entity\ntldr: pictures\n---\n\n# Pic\n\n"
"![ok](../assets/ok.png)\n\n![x](../assets/../../../secret.txt)\n"
"![y](../assets/../../../../escaped.txt)\n", encoding="utf-8")
r = run(self.kb, STARLIGHT, "--mode", "content-only", "--today", "2026-11-11")
self.assertEqual(r.returncode, 0, r.stdout + r.stderr)
out = self.kb / "outputs" / "starlight"
self.assertTrue((out / "public/assets/ok.png").is_file(), "a normal asset should still be copied")
leaked = [p for p in self.outer.rglob("secret.txt") if p != self.outer / "secret.txt"]
self.assertEqual(leaked, [], f"traversal copied the file to {leaked}")
self.assertIn("not copied", r.stderr)
if __name__ == "__main__":
unittest.main()