From bd73021ac4eded95b56cd456d15a245e348cf751 Mon Sep 17 00:00:00 2001 From: TakuyaYagam1 Date: Sun, 29 Nov 2026 16:17:00 +0300 Subject: [PATCH] fix(towercore): block reads during pending handover - stop the active prepare-then-export attack sequence - preserve reads after handover commit or abort --- towercore/guard.py | 23 ++++++++++++++++++++++- towercore/test_guard.py | 23 +++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/towercore/guard.py b/towercore/guard.py index 1fa5a99..8a9127c 100755 --- a/towercore/guard.py +++ b/towercore/guard.py @@ -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() diff --git a/towercore/test_guard.py b/towercore/test_guard.py index 81d4d81..46e4a71 100644 --- a/towercore/test_guard.py +++ b/towercore/test_guard.py @@ -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()