Threat model as code. Every repo carries a versioned moat.yaml describing the system; Moat generates a STRIDE threat register from it.
A violation is something that crossed the moat.
- A YAML file is diffable. A PR that adds a service shows up as a model change.
- CI can fail when a new trust boundary appears with no threats attached.
- The model cannot quietly drift from the code the way a diagram in Figma does.
pnpm add -D @thirtn/moat-cliThe binary is moat. (moat is taken on npm by an unrelated package, so the install name is scoped while the command is not.)
moat init # scaffold a commented moat.yaml
moat validate # schema + referential integrity; exit 1 on error
moat generate # write the threat register into the file, in place
moat report # render markdown, with a mermaid DFD at the end
moat check # CI gate; exit 1 on open high-or-above and lapsed acceptancesA worked example is in examples/acme-payments — a fictional billing system sized to fire every rule in the registry — with its generated register in THREATS.md.
Two layers.
Baseline — the completeness floor. Every element gets its full category set regardless of properties.
| Element type | Categories |
|---|---|
| External entity | S, R |
| Process | S, T, R, I, D, E |
| Data store | T, I, D, plus R when stores_logs: true |
| Data flow | T, I, D |
Context rules — fire on properties and escalate severity, or add a category the baseline does not cover.
| Rule | Condition | Effect |
|---|---|---|
unauth-internet-process |
process, internet_facing, authn: none |
S critical, E high |
no-authz |
process, authz: none |
E high |
secrets-process |
process, handles_secrets |
I high |
no-logging |
process, no stores_logs store reachable |
R medium |
pii-store |
data store, pii |
I critical, T high |
unencrypted-store |
data store, encrypted_at_rest: false |
I high |
no-backups |
data store, backups: false |
D high |
plaintext-crossing |
flow, crossing, unencrypted | I critical, T high |
pii-in-transit |
flow, carries_pii, unencrypted |
I critical |
unauth-flow |
flow, authenticated: false, into a non-internet boundary |
S high (adds the threat) |
Severity is escalate-only: when two rules hit one category, the higher wins. Defaults when nothing fires are medium, or low for external entities.
Rules are data. Adding one is adding an object to the registry in rules/context.ts — the engine does not change.
Rules fire only on an explicit value. A flow with no encrypted: key never invents a threat; instead it appears under Model completeness in validate and in the report:
2 unanswered security properties (rules do not fire on absent values):
· postgres: `encrypted_at_rest` not specified — is it encrypted at rest?
· postgres: `backups` not specified — is it backed up?
The alternative — treating unknown as insecure — turns a half-written model into a wall of criticals that people learn to scroll past. Gaps never fail check.
Protocols that state their own transport security count as explicit: protocol: http derives encrypted: false, https derives true. Protocols with optional TLS (redis, amqp, smtp, grpc) derive nothing, because they genuinely say nothing.
id = "t-" + sha256(system.name + target.id + category + rule.id)[0..6]
rule.id is the rule that generated the threat, never a context rule that escalated it. Regenerating an unchanged model produces byte-identical ids, and fixing a weakness does not renumber the threat and drop its owner.
moat generate rewrites only the threats block, splicing it into the original text. Comments everywhere else survive untouched.
status,owner,accepted_until,accepted_reason,mitigated_byandnotessurvive regeneration.- Newly generated threats arrive as
open. - Threats that no longer generate become
orphaned. They are never deleted. source: manualthreats are left alone entirely.- Severity is recomputed from the rules unless
severity_override: true.
title and description are machine-owned and follow the same idiom: set description_override: true to keep your own wording. Anything replaced is listed by moat generate, and the file is in version control, so the previous text is one git diff away.
Preserving every stored description unconditionally sounds safer, but it freezes a register against every later improvement to the rules — you end up with prose that contradicts the severity printed next to it. Commentary the rules cannot know belongs in notes, which is never generated and never overwritten.
Mitigations resolve after merge: a mitigation naming both the target and the category closes its threats. Delete the mitigation and they reopen, rather than leaving a register that claims cover it no longer has.
moat validate && moat checkcheck fails on any open threat at high or above, and on any accepted threat whose accepted_until has passed. Accepted risk should be cheap to declare and impossible to forget.
On a system with an existing register, use delta-only reporting so nobody has to read 300 rows twice:
moat check --baseline .moat-baseline.yamlOnly threats absent from the baseline can fail the build. A lapsed acceptance still fails regardless — otherwise a team could baseline away the one deadline they agreed to.
| Package | Contents |
|---|---|
@thirtn/moat-core |
Schema, rules, engine, merge, reports. Pure: parsed objects in, objects and strings out. No filesystem, network, process or clock access, so it runs unchanged in a NestJS API or a browser. |
@thirtn/moat-cli |
The moat binary. Owns all I/O. |
pnpm install
pnpm build
pnpm testNo web UI, LLM calls, repo scanning, persistence beyond the YAML file, or DREAD/CVSS scoring. The four-level severity enum is enough.