Skip to content

Commit 941932c

Browse files
l46kokcopybara-github
authored andcommitted
Prevent planning createMap for unsupported keys
PiperOrigin-RevId: 875751638
1 parent 0737691 commit 941932c

5 files changed

Lines changed: 31 additions & 4 deletions

File tree

common/src/main/java/dev/cel/common/exceptions/CelInvalidArgumentException.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,8 @@ public final class CelInvalidArgumentException extends CelRuntimeException {
2424
public CelInvalidArgumentException(Throwable cause) {
2525
super(cause, CelErrorCode.INVALID_ARGUMENT);
2626
}
27+
28+
public CelInvalidArgumentException(String message) {
29+
super(message, CelErrorCode.INVALID_ARGUMENT);
30+
}
2731
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,6 @@ _TESTS_TO_SKIP_PLANNER = [
175175
"timestamps/timestamp_range/sub_time_duration_under",
176176

177177
# Skip until fixed.
178-
"fields/qualified_identifier_resolution/map_key_float",
179-
"fields/qualified_identifier_resolution/map_key_null",
180178
"fields/qualified_identifier_resolution/map_value_repeat_key_heterogeneous",
181179
"optionals/optionals/map_null_entry_no_such_key",
182180
"optionals/optionals/map_present_key_invalid_field",

runtime/src/main/java/dev/cel/runtime/planner/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ java_library(
334334
":localized_evaluation_exception",
335335
":planned_interpretable",
336336
"//common/exceptions:duplicate_key",
337+
"//common/exceptions:invalid_argument",
337338
"//runtime:evaluation_exception",
338339
"//runtime:interpretable",
339340
"@maven//:com_google_errorprone_error_prone_annotations",

runtime/src/main/java/dev/cel/runtime/planner/EvalCreateMap.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
import com.google.common.base.Preconditions;
1818
import com.google.common.collect.ImmutableMap;
1919
import com.google.common.collect.Sets;
20+
import com.google.common.primitives.UnsignedLong;
2021
import com.google.errorprone.annotations.Immutable;
2122
import dev.cel.common.exceptions.CelDuplicateKeyException;
23+
import dev.cel.common.exceptions.CelInvalidArgumentException;
2224
import dev.cel.runtime.CelEvaluationException;
2325
import dev.cel.runtime.GlobalResolver;
2426
import java.util.HashSet;
@@ -46,11 +48,22 @@ public Object eval(GlobalResolver resolver, ExecutionFrame frame) throws CelEval
4648
HashSet<Object> keysSeen = Sets.newHashSetWithExpectedSize(keys.length);
4749

4850
for (int i = 0; i < keys.length; i++) {
49-
Object key = keys[i].eval(resolver, frame);
51+
PlannedInterpretable keyInterpretable = keys[i];
52+
Object key = keyInterpretable.eval(resolver, frame);
53+
if (!(key instanceof String
54+
|| key instanceof Long
55+
|| key instanceof UnsignedLong
56+
|| key instanceof Boolean)) {
57+
throw new LocalizedEvaluationException(
58+
new CelInvalidArgumentException("Unsupported key type: " + key),
59+
keyInterpretable.exprId());
60+
}
61+
5062
Object val = values[i].eval(resolver, frame);
5163

5264
if (!keysSeen.add(key)) {
53-
throw new LocalizedEvaluationException(CelDuplicateKeyException.of(key), keys[i].exprId());
65+
throw new LocalizedEvaluationException(
66+
CelDuplicateKeyException.of(key), keyInterpretable.exprId());
5467
}
5568

5669
if (isOptional[i]) {

runtime/src/test/java/dev/cel/runtime/planner/ProgramPlannerTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,17 @@ public void plan_createMap_containsDuplicateKey_throws() throws Exception {
390390
.contains("evaluation error at <input>:20: duplicate map key [true]");
391391
}
392392

393+
@Test
394+
public void plan_createMap_unsupportedKeyType_throws() throws Exception {
395+
CelAbstractSyntaxTree ast = compile("{1.0: 'foo'}");
396+
Program program = PLANNER.plan(ast);
397+
398+
CelEvaluationException e = assertThrows(CelEvaluationException.class, program::eval);
399+
assertThat(e)
400+
.hasMessageThat()
401+
.contains("evaluation error at <input>:1: Unsupported key type: 1.0");
402+
}
403+
393404
@Test
394405
public void plan_createStruct() throws Exception {
395406
CelAbstractSyntaxTree ast = compile("cel.expr.conformance.proto3.TestAllTypes{}");

0 commit comments

Comments
 (0)