fix: connect to INWX via IPv4 only to simplify IP allowlists
This commit is contained in:
2 files changed
+55
-1
No files matched your search
@@ -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:
|
||||
|
||||
Reference in new issue
Block a user