Skip to content

知らない分野を、原文にたどりつくまで芋づる式に掘る Claude Code のスキル

Python

fix: validate source names and extract file boundaries

nsh1lf2d244efacfb
diff --git a/skills/imozuru/scripts/fetch.py b/skills/imozuru/scripts/fetch.py
index 764852d..5327f54 100755
--- a/skills/imozuru/scripts/fetch.py
+++ b/skills/imozuru/scripts/fetch.py
@@ -354,8 +354,21 @@ def save_verified(saved: Path, kept: Path, destination: Path) -> Path:
return target
+def valid_source_name(name: str) -> bool:
+ """保存する原文の名前は、パスではなく単一のファイル名に限る。"""
+ device = name.split(".", 1)[0].rstrip(" ").upper()
+ return (bool(name.strip(" .")) and name == name.rstrip(" .")
+ and not any(c in name for c in "/\\:")
+ and not any(ord(c) < 32 or 127 <= ord(c) <= 159 for c in name)
+ and device not in {"CON", "PRN", "AUX", "NUL", "CONIN$", "CONOUT$"}
+ and not re.fullmatch(r"(?:COM|LPT)[1-9¹²³]", device))
+
+
def fetch_source(sources: Path, name: str, url: str, destination: Path) -> int:
"""sources は今回の取得専用の一時置き場。取得・変換・検査中は保存済みの原文に触れない。"""
+ if not valid_source_name(name):
+ print(f"FAIL\t{name!r}\t原文の名前は単一のファイル名にする")
+ return 1
if X_POST.match(url):
print(f"FAIL\t{name}\tX はログインの壁で本文が取れない(URL を map.md に残す)")
@@ -455,6 +468,9 @@ def main() -> int:
print(__doc__, file=sys.stderr)
return 2
sources, name, url = Path(sys.argv[1]), sys.argv[2], sys.argv[3]
+ if not valid_source_name(name):
+ print(f"FAIL\t{name!r}\t原文の名前は単一のファイル名にする")
+ return 1
sources.mkdir(parents=True, exist_ok=True)
# 変換や FAIL の後始末は一時置き場だけで行う。同名の原文を検査前に上書きしない。
# 保存先の中に作るので、成功時の replace はファイルシステムをまたがない。
diff --git a/skills/imozuru/scripts/report.py b/skills/imozuru/scripts/report.py
index 4eaffae..493b759 100644
--- a/skills/imozuru/scripts/report.py
+++ b/skills/imozuru/scripts/report.py
@@ -25,7 +25,7 @@ import html
import json
import re
import sys
-from pathlib import Path
+from pathlib import Path, PureWindowsPath
# 前後に見せる行数。抜き書きがどんな流れの中にあったかを、ページの中で開いて確かめられるように。
CONTEXT_LINES = 6
@@ -39,8 +39,18 @@ def norm(s: str) -> str:
def verify(src_dir: Path, q: dict) -> tuple[bool, list[str], int, int]:
"""抜き書きが原文の指定の行(前後2行の余裕つき)にそのまま在るか。前後の文脈も返す。"""
- path = src_dir / q["file"]
- if not path.exists():
+ relative = Path(q["file"])
+ if relative.is_absolute() or PureWindowsPath(q["file"]).is_absolute() or ".." in relative.parts:
+ return False, [], 0, 0
+ try:
+ root = src_dir.resolve()
+ topic = src_dir.parent.resolve()
+ path = (src_dir / relative).resolve()
+ except (OSError, RuntimeError, ValueError):
+ return False, [], 0, 0
+ if root == topic or not root.is_relative_to(topic):
+ return False, [], 0, 0
+ if not path.is_relative_to(root) or not path.is_file():
return False, [], 0, 0
# splitlines() はページ区切り(\f)も改行に数え、grep・sed と行番号がずれる。改行だけで数える
lines = path.read_text(encoding="utf-8", errors="replace").split("\n")
@@ -50,8 +60,9 @@ def verify(src_dir: Path, q: dict) -> tuple[bool, list[str], int, int]:
# 2段組の PDF は左右の段が1行に混ざり、何行かにまたがる文はそのままでは照合できない
# (Yoshida 2008 で起きた)。抜き書きを「…」で区切れば、区切りごとに順に照らす。
# 区切りはページにも「…」として残るので、省いたことが読む人に見える。
- ok, pos = True, 0
- for piece in [norm(p) for p in q["text"].split("…") if p.strip()]:
+ pieces = [norm(p) for p in q["text"].split("…") if norm(p)]
+ ok, pos = bool(pieces), 0
+ for piece in pieces:
at = region.find(piece, pos)
if at < 0:
ok = False
diff --git a/tests/test_file_boundaries.py b/tests/test_file_boundaries.py
new file mode 100644
index 0000000..85c849a
--- /dev/null
+++ b/tests/test_file_boundaries.py
@@ -0,0 +1,109 @@
+"""取得名と抜き書きの参照先が sources/ の境界を守ることを検査する。"""
+
+import contextlib
+import importlib.util
+import io
+import json
+import sys
+import tempfile
+import unittest
+from pathlib import Path
+from unittest.mock import patch
+
+
+SCRIPTS = Path(__file__).resolve().parents[1] / "skills/imozuru/scripts"
+
+
+def load(name):
+ spec = importlib.util.spec_from_file_location(f"boundary_{name}", SCRIPTS / f"{name}.py")
+ module = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(module)
+ return module
+
+
+fetch = load("fetch")
+report = load("report")
+
+
+class FileBoundaryTests(unittest.TestCase):
+ def setUp(self):
+ tmp = tempfile.TemporaryDirectory()
+ self.addCleanup(tmp.cleanup)
+ self.topic = Path(tmp.name) / "topic"
+ self.sources = self.topic / "sources"
+ self.sources.mkdir(parents=True)
+
+ def quote(self, file, text="Original sentence"):
+ return {"file": file, "line": 1, "text": text}
+
+ def test_fetch_rejects_unsafe_names_before_side_effects(self):
+ destination = self.topic / "saved"
+ destination.mkdir()
+ original = destination / "outside.md"
+ original.write_text("Keep this source", encoding="utf-8")
+ for name in ("", ".", "..", "...", " ", "../outside", "a/b", "a\\b",
+ "/outside", "C:\\outside", "C:outside", "CON", "a\x00b", "a\nb"):
+ with self.subTest(name=name), patch.object(fetch, "download") as download:
+ with contextlib.redirect_stdout(io.StringIO()) as output:
+ self.assertEqual(fetch.fetch_source(self.sources, name, "https://example.test/a", destination), 1)
+ self.assertIn("FAIL\t", output.getvalue())
+ download.assert_not_called()
+ self.assertEqual(original.read_text(encoding="utf-8"), "Keep this source")
+ self.assertEqual(list(self.sources.iterdir()), [])
+
+ def test_fetch_accepts_author_year_unicode_name(self):
+ name = "髙橋-2024-色彩の研究"
+ body = b"Verified article text. " * 80
+ with patch.object(fetch, "download", return_value=(body, "text/plain")):
+ with contextlib.redirect_stdout(io.StringIO()):
+ self.assertEqual(fetch.fetch_source(self.sources, name, "https://example.test/a", self.sources), 0)
+ self.assertEqual((self.sources / f"{name}.md").read_bytes(), body)
+
+ def test_report_rejects_outside_paths(self):
+ outside = self.topic / "outside.md"
+ outside.write_text("Original sentence", encoding="utf-8")
+ for file in ("../outside.md", str(outside), "C:\\outside.md", "\x00"):
+ with self.subTest(file=file):
+ self.assertEqual(report.verify(self.sources, self.quote(file)), (False, [], 0, 0))
+
+ def test_report_rejects_symlink_escapes(self):
+ outside = self.topic / "outside.md"
+ outside.write_text("Original sentence", encoding="utf-8")
+ external = self.topic.parent / "external"
+ external.mkdir()
+ (external / "paper.md").write_text("Original sentence", encoding="utf-8")
+ try:
+ (self.sources / "link.md").symlink_to(outside)
+ (self.topic / "linked-sources").symlink_to(external, target_is_directory=True)
+ except (OSError, NotImplementedError) as exc:
+ self.skipTest(f"Symlinks unavailable: {exc}")
+ self.assertEqual(report.verify(self.sources, self.quote("link.md")), (False, [], 0, 0))
+ self.assertEqual(report.verify(self.topic / "linked-sources", self.quote("paper.md")),
+ (False, [], 0, 0))
+
+ def test_report_rejects_empty_extracts_and_accepts_nested_source(self):
+ nested = self.sources / "archive"
+ nested.mkdir()
+ (nested / "paper.md").write_text("Original sentence", encoding="utf-8")
+ for text in ("", " \t\n ", "…", " … … "):
+ with self.subTest(text=text):
+ self.assertEqual(report.verify(self.sources, self.quote("archive/paper.md", text))[0], False)
+ ok, context, _, _ = report.verify(self.sources, self.quote("archive/paper.md"))
+ self.assertTrue(ok)
+ self.assertIn("Original sentence", context)
+
+ def test_failed_report_keeps_prior_content(self):
+ (self.sources / "paper.md").write_text("Original sentence", encoding="utf-8")
+ data = {"title": "Test", "question": "Q", "stopped": "Done", "claims": [
+ {"claim": "Claim", "status": "付いた", "quotes": [self.quote("paper.md", "…")]}]}
+ (self.topic / "extracts.json").write_text(json.dumps(data), encoding="utf-8")
+ prior = self.topic / "report.html"
+ prior.write_text("Previous report", encoding="utf-8")
+ with patch.object(sys, "argv", ["report.py", str(self.topic)]), \
+ contextlib.redirect_stdout(io.StringIO()):
+ self.assertEqual(report.main(), 1)
+ self.assertEqual(prior.read_text(encoding="utf-8"), "Previous report")
+
+
+if __name__ == "__main__":
+ unittest.main()