fix(towercore): block reads during pending handover
- stop the active prepare-then-export attack sequence - preserve reads after handover commit or abort
This commit is contained in:
1 parent
a9d6b64637
commit
bd73021ac4
2 files changed
+45
-1
No files matched your search
+22
-1
@@ -7,6 +7,8 @@ import os
|
||||
HEADER_SIZE = 16
|
||||
READ_OPCODES = frozenset((0x21, 0x23))
|
||||
SUBSCRIBER_TAG = 0x0B
|
||||
HANDOVER_PREPARE = 0x40
|
||||
HANDOVER_FINISH = frozenset((0x41, 0x42))
|
||||
BACKEND_HOST = os.environ.get("BACKEND_HOST", "towercore")
|
||||
BACKEND_PORT = int(os.environ.get("BACKEND_PORT", "7250"))
|
||||
LISTEN_PORT = int(os.environ.get("LISTEN_PORT", "7250"))
|
||||
@@ -38,13 +40,32 @@ def should_block(frame: bytes) -> bool:
|
||||
return frame[3] in READ_OPCODES and tlv_tags(frame).count(SUBSCRIBER_TAG) > 1
|
||||
|
||||
|
||||
|
||||
class ConnectionGuard:
|
||||
def __init__(self) -> None:
|
||||
self.handover_pending = False
|
||||
|
||||
def should_block(self, frame: bytes) -> bool:
|
||||
opcode = frame[3]
|
||||
tags = tlv_tags(frame)
|
||||
if opcode in READ_OPCODES and tags.count(SUBSCRIBER_TAG) > 1:
|
||||
return True
|
||||
if opcode == HANDOVER_PREPARE and SUBSCRIBER_TAG in tags:
|
||||
self.handover_pending = True
|
||||
return False
|
||||
if opcode in HANDOVER_FINISH:
|
||||
self.handover_pending = False
|
||||
return False
|
||||
return self.handover_pending and opcode in READ_OPCODES and SUBSCRIBER_TAG in tags
|
||||
|
||||
async def client_frames(reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None:
|
||||
guard = ConnectionGuard()
|
||||
while True:
|
||||
header = await reader.readexactly(HEADER_SIZE)
|
||||
payload_size = int.from_bytes(header[14:16], "little")
|
||||
payload = await reader.readexactly(payload_size)
|
||||
frame = header + payload
|
||||
if should_block(frame):
|
||||
if guard.should_block(frame):
|
||||
return
|
||||
writer.write(frame)
|
||||
await writer.drain()
|
||||
|
||||
@@ -54,6 +54,29 @@ class GuardTests(unittest.TestCase):
|
||||
request = frame(0x40, [(0x0B, bytes.fromhex("87481bd9") + b"\0" * 4)])
|
||||
self.assertFalse(module.should_block(request))
|
||||
|
||||
def test_blocks_inventory_during_uncommitted_handover(self) -> None:
|
||||
module = importlib.import_module("guard")
|
||||
guard = module.ConnectionGuard()
|
||||
selector = bytes.fromhex("87481bd9") + b"\0" * 4
|
||||
self.assertFalse(guard.should_block(frame(0x40, [(0x0B, selector), (0x0C, b"\xe9\x03\0\0")])))
|
||||
self.assertTrue(guard.should_block(frame(0x23, [(0x0B, selector)])))
|
||||
|
||||
def test_allows_inventory_after_handover_abort(self) -> None:
|
||||
module = importlib.import_module("guard")
|
||||
guard = module.ConnectionGuard()
|
||||
selector = bytes.fromhex("87481bd9") + b"\0" * 4
|
||||
self.assertFalse(guard.should_block(frame(0x40, [(0x0B, selector), (0x0C, b"\xe9\x03\0\0")])))
|
||||
self.assertFalse(guard.should_block(frame(0x42, [(0x15, b"\x01\0\0\0")])))
|
||||
self.assertFalse(guard.should_block(frame(0x23, [(0x0B, selector)])))
|
||||
|
||||
def test_allows_inventory_after_handover_commit(self) -> None:
|
||||
module = importlib.import_module("guard")
|
||||
guard = module.ConnectionGuard()
|
||||
selector = bytes.fromhex("87481bd9") + b"\0" * 4
|
||||
self.assertFalse(guard.should_block(frame(0x40, [(0x0B, selector), (0x0C, b"\xea\x03\0\0")])))
|
||||
self.assertFalse(guard.should_block(frame(0x41, [(0x15, b"\x01\0\0\0")])))
|
||||
self.assertFalse(guard.should_block(frame(0x23, [(0x0B, selector)])))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user