fix(stepfunctions): bound JSONata evaluation by time and depth (#2697)

JSONata evaluation ran unbounded. A recursive expression with no base case
pinned a core and left the execution RUNNING for ever: the library optimises
the tail call, so it loops rather than overflowing the stack and no Error ever
reaches the executor. Deep non-tail recursion did throw, but a StackOverflowError
the evaluator's catch cannot see.

Bound each expression with Frame#setRuntimeBounds, which the library has and
Floci never called. Either bound raises a JSONata error, so the existing catch
turns it into States.QueryEvaluationError, and the state fails through the same
path as any other evaluation error: catchable by Catch, retryable by Retry.

Five seconds and 500 levels sit deliberately above AWS. Measured with
test-state, AWS refuses a non-tail-recursive function at a nesting depth of 125
and accepts it at 95, and the widest sequence it accepts evaluates here in about
100 ms, so no expression AWS runs can reach either bound.

Closes #2667

Co-authored-by: Facundo Paredes <25681464+facuparedes@users.noreply.github.com>
This commit is contained in:
Facundo ParedesandFacundo Paredes authored and GitHub committed 2026-08-28 20:55:29 -04:00
1 parent ac94232910
commit 395401807b
3 files changed
+120 -3

No files matched your search

@@ -74,12 +74,49 @@ public class JsonataEvaluator {
"a", "arrays", "b", "booleans", "f", "functions",
"n", "numbers", "o", "objects", "s", "strings");
/**
* How long one expression may evaluate before the state fails with
* {@code States.QueryEvaluationError}. AWS bounds evaluation and Floci did not, so an
* expression with no base case pinned a core and left the execution {@code RUNNING} for ever.
* JSONata optimises the tail call, so such an expression loops instead of overflowing the
* stack: no {@code Error} is thrown and only a clock can end it.
*
* <p>Five seconds is the dashjoin library's own default and roughly fifty times the slowest
* evaluation of a payload AWS itself accepts: AWS refuses a sequence of 900,000 elements on
* its memory limit, and summing one that size here takes about 100 ms. Deliberately generous,
* because a bound that fails a working state machine costs more than one that lets a runaway
* expression burn a core for five seconds.
*/
static final long EVALUATION_TIMEOUT_MILLIS = 5_000;
/**
* How deep evaluation may nest before the state fails. A non-tail-recursive function nests at
* roughly {@code 3n+5} for {@code $f(n)}; measured against AWS with {@code test-state}, AWS
* accepts {@code n=30} (depth 95) and refuses {@code n=40} (depth 125), so its own ceiling
* sits near 100. Five times that leaves no expression AWS accepts able to reach this bound,
* and it still trips before the JVM stack does: on a 1 MB thread stack the bound raises a
* JSONata error while 1000 or more overflows the stack instead.
*/
static final int MAX_EVALUATION_DEPTH = 500;
private final ObjectMapper objectMapper;
private final ObjectReader strictJsonReader;
private final Map<String, Jsonata.JFunction> stepFunctionsExtensions;
private final long evaluationTimeoutMillis;
private final int maxEvaluationDepth;
@Inject
public JsonataEvaluator(ObjectMapper objectMapper) {
this(objectMapper, EVALUATION_TIMEOUT_MILLIS, MAX_EVALUATION_DEPTH);
}
/**
* Bounds evaluation at values other than the shipped ones, so a test can trip the time bound
* without waiting {@link #EVALUATION_TIMEOUT_MILLIS} for it.
*/
JsonataEvaluator(ObjectMapper objectMapper, long evaluationTimeoutMillis, int maxEvaluationDepth) {
this.evaluationTimeoutMillis = evaluationTimeoutMillis;
this.maxEvaluationDepth = maxEvaluationDepth;
this.objectMapper = objectMapper;
// AWS rejects what a lenient parser accepts: a second document after the first, a repeated
// key, an empty string. All three come back as D3137 there, so $parse reads through this
@@ -126,6 +163,10 @@ public class JsonataEvaluator {
try {
Jsonata jsonataExpr = jsonata(expr);
Jsonata.Frame frame = jsonataExpr.createFrame();
// Without this the library evaluates unbounded: a recursive expression with no base
// case loops for ever and the execution never leaves RUNNING. The bounds raise a
// JSONata error, which the catch below turns into States.QueryEvaluationError.
frame.setRuntimeBounds(evaluationTimeoutMillis, maxEvaluationDepth);
stepFunctionsExtensions.forEach(frame::bind);
// Workflow variables (the Assign field) are referenced as top-level $name in AWS's
// JSONata dialect, e.g. $CheckpointCount. They are bound alongside $states, never