fix(towercore): return protocol errors for denied reads

- replace malformed empty read responses with native not-found frames
This commit is contained in:
TakuyaYagam1 committed 2026-11-29 16:44:30 +03:00
1 parent 903d8872c2
commit b16b42b0fa
2 files changed
+10 -9

No files matched your search

+6 -3
View File
@@ -44,8 +44,11 @@ def should_block(frame: bytes) -> bool:
return frame[3] in READ_OPCODES and tlv_tags(frame).count(SUBSCRIBER_TAG) > 1
def empty_response(frame: bytes) -> bytes:
return frame[:14] + b"\0\0"
def denied_response(frame: bytes) -> bytes:
message = b"not found"
payload = (0x22).to_bytes(2, "little") + len(message).to_bytes(2, "little") + message
return b"LT\x01\xfe" + frame[4:14] + len(payload).to_bytes(2, "little") + payload
@@ -92,7 +95,7 @@ async def client_frames(
payload = await reader.readexactly(payload_size)
frame = header + payload
if guard.should_block(frame):
client_writer.write(empty_response(frame))
client_writer.write(denied_response(frame))
await client_writer.drain()
continue
backend_writer.write(frame)
+4 -6
View File
@@ -77,11 +77,6 @@ class GuardTests(unittest.TestCase):
self.assertFalse(guard.should_block(frame(0x41, [(0x15, b"\x01\0\0\0")])))
self.assertFalse(guard.should_block(frame(0x23, [(0x0B, selector)])))
def test_returns_empty_success_for_blocked_inventory(self) -> None:
module = importlib.import_module("guard")
request = frame(0x23, [(0x0B, bytes.fromhex("87481bd9") + b"\0" * 4)])
response = module.empty_response(request)
self.assertEqual(response, request[:14] + b"\0\0")
def test_blocks_cross_subscriber_reads_without_disconnect(self) -> None:
module = importlib.import_module("guard")
@@ -90,7 +85,10 @@ class GuardTests(unittest.TestCase):
guard.observe_server(frame(0x02, [(0x09, own_tmsi)]))
peer_request = frame(0x21, [(0x0B, b"\x55\x66\x77\x88" + b"\0" * 4)])
self.assertTrue(guard.should_block(peer_request))
self.assertEqual(module.empty_response(peer_request), peer_request[:14] + b"\0\0")
response = module.denied_response(peer_request)
self.assertEqual(response[3], 0xFE)
self.assertEqual(response[4:14], peer_request[4:14])
self.assertEqual(module.tlvs(response), [(0x22, b"not found")])
def test_allows_attached_subscriber_reads(self) -> None:
module = importlib.import_module("guard")