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:
TakuyaYagam1 committed 2026-11-29 16:17:00 +03:00
1 parent a9d6b64637
commit bd73021ac4
2 files changed
+45 -1

No files matched your search

+22 -1
View File
@@ -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()