|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "validator/regex_validator.h" |
| 16 | + |
| 17 | +#include <utility> |
| 18 | +#include <vector> |
| 19 | + |
| 20 | +#include "absl/log/absl_check.h" |
| 21 | +#include "absl/strings/str_cat.h" |
| 22 | +#include "absl/strings/string_view.h" |
| 23 | +#include "common/constant.h" |
| 24 | +#include "common/expr.h" |
| 25 | +#include "common/navigable_ast.h" |
| 26 | +#include "internal/re2_options.h" |
| 27 | +#include "validator/validator.h" |
| 28 | +#include "re2/re2.h" |
| 29 | + |
| 30 | +namespace cel { |
| 31 | + |
| 32 | +namespace { |
| 33 | + |
| 34 | +bool CheckPattern(ValidationContext& context, const NavigableAstNode& node, |
| 35 | + int arg_index) { |
| 36 | + ABSL_DCHECK(node.expr()->has_call_expr()); |
| 37 | + const auto& call_expr = node.expr()->call_expr(); |
| 38 | + |
| 39 | + const Expr* pattern_expr = nullptr; |
| 40 | + |
| 41 | + if (call_expr.has_target()) { |
| 42 | + if (arg_index == 0) { |
| 43 | + pattern_expr = &call_expr.target(); |
| 44 | + } else if (call_expr.args().size() > arg_index - 1) { |
| 45 | + pattern_expr = &call_expr.args()[arg_index - 1]; |
| 46 | + } |
| 47 | + } else if (call_expr.args().size() > arg_index) { |
| 48 | + pattern_expr = &call_expr.args()[arg_index]; |
| 49 | + } |
| 50 | + |
| 51 | + if (pattern_expr == nullptr || !pattern_expr->has_const_expr()) { |
| 52 | + return true; |
| 53 | + } |
| 54 | + |
| 55 | + const auto& const_expr = pattern_expr->const_expr(); |
| 56 | + if (!const_expr.has_string_value()) { |
| 57 | + return true; |
| 58 | + } |
| 59 | + |
| 60 | + absl::string_view pattern_string = const_expr.string_value(); |
| 61 | + RE2 re(pattern_string, internal::MakeRE2Options()); |
| 62 | + if (!re.ok()) { |
| 63 | + context.ReportErrorAt( |
| 64 | + pattern_expr->id(), |
| 65 | + absl::StrCat("invalid regular expression: ", re.error())); |
| 66 | + return false; |
| 67 | + } |
| 68 | + return true; |
| 69 | +} |
| 70 | + |
| 71 | +} // namespace |
| 72 | + |
| 73 | +Validation RegexPatternValidator( |
| 74 | + absl::string_view id, std::vector<RegexPatternValidatorConfig> config) { |
| 75 | + return Validation( |
| 76 | + [config = std::move(config)](ValidationContext& context) -> bool { |
| 77 | + bool result = true; |
| 78 | + for (const auto& node : |
| 79 | + context.navigable_ast().Root().DescendantsPostorder()) { |
| 80 | + if (node.node_kind() == NodeKind::kCall) { |
| 81 | + for (const auto& config : config) { |
| 82 | + if (node.expr()->call_expr().function() == config.function_name) { |
| 83 | + if (!CheckPattern(context, node, config.pattern_arg_index)) { |
| 84 | + result = false; |
| 85 | + } |
| 86 | + break; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + return result; |
| 92 | + }, |
| 93 | + id); |
| 94 | +} |
| 95 | + |
| 96 | +} // namespace cel |
0 commit comments