Skip to content

Commit

Permalink
Add CelOptions to disable comprehension
Browse files Browse the repository at this point in the history
Fixes #484

PiperOrigin-RevId: 700739705
  • Loading branch information
l46kok authored and copybara-github committed Nov 27, 2024
1 parent cff6d49 commit da3f8b6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
14 changes: 14 additions & 0 deletions bundle/src/test/java/dev/cel/bundle/CelImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2015,6 +2015,20 @@ public void program_listConcatenationDisabled_throws() throws Exception {
assertThat(e.getErrorCode()).isEqualTo(CelErrorCode.OVERLOAD_NOT_FOUND);
}

@Test
public void program_comprehensionDisabled_throws() throws Exception {
Cel cel =
standardCelBuilderWithMacros()
.setOptions(CelOptions.current().enableComprehension(false).build())
.build();
CelAbstractSyntaxTree ast = cel.compile("['foo', 'bar'].map(x, x)").getAst();

CelEvaluationException e =
assertThrows(CelEvaluationException.class, () -> cel.createProgram(ast).eval());
assertThat(e).hasMessageThat().contains("Iteration budget exceeded: 0");
assertThat(e.getErrorCode()).isEqualTo(CelErrorCode.ITERATION_BUDGET_EXCEEDED);
}

@Test
public void toBuilder_isImmutable() {
CelBuilder celBuilder = CelFactory.standardCelBuilder();
Expand Down
12 changes: 11 additions & 1 deletion common/src/main/java/dev/cel/common/CelOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ public enum ProtoUnsetFieldOptions {

public abstract boolean enableListConcatenation();

public abstract boolean enableComprehension();

public abstract Builder toBuilder();

public ImmutableSet<ExprFeatures> toExprFeatures() {
Expand Down Expand Up @@ -209,7 +211,8 @@ public static Builder newBuilder() {
.fromProtoUnsetFieldOption(ProtoUnsetFieldOptions.BIND_DEFAULT)
.enableStringConversion(true)
.enableStringConcatenation(true)
.enableListConcatenation(true);
.enableListConcatenation(true)
.enableComprehension(true);
}

/**
Expand Down Expand Up @@ -531,6 +534,13 @@ public abstract static class Builder {
*/
public abstract Builder enableListConcatenation(boolean value);

/**
* Enables comprehension (macros) for the runtime. Setting false has the same effect with
* assigning 0 for {@link #comprehensionMaxIterations()}. This option exists to maintain parity
* with cel-cpp interpreter options.
*/
public abstract Builder enableComprehension(boolean value);

public abstract CelOptions build();
}
}
5 changes: 3 additions & 2 deletions runtime/src/main/java/dev/cel/runtime/DefaultInterpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,10 @@ public Object evalTrackingUnknowns(
Optional<? extends FunctionResolver> functionResolver,
CelEvaluationListener listener)
throws InterpreterException {
int comprehensionMaxIterations =
celOptions.enableComprehension() ? celOptions.comprehensionMaxIterations() : 0;
ExecutionFrame frame =
new ExecutionFrame(
listener, resolver, functionResolver, celOptions.comprehensionMaxIterations());
new ExecutionFrame(listener, resolver, functionResolver, comprehensionMaxIterations);
IntermediateResult internalResult = evalInternal(frame, ast.getExpr());
return internalResult.value();
}
Expand Down

0 comments on commit da3f8b6

Please sign in to comment.