Skip to content

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

Python

fetch・watch: プロキシを通す http を止めない・プロキシ越しでは rebinding を防げないと説明に書く(#13 のレビュー)

MaryCachecc84872290e5
diff --git a/README.en.md b/README.en.md
index d88ec4d..5540f6b 100644
--- a/README.en.md
+++ b/README.en.md
@@ -241,7 +241,7 @@ The record is assembled by machine from every hit of every query and from the fe
- The searchers search the web, so the content of your question leaves your machine as search terms. Don't use the skill for research involving confidential or personal data.
- The skill does not use ways around paywalls, and sources it cannot reach are reported as "not reached".
- Saved sources are copies for reading locally, so please don't redistribute them.
-- The fetching scripts only fetch http(s) URLs, and never fetch local or internal addresses (127.0.0.1, 192.168.x.x and so on), so that a crafted URL in search results cannot read services running on your machine. Some VPN and proxy tools resolve even public site names to internal addresses, and then nothing can be fetched. Only in that case, set the environment variable `IMOZURU_ALLOW_PRIVATE=1` to turn this address check off.
+- The fetching scripts only fetch http(s) URLs, and never fetch local or internal addresses (127.0.0.1, 192.168.x.x and so on), so that a crafted URL in search results cannot read services running on your machine. Some VPN and proxy tools resolve even public site names to internal addresses, and then nothing can be fetched. Only in that case, set the environment variable `IMOZURU_ALLOW_PRIVATE=1` to turn this address check off. When using a proxy, the proxy resolves the hostname, so pinning the connection to a checked IP cannot prevent DNS rebinding.
## License
diff --git a/README.md b/README.md
index c228f7c..301885e 100644
--- a/README.md
+++ b/README.md
@@ -240,7 +240,7 @@ Claudeが切り出すときに言い換えが紛れ込まないよう、`report.
- 検索役はWebを検索するため、問いの内容が検索語として外部に送られます。機密情報や個人データが関わる調べものには使わないでください。
- 有料の論文を回避して入手する手段は使わず、取得できなかったものは「届かなかった」と報告します。
- 保存した原文は手元で読むための写しなので、再配布しないでください。
-- 原文を取るスクリプトは、http(s) 以外のURLと、手元や内側の宛先(127.0.0.1、192.168.x.xなど)には取りに行きません。検索結果に細工したURLを置かれて、手元で動いているサービスの中身を読まれないようにするためです。VPNやプロキシの道具によっては、外のサイトの名前まで内側の宛先に引けることがあり、そのときは何も取れなくなります。その場合だけ、環境変数 `IMOZURU_ALLOW_PRIVATE=1` を設定すると、この宛先の検査を外せます。
+- 原文を取るスクリプトは、http(s) 以外のURLと、手元や内側の宛先(127.0.0.1、192.168.x.xなど)には取りに行きません。検索結果に細工したURLを置かれて、手元で動いているサービスの中身を読まれないようにするためです。VPNやプロキシの道具によっては、外のサイトの名前まで内側の宛先に引けることがあり、そのときは何も取れなくなります。その場合だけ、環境変数 `IMOZURU_ALLOW_PRIVATE=1` を設定すると、この宛先の検査を外せます。プロキシを通すときは名前をプロキシが引くので、検査したIPへつなぐ形ではDNS rebindingを防げません。
## ライセンス
diff --git a/skills/imozuru/scripts/fetch.py b/skills/imozuru/scripts/fetch.py
index be47861..2df5cf6 100755
--- a/skills/imozuru/scripts/fetch.py
+++ b/skills/imozuru/scripts/fetch.py
@@ -112,7 +112,8 @@ def _pinned_connection(host: str, port: int, timeout, source_address):
url_problem で名前を引いてから urllib がつなぐまでの間に、引き先を 127.0.0.1 などへ
変えられると検査をすり抜ける(DNS rebinding)。つなぐ時に引いた結果をもう一度確かめ、
その IP にじかにつなぐので、検査した宛先と実際の宛先がずれない。HTTPS の証明書の確認と
- SNI はホスト名のまま行われる(2026-10-22 手元で追加)。
+ SNI はホスト名のまま行われる。
+ プロキシを通すときは名前をプロキシが引くので、この固定では DNS rebinding を防げない。
"""
try:
addrs = list(resolve(host))
@@ -155,6 +156,8 @@ class _PinnedHTTPSConnection(_PinnedMixin, http.client.HTTPSConnection):
class _PinnedHTTPHandler(urllib.request.HTTPHandler):
def http_open(self, req):
+ if req.has_proxy():
+ return self.do_open(http.client.HTTPConnection, req)
return self.do_open(_PinnedHTTPConnection, req)
@@ -190,6 +193,7 @@ def url_problem(url: str) -> str | None:
宛先のときだけ通す。10進や16進で書いた IP も、名前を引けば 127.0.0.1 などになるので同じく止まる。
名前を引いてから接続するまでの間に引き先を変える手(DNS rebinding)は、_pinned_connection が
つなぐ直前に引き直して確かめた IP へじかにつなぐことで防ぐ。
+ プロキシを通すときは名前をプロキシが引くので、この固定では DNS rebinding を防げない。
ホストの部分に「@」か「%」があれば断る。urlsplit は「[email protected]」の a.example を見るが、
urllib は [email protected] をそのまま引き、「%61」は urllib だけが a に戻す。検査とつなぐ先がずれる。
diff --git a/skills/imozuru/scripts/watch.py b/skills/imozuru/scripts/watch.py
index 16a1185..779ba0b 100644
--- a/skills/imozuru/scripts/watch.py
+++ b/skills/imozuru/scripts/watch.py
@@ -77,6 +77,7 @@ def url_problem(url: str) -> str | None:
宛先のときだけ通す。10進や16進で書いた IP も、名前を引けば 127.0.0.1 などになるので同じく止まる。
名前を引いてから接続するまでの間に引き先を変える手(DNS rebinding)は、_pinned_connection が
つなぐ直前に引き直して確かめた IP へじかにつなぐことで防ぐ。
+ プロキシを通すときは名前をプロキシが引くので、この固定では DNS rebinding を防げない。
ホストの部分に「@」か「%」があれば断る。urlsplit は「[email protected]」の a.example を見るが、
urllib は [email protected] をそのまま引き、「%61」は urllib だけが a に戻す。検査とつなぐ先がずれる。
@@ -154,7 +155,8 @@ def _pinned_connection(host: str, port: int, timeout, source_address):
url_problem で名前を引いてから urllib がつなぐまでの間に、引き先を 127.0.0.1 などへ
変えられると検査をすり抜ける(DNS rebinding)。つなぐ時に引いた結果をもう一度確かめ、
その IP にじかにつなぐので、検査した宛先と実際の宛先がずれない。HTTPS の証明書の確認と
- SNI はホスト名のまま行われる(2026-10-22 手元で追加)。
+ SNI はホスト名のまま行われる。
+ プロキシを通すときは名前をプロキシが引くので、この固定では DNS rebinding を防げない。
"""
try:
addrs = list(resolve(host))
@@ -197,6 +199,8 @@ class _PinnedHTTPSConnection(_PinnedMixin, http.client.HTTPSConnection):
class _PinnedHTTPHandler(urllib.request.HTTPHandler):
def http_open(self, req):
+ if req.has_proxy():
+ return self.do_open(http.client.HTTPConnection, req)
return self.do_open(_PinnedHTTPConnection, req)
diff --git a/tests/test_portability.py b/tests/test_portability.py
index c514a93..87bacfb 100644
--- a/tests/test_portability.py
+++ b/tests/test_portability.py
@@ -30,7 +30,7 @@ def load(name):
fetch = load("fetch")
watch = load("watch")
# テストの宛先(example.test と手元の小さなサーバー)を、インターネット上の宛先に引けたことにする。
-# 引いた結果を確かめるだけで、接続は urllib が別に名前を引いて行うので、手元のサーバーにつながる
+# 接続時もこの resolve で引いた IP を確かめ、下の _dial で手元のサーバーにつなぐ
fetch.resolve = watch.resolve = lambda host: {"93.184.215.14"}
# _pinned_connection は引いた IP へじかにつなぐので、見せかけの IP は手元のサーバーへ回す
import socket as _socket
diff --git a/tests/test_security.py b/tests/test_security.py
index 54d9e6b..af3fe71 100644
--- a/tests/test_security.py
+++ b/tests/test_security.py
@@ -16,6 +16,7 @@ import tempfile
import threading
import time
import unittest
+import urllib.request
from pathlib import Path
from unittest.mock import MagicMock, patch
@@ -74,6 +75,43 @@ class _Server(http.server.BaseHTTPRequestHandler):
pass
+class _Proxy(_Server):
+ def do_GET(self):
+ body = b"via-proxy"
+ self.send_response(200)
+ self.send_header("Content-Length", str(len(body)))
+ self.end_headers()
+ self.wfile.write(body)
+
+
+class HttpProxyTests(unittest.TestCase):
+ def setUp(self):
+ server = http.server.HTTPServer(("127.0.0.1", 0), _Proxy)
+ threading.Thread(target=server.serve_forever, daemon=True).start()
+ self.addCleanup(server.server_close)
+ self.addCleanup(server.shutdown)
+ proxy_url = f"http://127.0.0.1:{server.server_port}"
+ env = patch.dict(os.environ, {"http_proxy": proxy_url, "no_proxy": "", "NO_PROXY": "",
+ "IMOZURU_ALLOW_PRIVATE": ""})
+ env.start()
+ self.addCleanup(env.stop)
+ for module in (fetch, watch):
+ opener = urllib.request.build_opener(
+ urllib.request.ProxyHandler({"http": proxy_url}),
+ module._Redirect308, module._PinnedHTTPHandler, module._PinnedHTTPSHandler)
+ for patcher in (patch.object(module, "_OPENER", opener),
+ patch.object(module, "resolve", lambda host: {PUBLIC_IP} if host == "example.test"
+ else REAL_RESOLVE(host))):
+ patcher.start()
+ self.addCleanup(patcher.stop)
+
+ def test_fetch_download_through_http_proxy(self):
+ self.assertEqual(fetch.download("http://example.test/x")[0], b"via-proxy")
+
+ def test_watch_fetch_through_http_proxy(self):
+ self.assertEqual(watch.fetch("http://example.test/x")[0], b"via-proxy")
+
+
class UnsafeUrlTests(unittest.TestCase):
"""手元や内側の宛先を取りに行かない。転送の先でも同じ。"""
@@ -84,7 +122,7 @@ class UnsafeUrlTests(unittest.TestCase):
self.addCleanup(self.server.shutdown)
self.base = f"http://localhost:{self.server.server_port}"
for module in (fetch, watch):
- dial = patch.object(module, "_dial", dial_public_as_local, create=True)
+ dial = patch.object(module, "_dial", dial_public_as_local)
dial.start()
self.addCleanup(dial.stop)