Installing a guard: the contract half of the datopia model.
An invariant is a Datalog query registered against an attribute. Installing an attribute installs the contract that guards it, and that is strictly more expressive than ownership -- row ownership is itself just one invariant you could write. It is also the reason a user can be allowed to write another user's attribute at all: the guard decides, not the namespace.
It was closed because it was remote code execution. :invariant/query is
attacker-supplied, edn/read-string-ed and handed to d/q, and datahike
resolves namespaced function symbols -- so the body was arbitrary Clojure
running on every validator. The fix put the whole invariant namespace into
ownership/protocol-attributes, which stopped the attacker and everyone
else with them.
Reopening it needs four checks, not one:
PARSE datalog-parser on the form, with :implicit-rules? set to
the rule names the registration DECLARES. That rejects both
malformed queries and calls to rules the query did not declare
-- without it the choice is between refusing every rule call
and accepting all of them.
SOURCES exactly four, in kontor's order: $before, $after, $empty+datoms, $datoms.
FUNCTIONS every called fn in *allowed-fns* (datahike's built-ins plus
subquery), bound EXPLICITLY rather than inherited -- it is a
dynamic var, and a guard that depends on what the caller left
in place is not a guard. The walk must recurse into RULE
BODIES, not just the query's :qwhere: a rule body is exactly
where a function call would otherwise hide.
SIZE a byte cap and a clause cap, on the query and on each rule program.
A registration is keyed by hasch/uuid of the READ FORM, not the string.
Two queries differing only in whitespace then dedupe to one address, and the
address is stable across printers.
Rule sets are immutable and addressed the same way. That is what stops a
supply-chain problem: if Bob's guard declares :invariant/uses [<digest>]
and Alice could edit that rule set, Alice could silently change what Bob's
guard means -- his attribute is still his, but the contract protecting it now
says something he never agreed to. She cannot change a hash. "Updating" a
rule set installs a new one, and consumers opt in by pointing at it.
A useful consequence: an invariant's meaning is fully determined by
(query form, rule digests), so "what does this guard actually check" is
answerable offline, without database state.
% concatenates safelyVerified end to end: (alice/reach ?x) parses and runs in datahike. Two
users may both define reach and nothing collides, because the namespace IS
the disambiguation -- no registry, no renaming, no allocation step. At
execution the boundary resolves :invariant/uses, concatenates the rule
vectors, and passes them as the single % argument.
A non-recursive guard is statically boundable, so it is priced by its bound. A recursive one is not, and not for want of trying: transitive closure over a chain grows quadratically in the edge count -- 45 pairs at 9 edges, 3,160 at 79 -- so output is not a function of input pattern cardinalities and there is nothing for a static estimator to read.
The correct answer is reserve-and-refund against a deterministic fuel counter, which datahike does not yet have (replikativ/datahike#964). Until it does, a recursive guard pays a flat high fee at registration. The user overpays, which is the tolerable direction; charging measured TIME would not be -- a warm node with a populated query cache measures near zero work and a cold one measures all of it, so peers would disagree about the fee.
Installing a guard: the contract half of the datopia model.
An invariant is a Datalog query registered against an attribute. Installing
an attribute installs the contract that guards it, and that is strictly more
expressive than ownership -- row ownership is itself just one invariant you
could write. It is also the reason a user can be allowed to write another
user's attribute at all: the guard decides, not the namespace.
## Why registration was closed, and what reopening needs
It was closed because it was remote code execution. `:invariant/query` is
attacker-supplied, `edn/read-string`-ed and handed to `d/q`, and datahike
resolves namespaced function symbols -- so the body was arbitrary Clojure
running on every validator. The fix put the whole `invariant` namespace into
`ownership/protocol-attributes`, which stopped the attacker and everyone
else with them.
Reopening it needs four checks, not one:
PARSE `datalog-parser` on the form, with `:implicit-rules?` set to
the rule names the registration DECLARES. That rejects both
malformed queries and calls to rules the query did not declare
-- without it the choice is between refusing every rule call
and accepting all of them.
SOURCES exactly four, in kontor's order: $before, $after,
$empty+datoms, $datoms.
FUNCTIONS every called fn in `*allowed-fns*` (datahike's built-ins plus
`subquery`), bound EXPLICITLY rather than inherited -- it is a
dynamic var, and a guard that depends on what the caller left
in place is not a guard. The walk must recurse into RULE
BODIES, not just the query's `:qwhere`: a rule body is exactly
where a function call would otherwise hide.
SIZE a byte cap and a clause cap, on the query and on each rule
program.
## Content addressing, and why it makes updates safe
A registration is keyed by `hasch/uuid` of the READ FORM, not the string.
Two queries differing only in whitespace then dedupe to one address, and the
address is stable across printers.
Rule sets are immutable and addressed the same way. That is what stops a
supply-chain problem: if Bob's guard declares `:invariant/uses [<digest>]`
and Alice could edit that rule set, Alice could silently change what Bob's
guard means -- his attribute is still his, but the contract protecting it now
says something he never agreed to. She cannot change a hash. "Updating" a
rule set installs a new one, and consumers opt in by pointing at it.
A useful consequence: an invariant's meaning is fully determined by
`(query form, rule digests)`, so "what does this guard actually check" is
answerable offline, without database state.
## Rule names are namespaced, so `%` concatenates safely
Verified end to end: `(alice/reach ?x)` parses and runs in datahike. Two
users may both define `reach` and nothing collides, because the namespace IS
the disambiguation -- no registry, no renaming, no allocation step. At
execution the boundary resolves `:invariant/uses`, concatenates the rule
vectors, and passes them as the single `%` argument.
## Pricing: a flat high fee for recursion, deliberately
A non-recursive guard is statically boundable, so it is priced by its bound.
A recursive one is not, and not for want of trying: transitive closure over
a chain grows quadratically in the edge count -- 45 pairs at 9 edges, 3,160
at 79 -- so output is not a function of input pattern cardinalities and
there is nothing for a static estimator to read.
The correct answer is reserve-and-refund against a deterministic fuel
counter, which datahike does not yet have (replikativ/datahike#964). Until
it does, a recursive guard pays a flat high fee at registration. The user
overpays, which is the tolerable direction; charging measured TIME would not
be -- a warm node with a populated query cache measures near zero work and a
cold one measures all of it, so peers would disagree about the fee.(address form)The content address of a query or rule program.
Over the READ FORM, so whitespace and formatting do not fork the address.
Note hasch has no coercion for clojure.lang.Ratio, so a literal ratio in
a query cannot be addressed -- validate rejects those before we get here.
The content address of a query or rule program. Over the READ FORM, so whitespace and formatting do not fork the address. Note `hasch` has no coercion for `clojure.lang.Ratio`, so a literal ratio in a query cannot be addressed -- `validate` rejects those before we get here.
(admit-registration
db
{:keys [chain schedule system-writers] :or {schedule cost/default-schedule}}
{:keys [sender public-key signature nonce registration] :as envelope})Decide, price and produce the transaction installing a guard.
Mirrors skuld.boundary/admit: the same refusal ordering, the same rule
that everything past the signature is charged. Returns
{:ok? :reason :fee :apply}.
Decide, price and produce the transaction installing a guard.
Mirrors `skuld.boundary/admit`: the same refusal ordering, the same rule
that everything past the signature is charged. Returns
`{:ok? :reason :fee :apply}`.Symbols that head a CLAUSE or a find-spec element, not a function call.
These appear in head position of a list exactly like a predicate does, so
the structural scan below has to know them or it flags every legitimate
or. Aggregates are included because they head a list in :find.
Symbols that head a CLAUSE or a find-spec element, not a function call. These appear in head position of a list exactly like a predicate does, so the structural scan below has to know them or it flags every legitimate `or`. Aggregates are included because they head a list in `:find`.
(fee {:keys [query rule-programs]})What this registration costs to install.
Size is charged because it is stored forever; recursion is charged because it opts out of static bounding. Neither is an estimate of execution cost -- that is charged to the transactor who triggers the guard, and remains unpriced pending datahike#964.
What this registration costs to install. Size is charged because it is stored forever; recursion is charged because it opts out of static bounding. Neither is an estimate of execution cost -- that is charged to the transactor who triggers the guard, and remains unpriced pending datahike#964.
What a registration costs.
:recursive is deliberately far above :base plus bytes. It is not an estimate of
anything -- it is the price of opting out of static bounding until fuel
metering exists.
What a registration costs. `:recursive` is deliberately far above `:base` plus bytes. It is not an estimate of anything -- it is the price of opting out of static bounding until fuel metering exists.
(install-tx-data {:keys [query rule-programs guards installer]})Tx-data installing registration, addressed by content.
:db.unique/identity on both addresses does the deduplication: registering
a query someone already registered upserts onto the same entity instead of
creating a second one, and installing a rule set twice is a no-op.
Tx-data installing `registration`, addressed by content. `:db.unique/identity` on both addresses does the deduplication: registering a query someone already registered upserts onto the same entity instead of creating a second one, and installing a rule set twice is a no-op.
Caps, in the units an attacker would push on.
Size is not a proxy for cost -- skuld.ledger.cost exists because text length
and work are unrelated -- but it does bound the parse, the storage, and how
much a single registration can occupy in the app-hash.
Caps, in the units an attacker would push on. Size is not a proxy for cost -- `skuld.ledger.cost` exists because text length and work are unrelated -- but it does bound the parse, the storage, and how much a single registration can occupy in the app-hash.
(may-guard? sender a system-writers)May sender install a guard on attribute a?
Your own namespace, freely -- it is your data, and a guard on it constrains only writes you could already control. Anything else is governance: a guard on shared schema constrains EVERY user who touches that attribute, forever, and one key should not be able to impose that on everyone.
May `sender` install a guard on attribute `a`? Your own namespace, freely -- it is your data, and a guard on it constrains only writes you could already control. Anything else is governance: a guard on shared schema constrains EVERY user who touches that attribute, forever, and one key should not be able to impose that on everyone.
(recursive? programs)Does any rule in programs call a rule defined by the same set?
Mutual recursion counts -- the fixpoint does not care which rule closed the cycle. Read on the parsed form rather than by scanning text, so a rule name appearing inside a string or a keyword does not read as a call.
Does any rule in `programs` call a rule defined by the same set? Mutual recursion counts -- the fixpoint does not care which rule closed the cycle. Read on the parsed form rather than by scanning text, so a rule name appearing inside a string or a keyword does not read as a call.
(registration-digest chain sender nonce registration)The bytes a registration signature covers.
Separate from authz/tx-digest because a registration is not tx-data: the
sender does not supply datoms at all. The boundary CONSTRUCTS the
:invariant/* and :rules/* datoms after validating, which is why those
attributes stay refused to senders in ownership/protocol-attributes --
a user never writes them directly, and so cannot write a guard that was
never checked.
:chain is covered for the same reason it is everywhere else: without it a
guard registered on a testnet is a valid guard on mainnet.
The bytes a registration signature covers. Separate from `authz/tx-digest` because a registration is not tx-data: the sender does not supply datoms at all. The boundary CONSTRUCTS the `:invariant/*` and `:rules/*` datoms after validating, which is why those attributes stay refused to senders in `ownership/protocol-attributes` -- a user never writes them directly, and so cannot write a guard that was never checked. `:chain` is covered for the same reason it is everywhere else: without it a guard registered on a testnet is a valid guard on mainnet.
(resolve-rules db uses)The concatenated rule vector for an invariant, ready to pass as %.
Concatenation is safe because rule names are namespaced -- verified end to
end, (alice/reach ?x) parses and runs. Two users may both define reach
and neither shadows the other.
The concatenated rule vector for an invariant, ready to pass as `%`. Concatenation is safe because rule names are namespaced -- verified end to end, `(alice/reach ?x)` parses and runs. Two users may both define `reach` and neither shadows the other.
(validate {:keys [query rule-programs]})Check a registration. Returns nil when admissible, or {:error … }.
Data rather than an exception because the boundary charges a fee for a refused registration and therefore has to keep going.
Check a registration. Returns nil when admissible, or `{:error … }`.
Data rather than an exception because the boundary charges a fee for a
refused registration and therefore has to keep going.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 |