Skip to content

Commit 0c2d771

Browse files
committed
Subset dev.cel environment in CelCommon and add regression test
1 parent c62d193 commit 0c2d771

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

xds/src/main/java/io/grpc/xds/internal/matcher/CelCommon.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,47 @@
3030
final class CelCommon {
3131
private static final CelOptions CEL_OPTIONS = CelOptions.newBuilder()
3232
.enableComprehension(false)
33-
.enableStringConversion(false)
34-
.enableStringConcatenation(false)
35-
.enableListConcatenation(false)
3633
.maxRegexProgramSize(100)
3734
.build();
35+
36+
private static final dev.cel.checker.CelStandardDeclarations DECLARATIONS =
37+
dev.cel.checker.CelStandardDeclarations.newBuilder()
38+
.filterFunctions((func, over) -> {
39+
if (func == dev.cel.checker.CelStandardDeclarations.StandardFunction.STRING) {
40+
return false;
41+
}
42+
if (func == dev.cel.checker.CelStandardDeclarations.StandardFunction.ADD) {
43+
String id = over.celOverloadDecl().overloadId();
44+
return !id.equals("add_string") && !id.equals("add_list");
45+
}
46+
return true;
47+
})
48+
.build();
49+
50+
private static final dev.cel.runtime.CelStandardFunctions FUNCTIONS =
51+
dev.cel.runtime.CelStandardFunctions.newBuilder()
52+
.filterFunctions((func, over) -> {
53+
if (func == dev.cel.runtime.CelStandardFunctions.StandardFunction.STRING) {
54+
return false;
55+
}
56+
if (func == dev.cel.runtime.CelStandardFunctions.StandardFunction.ADD) {
57+
String id = over.toString();
58+
return !id.equals("ADD_STRING") && !id.equals("ADD_LIST");
59+
}
60+
return true;
61+
})
62+
.build();
63+
3864
static final CelCompiler COMPILER = CelCompilerFactory.standardCelCompilerBuilder()
65+
.setStandardEnvironmentEnabled(false)
66+
.setStandardDeclarations(DECLARATIONS)
3967
.addVar("request", SimpleType.DYN)
4068
.setOptions(CEL_OPTIONS)
4169
.build();
70+
4271
static final CelRuntime RUNTIME = CelRuntimeFactory.standardCelRuntimeBuilder()
72+
.setStandardEnvironmentEnabled(false)
73+
.setStandardFunctions(FUNCTIONS)
4374
.setOptions(CEL_OPTIONS)
4475
.build();
4576

xds/src/test/java/io/grpc/xds/internal/matcher/CelEnvironmentTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,41 @@ public void headers_binaryHeader() {
149149
assertThat(headers.containsKey("test-bin")).isTrue();
150150
}
151151

152+
@Test
153+
public void celEnvironment_disabledFeatures_throwsValidationException() {
154+
// String concatenation
155+
try {
156+
io.grpc.xds.internal.matcher.CelCommon.COMPILER.compile("'a' + 'b'").getAst();
157+
org.junit.Assert.fail("String concatenation should be disabled");
158+
} catch (dev.cel.common.CelValidationException e) {
159+
assertThat(e).hasMessageThat().contains("found no matching overload for '_+_'");
160+
}
161+
162+
// List concatenation
163+
try {
164+
io.grpc.xds.internal.matcher.CelCommon.COMPILER.compile("[1] + [2]").getAst();
165+
org.junit.Assert.fail("List concatenation should be disabled");
166+
} catch (dev.cel.common.CelValidationException e) {
167+
assertThat(e).hasMessageThat().contains("found no matching overload for '_+_'");
168+
}
169+
170+
// String conversion
171+
try {
172+
io.grpc.xds.internal.matcher.CelCommon.COMPILER.compile("string(1)").getAst();
173+
org.junit.Assert.fail("String conversion should be disabled");
174+
} catch (dev.cel.common.CelValidationException e) {
175+
assertThat(e).hasMessageThat().contains("undeclared reference to 'string'");
176+
}
177+
178+
// Comprehensions
179+
try {
180+
io.grpc.xds.internal.matcher.CelCommon.COMPILER.compile("[1, 2, 3].all(x, x > 0)").getAst();
181+
org.junit.Assert.fail("Comprehensions should be disabled");
182+
} catch (dev.cel.common.CelValidationException e) {
183+
assertThat(e).hasMessageThat().contains("undeclared reference to 'all'");
184+
}
185+
}
186+
152187
@Test
153188
public void celEnvironment_method_fallback() {
154189
MatchContext context = mock(MatchContext.class);

0 commit comments

Comments
 (0)