@statewalker/webrun-biscuit 0.2.0 → 0.2.2

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022-2026 statewalker
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -172,7 +172,9 @@ agnostic; tests may use Node APIs, the library may not.
172
172
  **Default run limits are looser than the reference.** The reference defaults `max_time` to 1
173
173
  millisecond, which is unreachably tight for a cold JS engine; this one defaults to 1 second. A caller
174
174
  exposed to untrusted tokens should lower it deliberately rather than treat the default as a
175
- denial-of-service bound.
175
+ denial-of-service bound. The limit is enforced where the work happens — inside the join, for rules,
176
+ 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).
176
178
 
177
179
  ### The corpus is fetched, not vendored
178
180
 
@@ -197,10 +199,10 @@ not to change the verdict.
197
199
  ### A green suite is not the claim; a suite that can fail is
198
200
 
199
201
  ```sh
200
- pnpm test # the main suite, 180 tests
201
- pnpm test:cross # against the reference implementation, 56 tests
202
+ pnpm test # the main suite, 182 tests
203
+ pnpm test:cross # against the reference implementation, 58 tests
202
204
  pnpm test:all # both
203
- pnpm mutate # inject 13 known defects, require the suite to catch each
205
+ pnpm mutate # inject 15 known defects, require the suite to catch each
204
206
  pnpm build # dist/ plus declarations
205
207
  ```
206
208
 
@@ -209,7 +211,7 @@ pnpm build # dist/ plus declarations
209
211
  | `01-proto` | byte-exact round-trip of all 38 sample tokens, i64 extremes, strict rejection |
210
212
  | `02-crypto` | signature chain outcomes and revocation ids for all samples |
211
213
  | `03-datalog` | engine unit tests ported from the Rust `datalog` module |
212
- | `04-parser` | terms, precedence, closures, scopes, predicate/expression disambiguation |
214
+ | `04-parser` | terms, precedence, closures, scopes, predicate/expression disambiguation, the reference's name and arity rules |
213
215
  | `05-conformance` | the official `samples.json` corpus — 38 tokens, 50 validations |
214
216
  | `06-builder` | write path, corpus rebuilt from source, forged seals, deny policies |
215
217
  | `07-api` | base64 and the public facade |
@@ -219,12 +221,12 @@ pnpm build # dist/ plus declarations
219
221
  | `11-parameters` | `{name}` binding, hostile strings, unbound and unused parameters |
220
222
  | `12-evaluate` | queries, their scope, token-less evaluation, failed-check rule text |
221
223
 
222
- `scripts/mutate.mjs` is the check on all of it. It injects thirteen defects — a lenient protobuf decoder,
224
+ `scripts/mutate.mjs` is the check on all of it. It injects fifteen defects — a lenient protobuf decoder,
223
225
  wrapping i64 arithmetic, `check all` degraded to `check if`, a universally trusting authorizer, `deny`
224
226
  treated as `allow`, unchecked seal signatures, an async verifier that ignores WebCrypto's verdict, a
225
- query that can read an attenuation block's facts, silently ignored parameters, missing version bounds,
227
+ query that can read an attenuation block's facts, silently ignored parameters, zero-term predicates, non-ASCII names, missing version bounds,
226
228
  and more — and requires each to break at least one test. A surviving mutation is a hole in the tests,
227
- not a success. All thirteen are caught. Two of them were **not** caught when the harness was first written: nothing verified a forged
229
+ not a success. All fifteen are caught. Two of them were **not** caught when the harness was first written: nothing verified a forged
228
230
  seal signature, and nothing exercised a matching `deny` policy.
229
231
 
230
232
  Each mutation's `find` string is a literal excerpt of the source, and must match exactly once. That is
@@ -242,6 +244,7 @@ is genuinely upstream, not a second reading of the same spec.
242
244
  | `02-ts-to-native` | we mint, the reference reads |
243
245
  | `03-interop-chains` | blocks appended alternately by both, sealing honoured across the boundary, base64 interop, agreement on the stamped Datalog version |
244
246
  | `04-random-differential` | generated programs, both directions, both algorithms — the reference is the oracle |
247
+ | `05-grammar` | predicate and variable names, and arity — the recorded table and generated names, re-asked of the reference |
245
248
 
246
249
  Nothing in `04` hard-codes the expected answer: each generated program is authorized by both
247
250
  implementations and the verdicts compared, so a disagreement is a finding either way. Programs are
@@ -285,6 +288,13 @@ checkout with missing samples.
285
288
 
286
289
  ## Known deviations from the reference
287
290
 
291
+ **Names follow the reference parser, not the specification's prose.** A predicate or variable name
292
+ is one or more of `[A-Za-z0-9_:]`, ASCII, with any of them first — so `_m`, `1a` and `::` are names
293
+ and `ärger` is not — and a predicate takes at least one term. That is what
294
+ `@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()`.
297
+
288
298
  **Regex uses JS `RegExp`, not RE2.** Every corpus pattern matches, but backreferences and lookaround
289
299
  are accepted where Rust would reject them. Documented, not enforced.
290
300
 
@@ -1 +1 @@
1
- {"version":3,"file":"authorizer.d.ts","sourceRoot":"","sources":["../src/authorizer.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAEL,KAAK,KAAK,EAGV,KAAK,QAAQ,EACb,KAAK,IAAI,EAKT,KAAK,SAAS,EACd,KAAK,IAAI,EACT,KAAK,SAAS,EACd,KAAK,KAAK,EAKX,MAAM,cAAc,CAAC;A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,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,YAAY;IAC3B,uDAAuD;IACvD,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,iFAAiF;AACjF,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,MAAM,EAAE,mBAAmB,CAAC;IACrC;;;;;OAKG;IACH,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,SAAS,EAAE,CAAC;IACzD,8EAA8E;IAC9E,QAAQ,IAAI,aAAa,CAAC;CAC3B;AAED,wBAAgB,SAAS,CACvB,KAAK,EAAE,WAAW,GAAG,IAAI,EACzB,aAAa,EAAE,MAAM,EACrB,OAAO,GAAE,gBAAqB,GAC7B,mBAAmB,CAErB;AAID;yCACyC;AACzC,wBAAgB,iBAAiB,CAC/B,KAAK,EAAE,WAAW,GAAG,IAAI,EACzB,aAAa,EAAE,MAAM,EACrB,OAAO,GAAE,gBAAqB,GAC7B,gBAAgB,CAGlB;AAID;;;GAGG;AACH,wBAAgB,QAAQ,CACtB,WAAW,EAAE,WAAW,GAAG,IAAI,EAC/B,aAAa,EAAE,MAAM,EACrB,OAAO,GAAE,gBAAqB,GAC7B,UAAU,CAqMZ"}
package/dist/builder.d.ts CHANGED
@@ -5,8 +5,8 @@
5
5
  */
6
6
  import { generateKeypair } from "./crypto.js";
7
7
  import type { Check, Predicate, Rule, Scope } from "./datalog.js";
8
- import { type BlockMsg, type PublicKeyMsg } from "./proto.js";
9
8
  import type { Params } from "./parser.js";
9
+ import { type BlockMsg, type PublicKeyMsg } from "./proto.js";
10
10
  export declare class BuilderError extends Error {
11
11
  }
12
12
  export interface Keypair {
@@ -1 +1 @@
1
- {"version":3,"file":"builder.d.ts","sourceRoot":"","sources":["../src/builder.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAIL,eAAe,EAIhB,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAE,KAAK,EAAM,SAAS,EAAE,IAA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B,WAAW,CAAC,EAAE,UAAU,GACvB,UAAU,CAEZ;AAED,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,UAAU,EAChB,OAAO,EAAE,YAAY,EACrB,OAAO,EAAE,MAAM,GACd,UAAU,CAWZ;AAED,wBAAgB,cAAc,CAC5B,IAAI,EAAE,UAAU,EAChB,OAAO,EAAE,YAAY,EACrB,WAAW,EAAE,UAAU,GAAG,SAAS,EACnC,iBAAiB,EAAE,UAAU,EAC7B,OAAO,EAAE,MAAM,GACd,UAAU,CAcZ;AAED,wBAAgB,iBAAiB,CAC/B,IAAI,EAAE,UAAU,EAChB,iBAAiB,EAAE,UAAU,EAC7B,OAAO,EAAE,MAAM,GACd,UAAU,CASZ;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,WAAW,GAAG,UAAU,CAE5D;AAED,wBAAgB,IAAI,CAAC,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,GAAG,UAAU,CAK3F;AAED,8CAA8C;AAC9C,wBAAgB,eAAe,CAAC,SAAS,GAAE,CAAC,GAAG,CAAK,GAAG;IACrD,SAAS,EAAE,UAAU,CAAC;IACtB,SAAS,EAAE,UAAU,CAAC;CACvB,CAIA;AAED,OAAO,EAAE,UAAU,EAAE,CAAC;AA2FtB,0EAA0E;AAC1E,wBAAgB,WAAW,CAAC,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,UAAU,EAAE,aAAa,SAAI,GAAG,IAAI,CAOjG;AAED;;;;;GAKG;AACH,wBAAsB,gBAAgB,CACpC,KAAK,EAAE,UAAU,EACjB,aAAa,EAAE,UAAU,EACzB,aAAa,SAAI,GAChB,OAAO,CAAC,IAAI,CAAC,CAWf;AA6FD,gEAAgE;AAChE,wBAAgB,aAAa,CAAC,KAAK,EAAE,UAAU,GAAG,UAAU,EAAE,CAE7D"}
package/dist/datalog.d.ts CHANGED
@@ -187,6 +187,18 @@ export declare class World {
187
187
  iterations: number;
188
188
  private generation;
189
189
  private indexCache;
190
+ /** wall-clock budget of the evaluation in progress; Infinity outside one */
191
+ private deadline;
192
+ private steps;
193
+ /**
194
+ * The time limit, enforced where the work is. Checking only between
195
+ * iterations let one combinatorial rule — or one check that derives nothing —
196
+ * run for seconds whatever `maxTimeMs` said. Polled every 1024 candidate
197
+ * facts, so the clock costs nothing measurable.
198
+ */
199
+ private readonly tick;
200
+ /** Close the budget: queries made after an evaluation are not charged against it. */
201
+ endBudget(): void;
190
202
  addFact(origin: Origin, fact: Fact): void;
191
203
  addRule(origin: number, trusted: TrustedOrigins, rule: Rule): void;
192
204
  factCount(): number;
@@ -1 +1 @@
1
- {"version":3,"file":"datalog.d.ts","sourceRoot":"","sources":["../src/datalog.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,eAAO,MAAM,UAAU,aAAa,CAAC;AAIrC,MAAM,MAAM,MAAM,GAAG;IAAE,CAAC,EAAE,KAAK,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,CAAC,EAAE,KAAK,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAEvE,MAAM,MAAM,IAAI,GACZ;IAAE,CAAC,EAAE,KAAK,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,GACvB;IAAE,CAAC,EAAE,KAAK,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,GACvB;IAAE,CAAC,EAAE,KAAK,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,GACvB;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,GACxB;IAAE,CAAC,EAAE,OAAO,CAAC;IAAC,CAAC,EAAE,UAAU,CAAA;CAAE,GAC7B;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,OAAO,CAAA;CAAE,GACzB;IAAE,CAAC,EAAE,KAAK,CAAC;IAAC,CAAC,EAAE,IAAI,EAAE,CAAA;CAAE,G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MAAM,EAAE,OAAO,EAAE,cAAc,EAAE,IAAI,EAAE,IAAI,GAAG,IAAI,CAEjE;IAED,SAAS,IAAI,MAAM,CAIlB;IAED,OAAO,CAAC,OAAO;IAkBf,iEAAiE;IACjE,GAAG,CAAC,MAAM,GAAE,SAA0B,GAAG,IAAI,CAqC5C;IAEA,KAAK,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC,CA4BlF;IAED,0EAA0E;IAC1E,KAAK,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,cAAc,GAAG,SAAS,EAAE,CAOtD;IAED,kEAAkE;IAClE,UAAU,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc,GAAG,OAAO,CAGvE;IAED,2EAA2E;IAC3E,aAAa,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,cAAc,GAAG,OAAO,CAkB1D;CACF"}
package/dist/index.d.ts CHANGED
@@ -13,7 +13,7 @@ export * from "./base64.js";
13
13
  export * from "./builder.js";
14
14
  export { SignatureError } from "./crypto.js";
15
15
  export { ExecutionError, type ExternFn, type Predicate, type RunLimits, type Term, } from "./datalog.js";
16
- export { type ParamValue, type Params, ParseError } from "./parser.js";
16
+ export { type Params, type ParamValue, ParseError } from "./parser.js";
17
17
  export { ProtoError } from "./proto.js";
18
18
  export * from "./version.js";
19
19
  /** An unverified token: it can be attenuated and re-serialized. */
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EACL,KAAK,mBAAmB,EACxB,KAAK,gBAAgB,EAErB,KAAK,UAAU,EAEf,KAAK,WAAW,EAGjB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAGL,KAAK,YAAY,EAEjB,eAA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,CAEjF;IACD,mEAAmE;IACnE,QAAQ,CAAC,cAAc,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,UAAU,CAEvE;CACF;AAED,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,CAAC"}
package/dist/index.js CHANGED
@@ -726,7 +726,7 @@ function variablesOf(rule) {
726
726
  return out;
727
727
  }
728
728
  /** backtracking join over the rule body, mirroring Rust's CombineIt */
729
- function* combine(predicates, facts, bindings, variables) {
729
+ function* combine(predicates, facts, bindings, variables, tick) {
730
730
  if (predicates.length === 0) {
731
731
  for (const v of variables) if (!bindings.has(v)) return;
732
732
  yield [Origin.of(), new Map(bindings)];
@@ -734,6 +734,7 @@ function* combine(predicates, facts, bindings, variables) {
734
734
  }
735
735
  const [head, ...rest] = predicates;
736
736
  for (const [origin, fact] of facts.get(`${head.name}/${head.terms.length}`) ?? []) {
737
+ tick();
737
738
  if (!matchPredicate(head, fact.predicate)) continue;
738
739
  const next = new Map(bindings);
739
740
  let ok = true;
@@ -748,7 +749,7 @@ function* combine(predicates, facts, bindings, variables) {
748
749
  }
749
750
  }
750
751
  if (!ok) continue;
751
- for (const [subOrigin, result] of combine(rest, facts, next, variables)) yield [subOrigin.union(origin), result];
752
+ for (const [subOrigin, result] of combine(rest, facts, next, variables, tick)) yield [subOrigin.union(origin), result];
752
753
  }
753
754
  }
754
755
  var World = class {
@@ -759,6 +760,22 @@ var World = class {
759
760
  iterations = 0;
760
761
  generation = 0;
761
762
  indexCache = /* @__PURE__ */ new Map();
763
+ /** wall-clock budget of the evaluation in progress; Infinity outside one */
764
+ deadline = Number.POSITIVE_INFINITY;
765
+ steps = 0;
766
+ /**
767
+ * The time limit, enforced where the work is. Checking only between
768
+ * iterations let one combinatorial rule — or one check that derives nothing —
769
+ * run for seconds whatever `maxTimeMs` said. Polled every 1024 candidate
770
+ * facts, so the clock costs nothing measurable.
771
+ */
772
+ tick = () => {
773
+ if ((++this.steps & 1023) === 0 && Date.now() >= this.deadline) throw new ExecutionError("Timeout");
774
+ };
775
+ /** Close the budget: queries made after an evaluation are not charged against it. */
776
+ endBudget() {
777
+ this.deadline = Number.POSITIVE_INFINITY;
778
+ }
762
779
  addFact(origin, fact) {
763
780
  let bucket = this.facts.get(origin.key);
764
781
  if (!bucket) {
@@ -808,12 +825,22 @@ var World = class {
808
825
  /** naive fixpoint: apply every rule until no new fact appears */
809
826
  run(limits = DEFAULT_LIMITS) {
810
827
  const deadline = Date.now() + limits.maxTimeMs;
828
+ this.deadline = deadline;
811
829
  let index = 0;
812
830
  for (;;) {
813
831
  const generated = [];
832
+ const fresh = /* @__PURE__ */ new Set();
833
+ const base = this.factCount();
834
+ const lastIteration = index + 1 === limits.maxIterations;
814
835
  for (const { origin, trusted, rule } of this.rules) {
815
836
  const facts = this.visible(trusted);
816
- for (const [o, f] of this.apply(rule, facts, origin)) generated.push([o, f]);
837
+ for (const [o, f] of this.apply(rule, facts, origin)) {
838
+ generated.push([o, f]);
839
+ const key = factKey(f);
840
+ if (this.facts.get(o.key)?.items.has(key)) continue;
841
+ fresh.add(`${o.key}|${key}`);
842
+ if (!lastIteration && base + fresh.size >= limits.maxFacts) throw new ExecutionError("TooManyFacts");
843
+ }
817
844
  }
818
845
  const before = this.factCount();
819
846
  for (const [o, f] of generated) this.addFact(o, f);
@@ -827,7 +854,7 @@ var World = class {
827
854
  }
828
855
  *apply(rule, facts, ruleOrigin) {
829
856
  const variables = variablesOf(rule);
830
- for (const [origin, bindings] of combine(rule.body, facts, /* @__PURE__ */ new Map(), variables)) {
857
+ for (const [origin, bindings] of combine(rule.body, facts, /* @__PURE__ */ new Map(), variables, this.tick)) {
831
858
  let pass = true;
832
859
  for (const ops of rule.expressions) {
833
860
  const res = evaluateExpression(ops, bindings, this.externs);
@@ -873,7 +900,7 @@ var World = class {
873
900
  queryMatchAll(rule, trusted) {
874
901
  const variables = variablesOf(rule);
875
902
  let found = false;
876
- for (const [, bindings] of combine(rule.body, this.visible(trusted), /* @__PURE__ */ new Map(), variables)) {
903
+ for (const [, bindings] of combine(rule.body, this.visible(trusted), /* @__PURE__ */ new Map(), variables, this.tick)) {
877
904
  found = true;
878
905
  for (const ops of rule.expressions) {
879
906
  const res = evaluateExpression(ops, bindings, this.externs);
@@ -942,8 +969,13 @@ function paramTerm(name, value) {
942
969
  }
943
970
  /** `{name}` in term position; `{true}`, `{false}` and `{null}` stay one-element sets */
944
971
  const PARAMETER = /^\{\s*([A-Za-z_][A-Za-z0-9_]*)\s*\}/;
945
- const NAME_START = /[\p{L}]/u;
946
- const NAME_CHAR = /[\p{L}\p{N}_:]/u;
972
+ /**
973
+ * A predicate or variable name is one or more of `[A-Za-z0-9_:]`, and any of them
974
+ * may come first — ASCII only. That is what the reference parser accepts, measured
975
+ * rather than read from the specification (`tests/grammar-cases.ts`): `_m`, `1a`
976
+ * and `::` are names, `ärger` is not.
977
+ */
978
+ const NAME_CHAR = /[A-Za-z0-9_:]/;
947
979
  var Parser = class {
948
980
  src;
949
981
  params;
@@ -1014,9 +1046,8 @@ var Parser = class {
1014
1046
  this.i += k.length;
1015
1047
  return true;
1016
1048
  }
1017
- /** variable names, unlike predicate names, may start with a digit ($0) */
1049
+ /** the name directly after `$` — `$ x` is not a variable, as in the reference */
1018
1050
  variableName() {
1019
- this.ws();
1020
1051
  const start = this.i;
1021
1052
  while (this.i < this.src.length && NAME_CHAR.test(this.src[this.i])) this.i++;
1022
1053
  if (this.i === start) throw new ParseError(`expected a variable name at offset ${this.i}`);
@@ -1025,9 +1056,8 @@ var Parser = class {
1025
1056
  name() {
1026
1057
  this.ws();
1027
1058
  const start = this.i;
1028
- if (this.i >= this.src.length || !NAME_START.test(this.src[this.i])) throw new ParseError(`expected a name at offset ${this.i}`);
1029
- this.i++;
1030
1059
  while (this.i < this.src.length && NAME_CHAR.test(this.src[this.i])) this.i++;
1060
+ if (this.i === start) throw new ParseError(`expected a name at offset ${this.i}`);
1031
1061
  return this.src.slice(start, this.i);
1032
1062
  }
1033
1063
  string() {
@@ -1200,8 +1230,9 @@ var Parser = class {
1200
1230
  predicate(allowVariables = true) {
1201
1231
  const name = this.name();
1202
1232
  this.expect("(");
1233
+ if (this.peek(")")) throw new ParseError(`predicate ${name} takes at least one term`);
1203
1234
  const terms = [];
1204
- if (!this.peek(")")) do
1235
+ do
1205
1236
  terms.push(this.term(allowVariables));
1206
1237
  while (this.eat(","));
1207
1238
  this.expect(")");
@@ -1577,7 +1608,7 @@ var Parser = class {
1577
1608
  const save = this.i;
1578
1609
  try {
1579
1610
  this.ws();
1580
- if (!NAME_START.test(this.src[this.i] ?? "")) return null;
1611
+ if (!NAME_CHAR.test(this.src[this.i] ?? "")) return null;
1581
1612
  const p = this.predicate();
1582
1613
  this.ws();
1583
1614
  const rest = this.src.slice(this.i);
@@ -2904,7 +2935,11 @@ function evaluate(loadedToken, authorizerSrc, options = {}) {
2904
2935
  };
2905
2936
  };
2906
2937
  let failure;
2907
- const settled = (result) => ({
2938
+ const settled = (result) => {
2939
+ world.endBudget();
2940
+ return evaluation(result);
2941
+ };
2942
+ const evaluation = (result) => ({
2908
2943
  result,
2909
2944
  snapshot,
2910
2945
  query(src, queryOptions = {}) {
package/dist/parser.d.ts CHANGED
@@ -48,7 +48,7 @@ export declare class Parser {
48
48
  private expect;
49
49
  /** keyword: matches only if not followed by a name character */
50
50
  private keyword;
51
- /** variable names, unlike predicate names, may start with a digit ($0) */
51
+ /** the name directly after `$` — `$ x` is not a variable, as in the reference */
52
52
  private variableName;
53
53
  private name;
54
54
  private string;
@@ -1 +1 @@
1
- {"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../src/parser.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAEL,KAAK,KAAK,EAEV,KAAK,IAAI,EAGT,KAAK,EAAE,EACP,KAAK,SAAS,EACd,KAAK,IAAI,EACT,KAAK,KAAK,EACV,KAAK,IAAI,EAEV,MAAM,cAAc,CAAC;AAEtB,qBAAa,UAAW,SAAQ,KAAK;CAAG;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AAO,CAAC,QAAQ;IA0BhB,0EAA0E;IAC1E,OAAO,CAAC,YAAY;IAiBpB,OAAO,CAAC,OAAO;IASf,SAAS,IAAI,SAAS,CAgCrB;IAED,KAAK,IAAI,SAAS,EAAE,CAQnB;CACF;AAED,eAAO,MAAM,KAAK,QAAS,MAAM,WAAW,MAAM,KAAG,SAAS,EAClB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@statewalker/webrun-biscuit",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
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.",
@@ -17,6 +17,7 @@
17
17
  },
18
18
  "exports": {
19
19
  ".": {
20
+ "source": "./src/index.ts",
20
21
  "types": "./dist/index.d.ts",
21
22
  "import": "./dist/index.js"
22
23
  }
@@ -32,9 +33,9 @@
32
33
  "devDependencies": {
33
34
  "@biscuit-auth/biscuit-wasm": "^0.6.0",
34
35
  "rimraf": "^6.1.3",
35
- "rolldown": "^1.2.4",
36
+ "rolldown": "^1.2.12",
36
37
  "typescript": "^7.0.2",
37
- "vitest": "^4.1.10"
38
+ "vitest": "^5.0.3"
38
39
  },
39
40
  "sideEffects": false,
40
41
  "publishConfig": {
package/src/authorizer.ts CHANGED
@@ -396,7 +396,9 @@ export function parseAuthorizer(src: string, params?: Params): AuthorizerCode {
396
396
  const statements: Statement[] = parser.parse();
397
397
  const unused = parser.unusedParameters();
398
398
  if (unused.length > 0)
399
- throw new ParseError(`unused parameter${unused.length > 1 ? "s" : ""} {${unused.join("}, {")}}`);
399
+ throw new ParseError(
400
+ `unused parameter${unused.length > 1 ? "s" : ""} {${unused.join("}, {")}}`,
401
+ );
400
402
  out.varNames = parser.variableNames();
401
403
  for (const st of statements) {
402
404
  if (st.k === "fact") out.facts.push(st.fact);
@@ -562,7 +564,11 @@ export function evaluate(
562
564
  };
563
565
 
564
566
  let failure: ExecutionError | undefined;
565
- const settled = (result: AuthorizationResult): Evaluation => ({
567
+ const settled = (result: AuthorizationResult): Evaluation => {
568
+ world.endBudget();
569
+ return evaluation(result);
570
+ };
571
+ const evaluation = (result: AuthorizationResult): Evaluation => ({
566
572
  result,
567
573
  snapshot,
568
574
  query(src: string, queryOptions: QueryOptions = {}): Predicate[] {
@@ -636,7 +642,12 @@ export function evaluate(
636
642
  for (let id = 1; id < token.blocks.length; id++) {
637
643
  token.blocks[id].checks.forEach((check, j) => {
638
644
  if (!runCheck(check, id, blockTrusted[id]))
639
- errors.push({ source: "block", blockId: id, checkId: j, rule: printCheck(check, name(id)) });
645
+ errors.push({
646
+ source: "block",
647
+ blockId: id,
648
+ checkId: j,
649
+ rule: printCheck(check, name(id)),
650
+ });
640
651
  });
641
652
  }
642
653
 
package/src/builder.ts CHANGED
@@ -15,6 +15,7 @@ import {
15
15
  sign,
16
16
  } from "./crypto.js";
17
17
  import type { Check, Op, Predicate, Rule, Scope, Term } from "./datalog.js";
18
+ import type { Params } from "./parser.js";
18
19
  import {
19
20
  type BiscuitMsg,
20
21
  type BlockMsg,
@@ -30,7 +31,6 @@ import {
30
31
  type ScopeMsg,
31
32
  type TermMsg,
32
33
  } from "./proto.js";
33
- import type { Params } from "./parser.js";
34
34
  import { DATALOG_3_2, requiredVersion } from "./version.js";
35
35
 
36
36
  const SIGNATURE_VERSION = 1;
package/src/crypto.ts CHANGED
@@ -275,7 +275,12 @@ type Subtle = {
275
275
  extractable: boolean,
276
276
  usages: string[],
277
277
  ): Promise<unknown>;
278
- verify(algorithm: string, key: unknown, signature: Uint8Array, data: Uint8Array): Promise<boolean>;
278
+ verify(
279
+ algorithm: string,
280
+ key: unknown,
281
+ signature: Uint8Array,
282
+ data: Uint8Array,
283
+ ): Promise<boolean>;
279
284
  exportKey(format: "jwk", key: unknown): Promise<{ x?: string }>;
280
285
  };
281
286
 
@@ -332,7 +337,10 @@ async function verifySignatureAsync(
332
337
  }
333
338
  }
334
339
 
335
- async function publicKeyFromSecretAsync(secret: Uint8Array, algorithm: number): Promise<Uint8Array> {
340
+ async function publicKeyFromSecretAsync(
341
+ secret: Uint8Array,
342
+ algorithm: number,
343
+ ): Promise<Uint8Array> {
336
344
  if (algorithm !== ALG_ED25519 || secret.length !== 32 || !subtle || !(await nativeEd25519()))
337
345
  return publicKeyFromSecret(secret, algorithm);
338
346
  try {
package/src/datalog.ts CHANGED
@@ -570,6 +570,8 @@ function* combine(
570
570
  facts: FactIndex,
571
571
  bindings: Bindings,
572
572
  variables: Set<number>,
573
+ /** called once per candidate fact: the join is where a budget has to bite */
574
+ tick: () => void,
573
575
  ): Generator<[Origin, Bindings]> {
574
576
  if (predicates.length === 0) {
575
577
  for (const v of variables) if (!bindings.has(v)) return;
@@ -578,6 +580,7 @@ function* combine(
578
580
  }
579
581
  const [head, ...rest] = predicates;
580
582
  for (const [origin, fact] of facts.get(`${head.name}/${head.terms.length}`) ?? []) {
583
+ tick();
581
584
  if (!matchPredicate(head, fact.predicate)) continue;
582
585
  const next = new Map(bindings);
583
586
  let ok = true;
@@ -592,7 +595,7 @@ function* combine(
592
595
  }
593
596
  }
594
597
  if (!ok) continue;
595
- for (const [subOrigin, result] of combine(rest, facts, next, variables))
598
+ for (const [subOrigin, result] of combine(rest, facts, next, variables, tick))
596
599
  yield [subOrigin.union(origin), result];
597
600
  }
598
601
  }
@@ -605,6 +608,25 @@ export class World {
605
608
  iterations = 0;
606
609
  private generation = 0;
607
610
  private indexCache = new Map<string, { generation: number; index: FactIndex }>();
611
+ /** wall-clock budget of the evaluation in progress; Infinity outside one */
612
+ private deadline = Number.POSITIVE_INFINITY;
613
+ private steps = 0;
614
+
615
+ /**
616
+ * The time limit, enforced where the work is. Checking only between
617
+ * iterations let one combinatorial rule — or one check that derives nothing —
618
+ * run for seconds whatever `maxTimeMs` said. Polled every 1024 candidate
619
+ * facts, so the clock costs nothing measurable.
620
+ */
621
+ private readonly tick = (): void => {
622
+ if ((++this.steps & 1023) === 0 && Date.now() >= this.deadline)
623
+ throw new ExecutionError("Timeout");
624
+ };
625
+
626
+ /** Close the budget: queries made after an evaluation are not charged against it. */
627
+ endBudget(): void {
628
+ this.deadline = Number.POSITIVE_INFINITY;
629
+ }
608
630
 
609
631
  addFact(origin: Origin, fact: Fact): void {
610
632
  let bucket = this.facts.get(origin.key);
@@ -649,13 +671,31 @@ export class World {
649
671
 
650
672
  /** naive fixpoint: apply every rule until no new fact appears */
651
673
  run(limits: RunLimits = DEFAULT_LIMITS): void {
674
+ // The budget stays open after `run` returns, so the checks and policies the
675
+ // authorizer evaluates next are bounded by it too; `endBudget` closes it.
652
676
  const deadline = Date.now() + limits.maxTimeMs;
677
+ this.deadline = deadline;
653
678
  let index = 0;
654
679
  for (;;) {
655
680
  const generated: [Origin, Fact][] = [];
681
+ // Distinct facts this iteration adds. Counted as they are derived, so
682
+ // `maxFacts` fires the moment the world WOULD exceed it — the verdict the
683
+ // end-of-iteration check reaches, without enumerating the rest of the
684
+ // product first. On the last permitted iteration the reference reports
685
+ // TooManyIterations ahead of TooManyFacts, so the early exit stands aside.
686
+ const fresh = new Set<string>();
687
+ const base = this.factCount();
688
+ const lastIteration = index + 1 === limits.maxIterations;
656
689
  for (const { origin, trusted, rule } of this.rules) {
657
690
  const facts = this.visible(trusted);
658
- for (const [o, f] of this.apply(rule, facts, origin)) generated.push([o, f]);
691
+ for (const [o, f] of this.apply(rule, facts, origin)) {
692
+ generated.push([o, f]);
693
+ const key = factKey(f);
694
+ if (this.facts.get(o.key)?.items.has(key)) continue;
695
+ fresh.add(`${o.key}|${key}`);
696
+ if (!lastIteration && base + fresh.size >= limits.maxFacts)
697
+ throw new ExecutionError("TooManyFacts");
698
+ }
659
699
  }
660
700
  const before = this.factCount();
661
701
  for (const [o, f] of generated) this.addFact(o, f);
@@ -671,7 +711,7 @@ export class World {
671
711
 
672
712
  *apply(rule: Rule, facts: FactIndex, ruleOrigin: number): Generator<[Origin, Fact]> {
673
713
  const variables = variablesOf(rule);
674
- for (const [origin, bindings] of combine(rule.body, facts, new Map(), variables)) {
714
+ for (const [origin, bindings] of combine(rule.body, facts, new Map(), variables, this.tick)) {
675
715
  let pass = true;
676
716
  for (const ops of rule.expressions) {
677
717
  const res = evaluateExpression(ops, bindings, this.externs);
@@ -719,7 +759,13 @@ export class World {
719
759
  queryMatchAll(rule: Rule, trusted: TrustedOrigins): boolean {
720
760
  const variables = variablesOf(rule);
721
761
  let found = false;
722
- for (const [, bindings] of combine(rule.body, this.visible(trusted), new Map(), variables)) {
762
+ for (const [, bindings] of combine(
763
+ rule.body,
764
+ this.visible(trusted),
765
+ new Map(),
766
+ variables,
767
+ this.tick,
768
+ )) {
723
769
  found = true;
724
770
  for (const ops of rule.expressions) {
725
771
  const res = evaluateExpression(ops, bindings, this.externs);
package/src/index.ts CHANGED
@@ -41,7 +41,7 @@ export {
41
41
  type RunLimits,
42
42
  type Term,
43
43
  } from "./datalog.js";
44
- export { type ParamValue, type Params, ParseError } from "./parser.js";
44
+ export { type Params, type ParamValue, ParseError } from "./parser.js";
45
45
  export { ProtoError } from "./proto.js";
46
46
  export * from "./version.js";
47
47
 
package/src/parser.ts CHANGED
@@ -76,8 +76,13 @@ export type Statement =
76
76
  | { k: "policy"; kind: "allow" | "deny"; queries: Rule[] }
77
77
  | { k: "blockScope"; scopes: Scope[] };
78
78
 
79
- const NAME_START = /[\p{L}]/u;
80
- const NAME_CHAR = /[\p{L}\p{N}_:]/u;
79
+ /**
80
+ * A predicate or variable name is one or more of `[A-Za-z0-9_:]`, and any of them
81
+ * may come first — ASCII only. That is what the reference parser accepts, measured
82
+ * rather than read from the specification (`tests/grammar-cases.ts`): `_m`, `1a`
83
+ * and `::` are names, `ärger` is not.
84
+ */
85
+ const NAME_CHAR = /[A-Za-z0-9_:]/;
81
86
 
82
87
  export class Parser {
83
88
  private i = 0;
@@ -155,9 +160,8 @@ export class Parser {
155
160
  this.i += k.length;
156
161
  return true;
157
162
  }
158
- /** variable names, unlike predicate names, may start with a digit ($0) */
163
+ /** the name directly after `$` — `$ x` is not a variable, as in the reference */
159
164
  private variableName(): string {
160
- this.ws();
161
165
  const start = this.i;
162
166
  while (this.i < this.src.length && NAME_CHAR.test(this.src[this.i])) this.i++;
163
167
  if (this.i === start) throw new ParseError(`expected a variable name at offset ${this.i}`);
@@ -167,10 +171,8 @@ export class Parser {
167
171
  private name(): string {
168
172
  this.ws();
169
173
  const start = this.i;
170
- if (this.i >= this.src.length || !NAME_START.test(this.src[this.i]))
171
- throw new ParseError(`expected a name at offset ${this.i}`);
172
- this.i++;
173
174
  while (this.i < this.src.length && NAME_CHAR.test(this.src[this.i])) this.i++;
175
+ if (this.i === start) throw new ParseError(`expected a name at offset ${this.i}`);
174
176
  return this.src.slice(start, this.i);
175
177
  }
176
178
 
@@ -316,12 +318,12 @@ export class Parser {
316
318
  predicate(allowVariables = true): Predicate {
317
319
  const name = this.name();
318
320
  this.expect("(");
321
+ // at least one term, as in the reference: `f()` is not a predicate
322
+ if (this.peek(")")) throw new ParseError(`predicate ${name} takes at least one term`);
319
323
  const terms: Term[] = [];
320
- if (!this.peek(")")) {
321
- do {
322
- terms.push(this.term(allowVariables));
323
- } while (this.eat(","));
324
- }
324
+ do {
325
+ terms.push(this.term(allowVariables));
326
+ } while (this.eat(","));
325
327
  this.expect(")");
326
328
  return { name, terms };
327
329
  }
@@ -602,7 +604,7 @@ export class Parser {
602
604
  const save = this.i;
603
605
  try {
604
606
  this.ws();
605
- if (!NAME_START.test(this.src[this.i] ?? "")) return null;
607
+ if (!NAME_CHAR.test(this.src[this.i] ?? "")) return null;
606
608
  const p = this.predicate();
607
609
  this.ws();
608
610
  const rest = this.src.slice(this.i);