diff --git a/snap_dyndns.py b/snap_dyndns.py index ecab47c..3fad631 100644 --- a/snap_dyndns.py +++ b/snap_dyndns.py @@ -13,10 +13,12 @@ import fcntl import hashlib import hmac import http +import http.client import http.cookiejar import ipaddress import json import os +import ssl import struct import sys import time @@ -50,13 +52,30 @@ def totp(secret, now=None): return f"{code:06d}" +class _IPv4Https(urllib.request.HTTPSHandler): + """HTTPS that only connects via IPv4. + + The INWX API account is restricted to the server's IPv4 address, but a + confined CGI process may prefer IPv6. Binding to 0.0.0.0 makes + socket.create_connection() skip every IPv6 address. + """ + + def https_open(self, req): + return self.do_open( + http.client.HTTPSConnection, + req, + context=ssl.create_default_context(), + source_address=("0.0.0.0", 0), + ) + + @contextlib.contextmanager def inwx_session(cfg): """Yield call(method, params) -> resData for a logged-in INWX session.""" inwx = cfg["inwx"] url = inwx.get("api", API_LIVE) opener = urllib.request.build_opener( - urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar()) + _IPv4Https, urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar()) ) def call(method, params): diff --git a/test_snap_dyndns.py b/test_snap_dyndns.py index 8e9dfac..7f15ba6 100644 --- a/test_snap_dyndns.py +++ b/test_snap_dyndns.py @@ -8,12 +8,15 @@ INWX_OTE_HOST (an existing A record there) are set in the environment. import base64 import hashlib +import http.client import io import json import os +import socket import tempfile import threading import unittest +from unittest import mock from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer import snap_dyndns as sd @@ -125,6 +128,24 @@ class HandleTest(unittest.TestCase): self.assertEqual(len(self.calls), 2) +class Ipv4BindTest(unittest.TestCase): + """Checks the premise of _IPv4Https: binding to 0.0.0.0 skips IPv6 addresses.""" + + def test_source_address_skips_ipv6(self): + try: + server = socket.create_server(("::1", 0), family=socket.AF_INET6) + except OSError: + self.skipTest("no IPv6 loopback") + self.addCleanup(server.close) + port = server.getsockname()[1] + try: + socket.create_connection(("localhost", port), timeout=2).close() + except OSError: + self.skipTest("localhost does not resolve to ::1") + with self.assertRaises(OSError): + socket.create_connection(("localhost", port), timeout=2, source_address=("0.0.0.0", 0)) + + class FakeInwx(ThreadingHTTPServer): """Minimal INWX JSON-RPC stand-in with cookie session and optional 2FA.""" @@ -228,6 +249,20 @@ class InwxClientTest(unittest.TestCase): sd.inwx_update(cfg, "example.com", HOST, "A", "1.2.3.4") self.assertNotIn("wrong-password", str(ctx.exception)) + def test_https_connection_is_ipv4_only(self): + seen = {} + + class FakeConnection(http.client.HTTPSConnection): + def __init__(self, *args, **kwargs): + seen.update(kwargs) + raise OSError("stop here") + + cfg = {"inwx": {"user": "u", "pass": "p", "api": "https://inwx.invalid/"}} + with mock.patch("http.client.HTTPSConnection", FakeConnection): + with self.assertRaises(OSError): + sd.inwx_update(cfg, "example.com", HOST, "A", "1.2.3.4") + self.assertEqual(seen["source_address"], ("0.0.0.0", 0)) + def test_end_to_end_only_changes_reach_inwx(self): srv = self.start() with tempfile.TemporaryDirectory() as tmp: