Skip to content

Assert allowed CelOptions during CelLiteRuntime environment construction #619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions runtime/src/main/java/dev/cel/runtime/LiteRuntimeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,42 @@ public CelLiteRuntimeBuilder addFunctionBindings(Iterable<CelFunctionBinding> bi
return this;
}

/** Throws if an unsupported flag in CelOptions is toggled. */
private static void assertAllowedCelOptions(CelOptions celOptions) {
String prefix = "Misconfigured CelOptions: ";
if (!celOptions.enableCelValue()) {
throw new IllegalArgumentException(prefix + "enableCelValue must be enabled.");
}
if (!celOptions.enableUnsignedLongs()) {
throw new IllegalArgumentException(prefix + "enableUnsignedLongs cannot be disabled.");
}
if (!celOptions.unwrapWellKnownTypesOnFunctionDispatch()) {
throw new IllegalArgumentException(
prefix + "unwrapWellKnownTypesOnFunctionDispatch cannot be disabled.");
}
if (!celOptions.enableStringConcatenation()) {
throw new IllegalArgumentException(
prefix
+ "enableStringConcatenation cannot be disabled. Subset the environment instead"
+ " using setStandardFunctions method.");
}
if (!celOptions.enableStringConversion()) {
throw new IllegalArgumentException(
prefix
+ "enableStringConversion cannot be disabled. Subset the environment instead using"
+ " setStandardFunctions method.");
}
if (!celOptions.enableListConcatenation()) {
throw new IllegalArgumentException(
prefix
+ "enableListConcatenation cannot be disabled. Subset the environment instead using"
+ " setStandardFunctions method.");
}
}

@Override
public CelLiteRuntime build() {
assertAllowedCelOptions(celOptions);
ImmutableMap.Builder<String, CelFunctionBinding> functionBindingsBuilder =
ImmutableMap.builder();
if (celStandardFunctions != null) {
Expand Down
1 change: 1 addition & 0 deletions runtime/src/test/java/dev/cel/runtime/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ cel_android_local_test(
"//:java_truth",
"//common:cel_ast_android",
"//common:cel_source_android",
"//common:options",
"//common:proto_ast_android",
"//common/ast:ast_android",
"//common/types:types_android",
Expand Down
30 changes: 30 additions & 0 deletions runtime/src/test/java/dev/cel/runtime/CelLiteRuntimeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import com.google.common.primitives.UnsignedLong;
import com.google.protobuf.ByteString;
import com.google.protobuf.ExtensionRegistryLite;
import com.google.testing.junit.testparameterinjector.TestParameter;
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
import dev.cel.common.CelAbstractSyntaxTree;
import dev.cel.common.CelOptions;
import dev.cel.common.CelProtoAbstractSyntaxTree;
import dev.cel.common.CelSource;
import dev.cel.common.ast.CelConstant;
Expand Down Expand Up @@ -63,6 +65,14 @@ public void programConstruction() throws Exception {
assertThat(program).isNotNull();
}

@Test
public void setCelOptions_unallowedOptionsSet_throws(@TestParameter CelOptionsTestCase testCase) {
assertThrows(
IllegalArgumentException.class,
() ->
CelLiteRuntimeFactory.newLiteRuntimeBuilder().setOptions(testCase.celOptions).build());
}

@Test
public void standardEnvironment_disabledByDefault() throws Exception {
CelLiteRuntime runtime = CelLiteRuntimeFactory.newLiteRuntimeBuilder().build();
Expand Down Expand Up @@ -187,4 +197,24 @@ private static CelAbstractSyntaxTree readCheckedExpr(String compiledCelTarget) t
CheckedExpr.parseFrom(checkedExprBytes, ExtensionRegistryLite.getEmptyRegistry());
return CelProtoAbstractSyntaxTree.fromCheckedExpr(checkedExpr).getAst();
}

private enum CelOptionsTestCase {
CEL_VALUE_DISABLED(newBaseTestOptions().enableCelValue(false).build()),
UNSIGNED_LONG_DISABLED(newBaseTestOptions().enableUnsignedLongs(false).build()),
UNWRAP_WKT_DISABLED(newBaseTestOptions().unwrapWellKnownTypesOnFunctionDispatch(false).build()),
STRING_CONCAT_DISABLED(newBaseTestOptions().enableStringConcatenation(false).build()),
STRING_CONVERSION_DISABLED(newBaseTestOptions().enableStringConversion(false).build()),
LIST_CONCATENATION_DISABLED(newBaseTestOptions().enableListConcatenation(false).build()),
;

private final CelOptions celOptions;

private static CelOptions.Builder newBaseTestOptions() {
return CelOptions.current().enableCelValue(true);
}

CelOptionsTestCase(CelOptions celOptions) {
this.celOptions = celOptions;
}
}
}