@statewalker/webrun-biscuit 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +32 -33
  2. package/package.json +6 -3
package/README.md CHANGED
@@ -1,11 +1,14 @@
1
1
  # @statewalker/webrun-biscuit
2
2
 
3
+ ## What it is
4
+
3
5
  [Biscuit](https://biscuitsec.org) authorization tokens in pure TypeScript: the protobuf codec, the
4
6
  signature chain, the Datalog engine, the text parser, the authorizer and the token builder. No WASM,
5
- no Node built-ins in `src/`, two runtime dependencies. It runs wherever the rest of the wire runs —
6
- Node, browsers, Workers and Durable Objects.
7
+ no Node built-ins in `src/`, two runtime dependencies. It runs in Node, browsers, Workers and Durable
8
+ Objects. It mints, attenuates, seals, verifies and authorizes tokens, and evaluates Datalog with or
9
+ without a token.
7
10
 
8
- ## Why this package exists
11
+ ## Why it exists
9
12
 
10
13
  There is no other pure JS/TS Biscuit implementation. npm carries `@biscuit-auth/biscuit-wasm` — the
11
14
  Rust crate compiled to WebAssembly — and a web-components package built on it. Nothing else.
@@ -18,10 +21,10 @@ megabyte of WebAssembly to do so.
18
21
  So this is a reimplementation, and the entire testing strategy below exists because a reimplementation
19
22
  of a security primitive is only worth having if you can show it agrees with the original.
20
23
 
21
- ## Installing and calling it
24
+ ## How to use
22
25
 
23
26
  ```sh
24
- npm install @statewalker/webrun-biscuit
27
+ pnpm add @statewalker/webrun-biscuit
25
28
  ```
26
29
 
27
30
  Two runtime dependencies, both external to the published bundle:
@@ -49,6 +52,8 @@ const result = verified.authorize('operation("read"); allow if user("alice");');
49
52
  type system then prevents authorizing a token whose signature chain was never checked, which is the
50
53
  mistake worth designing against.
51
54
 
55
+ ## Examples
56
+
52
57
  ### Parameters: never splice a value into Datalog
53
58
 
54
59
  Anything that did not come from you — a user id, a request path, a role name —
@@ -75,9 +80,9 @@ block and authorizer facts) unless it says `trusting`, so an attenuation block c
75
80
  inject what it reads:
76
81
 
77
82
  ```ts
78
- const ev = verified.evaluate("allow if true;");
83
+ const ev = verified.evaluate('operation("read"); allow if true;');
79
84
  ev.result; // { kind: "ok", policy: 0 }
80
- ev.query("claim($s) <- subject($s)"); // [{ name: "claim", terms: [{ t: "str", v: "alice" }] }]
85
+ ev.query("claim($s) <- user($s)"); // [{ name: "claim", terms: [{ t: "str", v: "alice" }] }]
81
86
  ```
82
87
 
83
88
  Pass `null` instead of a token to decide from the authorizer's own facts and rules:
@@ -102,7 +107,7 @@ consume one list of signature checks, so neither can skip a check the other make
102
107
  const verified = await Biscuit.fromBase64(token).verifyAsync(root.publicKey);
103
108
  ```
104
109
 
105
- ### Examples
110
+ ### secp256r1, key ids and third-party blocks
106
111
 
107
112
  secp256r1 works the same way, with the algorithm passed at both ends:
108
113
 
@@ -121,7 +126,8 @@ import { peekRootKeyId } from "@statewalker/webrun-biscuit";
121
126
  const key = roots[peekRootKeyId(bytes) ?? 0];
122
127
  ```
123
128
 
124
- A third party can sign a block without ever holding the token:
129
+ A third party can sign a block without ever holding the token. The token must not be sealed —
130
+ `appendThirdParty` on a sealed token throws `BuilderError`:
125
131
 
126
132
  ```ts
127
133
  const request = Biscuit.fromBase64(token).thirdPartyRequest();
@@ -174,7 +180,7 @@ millisecond, which is unreachably tight for a cold JS engine; this one defaults
174
180
  exposed to untrusted tokens should lower it deliberately rather than treat the default as a
175
181
  denial-of-service bound. The limit is enforced where the work happens — inside the join, for rules,
176
182
  checks and policies alike — so it bounds a single combinatorial rule, not only the number of
177
- iterations (up to 0.2.0 it was read only between iterations).
183
+ iterations.
178
184
 
179
185
  ### The corpus is fetched, not vendored
180
186
 
@@ -198,11 +204,13 @@ not to change the verdict.
198
204
 
199
205
  ### A green suite is not the claim; a suite that can fail is
200
206
 
207
+ Run from `packages/webrun-biscuit` (or prefix with `pnpm --filter @statewalker/webrun-biscuit`):
208
+
201
209
  ```sh
202
- pnpm test # the main suite, 182 tests
203
- pnpm test:cross # against the reference implementation, 58 tests
210
+ pnpm test # the main suite (fetches the corpus first)
211
+ pnpm test:cross # against the reference implementation
204
212
  pnpm test:all # both
205
- pnpm mutate # inject 15 known defects, require the suite to catch each
213
+ pnpm mutate # inject known defects, require the suite to catch each
206
214
  pnpm build # dist/ plus declarations
207
215
  ```
208
216
 
@@ -221,13 +229,13 @@ pnpm build # dist/ plus declarations
221
229
  | `11-parameters` | `{name}` binding, hostile strings, unbound and unused parameters |
222
230
  | `12-evaluate` | queries, their scope, token-less evaluation, failed-check rule text |
223
231
 
224
- `scripts/mutate.mjs` is the check on all of it. It injects fifteen defects — a lenient protobuf decoder,
232
+ `scripts/mutate.mjs` is the check on all of it. It injects seventeen defects — a lenient protobuf decoder,
225
233
  wrapping i64 arithmetic, `check all` degraded to `check if`, a universally trusting authorizer, `deny`
226
234
  treated as `allow`, unchecked seal signatures, an async verifier that ignores WebCrypto's verdict, a
227
235
  query that can read an attenuation block's facts, silently ignored parameters, zero-term predicates, non-ASCII names, missing version bounds,
228
236
  and more — and requires each to break at least one test. A surviving mutation is a hole in the tests,
229
- not a success. All fifteen are caught. Two of them were **not** caught when the harness was first written: nothing verified a forged
230
- seal signature, and nothing exercised a matching `deny` policy.
237
+ not a success. All of them are caught; the forged-seal and matching-`deny` mutations are the ones a
238
+ suite without `06-builder`'s negative cases would miss.
231
239
 
232
240
  Each mutation's `find` string is a literal excerpt of the source, and must match exactly once. That is
233
241
  why a reformat of `src/` makes the harness fail loudly rather than quietly stop testing anything.
@@ -254,7 +262,7 @@ because uniform sampling produces mostly `noMatchingPolicy`, where the engine ba
254
262
  That suite is kept out of `pnpm test` on purpose, and it skips gracefully when the reference is not
255
263
  installed, so it never becomes a hard dependency of the main suite.
256
264
 
257
- ## What will surprise you
265
+ ### What will surprise you
258
266
 
259
267
  **The reference build's first `authorize` call reports a timeout that is not real.** Every fresh
260
268
  Authorizer reports a `RunLimit` timeout on its first call even with a 60-second budget; later calls on
@@ -275,25 +283,23 @@ reliable accessor.
275
283
  walking up to `node_modules`, reads the `.wasm`, and supplies its imports by hand. Running Node with
276
284
  `--experimental-wasm-modules` does work, but needing a flag for `pnpm test` is worse.
277
285
 
278
- **A failed corpus download used to poison `samples/` permanently.** `samples.json` was written before
279
- the tokens it names while the "already fetched" guard checked only `samples.json`, so an interrupted
280
- fetch left a directory that every later run skipped as complete — and the tests then failed on missing
281
- files forever. The manifest is now written last, and the guard checks every file it names. Downloads
282
- are also pooled at six with retries, because `raw.githubusercontent.com` resets connections when
286
+ **An interrupted corpus download must not look complete.** `scripts/fetch-samples.mjs` writes the
287
+ `samples.json` manifest last, and its "already fetched" guard checks every file the manifest names;
288
+ otherwise an interrupted fetch would leave a directory every later run skips, and the tests would fail
289
+ on missing files. Downloads are pooled at six with retries, because `raw.githubusercontent.com` resets connections when
283
290
  several dozen requests arrive at once and surfaces it as a bare `TypeError: fetch failed`.
284
291
 
285
292
  **`pnpm test` fetches the corpus explicitly, not through `pretest`.** pnpm does not run `pre`/`post`
286
293
  scripts by default, so a `pretest` hook would silently never fire and the suite would fail on a clean
287
294
  checkout with missing samples.
288
295
 
289
- ## Known deviations from the reference
296
+ ### Known deviations from the reference
290
297
 
291
298
  **Names follow the reference parser, not the specification's prose.** A predicate or variable name
292
299
  is one or more of `[A-Za-z0-9_:]`, ASCII, with any of them first — so `_m`, `1a` and `::` are names
293
300
  and `ärger` is not — and a predicate takes at least one term. That is what
294
301
  `@biscuit-auth/biscuit-wasm` accepts; `tests/grammar-cases.ts` records it and `05-grammar` re-asks the
295
- reference, including for generated names. Versions up to 0.2.0 required a Unicode letter first and
296
- accepted `f()`.
302
+ reference, including for generated names.
297
303
 
298
304
  **Regex uses JS `RegExp`, not RE2.** Every corpus pattern matches, but backreferences and lookaround
299
305
  are accepted where Rust would reject them. Documented, not enforced.
@@ -319,13 +325,6 @@ third-party flow works and is tested, but the cross-implementation one does not.
319
325
  Semi-naive evaluation is not implemented either. The fixpoint re-derives every fact each round; the
320
326
  index makes that cheap at token scale, but not at request scale with large fact sets.
321
327
 
322
- ## Design notes
323
-
324
- `docs/webrun-biscuit/` in this repository carries the working documents from the implementation
325
- sessions: how the reference actually works, why `mapbox/pbf` was rejected for the protobuf layer, what
326
- the red-green cycle caught, the corpus findings, and what it took to make random differential testing
327
- actually detect a defect.
328
-
329
328
  ## License
330
329
 
331
- MIT — see the repository `LICENSE`.
330
+ MIT
package/package.json CHANGED
@@ -1,10 +1,13 @@
1
1
  {
2
2
  "name": "@statewalker/webrun-biscuit",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Biscuit authorization tokens in pure TypeScript: proto2 codec, Ed25519 and secp256r1 signature chain, Datalog engine, parser, authorizer and builder. No WASM, no Node built-ins — runs in Node, browsers, Workers and Durable Objects.",
7
- "homepage": "https://github.com/statewalker/webrun-wire",
7
+ "bugs": {
8
+ "url": "https://github.com/statewalker/httpeers/issues"
9
+ },
10
+ "homepage": "https://github.com/statewalker/httpeers/tree/main/packages/webrun-biscuit#readme",
8
11
  "author": {
9
12
  "name": "Mikhail Kotelnikov",
10
13
  "email": "mikhail.kotelnikov@gmail.com"
@@ -12,7 +15,7 @@
12
15
  "license": "MIT",
13
16
  "repository": {
14
17
  "type": "git",
15
- "url": "git@github.com:statewalker/webrun-wire.git",
18
+ "url": "git+https://github.com/statewalker/httpeers.git",
16
19
  "directory": "packages/webrun-biscuit"
17
20
  },
18
21
  "exports": {