-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcel-engine.ts
More file actions
222 lines (208 loc) · 9.29 KB
/
Copy pathcel-engine.ts
File metadata and controls
222 lines (208 loc) · 9.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
* CEL dialect engine — wraps `@marcbachmann/cel-js` with the ObjectStack
* stdlib, bounded execution limits, and result coercion.
*
* Why a thin wrapper:
*
* - cel-js returns `BigInt` for ints. The kernel and CRM expect plain
* numbers, so we coerce at the boundary.
* - cel-js parses dotted names as receiver-typed methods; we register
* `now()`, `today()`, `daysFromNow()` as bare functions and let `os.*`
* refer to context data only (see {@link buildScope}).
* - Bounds (`maxAstNodes`, `maxDepth`, …) are enforced spec-wide so
* third-party plugins can't ship runaway predicates.
*/
import { Environment } from '@marcbachmann/cel-js';
import type { Expression } from '@objectstack/spec';
import { buildScope, registerStdLib } from './stdlib';
import type { DialectEngine, EvalContext, EvalResult } from './types';
/**
* Default execution bounds. Picked conservatively — every metadata-authored
* expression we've seen is well under these. If you hit them, the expression
* is too complex for ObjectStack and should be moved to a hook (`dialect: js`).
*/
export const DEFAULT_LIMITS = {
maxAstNodes: 256,
maxDepth: 32,
maxListElements: 64,
maxMapEntries: 64,
maxCallArguments: 16,
} as const;
function buildEnv(now: () => Date): Environment {
const env = new Environment({
unlistedVariablesAreDyn: true,
enableOptionalTypes: true,
limits: DEFAULT_LIMITS,
});
return registerStdLib(env, now);
}
/** Coerce cel-js's BigInt-flavored return into spec-friendly JS values. */
function coerce(value: unknown): unknown {
if (typeof value === 'bigint') {
// BigInt → number when safe, else string to avoid silent truncation.
if (value >= BigInt(Number.MIN_SAFE_INTEGER) && value <= BigInt(Number.MAX_SAFE_INTEGER)) {
return Number(value);
}
return value.toString();
}
if (Array.isArray(value)) return value.map(coerce);
if (value && typeof value === 'object' && !(value instanceof Date)) {
const out: Record<string, unknown> = {};
for (const [k, v] of Object.entries(value)) out[k] = coerce(v);
return out;
}
return value;
}
/**
* A string that is *entirely* a JS number literal: optional sign, integer
* and/or fractional part, optional exponent. Deliberately strict — `"5.0"`,
* `"250000.00"`, `"-3"`, `"1e3"` match; `"5px"`, `"0x10"`, `" "`, `""`,
* `"1,000"`, `"v2"` do not.
*/
// The fractional part is a single optional `(?:\.\d*)?` group anchored by the
// literal `.` — never the ambiguous `\d+\.?\d*`, whose adjacent unbounded
// quantifiers (`\d+\d*` when the dot is absent) backtrack polynomially on long
// digit runs (CodeQL ReDoS). This matches the same strings without the hazard.
const NUMERIC_STRING_RE = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?$/;
/**
* A string that is an ISO-8601 date (`"2026-06-20"`) or date-time
* (`"2026-06-20T08:15:35.244Z"`, `"2026-06-20 08:15"`, `"...+02:00"`). Strict
* and anchored — no nested unbounded quantifiers, so no ReDoS hazard (every
* sub-group is bounded or a single `\.\d+`). `Field.date` / `Field.datetime`
* serialize to these; cel-js compares them as `string` and faults against the
* `google.protobuf.Timestamp` returned by `today()` / `now()` / `daysFromNow()`.
*/
const ISO_TEMPORAL_STRING_RE =
/^\d{4}-\d{2}-\d{2}(?:[T ]\d{2}:\d{2}(?::\d{2})?(?:\.\d+)?(?:Z|[+-]\d{2}:?\d{2})?)?$/;
/**
* cel-js raises `no such overload: dyn <op> int` (and kin) when a comparison
* or arithmetic operator sees a `string` on one side and a number on the
* other. ADR-0032 §1c — numeric fields that serialize as strings (`Field.rating`
* → `"5.0"`, `Field.currency` → `"250000.00"`, `Field.percent`) trip this in
* flow conditions / formulas (#1530, #1534) even though the schema and the
* build-time validator treat them as numeric.
*/
function isNumericOverloadError(err: unknown): boolean {
const message = err instanceof Error ? err.message : String(err);
return /no such overload/i.test(message);
}
/**
* Recursively coerce string values that faulted a CEL overload into their
* intended primitive: entirely-numeric literals → `number` (#1534), and
* ISO-8601 date / date-time strings → `Date` (cel-js `google.protobuf.Timestamp`)
* (#1530). Used only on the {@link isNumericOverloadError} retry path, so it can
* never change a comparison that already evaluated cleanly — it only rescues one
* that already faulted. Strings that are neither (a zip like `"02134"`, free
* text) pass through untouched; if the retry still cannot type-check, the
* original loud error is preserved.
*/
function hydrateOverloadStrings(value: unknown): unknown {
if (typeof value === 'string') {
const trimmed = value.trim();
if (trimmed.length > 0) {
if (NUMERIC_STRING_RE.test(trimmed)) {
const n = Number(trimmed);
if (Number.isFinite(n)) return n;
} else if (ISO_TEMPORAL_STRING_RE.test(trimmed)) {
const ms = Date.parse(trimmed);
if (!Number.isNaN(ms)) return new Date(ms);
}
}
return value;
}
if (Array.isArray(value)) return value.map(hydrateOverloadStrings);
if (value && typeof value === 'object' && !(value instanceof Date)) {
const out: Record<string, unknown> = {};
for (const [k, v] of Object.entries(value)) out[k] = hydrateOverloadStrings(v);
return out;
}
return value;
}
function classifyError(err: unknown): EvalResult<never> {
const message = err instanceof Error ? err.message : String(err);
let kind: 'parse' | 'type' | 'runtime' | 'bounds' = 'runtime';
if (/Exceeded max/i.test(message)) kind = 'bounds';
else if (/parse|unexpected|syntax/i.test(message)) kind = 'parse';
else if (/type|unknown variable|undeclared/i.test(message)) kind = 'type';
return { ok: false, error: { kind, message } };
}
export const celEngine: DialectEngine = {
dialect: 'cel',
compile(source: string): EvalResult<unknown> {
try {
// We use a wall-clock now() here purely for parse-time stdlib
// type-checking; the function is never actually called.
const env = buildEnv(() => new Date(0));
const compiled = env.parse(source);
// Surface check errors eagerly. cel-js's `check()` returns a
// `TypeCheckResult` object (`{ valid, type?, error? }`) — NOT an array —
// so the type fault (including `found no matching overload for 'PRIOR(dyn)'`
// when a condition calls an UNKNOWN function) only surfaces when we read
// `valid === false`. The previous `Array.isArray(...)` guard never matched
// an object, so unknown-function predicates type-checked clean and were
// silently accepted by `objectstack build` / `registerFlow`, then no-op'd
// the flow at runtime (#1877). Reading the documented shape closes that.
const checkResult = compiled.check?.();
if (checkResult && checkResult.valid === false) {
return {
ok: false,
error: { kind: 'type', message: checkResult.error?.message ?? 'expression failed type checking' },
};
}
return { ok: true, value: compiled.ast };
} catch (err) {
return classifyError(err);
}
},
evaluate<T = unknown>(expr: Expression, ctx: EvalContext): EvalResult<T> {
if (expr.dialect !== 'cel') {
return {
ok: false,
error: { kind: 'dialect', message: `celEngine cannot evaluate dialect '${expr.dialect}'` },
};
}
const source = expr.source;
if (typeof source !== 'string' || source.length === 0) {
// AST-only inputs: cel-js does not currently expose a public API to
// re-execute a parsed AST without re-serializing. We persist `source`
// as the canonical form during M9.1 and revisit AST-only execution in
// M9.7 when we cut the spec persistence over.
return {
ok: false,
error: { kind: 'parse', message: 'AST-only evaluation not yet supported; persist `source`' },
};
}
const now = () => ctx.now ?? new Date();
try {
const env = buildEnv(now);
const scope = buildScope(ctx);
try {
const raw = env.evaluate(source, scope);
return { ok: true, value: coerce(raw) as T };
} catch (err) {
// ADR-0032 §1c — string-serialized fields make CEL raise
// `no such overload`: numeric fields (`rating` → `"5.0"`,
// `amount` → `"250000.00"`) on `record.rating >= 4` (#1534), and
// date/datetime fields (`end_date` → `"2026-06-20"`) on
// `record.end_date <= daysFromNow(60)` (#1530), since cel-js compares the
// raw string against the `google.protobuf.Timestamp` from `today()` etc.
// Hydrate those strings to number / Date and retry ONCE. This only runs
// after a fault, so a comparison that already evaluated cleanly is never
// re-interpreted; if the retry still cannot type-check, the original loud
// error is reported.
if (!isNumericOverloadError(err)) throw err;
const hydrated = hydrateOverloadStrings(scope) as Record<string, unknown>;
try {
const raw = env.evaluate(source, hydrated);
return { ok: true, value: coerce(raw) as T };
} catch {
// Hydration did not resolve it — surface the original fault, not the
// retry's, so the message reflects what the author actually wrote.
throw err;
}
}
} catch (err) {
return classifyError(err);
}
},
};