transit: tolerate non-ascii bad handshake

I think somebody was port-scanning the server (or pointed some
non-wormhole client at it), and caused some exceptions in the logs.
These are still bad handshakes, but should be logged normally instead of
throwing exceptions.
This commit is contained in:
Brian Warner 2016-08-22 23:13:04 -07:00
parent f246fdfa7d
commit 0004315431
2 changed files with 21 additions and 1 deletions

View File

@ -60,7 +60,7 @@ class TransitConnection(protocol.Protocol):
self._token_buffer += data
buf = self._token_buffer
wanted = len("please relay \n")+32*2
if len(buf) < wanted-1 and "\n" in buf:
if len(buf) < wanted-1 and b"\n" in buf:
self.transport.write(b"bad handshake\n")
log.msg("transit handshake early failure")
return self.disconnect()

View File

@ -1159,6 +1159,26 @@ class Transit(ServerBase, unittest.TestCase):
a1.transport.loseConnection()
@defer.inlineCallbacks
def test_binary_handshake(self):
ep = clientFromString(reactor, self.transit)
a1 = yield connectProtocol(ep, Accumulator())
binary_bad_handshake = b"\x00\x01\xe0\x0f\n\xff"
# the embedded \n makes the server trigger early, before the full
# expected handshake length has arrived. A non-wormhole client
# writing non-ascii junk to the transit port used to trigger a
# UnicodeDecodeError when it tried to coerce the incoming handshake
# to unicode, due to the ("\n" in buf) check. This was fixed to use
# (b"\n" in buf). This exercises the old failure.
a1.transport.write(binary_bad_handshake)
exp = b"bad handshake\n"
yield a1.waitForBytes(len(exp))
self.assertEqual(a1.data, exp)
a1.transport.loseConnection()
@defer.inlineCallbacks
def test_impatience(self):
ep = clientFromString(reactor, self.transit)