1import importlib.util
2from pathlib import Path
3import socket
4import threading
5import time
6import unittest
7
8
9spec = importlib.util.spec_from_file_location(
10 "upnp_discover", Path(__file__).resolve().parents[1] / "nixos/upnp-discover.py"
11)
12upnp = importlib.util.module_from_spec(spec)
13spec.loader.exec_module(upnp)
14GATEWAY = "127.0.0.1"
15LOCATION = "http://127.0.0.1:1900/random-after-reboot/rootDesc.xml"
16RESPONSE = (
17 f"HTTP/1.1 200 OK\r\nST: upnp:rootdevice\r\nLOCATION: {LOCATION}\r\n\r\n"
18).encode("ascii")
19
20
21class DiscoveryTests(unittest.TestCase):
22 def test_gateway_extension_headers(self):
23 packet = (
24 "HTTP/1.1 200 OK\r\nCACHE-CONTROL: max-age=120\r\nST: upnp:rootdevice\r\n"
25 "USN: uuid:00000000-0000-0000-0000-000000000001::upnp:rootdevice\r\nEXT:\r\n"
26 "SERVER: TP-LINK/TP-LINK UPnP/1.1 MiniUPnPd/1.8\r\n"
27 f"LOCATION: {LOCATION}\r\n"
28 'OPT: "http://schemas.upnp.org/upnp/1/0/"; ns=01\r\n01-NLS: 1\r\n'
29 "BOOTID.UPNP.ORG: 1\r\nCONFIGID.UPNP.ORG: 1337\r\n\r\n"
30 ).encode("ascii")
31 self.assertEqual(upnp.description_location(packet, (GATEWAY, 1900), GATEWAY), LOCATION)
32
33 def test_response_validation(self):
34 self.assertEqual(upnp.description_location(RESPONSE, (GATEWAY, 1900), GATEWAY), LOCATION)
35 alternate_port = RESPONSE.replace(b":1900/random", b":8080/random")
36 self.assertEqual(
37 upnp.description_location(alternate_port, (GATEWAY, 1900), GATEWAY),
38 LOCATION.replace(":1900/", ":8080/"),
39 )
40 invalid = [
41 RESPONSE.replace(b"127.0.0.1:1900", b"example.net:1900"),
42 RESPONSE.replace(b"127.0.0.1:1900", b"127.0.0.2:1900"),
43 RESPONSE.replace(b":1900/", b":0/"),
44 RESPONSE.replace(b":1900/", b":65536/"),
45 RESPONSE.replace(b":1900/", b":not-a-port/"),
46 RESPONSE.replace(b"http://", b"https://"),
47 RESPONSE.replace(b"http://", b"http://user@"),
48 RESPONSE.replace(b"rootDesc.xml", b"rootDesc.xml#fragment"),
49 RESPONSE.replace(b"rootDesc.xml", b"root\tDesc.xml"),
50 RESPONSE.replace(b"200 OK", b"404 Not Found"),
51 RESPONSE.replace(b"upnp:rootdevice", b"ssdp:all"),
52 RESPONSE.replace(b"LOCATION:", b"MISSING:"),
53 RESPONSE.replace(b"LOCATION:", b"BAD NAME:"),
54 RESPONSE.replace(b"LOCATION:", b"BAD/NAME:"),
55 RESPONSE.replace(b"\r\n\r\n", b"\r\nLOCATION: http://evil/\r\n\r\n"),
56 RESPONSE.replace(b"\r\n\r\n", b"\r\n folded\r\n\r\n"),
57 RESPONSE.replace(b"\r\n\r\n", b"\r\n"),
58 b"<html>404</html>",
59 b"\xff" + RESPONSE,
60 RESPONSE + b"x" * 8192,
61 ]
62 for packet in invalid:
63 with self.subTest(packet=packet[:100]):
64 self.assertIsNone(upnp.description_location(packet, (GATEWAY, 1900), GATEWAY))
65 self.assertIsNone(upnp.description_location(RESPONSE, ("127.0.0.2", 1900), GATEWAY))
66 self.assertIsNone(upnp.description_location(RESPONSE, (GATEWAY, 1901), GATEWAY))
67
68 def test_real_udp_ignores_invalid_then_accepts_gateway(self):
69 errors = []
70 with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as gateway:
71 gateway.bind((GATEWAY, 1900))
72 gateway.settimeout(1)
73
74 def respond():
75 try:
76 request, sender = gateway.recvfrom(8192)
77 self.assertIn(b"M-SEARCH * HTTP/1.1\r\n", request)
78 self.assertIn(b"ST: upnp:rootdevice\r\n\r\n", request)
79 with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as rogue:
80 rogue.sendto(RESPONSE, sender)
81 gateway.sendto(RESPONSE.replace(b"127.0.0.1:1900", b"evil:1900"), sender)
82 gateway.sendto(b"HTTP/1.1 200 OK\r\n\r\n", sender)
83 gateway.sendto(RESPONSE, sender)
84 except BaseException as error:
85 errors.append(error)
86
87 thread = threading.Thread(target=respond)
88 thread.start()
89 try:
90 self.assertEqual(upnp.discover(GATEWAY, timeout=0.5), LOCATION)
91 finally:
92 thread.join(timeout=2)
93 self.assertFalse(thread.is_alive())
94 self.assertEqual(errors, [])
95
96 def test_timeout_is_bounded(self):
97 with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as gateway:
98 gateway.bind((GATEWAY, 1900))
99 started = time.monotonic()
100 with self.assertRaises(TimeoutError):
101 upnp.discover(GATEWAY, timeout=0.05)
102 self.assertLess(time.monotonic() - started, 0.5)
103
104 def test_invalid_response_flood_is_bounded(self):
105 with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as gateway:
106 gateway.bind((GATEWAY, 1900))
107 gateway.settimeout(1)
108
109 def flood():
110 _, sender = gateway.recvfrom(8192)
111 for _ in range(128):
112 gateway.sendto(b"invalid", sender)
113
114 thread = threading.Thread(target=flood)
115 thread.start()
116 started = time.monotonic()
117 try:
118 with self.assertRaises(TimeoutError):
119 upnp.discover(GATEWAY, timeout=0.5)
120 finally:
121 thread.join(timeout=2)
122 self.assertFalse(thread.is_alive())
123 self.assertLess(time.monotonic() - started, 1)
124
125
126if __name__ == "__main__":
127 unittest.main()