|
| 1 | +/* |
| 2 | + * Copyright 2026 The gRPC Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.grpc.xds.internal.matcher; |
| 18 | + |
| 19 | +import com.google.common.collect.ImmutableSet; |
| 20 | +import dev.cel.common.CelAbstractSyntaxTree; |
| 21 | +import dev.cel.common.CelOptions; |
| 22 | +import dev.cel.common.ast.CelReference; |
| 23 | +import dev.cel.runtime.CelRuntime; |
| 24 | +import dev.cel.runtime.CelRuntimeFactory; |
| 25 | +import dev.cel.runtime.CelStandardFunctions; |
| 26 | +import dev.cel.runtime.CelStandardFunctions.StandardFunction; |
| 27 | +import dev.cel.runtime.standard.AddOperator.AddOverload; |
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +/** |
| 31 | + * Shared utilities for CEL-based matchers and extractors. |
| 32 | + */ |
| 33 | +final class CelCommon { |
| 34 | + private static final CelOptions CEL_OPTIONS = CelOptions.newBuilder() |
| 35 | + .enableComprehension(false) |
| 36 | + .maxRegexProgramSize(100) |
| 37 | + .build(); |
| 38 | + private static final String REQUEST_VARIABLE = "request"; |
| 39 | + private static final CelStandardFunctions FUNCTIONS = |
| 40 | + CelStandardFunctions.newBuilder() |
| 41 | + .filterFunctions((func, over) -> { |
| 42 | + if (func == StandardFunction.STRING) { |
| 43 | + return false; |
| 44 | + } |
| 45 | + if (func == StandardFunction.ADD) { |
| 46 | + return !over.equals(AddOverload.ADD_STRING) |
| 47 | + && !over.equals(AddOverload.ADD_LIST); |
| 48 | + } |
| 49 | + return true; |
| 50 | + }) |
| 51 | + .build(); |
| 52 | + |
| 53 | + /** |
| 54 | + * Set of allowed function names based on gRFC A106. |
| 55 | + */ |
| 56 | + private static final ImmutableSet<String> ALLOWED_FUNCTIONS = ImmutableSet.of( |
| 57 | + "size", "matches", "contains", "startsWith", "endsWith", "timestamp", "duration", |
| 58 | + "int", "uint", "double", "bytes", "bool", "==", "!=", ">", "<", ">=", "<=", |
| 59 | + "&&", "||", "!", "+", "-", "*", "/", "%", "in", "has", "or", "equals", |
| 60 | + "index_map", "divide_int64", "int64_to_int64", "uint64_to_int64", |
| 61 | + "double_to_int64", "string_to_int64", "timestamp_to_int64"); |
| 62 | + |
| 63 | + static final CelRuntime RUNTIME = CelRuntimeFactory.standardCelRuntimeBuilder() |
| 64 | + .setStandardEnvironmentEnabled(false) |
| 65 | + .setStandardFunctions(FUNCTIONS) |
| 66 | + .setOptions(CEL_OPTIONS) |
| 67 | + .build(); |
| 68 | + |
| 69 | + private CelCommon() {} |
| 70 | + |
| 71 | + /** |
| 72 | + * Validates that the AST only references the allowed variable ("request") |
| 73 | + * and supported functions as defined in gRFC A106. |
| 74 | + */ |
| 75 | + static void checkAllowedReferences(CelAbstractSyntaxTree ast) { |
| 76 | + for (Map.Entry<Long, CelReference> entry : ast.getReferenceMap().entrySet()) { |
| 77 | + CelReference ref = entry.getValue(); |
| 78 | + |
| 79 | + // Check for variables (where overloadIds is empty) |
| 80 | + if (!ref.value().isPresent() && ref.overloadIds().isEmpty()) { |
| 81 | + if (!REQUEST_VARIABLE.equals(ref.name())) { |
| 82 | + throw new IllegalArgumentException( |
| 83 | + "CEL expression references unknown variable: " + ref.name()); |
| 84 | + } |
| 85 | + } else if (!ref.overloadIds().isEmpty()) { |
| 86 | + String name = ref.name(); |
| 87 | + if (name.isEmpty()) { |
| 88 | + boolean allowed = false; |
| 89 | + for (String id : ref.overloadIds()) { |
| 90 | + if (ALLOWED_FUNCTIONS.contains(id)) { |
| 91 | + allowed = true; |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
| 95 | + if (!allowed) { |
| 96 | + throw new IllegalArgumentException( |
| 97 | + "CEL expression references unknown function with overload IDs: " |
| 98 | + + ref.overloadIds()); |
| 99 | + } |
| 100 | + } else if (!ALLOWED_FUNCTIONS.contains(name)) { |
| 101 | + throw new IllegalArgumentException( |
| 102 | + "CEL expression references unknown function: " + name); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments