Skip to content

Commit a1a77dc

Browse files
l46kokcopybara-github
authored andcommitted
Assert allowed CelOptions during CelLiteRuntime environment construction
PiperOrigin-RevId: 732248422
1 parent 2c4026f commit a1a77dc

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

runtime/src/main/java/dev/cel/runtime/LiteRuntimeImpl.java

+34
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,42 @@ public CelLiteRuntimeBuilder addFunctionBindings(Iterable<CelFunctionBinding> bi
6868
return this;
6969
}
7070

71+
/** Throws if an unsupported flag in CelOptions is toggled. */
72+
private static void assertAllowedCelOptions(CelOptions celOptions) {
73+
String prefix = "Misconfigured CelOptions: ";
74+
if (!celOptions.enableCelValue()) {
75+
throw new IllegalArgumentException(prefix + "enableCelValue must be enabled.");
76+
}
77+
if (!celOptions.enableUnsignedLongs()) {
78+
throw new IllegalArgumentException(prefix + "enableUnsignedLongs cannot be disabled.");
79+
}
80+
if (!celOptions.unwrapWellKnownTypesOnFunctionDispatch()) {
81+
throw new IllegalArgumentException(
82+
prefix + "unwrapWellKnownTypesOnFunctionDispatch cannot be disabled.");
83+
}
84+
if (!celOptions.enableStringConcatenation()) {
85+
throw new IllegalArgumentException(
86+
prefix
87+
+ "enableStringConcatenation cannot be disabled. Subset the environment instead"
88+
+ " using setStandardFunctions method.");
89+
}
90+
if (!celOptions.enableStringConversion()) {
91+
throw new IllegalArgumentException(
92+
prefix
93+
+ "enableStringConversion cannot be disabled. Subset the environment instead using"
94+
+ " setStandardFunctions method.");
95+
}
96+
if (!celOptions.enableListConcatenation()) {
97+
throw new IllegalArgumentException(
98+
prefix
99+
+ "enableListConcatenation cannot be disabled. Subset the environment instead using"
100+
+ " setStandardFunctions method.");
101+
}
102+
}
103+
71104
@Override
72105
public CelLiteRuntime build() {
106+
assertAllowedCelOptions(celOptions);
73107
ImmutableMap.Builder<String, CelFunctionBinding> functionBindingsBuilder =
74108
ImmutableMap.builder();
75109
if (celStandardFunctions != null) {

runtime/src/test/java/dev/cel/runtime/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ cel_android_local_test(
160160
"//:java_truth",
161161
"//common:cel_ast_android",
162162
"//common:cel_source_android",
163+
"//common:options",
163164
"//common:proto_ast_android",
164165
"//common/ast:ast_android",
165166
"//common/types:types_android",

runtime/src/test/java/dev/cel/runtime/CelLiteRuntimeTest.java

+30
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
import com.google.common.primitives.UnsignedLong;
2424
import com.google.protobuf.ByteString;
2525
import com.google.protobuf.ExtensionRegistryLite;
26+
import com.google.testing.junit.testparameterinjector.TestParameter;
2627
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
2728
import dev.cel.common.CelAbstractSyntaxTree;
29+
import dev.cel.common.CelOptions;
2830
import dev.cel.common.CelProtoAbstractSyntaxTree;
2931
import dev.cel.common.CelSource;
3032
import dev.cel.common.ast.CelConstant;
@@ -63,6 +65,14 @@ public void programConstruction() throws Exception {
6365
assertThat(program).isNotNull();
6466
}
6567

68+
@Test
69+
public void setCelOptions_unallowedOptionsSet_throws(@TestParameter CelOptionsTestCase testCase) {
70+
assertThrows(
71+
IllegalArgumentException.class,
72+
() ->
73+
CelLiteRuntimeFactory.newLiteRuntimeBuilder().setOptions(testCase.celOptions).build());
74+
}
75+
6676
@Test
6777
public void standardEnvironment_disabledByDefault() throws Exception {
6878
CelLiteRuntime runtime = CelLiteRuntimeFactory.newLiteRuntimeBuilder().build();
@@ -187,4 +197,24 @@ private static CelAbstractSyntaxTree readCheckedExpr(String compiledCelTarget) t
187197
CheckedExpr.parseFrom(checkedExprBytes, ExtensionRegistryLite.getEmptyRegistry());
188198
return CelProtoAbstractSyntaxTree.fromCheckedExpr(checkedExpr).getAst();
189199
}
200+
201+
private enum CelOptionsTestCase {
202+
CEL_VALUE_DISABLED(newBaseTestOptions().enableCelValue(false).build()),
203+
UNSIGNED_LONG_DISABLED(newBaseTestOptions().enableUnsignedLongs(false).build()),
204+
UNWRAP_WKT_DISABLED(newBaseTestOptions().unwrapWellKnownTypesOnFunctionDispatch(false).build()),
205+
STRING_CONCAT_DISABLED(newBaseTestOptions().enableStringConcatenation(false).build()),
206+
STRING_CONVERSION_DISABLED(newBaseTestOptions().enableStringConversion(false).build()),
207+
LIST_CONCATENATION_DISABLED(newBaseTestOptions().enableListConcatenation(false).build()),
208+
;
209+
210+
private final CelOptions celOptions;
211+
212+
private static CelOptions.Builder newBaseTestOptions() {
213+
return CelOptions.current().enableCelValue(true);
214+
}
215+
216+
CelOptionsTestCase(CelOptions celOptions) {
217+
this.celOptions = celOptions;
218+
}
219+
}
190220
}

0 commit comments

Comments
 (0)