Under the hood

MIR binary format

The canonical byte-level policy contract, its limits, ordering and SHA-256 identity.

MIR means Maelys Intermediate Representation. It is the compact binary contract passed from policy production to enforcement. Humans normally write JSON and inspect JSON; runtimes exchange MIR because it has exactly one valid byte representation for one decision.

Why a binary format exists

Canonical MIR provides:

  • one stable decision identity independent of JSON whitespace or member order;
  • strict bounds and fixed-width integer encodings;
  • rejection of unknown flags, records and trailing bytes;
  • fast native and WebAssembly decoding;
  • a small contract that Warden can verify before enforcement.
CODE
policy.json ──compile──► policy.mir ──SHA-256──► decision fingerprint
     │                       │
human source            normative bytes

The formatted JSON returned by inspect is a view, not a second canonical format.

Global rules

  • all integers are unsigned and big-endian;
  • maximum document size is 1 MiB;
  • strings are shortest-form UTF-8 without NUL and have no terminator on the wire;
  • the decoder accepts only format version 2;
  • there are no trailing bytes or extension records in v2.

Header: 20 bytes

OffsetSizeMeaning
04ASCII magic MMIR
42format version, exactly 2
62flags, exactly zero
84filesystem record count
121network mode: 1 none, 2 direct, 3 mediated
131process-tree confinement: 0 disabled, 1 required
142reserved, exactly zero
164network destination count

Filesystem record

Each record has a 12-byte prefix followed by path_length bytes.

OffsetSizeMeaning
01record kind, exactly 1
11access: 1 read, 2 write, 3 deny
21root: 1 minimal-runtime, 2 workspace, 3 temp, 4 host
31scope: 1 exact, 2 tree
41missing: 1 error, 2 skip
53reserved, exactly zero
84path length
12Nnormalized path bytes

Records are sorted by (root, path, scope, missing, access). Duplicate targets are collapsed with deny > write > read. Access is deny-default and write implies read.

Network destination record

Each destination has an 8-byte prefix followed by host_length ASCII bytes.

OffsetSizeMeaning
01record kind, exactly 2
11protocol, exactly 1 for TCP
22port in 1..65535
44hostname length in 1..253
8Ncanonical lowercase DNS hostname

Destination records exist exactly when the network mode is mediated, which requires at least one. They are sorted by (host, protocol, port) and deduplicated. none and direct require a zero destination count.

Canonicality check

The decoder validates structure and semantic limits, reconstructs the model, re-encodes it and compares the result with the input. Any alternative representation is rejected as non-canonical.

This matters because policy identity is:

CODE
decisionDigest = lowercase_hex(SHA-256(all canonical MIR bytes))

The digest covers the header, every filesystem record and every mediated destination.

Limits

ResourceLimit
encoded document1 MiB
filesystem rules4096
bytes in one path string4096
network destinations1024
hostname bytes253

Compatibility boundaries

Product version, public C ABI and MIR format are independent:

  • product 0.3.0 identifies a release bundle;
  • Sandbox Policy ABI 3 identifies the C API layout/contract;
  • MIR format 2 identifies bytes exchanged with consumers.

A future library may change its C ABI while continuing to emit exactly MIR v2. A new MIR format requires an explicit decoder/encoder compatibility decision.