MCPcopy Create free account
hub / github.com/ElementsProject/lightning / test_connect_proxy_maxlen_hostname

Function test_connect_proxy_maxlen_hostname

tests/test_connection.py:5107–5185  ·  view source on GitHub ↗

A maximum-length hostname must produce a well-formed SOCKS5 request. The request is assembled in a fixed buffer, and the hostname was copied into it without checking that it fit, which overran the buffer and corrupted the adjacent length field. That length was then used for the wri

(node_factory)

Source from the content-addressed store, hash-verified

5105
5106
5107def test_connect_proxy_maxlen_hostname(node_factory):
5108 """A maximum-length hostname must produce a well-formed SOCKS5 request.
5109
5110 The request is assembled in a fixed buffer, and the hostname was copied
5111 into it without checking that it fit, which overran the buffer and
5112 corrupted the adjacent length field. That length was then used for the
5113 write, so the proxy got a wildly oversized read of connectd's memory
5114 instead of the request (and connectd died on the way).
5115
5116 A gossiped DNS address reaches the same builder, so this covers that
5117 path too.
5118 """
5119 # A minimal SOCKS5 server: accept the "no authentication" greeting,
5120 # then collect whatever request connectd sends us.
5121 received = []
5122 listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
5123 listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
5124 listener.bind(('127.0.0.1', 0))
5125 listener.listen(1)
5126 proxyport = listener.getsockname()[1]
5127
5128 def serve():
5129 data = b''
5130 conn, _ = listener.accept()
5131 with conn:
5132 try:
5133 conn.settimeout(TIMEOUT)
5134 conn.recv(len(b'\x05\x01\x00'))
5135 conn.sendall(b'\x05\x00')
5136 # Deliberately ask for far more than the largest legal
5137 # request (262 bytes), so an oversized write is visible
5138 # here rather than silently truncated by us. We stop on the
5139 # timeout, once connectd has finished writing and is waiting
5140 # for a reply we're never going to send.
5141 conn.settimeout(2)
5142 while len(data) < 65536:
5143 more = conn.recv(65536)
5144 if not more:
5145 break
5146 data += more
5147 except OSError:
5148 pass
5149 received.append(data)
5150
5151 server = threading.Thread(target=serve, daemon=True)
5152 server.start()
5153
5154 l1 = node_factory.get_node(options={'proxy': '127.0.0.1:{}'.format(proxyport),
5155 'always-use-proxy': 'true'},
5156 # Without the fix connectd dies here, and we
5157 # want that to be a test failure rather than
5158 # a teardown error.
5159 may_fail=True, broken_log='.*')
5160
5161 # unresolved.name[256] is what limits us: 255 is the longest we can ask for.
5162 hostname = 'a' * (255 - len('.example.com')) + '.example.com'
5163 assert len(hostname) == 255
5164

Callers

nothing calls this directly

Calls 10

only_oneFunction · 0.90
joinMethod · 0.80
is_in_logMethod · 0.80
startMethod · 0.45
get_nodeMethod · 0.45
connectMethod · 0.45
closeMethod · 0.45
getinfoMethod · 0.45
encodeMethod · 0.45
to_bytesMethod · 0.45

Tested by

no test coverage detected