Skip to content

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

Python

fix: yt-dlp と ax に渡す URL の前に「--」を置く

MaryCache8b9989886d59
diff --git a/skills/imozuru/scripts/fetch.py b/skills/imozuru/scripts/fetch.py
index fbd68ed..cd40bf7 100755
--- a/skills/imozuru/scripts/fetch.py
+++ b/skills/imozuru/scripts/fetch.py
@@ -269,7 +269,8 @@ def html_to_md(url: str, out: Path, body: bytes | None = None, ctype: str = "")
ctype は body の Content-Type。文字コードの名乗りを読むのに使う。
"""
if HAS_AX:
- res = subprocess.run(["ax", url, "--md", "--all"], capture_output=True,
+ # URL は「--」の後ろに置く。「-」で始まる文字列をオプションとして読ませない
+ res = subprocess.run(["ax", "--md", "--all", "--", url], capture_output=True,
encoding="utf-8", errors="replace")
text = res.stdout
else:
@@ -294,7 +295,7 @@ def ax_save(url: str, out: Path) -> bool:
ax が無いときは urllib で取り直す。
"""
if HAS_AX:
- subprocess.run(["ax", url, "-o", str(out)], capture_output=True)
+ subprocess.run(["ax", "-o", str(out), "--", url], capture_output=True)
else:
try:
out.write_bytes(download(url)[0])
@@ -335,7 +336,7 @@ def youtube_to_md(url: str, out: Path) -> str:
["yt-dlp", "--no-update", "--skip-download", flag, "--encoding", "utf-8",
"--sub-langs", langs, "--sub-format", "vtt", "--no-simulate",
"--print", "%(title)s\t%(uploader)s\t%(upload_date)s\t%(language|)s",
- "-o", str(tmp / "v.%(ext)s"), url],
+ "-o", str(tmp / "v.%(ext)s"), "--", url],
capture_output=True, encoding="utf-8", errors="replace")
if any(tmp.glob("*.vtt")):
break
diff --git a/tests/test_fetch.py b/tests/test_fetch.py
index a833662..281440b 100644
--- a/tests/test_fetch.py
+++ b/tests/test_fetch.py
@@ -201,6 +201,40 @@ class FetchPreservationTests(unittest.TestCase):
self.assertIn("http(s) の URL だけを取りに行く", self.stdout.getvalue())
+class OptionInjectionTests(unittest.TestCase):
+ """外のコマンドへ渡す URL は「--」の直後に置く。「-」で始まる URL をオプションとして読ませない。"""
+
+ URL = "--exec=touch /tmp/pwned"
+
+ def setUp(self):
+ tmp = tempfile.TemporaryDirectory()
+ self.addCleanup(tmp.cleanup)
+ self.out = Path(tmp.name) / "page.md"
+
+ def capture(self, call):
+ with patch.object(fetch.subprocess, "run",
+ return_value=subprocess.CompletedProcess([], 0, "", "")) as run:
+ call()
+ return [c.args[0] for c in run.call_args_list]
+
+ def assert_url_after_separator(self, cmds):
+ self.assertTrue(cmds)
+ for cmd in cmds:
+ with self.subTest(cmd=cmd):
+ self.assertEqual(cmd[-2:], ["--", self.URL])
+
+ def test_ax_markdown(self):
+ with patch.object(fetch, "HAS_AX", True):
+ self.assert_url_after_separator(self.capture(lambda: fetch.html_to_md(self.URL, self.out)))
+
+ def test_ax_save(self):
+ with patch.object(fetch, "HAS_AX", True):
+ self.assert_url_after_separator(self.capture(lambda: fetch.ax_save(self.URL, self.out)))
+
+ def test_yt_dlp(self):
+ self.assert_url_after_separator(self.capture(lambda: fetch.youtube_to_md(self.URL, self.out)))
+
+
class YoutubeCaptionTests(unittest.TestCase):
"""yt-dlp の呼び出しを差し替えて、どの字幕を指定するかを検査する。"""