Invariant dispatch that fires for what a transaction actually touches.
kontor.invariant/assert-invariants schedules invariants by asking
get-attribute which attributes a transaction MENTIONS. For destructive
forms that is nobody:
(defmethod get-attribute :db/retractEntity [_] nil)
and the reasoning given is sound for the use kontor puts invariants to -- "none asserts a new attribute value, so per-attribute invariants have nothing to schedule against". It is exactly wrong when the invariant IS the access control, which is the datopia model: the guard on an attribute must fire when that attribute's data is DESTROYED, or deletion is the way around every guard.
What actually happens is stranger than it not firing. The nil binds as
UNBOUND in the lookup query, so it matches every registered rule. Measured
with a deny-all invariant on :guarded/value:
write :guarded/value BLOCKED correct
retractEntity a guarded entity BLOCKED
retractEntity an UNRELATED one BLOCKED wrong
write an unrelated attribute allowed correct
Fail-closed, which is the lucky direction, and wrong twice. An unrelated
invariant blocks unrelated deletions -- once any invariant exists that does
not hold for the retractEntity shape, nothing can be retracted. And every
retractEntity evaluates EVERY invariant in the database, which is O(N)
amplification from a three-token transaction, in a model where N grows with
adoption because invariants are the contracts.
Dispatch on PRODUCED datoms:
submitted: retractEntity guarded -> nil | unrelated -> nil
produced: -> #{:guarded/value} | -> #{:unrelated/thing}
This is the third time the same root cause has surfaced. Submitted forms do
not name what a transaction touches: it broke attribute scoping
(skuld.ledger.authz), it broke row ownership through the :db/isComponent
cascade (skuld.ledger.ownership), and it breaks invariant dispatch here. The
dispatcher reads the settled candidate report from the boundary.
Trusted four-source queries reuse Kontor's prepared transition. Signed value guards use their own restricted evaluator on the same report.
Invariant dispatch that fires for what a transaction actually touches.
## The bug this exists to correct
`kontor.invariant/assert-invariants` schedules invariants by asking
`get-attribute` which attributes a transaction MENTIONS. For destructive
forms that is nobody:
(defmethod get-attribute :db/retractEntity [_] nil)
and the reasoning given is sound for the use kontor puts invariants to --
"none asserts a new attribute value, so per-attribute invariants have
nothing to schedule against". It is exactly wrong when the invariant IS the
access control, which is the datopia model: the guard on an attribute must
fire when that attribute's data is DESTROYED, or deletion is the way around
every guard.
What actually happens is stranger than it not firing. The `nil` binds as
UNBOUND in the lookup query, so it matches every registered rule. Measured
with a deny-all invariant on `:guarded/value`:
write :guarded/value BLOCKED correct
retractEntity a guarded entity BLOCKED
retractEntity an UNRELATED one BLOCKED wrong
write an unrelated attribute allowed correct
Fail-closed, which is the lucky direction, and wrong twice. An unrelated
invariant blocks unrelated deletions -- once any invariant exists that does
not hold for the retractEntity shape, nothing can be retracted. And every
`retractEntity` evaluates EVERY invariant in the database, which is O(N)
amplification from a three-token transaction, in a model where N grows with
adoption because invariants are the contracts.
## The correction
Dispatch on PRODUCED datoms:
submitted: retractEntity guarded -> nil | unrelated -> nil
produced: -> #{:guarded/value} | -> #{:unrelated/thing}
This is the third time the same root cause has surfaced. Submitted forms do
not name what a transaction touches: it broke attribute scoping
(`skuld.ledger.authz`), it broke row ownership through the `:db/isComponent`
cascade (`skuld.ledger.ownership`), and it breaks invariant dispatch here. The
dispatcher reads the settled candidate report from the boundary.
Trusted four-source queries reuse Kontor's prepared transition. Signed
value guards use their own restricted evaluator on the same report.(assert-registrations-valid! datoms)Refuse any :invariant/query this transaction REGISTERS that kontor's own
validator rejects.
This is the hole that bypassing assert-invariants opened, and it was
remote code execution. kontor validates a query at registration time inside
assert-invariants; correcting the DISPATCH meant calling invariant-holds?
directly, which skipped the validation with it. An attacker-supplied query
is edn/read-string-ed and handed to d/q, and datahike resolves
namespaced function symbols -- so the query body is arbitrary Clojure,
running on every validator, deterministically.
Registering it on an attribute every transaction touches (:urd/sender is
stamped on all of them) makes it run on every transaction for a one-time
fee. A file was written on the validator to demonstrate it.
*allowed-fns* is bound explicitly rather than inherited: it is a dynamic
var, and a guard that depends on whatever the caller happened to leave in
place is not a guard.
Refuse any `:invariant/query` this transaction REGISTERS that kontor's own validator rejects. This is the hole that bypassing `assert-invariants` opened, and it was remote code execution. kontor validates a query at registration time inside `assert-invariants`; correcting the DISPATCH meant calling `invariant-holds?` directly, which skipped the validation with it. An attacker-supplied query is `edn/read-string`-ed and handed to `d/q`, and datahike resolves namespaced function symbols -- so the query body is arbitrary Clojure, running on every validator, deterministically. Registering it on an attribute every transaction touches (`:urd/sender` is stamped on all of them) makes it run on every transaction for a one-time fee. A file was written on the validator to demonstrate it. `*allowed-fns*` is bound explicitly rather than inherited: it is a dynamic var, and a guard that depends on whatever the caller happened to leave in place is not a guard.
(touched-attributes report)(touched-attributes db tx-data)Attributes of the datoms tx-data PRODUCES against db.
The whole correction in one function. Takes a report when the caller already has one, including the candidate settlement in block admission.
Attributes of the datoms `tx-data` PRODUCES against `db`. The whole correction in one function. Takes a report when the caller already has one, including the candidate settlement in block admission.
(violations report tx-data)Registered invariants that do NOT hold for this transaction.
Returns a vector of {:attribute a :query q}, empty when everything holds.
Data rather than an exception because the boundary has to charge a fee for a
refused transaction, which means it needs to keep going.
Dispatch and evaluation take DIFFERENT inputs, and conflating them is easy:
report supplies the produced datoms that say WHICH invariants to run,
while tx-data is the transaction FORMS that kontor re-applies to build the
four sources. Passing (:tx-data report) -- produced datoms -- as the
second argument type-checks and is wrong.
nil can never enter the attribute set here, so the unbound-match
behaviour that makes retractEntity trigger everything cannot arise.
Registered invariants that do NOT hold for this transaction.
Returns a vector of `{:attribute a :query q}`, empty when everything holds.
Data rather than an exception because the boundary has to charge a fee for a
refused transaction, which means it needs to keep going.
Dispatch and evaluation take DIFFERENT inputs, and conflating them is easy:
`report` supplies the produced datoms that say WHICH invariants to run,
while `tx-data` is the transaction FORMS that kontor re-applies to build the
four sources. Passing `(:tx-data report)` -- produced datoms -- as the
second argument type-checks and is wrong.
`nil` can never enter the attribute set here, so the unbound-match
behaviour that makes `retractEntity` trigger everything cannot arise.cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |