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:
1 parent
a3e4e85dfd
commit
9c1c7cec76
4 files changed
+108
-7
No files matched your search
+10
-2
@@ -8,7 +8,8 @@ Scope: **reads only on the wire, enforced.** Every outgoing frame passes
|
||||
`micromate.opcodes.check_transmit()` in `_send`: a write opcode is refused
|
||||
unless this connection was constructed with it in `allowed_writes` (nothing in
|
||||
the codebase does so yet), and `0xA2` -- CONFIRM ERASE on Series III -- is
|
||||
refused always. No command has ever been originated against a unit by this
|
||||
refused always. The gate is an allowlist: a SUB not catalogued in
|
||||
`opcodes` (read or write) is refused as unknown. No command has ever been originated against a unit by this
|
||||
project; every write in ``docs/micromate_protocol_reference.md`` was performed
|
||||
by THOR while we recorded. The first thing this codebase ever sends to a
|
||||
customer's instrument should be a decision someone made on purpose, not a side
|
||||
@@ -47,7 +48,7 @@ from typing import Optional
|
||||
from minimateplus.transport import BaseTransport
|
||||
|
||||
from .framing import MicromateFrame, MicromateFrameParser, build_request, request_sub
|
||||
from .opcodes import check_transmit
|
||||
from .opcodes import WRITE_SUBS, check_transmit
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
@@ -271,6 +272,12 @@ class MicromateProtocol:
|
||||
self._recv_timeout = recv_timeout
|
||||
self._strict = strict_checksums
|
||||
self._allowed_writes = frozenset(allowed_writes)
|
||||
unknown = self._allowed_writes - set(WRITE_SUBS)
|
||||
if unknown:
|
||||
raise ValueError(
|
||||
"allowed_writes may only name catalogued write SUBs; not in "
|
||||
"micromate.opcodes.WRITE_SUBS: "
|
||||
+ ", ".join(f"0x{x:02X}" for x in sorted(unknown)))
|
||||
self._parser = MicromateFrameParser()
|
||||
self._pending: list[MicromateFrame] = []
|
||||
|
||||
@@ -520,6 +527,7 @@ class MicromateProtocol:
|
||||
def _send(self, frame: bytes) -> None:
|
||||
# The gate sees the bytes that are about to go out, not the caller's
|
||||
# intent: every transmit in this package passes through here.
|
||||
frame = bytes(frame) # checked bytes == written bytes
|
||||
check_transmit(request_sub(frame), self._allowed_writes)
|
||||
log.debug("TX %d bytes: %s", len(frame), frame.hex())
|
||||
self._transport.write(frame)
|
||||
|
||||
Reference in new issue
Block a user