fix(micromate): the opcode gate is an allowlist -- unknown SUBs refused; tables read-only

READ_SUBS catalogues every read; a SUB in neither table is refused, and
allowed_writes may only name catalogued writes.  Tables are MappingProxyType,
_send checks the exact bytes it writes, check_transmit documents precedence.
Tests cover unknown opcodes, concatenation smuggling and table immutability.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
This commit is contained in:
serversdownandClaude Opus 5.5 committed 2026-10-22 17:19:52 -04:00
1 parent a3e4e85dfd
commit 9c1c7cec76
4 files changed
+108 -7

No files matched your search

+48 -1
View File
@@ -11,7 +11,7 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import micromate.protocol as mm_proto # noqa: E402
from micromate.framing import build_request # noqa: E402
from micromate.opcodes import ( # noqa: E402
CROSS_SERIES_COLLISIONS, WRITE_SUBS, OpcodeRefused, check_transmit,
CROSS_SERIES_COLLISIONS, READ_SUBS, WRITE_SUBS, OpcodeRefused, check_transmit,
)
from test_micromate_protocol import frame, proto # noqa: E402
@@ -73,3 +73,50 @@ def test_a_frame_that_is_not_a_request_is_refused_not_sent():
with pytest.raises(ValueError):
p._send(b"\x41\x03")
assert t.written == []
def test_every_protocol_read_sub_is_in_the_read_table():
consts = {k: v for k, v in vars(mm_proto).items() if k.startswith("SUB_")}
assert len(consts) == 19
assert set(consts.values()) <= set(READ_SUBS)
assert {0x94, 0x48} <= set(READ_SUBS)
def test_read_and_write_tables_are_disjoint():
assert not (set(READ_SUBS) & set(WRITE_SUBS))
@pytest.mark.parametrize("sub", [0xA3, 0x69, 0x74, 0x98, 0xEE])
def test_an_unknown_opcode_is_refused_and_nothing_is_written(sub):
p, t = proto([])
with pytest.raises(OpcodeRefused, match="unknown opcode"):
p._send(build_request(sub, 0))
assert t.written == []
def test_allowed_writes_may_only_name_catalogued_writes():
for bad in (frozenset({0xEE}), frozenset({0x5B})):
with pytest.raises(ValueError):
proto([], allowed_writes=bad)
def test_the_tables_are_read_only():
for table in (WRITE_SUBS, READ_SUBS, CROSS_SERIES_COLLISIONS):
with pytest.raises((TypeError, AttributeError)):
table.pop(next(iter(table)))
with pytest.raises(TypeError):
table[0x00] = "x"
@pytest.mark.parametrize("tail", [build_request(0x96, 0xFFFF), A2_WIRE])
def test_a_read_followed_by_a_write_in_one_send_is_refused(tail):
p, t = proto([], allowed_writes=frozenset({0x96}))
with pytest.raises(ValueError):
p._send(build_request(0x5B, 0x0030) + tail)
assert t.written == []
def test_a_bytearray_frame_is_checked_and_written_as_the_same_bytes():
p, t = proto([])
p._send(bytearray(build_request(0x5B, 0x0030)))
assert t.written == [build_request(0x5B, 0x0030)]