Strig
English · Português (Brasil)
An authorization library for Rust. A validated permission catalog and a decision engine that combines RBAC, ABAC and ReBAC over the same grants, with governance and auditing of changes, and real-time propagation of those changes all the way to the client.
The core does no IO, does not depend on an async runtime and compiles to WebAssembly. Real time, transports and bindings live in separate crates, each one optional.
Status: pre-1.0. The API can still change between minor versions. Releases are published as
strigon crates.io (API docs on docs.rs) and on npm. Formerly Themis (cratethemis-auth, npm packagethemis-auth): the project was renamed to Strig, see the migration note.
Crates
| Crate | What it is for | Notable dependencies |
|---|---|---|
strig |
Catalog, decision engine, governance, auditing (audit feature, on by default), JSON spec |
serde, thiserror; sha2 and serde_json with audit |
strig-sync |
Change hub, Transport trait, framework-independent SSE/WebSocket sessions, capability cache |
tokio |
strig-redis |
Transport over Redis pub/sub |
redis |
strig-postgres |
Transport over Postgres LISTEN/NOTIFY, with the notice sent inside the transaction |
tokio-postgres |
strig-axum |
axum SSE and WebSocket routes over the strig-sync sessions |
axum |
strig-inspect |
Inspector: the trace of each decision, grant simulation, access matrix and audit trail reading. Optional gpui window (gui feature) and terminal commands |
gpui-pre, gpui-component |
strig-server |
HTTP decision server over an Engine and a grant source, and a Rust client with timeout and circuit breaker that denies when it gets no answer |
axum, hyper-util (client) |
strig-macros |
policy! and include_policy!: a policy written in the DSL is validated while the crate compiles |
strig |
strig-node |
JS bindings: native module (N-API) for Node and Bun, WebAssembly for the browser, real-time client and server in JS | napi or wasm-bindgen |
Depend on it from another crate:
[]
= "0.6"
= "0.6"
Concepts
| Concept | Type | Role |
|---|---|---|
| Catalog | Catalog, PermissionDef |
Dotted keys (document.invoice.export), accepted targets, implied_by, free-form metadata (meta). Validated on construction: malformed key, duplicate, unregistered target, missing parent, cycle, and the application's own rules (CatalogRule). Exports and imports JSON. |
| Resource | Resource, Anchor |
Path from the most specific to the broadest (document:7 → doc_type:invoice → project:42 → org:9), with an implicit global at the end, plus attributes. |
| Subject | Subject |
Id, roles (optionally scoped), principals (authenticated, anonymous or your own), attributes. |
| Grant | Grant |
Subject (User, Role, Principal, Relation, Any), permission, target, effect, optional scope and condition. |
| Engine | Engine |
Overrides, collection of the applicable grants, combination by strategy. Returns a Decision with the reason, the matched grants and warnings. |
| Governance | Governance, Invariant |
Applies a grant change only if every invariant passes, and records the outcome in the audit trail. |
Usage
use *;
let catalog = builder
.target_kinds
.broad_target
.permission
.permission
.build?;
let engine = builder
.override_with
.build?;
let grants = vec!;
let dave = user.with_role_in;
let invoice = new
.within
.within
.within;
let decision = engine.decide;
assert!; // g2 sits on a more specific step than g1
The same configuration can come from JSON, TOML or YAML through EngineSpec (catalog, role inheritance, relations, strategy, overrides). EngineSpec::builder() returns the EngineBuilder so you can add in code what does not serialize, such as condition functions.
Examples: cargo run -p strig --example documents (the three models together) and cargo run -p strig-axum --example server (real-time server).
How a decision is made
- Permission not in the catalog: deny (
Reason::UnknownPermission). - Overrides, in order. The first one that answers decides.
override_excepttakes permissions (and the ones they imply) out of an override's reach. - Collection: a grant is a candidate when its permission is the requested one or one that implies it, its scope is on the resource path, its target matches a step of the path, its subject matches and its condition is true.
- The strategy picks the deciding grant. With no candidate, deny.
A condition or relation that cannot be evaluated (missing attribute, engine without a TupleStore) never opens access: the allow is dropped and the deny is applied. The reason goes into Decision::warnings.
| Strategy | Rule |
|---|---|
MostSpecific (default) |
The most specific step with any grant decides; on that step, deny wins. |
DenyOverrides |
Any deny wins. |
PermitOverrides |
Any allow wins. |
FirstApplicable |
The first grant in source order decides. |
The three models
RBAC. RoleGraph::builder().inherit("maintainer", "member") gives maintainer the grants of member. Subject::with_role_in(role, anchor) limits the role to the resources under that step.
ABAC. Grant::when(condition) with a serializable Condition: eq, ne, lt, le, gt, ge, is_in, exists, all, any, not and call, plus starts_with, ends_with and contains on text (contains also tests list membership), any_in and all_in on lists, ip_in for an address in a CIDR or a list of CIDRs, and time_between and weekday_in on the decision instant, always in UTC. The time conditions read the instant of Context::at; without one they cannot be resolved, so the allow does not apply. There is no regular-expression condition. Operands read subject.*, resource.* and env.*. Your own functions are added with ConditionRegistry::register and checked by Engine::validate_grant.
ReBAC. object#relation@subject tuples in a TupleStore and per-type rules in a RelationSchema (This, Computed, TupleToUserset, Union, Intersection, Exclusion), inspired by relationship-based access control. user:* stands for every user. A grant with SubjectMatcher::relation("editor") applies to whoever holds editor on the step matched by the target. The search has a maximum depth (max_relation_depth, 32 by default) and cuts cycles.
Governance
let governance = new
.with
.with
.with
.with
.with
.audited;
let next = governance.apply_with?;
// persist `next` only on Ok
Assigning or revoking a role goes through the same invariants and is audited as role_assign/role_revoke; the grant set comes back unchanged and the subject to persist comes from Mutation::subject_after.
let change = AssignRole ;
governance.apply_with?;
let bob = change.subject_after.expect; // persist bob's roles
Forbid applies to a role assignment only when it targets a user, and it ignores scope. NoEscalation keeps an actor from handing out what they do not hold: an allow grant or a role assignment is refused unless the actor holds every permission it confers, and each one it implies, where it would apply. See Catalog governance.
Add your own rule by implementing Invariant. The list can also come from JSON (InvariantSpec).
Catalog governance
The catalog carries the rules that governance enforces, so they live next to the permission they protect:
Flag on PermissionDef |
Effect |
|---|---|
depends_on |
Holding the permission requires holding each listed one on the same resource (Prerequisites). |
non_delegable |
Not delegable by whoever merely holds it: only a manager hands it out. |
management_only |
Grantable only to management roles. |
override_only |
Valid only through an override: a plain grant of it does not count. |
NoEscalation completes the set: an actor hands out only what they hold, so holding org.roles.edit is not a way to give someone a stronger role than your own. NoLockout can carry a reserve (with_reserve): when a change would leave a critical permission without a holder, the Violation brings a Suggestion with the role assignment that fixes it. Nothing is applied; the suggestion is also in the audit record. RoleTemplate::materialize turns a template of grants into ordinary grants, with the template id and version in the change metadata and no live link back.
let catalog = builder
.target_kind
.permission
.permission
.permission
.permission
.build?;
let engine = builder.build?;
let governance = new
.with
.with
.with;
// `lead` holds `project.edit` only, so handing out `project.view` is an escalation.
let current = vec!;
let handout = Add;
let refused = governance.apply.unwrap_err;
assert_eq!;
Auditing
With the audit feature, every change that goes through governance becomes an AuditRecord, whether applied or refused. ReBAC tuples and role assignments are recorded with Auditor::tuple_change and Auditor::role_change. If the sink fails to write, governance refuses the change with the audit violation: no record, no change.
let auditor = new;
Built-in sinks: MemoryAuditLog and JsonLinesAudit (any io::Write). A database, queue or SIEM plugs in by implementing AuditSink.
Levels and storage
Each level includes everything from the previous one. Sizes come from the reference record (audit::reference_record): a grant with a scope and a two-clause condition, UUID ids, an actor with two roles and three request metadata entries. They are compact JSON bytes in JSON Lines, without compression or indexes. The estimates_match_reference_records test fails if the measured size drifts more than 10% from the documented one, and cargo run -p strig --example audit_storage measures again.
| Level | What it keeps | Bytes per record | Per 1 million records |
|---|---|---|---|
minimal |
When, who, action, object id, outcome | 175 | 175 MB (167 MiB) |
standard |
Plus the grant summary (permission, subject, target, effect, scope, whether it has a condition) and the names of the invariants that refused | 388 | 388 MB (370 MiB) |
detailed |
Plus the whole grant with its condition, the full actor, the violation messages and the request metadata | 1,097 | 1.10 GB (1,046 MiB) |
forensic |
Plus the SHA-256 digest of the grant set before and after, and the hash chain between records | 1,415 | 1.42 GB (1,349 MiB) |
Access decisions can be audited too: Auditor::decide(&engine, &ctx, &request, &meta) decides and records, and at the forensic level it uses Engine::explain and keeps the full trace. Reference record: three grants in the source (one with a condition), a resource with three steps, UUID ids and three metadata entries (audit::reference_decision_record, test decision_estimates_match_reference_records).
| Level | What it keeps from a decision | Bytes per record | Per 1 million records |
|---|---|---|---|
minimal |
When, subject, permission@resource, allowed or denied |
285 | 285 MB (272 MiB) |
standard |
Plus the reason (override, deciding grant and step, or no grant) and the grants that matched | 669 | 669 MB (638 MiB) |
detailed |
Plus the full subject and resource, the warnings and the request metadata; enough to re-evaluate the decision later | 1,239 | 1.24 GB (1,182 MiB) |
forensic |
Plus the full trace (Explanation) and the hash chain |
3,582 | 3.58 GB (3,416 MiB) |
The forensic trace grows with the number of grants the source returns per decision. AuditLevel::estimate_decision_bytes(n) gives the projection in code.
AuditLevel::estimate_bytes(n) returns the same projection for changes. In a database, add the row overhead (in Postgres, about 28 bytes between the tuple header and the item pointer) and the indexes you create. Long conditions or large metadata grow detailed and forensic proportionally.
At the forensic level, each record carries the hash of the previous one. audit::verify_chain detects an edited or removed record. Auditor::resume_chain continues the chain after a restart.
Validity windows
Grant::valid_between(from, until) (or valid_from and valid_until) limits a grant in time, in milliseconds since the Unix epoch. The window is closed at the start and open at the end. The instant comes from the caller: Context::at(now) for decisions and Governance::at(now) for changes. Without an instant, a grant with a window does not apply, so a missing clock fails closed. CapabilityCache cuts its entries at the next boundary, and a job removes the expired grants.
let grant = allow
.valid_between;
let grants = vec!;
let ana = user;
let project = new;
let request = new;
assert!;
assert!;
assert!;
Reverse queries and filtered listing
Instead of asking "may this subject do this?", ask who can, or what a subject can reach. QueryContext carries the grants (and .at(now)); QueryLimits bounds the work and PgTables maps the same questions to SQL.
| Call | Answers |
|---|---|
engine.list(&qctx, &subject, permission, kind) |
An AccessFilter for the resources of that kind the subject can reach, to turn into a WHERE. |
engine.who_can(&qctx, permission) |
The parties named by an allow grant of the permission, anywhere. A deny, an override or a condition is not evaluated; who_can_on settles that for one resource. |
engine.who_can_on(..) |
The same, on one resource. |
engine.access_of(..) |
Every party with access to a resource, with the permissions of each. |
engine.check_for(..) |
A check for a subject reference: a SubjectResolver loads the subject and the answer names the path that decided. |
let qctx = new.at;
let filter = engine.list;
let who = engine.who_can;
assert_eq!;
Exclusive resources
With the exclusive feature, a resource can have one holder at a time. Terms::new(priority, ttl_ms) states how strong the claim is and for how long; a higher priority takes the resource over only when the claimant also holds exclusive.preempt. Permissions and time are checked by the same engine as any other decision.
let mut registry = default;
let now = 0; // milliseconds since the Unix epoch, passed by the caller
let first = registry.acquire_checked?;
assert_eq!;
let second = registry.acquire_checked?;
assert!;
Real time
grant change ──► Hub::publish ──► local sessions (SSE/WebSocket) and CapabilityCache
│
└─► Transport (Redis, Postgres, your own) ──► Hub on the other instances
The notice (Change) states the kind of change and the affected step, without the grants. Receivers reload from the source, so the notice fits any transport. A session watches scopes and, with a CapabilityProvider, pushes the capabilities of each affected scope back to the client. Changes that arrive together are recomputed once. When the transport drops and comes back, or a client falls behind, the hub emits Resync and everything is reloaded.
let hub = builder
.transport
.build;
let _relay = hub.spawn_relay;
let cache = new;
hub.on_change;
let provider = new.with_cache;
let realtime = new.with_provider;
// in the routes: realtime.sse(subject, scopes) or realtime.websocket(upgrade, subject, scopes)
Transports:
- Redis (
strig-redis): pub/sub, publishing with automatic reconnection. - Postgres (
strig-postgres):LISTEN/NOTIFY. Callingstrig_postgres::notify(&tx, channel, &change)inside the transaction that writes the grant makes the notice go out only on commit. TLS throughPgTransport::with_tls. - Your own: NATS, Kafka or any other broker implements
Transport(publishandsubscribe).
Client protocol (JSON, identical over SSE and WebSocket): the server sends hello, capabilities, change, resync, error and ping; the client sends watch, unwatch and refresh (WebSocket only). To use another HTTP framework, forward the stream from Session::open and write each message with ServerMessage::to_sse or to_json.
Decision server and remote client
strig-server answers decisions over HTTP, for services that do not embed the engine. DecisionServer::router() returns an axum Router with POST /v1/check, /v1/check/batch, /v1/list, /v1/who and /v1/access, plus GET /health/live and /health/ready. The server reads the grants through a DecisionSource; MemorySource holds a Snapshot you can replace. The time of a decision is the server's clock. The contract is in crates/strig-server/API.md.
let source = new;
let router = new.router;
let listener = bind.await?;
serve.await?;
The client fails closed: only a 200 is an answer. A timeout, a refused connection, a 503 or an answer it cannot read becomes ClientDecision::Denied with the reason, and a circuit breaker stops calling a server that keeps failing.
let config = default
.with_timeout
.with_breaker;
let client = http;
let request = new;
if client.check.await.is_allowed
JavaScript: Node, Bun and the browser
Install with npm install strig. The package lives in crates/strig-node/js. scripts/build.sh builds the native module (js/native/strig.<platform>-<arch>.node) and the WebAssembly (js/wasm/).
import from 'strig';
const = await ; // native on Node/Bun, wasm in the browser
const engine = ; // the same EngineSpec as the server
const ctx = ;
engine.;
engine.;
On Node and Bun, load() tries the native module and falls back to wasm. STRIG_BACKEND=native|wasm forces one of them.
Frontend. PermissionClient (/client) connects over SSE or WebSocket, keeps the capabilities of each scope and decides locally with the same engine. Client-side decisions are for the interface; the server still decides on every operation.
import from 'strig/client';
const client = .;
client.;
client.;
JS backend (/server). Hub, openSession, sseResponse (Fetch API: Bun.serve, Hono, Deno), pipeSse (Node's http) and the redisTransport (node-redis and Bun's RedisClient), ioredisTransport and pgTransport (pg package) transports. Notices share the strig-sync format, so Rust and JS instances exchange changes over the same channel.
Web frameworks. Express, Fastify and NestJS decide on each route with the same options. subject(req) returns the subject (nothing means 401), resource(req) the resource, and exactly one of local: { engine, context, now } or remote: RemoteClient says where the decision comes from. A deny is 403, a failure to decide is 503, and an error in your extractor is 500 with no detail, never a pass. denyStatus, failureStatus, unauthenticatedStatus, errorStatus and onError change that.
import from 'strig';
import from 'strig/express';
const = await ;
const options = ;
app.;
- Fastify (
strig/fastify): the sameauthorize, andplugin, which addsfastify.strigCan. - NestJS (
strig/nestjs):@Can('document.view')on a handler or a controller, andnew StrigGuard(options)as the guard. Express, Fastify and NestJS are optional peer dependencies; the integration you do not import costs nothing, and a missing peer raisesMissingPeerError. - Remote (
strig/remote):RemoteClienttalks to a decision server, with timeout and breaker, and denies when there is no answer. Pass it asremoteinstead oflocal.
import from 'strig/remote';
const remote = ;
const = await remote.;
Binding limits: custom condition functions (call) and custom overrides exist only in Rust. The JSON spec covers the built-in overrides (resource_owner, permission_holder).
Policy language
A policy can be written as text instead of JSON, in the dsl feature of strig. There are three syntaxes for the same policy: Blocks, Rules and Actors, and convert moves between them. Every error carries a position and a hint, and the validated JSON stays the source of truth. The full guide, with the three syntaxes side by side and the grammar, is in docs/dsl.md.
use ;
const SOURCE: &str = "language blocks
target_kinds = [org]
permission org.read {
targets = [org]
}
";
let parsed = parse?;
let json = parsed.policy.to_json?;
let as_rules = convert?;
The conditions of when are written in the three syntaxes as a starts_with b, env.ip ip_in "10.0.0.0/8", time_between "09:00" "17:30" and weekday_in ["mon", "tue"]; the validator refuses a CIDR, a time of day or a weekday it cannot read. When a name is a slip away from a declared one (a permission, a role, a parent), the hint adds did you mean `x`?.
Breaking in 0.5.0: in the Rules and Actors syntaxes a permission line writes depends_on <ids> where it wrote needs <ids>, the same word as the JSON field. needs is now an error with the hint write: depends_on <ids>; replace it in existing .strig files, in the same position on the line. The JSON does not change.
strig::dsl::format and is_formatted rewrite a source in the canonical layout of its syntax, keeping the comments, and strig-inspect fmt [--check] <file|dir> does it for .strig files (--check lists the ones that would change and exits with 1).
To try the language without installing anything, run the playground: it runs the engine in the browser. There is no hosted copy; build and serve it locally:
With dsl-fs, strig::dsl::fs::read_dir reads every *.strig file of a directory into a Project, and write_file writes a policy back.
Policy language in Node
The same language is in the JavaScript binding, native and WebAssembly, behind load(). The policy is the canonical JSON, with the comments in notes, and it is the same for the three syntaxes.
import from 'strig';
const strig = await ;
const source = `language blocks
target_kinds = [org]
permission org.read {
targets = [org]
}
`;
const policy = strig.; // canonical JSON
const rules = strig.; // 'blocks' | 'rules' | 'actors'
const actors = strig.; // text to text
const merged = strig.;
const fromDisk = strig.?.; // native module only
format(source)returns the source in the canonical layout of its syntax andisFormatted(source)says whether it already is.loadProjectDir(dir)exists only on the native module, because the WebAssembly build has no file access. It reads the*.strigfiles of the directory and refuses a policy file that is a symbolic link.diritself must come from a trusted source.- Every failure is an
Errorwith acodethat is the same on both targets:DSL_PARSE,DSL_VALIDATE,DSL_CONFLICT(two sources declare the same thing; both positions are reported),DSL_UNREPRESENTABLE(the syntax cannot say it),DSL_UNSUPPORTED,DSL_LIMIT(a size or count limit),DSL_DUPLICATE_SOURCE(the samepathtwice inloadProject) andDSL_IO(a file or a link inloadProjectDir; the message carries the path, so do not pass it to a remote client). error.diagnosticsis a list of{ file, line, column, message, hint, related? }, at most 100, with a last entry saying "and N more" when some were left out.file,lineandcolumnarenullfor a policy that did not come from text, andrelated(a{ file, line, column }) appears on aDSL_CONFLICT.
try catch
Macros
policy! and include_policy! (crate strig-macros) check the policy while the crate compiles: a policy that does not validate does not build. They are not behind a strig feature; the guide explains why.
use ;
let inline: Policy = policy!;
let from_file: Policy = include_policy!;
Inspector (strig-inspect)
An optional crate, outside the default-members: cargo build -p strig-inspect. The window uses gpui (gpui-pre =0.3.11 and gpui-component =0.9.0); without the gui feature (--no-default-features) you keep the library and the terminal commands.
The scenario is a JSON file with the policy (spec, the same EngineSpec), the data (grants, tuples, env) and the subjects and resources to examine, by name. There is an example in crates/strig-inspect/examples/scenario.json, with a forensic trail of six decisions in examples/audit.jsonl, generated by:
- Evaluation (
avaliacao): pick a subject, permission and resource and see the decision, the reason, the resource path with the deciding step, the effective roles, each override and the fate of each grant (candidate, unrelated permission, out of scope, target off the path, subject does not match, condition false or not evaluable), with the condition tree and the resolved values. It is built onEngine::explain, also available from Rust and JS. - Simulation: turn grants on and off (or add some with
--add) and see whether the set would allow the access. When the decision changes, the panel also shows the decision with the original grants. - Matrix (
matriz): every catalog permission for every subject on a resource, with the cells the simulation changed marked. Clicking a cell opens its trace. - Audit (
auditoria): reads the JSON Lines trail, checks the hash chain of theforensicrecords, shows the recorded trace and re-evaluatesdetailedorforensicdecisions against the current policy, pointing out the ones that would change.
strig-inspect fmt [--check] <file|dir> formats .strig files without opening the window. Files are reloaded on their own when they change on disk. The window follows the system light or dark appearance, with the palette of the playground: jade for structure and allow, clay for deny, gold for who, where and numbers. The window embeds the Noto Sans and JetBrains Mono fonts (OFL license, in crates/strig-inspect/assets/fonts) and its own icons, so it looks the same on any machine. The interface text is in Brazilian Portuguese.
Extension points
| Trait | For |
|---|---|
GrantSource |
Reading grants from a database, a cache or more than one source ((A, B) tuples chain). |
TupleStore |
Keeping ReBAC tuples outside memory. |
Override |
Owner, superadmin, suspended account, maintenance window. FnOverride takes a closure. |
Strategy |
Another combination rule. |
Invariant |
Another grant change rule. |
CatalogRule |
Your own catalog validation (RequireMeta, FnRule). |
AuditSink |
Where the audit trail goes. |
Transport |
The medium between instances for change notices. |
CapabilityProvider, GrantLoader |
How a session computes the capabilities it sends. |
ChangeSink |
A synchronous reaction to each change (cache, metric). |
Development
# transports against real servers (skipped without the variables)
STRIG_REDIS_URL=redis://127.0.0.1:6379/ STRIG_PG_URL=postgres://postgres:pw@127.0.0.1/postgres
# JS: build and tests on Node and Bun
&& && &&
The JS tests also accept STRIG_RUST_SERVER (the URL of the strig-axum server example) to test the client against the Rust server.
The gpui window of strig-inspect needs the Wayland and X11 development libraries on Linux (for example libxkbcommon-dev, libwayland-dev, libxcb1-dev, libvulkan-dev).
Source comments and the inspector interface are in Brazilian Portuguese; issues and pull requests are welcome in English or Portuguese. See CONTRIBUTING.md.
Migrating from Themis
Strig was called Themis until 0.5.0. The code is the same; only names changed.
| Before | Now |
|---|---|
crate themis-auth (library themis) |
crate strig (library strig) |
themis-sync, themis-redis, themis-postgres, themis-axum, themis-macros, themis-server |
strig-sync, strig-redis, strig-postgres, strig-axum, strig-macros, strig-server |
npm package themis-auth |
npm package strig |
themis-inspect |
strig-inspect |
.themis policy files |
.strig policy files |
THEMIS_* environment variables |
STRIG_* environment variables |
Replace use themis:: with use strig:: and rename the files and variables above. The repository moved to The-Hades-Organization/Strig.
License
MIT. The fonts embedded in strig-inspect are under the SIL Open Font License 1.1 (see crates/strig-inspect/assets/fonts).