Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion checker/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ cc_library(
":proto_type_mask",
":type_check_env",
":type_inference_context",
":type_validation",
"//checker:checker_options",
"//checker:type_check_issue",
"//checker:type_checker",
Expand All @@ -153,7 +154,6 @@ cc_library(
"//common:standard_definitions",
"//common:type",
"//common:type_kind",
"//internal:lexis",
"//internal:status_macros",
"//parser:macro",
"@com_google_absl//absl/base:no_destructor",
Expand Down Expand Up @@ -240,6 +240,20 @@ cc_test(
],
)

cc_library(
name = "type_validation",
srcs = ["type_validation.cc"],
hdrs = ["type_validation.h"],
deps = [
"//common:type",
"//common:type_kind",
"//internal:lexis",
"//internal:status_macros",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
],
)

cc_library(
name = "type_inference_context",
srcs = ["type_inference_context.cc"],
Expand Down
75 changes: 6 additions & 69 deletions checker/internal/type_checker_builder_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@
#include "checker/internal/proto_type_mask.h"
#include "checker/internal/type_check_env.h"
#include "checker/internal/type_checker_impl.h"
#include "checker/internal/type_validation.h"
#include "checker/type_checker.h"
#include "checker/type_checker_builder.h"
#include "common/container.h"
#include "common/decl.h"
#include "common/type.h"
#include "common/type_introspector.h"
#include "common/type_kind.h"
#include "internal/lexis.h"
#include "internal/status_macros.h"
#include "parser/macro.h"
#include "google/protobuf/descriptor.h"
Expand Down Expand Up @@ -204,86 +203,24 @@ std::optional<FunctionDecl> FilterDecl(FunctionDecl decl,
return filtered;
}

absl::Status ValidateType(const Type& t, bool check_type_param_name,
int depth_limit, int remaining_depth) {
if (remaining_depth-- <= 0) {
return absl::InvalidArgumentError(
absl::StrCat("type nesting limit of ", depth_limit, " exceeded"));
}
switch (t.kind()) {
case TypeKind::kTypeParam: {
if (!check_type_param_name) {
return absl::OkStatus();
}
const TypeParamType& type_param = t.GetTypeParam();
if (!internal::LexisIsIdentifier(type_param.name())) {
return absl::InvalidArgumentError(
absl::StrCat("type parameter name '", type_param.name(),
"' is not a valid identifier"));
}
return absl::OkStatus();
}
case TypeKind::kList: {
Type element_type = t.AsList()->GetElement();
return ValidateType(element_type, check_type_param_name, depth_limit,
remaining_depth);
}
case TypeKind::kMap: {
Type key_type = t.AsMap()->GetKey();
Type value_type = t.AsMap()->GetValue();
CEL_RETURN_IF_ERROR(ValidateType(key_type, check_type_param_name,
depth_limit, remaining_depth));
return ValidateType(value_type, check_type_param_name, depth_limit,
remaining_depth);
}
case TypeKind::kStruct: {
auto message_type = t.AsMessage();
if (message_type.has_value() && !static_cast<bool>(*message_type)) {
return absl::InvalidArgumentError(
"an empty message type cannot be used in a type declaration");
}
return absl::OkStatus();
}
case TypeKind::kOpaque: {
OpaqueType opaque_type = t.GetOpaque();
for (Type type_param : opaque_type.GetParameters()) {
CEL_RETURN_IF_ERROR(ValidateType(type_param, check_type_param_name,
depth_limit, remaining_depth));
}
return absl::OkStatus();
}
case TypeKind::kType: {
TypeType type_type = t.GetType();
for (Type type_param : type_type.GetParameters()) {
CEL_RETURN_IF_ERROR(ValidateType(type_param, check_type_param_name,
depth_limit, remaining_depth));
}
return absl::OkStatus();
}
default:
break;
}
return absl::OkStatus();
}

absl::Status ValidateFunctionDecl(const FunctionDecl& decl,
bool check_type_param_name, int depth_limit) {
CEL_RETURN_IF_ERROR(CheckStdMacroOverlap(decl));
for (const auto& ovl : decl.overloads()) {
CEL_RETURN_IF_ERROR(ValidateType(ovl.result(), check_type_param_name,
depth_limit, depth_limit));
CEL_RETURN_IF_ERROR(ValidateTypeForDeclaration(
ovl.result(), check_type_param_name, depth_limit));
for (const auto& arg : ovl.args()) {
CEL_RETURN_IF_ERROR(
ValidateType(arg, check_type_param_name, depth_limit, depth_limit));
ValidateTypeForDeclaration(arg, check_type_param_name, depth_limit));
}
}
return absl::OkStatus();
}

absl::Status ValidateVariableDecl(const VariableDecl& decl,
bool check_type_param_name, int depth_limit) {
return ValidateType(decl.type(), check_type_param_name, depth_limit,
depth_limit);
return ValidateTypeForDeclaration(decl.type(), check_type_param_name,
depth_limit);
}

} // namespace
Expand Down
97 changes: 97 additions & 0 deletions checker/internal/type_validation.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "checker/internal/type_validation.h"

#include "absl/status/status.h"
#include "absl/strings/str_cat.h"
#include "common/type.h"
#include "common/type_kind.h"
#include "internal/lexis.h"
#include "internal/status_macros.h"

namespace cel::checker_internal {
namespace {

absl::Status ValidateType(const Type& t, bool check_type_param_name,
int depth_limit, int remaining_depth) {
if (remaining_depth-- <= 0) {
return absl::InvalidArgumentError(
absl::StrCat("type nesting limit of ", depth_limit, " exceeded"));
}
switch (t.kind()) {
case TypeKind::kTypeParam: {
if (!check_type_param_name) {
return absl::OkStatus();
}
const TypeParamType& type_param = t.GetTypeParam();
if (!internal::LexisIsIdentifier(type_param.name())) {
return absl::InvalidArgumentError(
absl::StrCat("type parameter name '", type_param.name(),
"' is not a valid identifier"));
}
return absl::OkStatus();
}
case TypeKind::kList: {
Type element_type = t.AsList()->GetElement();
return ValidateType(element_type, check_type_param_name, depth_limit,
remaining_depth);
}
case TypeKind::kMap: {
Type key_type = t.AsMap()->GetKey();
Type value_type = t.AsMap()->GetValue();
CEL_RETURN_IF_ERROR(ValidateType(key_type, check_type_param_name,
depth_limit, remaining_depth));
return ValidateType(value_type, check_type_param_name, depth_limit,
remaining_depth);
}
case TypeKind::kStruct: {
auto message_type = t.AsMessage();
if (message_type.has_value() && !static_cast<bool>(*message_type)) {
return absl::InvalidArgumentError(
"an empty message type cannot be used in a type declaration");
}
return absl::OkStatus();
}
case TypeKind::kOpaque: {
OpaqueType opaque_type = t.GetOpaque();
for (Type type_param : opaque_type.GetParameters()) {
CEL_RETURN_IF_ERROR(ValidateType(type_param, check_type_param_name,
depth_limit, remaining_depth));
}
return absl::OkStatus();
}
case TypeKind::kType: {
TypeType type_type = t.GetType();
for (Type type_param : type_type.GetParameters()) {
CEL_RETURN_IF_ERROR(ValidateType(type_param, check_type_param_name,
depth_limit, remaining_depth));
}
return absl::OkStatus();
}
default:
break;
}
return absl::OkStatus();
}

} // namespace

absl::Status ValidateTypeForDeclaration(const Type& type,
bool check_type_param_name,
int depth_limit) {
return ValidateType(type, check_type_param_name, depth_limit, depth_limit);
}

} // namespace cel::checker_internal
38 changes: 38 additions & 0 deletions checker/internal/type_validation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef THIRD_PARTY_CEL_CPP_CHECKER_INTERNAL_TYPE_VALIDATION_H_
#define THIRD_PARTY_CEL_CPP_CHECKER_INTERNAL_TYPE_VALIDATION_H_

#include "absl/status/status.h"
#include "common/type.h"

namespace cel::checker_internal {

// Validates a type used in a declaration (e.g. of a variable, a function
// overload or a custom type).
//
// Returns an error if `type` is nested deeper than `depth_limit`, if
// `check_type_param_name` is true and a type parameter name is not a valid
// identifier, or if `type` contains an empty message type.
//
// The validation walks `type` as a tree, so callers must bound the expanded
// size of types that share nested type handles.
absl::Status ValidateTypeForDeclaration(const Type& type,
bool check_type_param_name,
int depth_limit);

} // namespace cel::checker_internal

#endif // THIRD_PARTY_CEL_CPP_CHECKER_INTERNAL_TYPE_VALIDATION_H_
3 changes: 3 additions & 0 deletions common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ cc_test(
"@com_google_absl//absl/base:no_destructor",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:status_matchers",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings:string_view",
"@com_google_absl//absl/types:optional",
"@com_google_protobuf//:protobuf",
],
)
Expand Down
12 changes: 10 additions & 2 deletions common/signature.cc
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,14 @@ absl::StatusOr<std::vector<std::string_view>> SplitTypeList(
return SignatureScanner(params, "Invalid type signature").SplitTopLevel(',');
}

absl::StatusOr<TypeSpec> ParseTypeSignature(std::string_view signature) {
constexpr int kMaxTypeSignatureDepth = 128;

absl::StatusOr<TypeSpec> ParseTypeSignature(
std::string_view signature, int remaining_depth = kMaxTypeSignatureDepth) {
if (remaining_depth <= 0) {
return absl::InvalidArgumentError(
"Invalid type signature: nesting depth exceeds maximum");
}
if (signature.empty()) {
return absl::InvalidArgumentError("Empty type signature");
}
Expand Down Expand Up @@ -498,7 +505,8 @@ absl::StatusOr<TypeSpec> ParseTypeSignature(std::string_view signature) {
}
CEL_ASSIGN_OR_RETURN(auto param_list, SplitTypeList(params_str));
for (std::string_view param_str : param_list) {
CEL_ASSIGN_OR_RETURN(auto param, ParseTypeSignature(param_str));
CEL_ASSIGN_OR_RETURN(auto param,
ParseTypeSignature(param_str, remaining_depth - 1));
params.push_back(std::move(param));
}
} else {
Expand Down
41 changes: 28 additions & 13 deletions common/type_spec_resolver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#undef GetMessage

namespace cel {
namespace {

absl::StatusOr<absl::optional<Type>> ResolveNamedType(
absl::string_view name, const TypeIntrospector& type_introspector) {
Expand All @@ -47,11 +46,6 @@ absl::StatusOr<absl::optional<Type>> ResolveNamedType(
return type_introspector.FindType(name);
}

bool TypeAcceptsParameters(const Type& type) {
return type.IsOpaque() || type.IsType() || type.IsList() || type.IsMap();
}
} // namespace

absl::StatusOr<Type> ConvertTypeSpecToType(
const TypeSpec& type_spec, const TypeIntrospector& type_introspector,
google::protobuf::Arena* arena) {
Expand Down Expand Up @@ -174,29 +168,50 @@ absl::StatusOr<Type> ConvertTypeSpecToType(

if (type_spec.has_abstract_type()) {
const std::string& name = type_spec.abstract_type().name();
const auto& param_specs = type_spec.abstract_type().parameter_types();

CEL_ASSIGN_OR_RETURN(absl::optional<Type> type,
ResolveNamedType(name, type_introspector));
if (type.has_value()) {
if (!TypeAcceptsParameters(*type) &&
!type_spec.abstract_type().parameter_types().empty()) {
if (param_specs.empty()) {
return *type;
}
if (!type->IsOpaque()) {
return absl::InvalidArgumentError(
absl::StrCat("Type '", name, "' cannot have type parameters"));
}
return *type;
if (!type->GetParameters().empty() &&
param_specs.size() != type->GetParameters().size()) {
return absl::InvalidArgumentError(absl::StrCat(
"Type '", name, "' expects ", type->GetParameters().size(),
" type parameter(s), got ", param_specs.size()));
}
// A resolved opaque type with type parameters at the use site is
// instantiated with those parameters below, consistent with cel-go and
// cel-java.
//
// The resolution is intentionally lenient: it accepts type parameters
// for opaque types registered without any, and it does not check that
// the use-site parameters match the registered ones beyond their count.
// This function is also used at evaluation time (e.g. to resolve type
// literals), where type parameterization is erased. Callers that
// resolve type declarations, such as `cel::Env`, enforce stricter
// checks.
}

// Otherwise fallback to OpaqueType
std::vector<Type> params;
params.reserve(type_spec.abstract_type().parameter_types().size());
for (const auto& param_spec : type_spec.abstract_type().parameter_types()) {
params.reserve(param_specs.size());
for (const auto& param_spec : param_specs) {
CEL_ASSIGN_OR_RETURN(
auto param,
ConvertTypeSpecToType(param_spec, type_introspector, arena));
params.push_back(std::move(param));
}
auto* allocated_name = google::protobuf::Arena::Create<std::string>(arena, name);
return Type(OpaqueType(arena, absl::string_view(*allocated_name), params));
absl::string_view resolved_name = type.has_value() ? type->name() : name;
return Type(OpaqueType(
arena, *google::protobuf::Arena::Create<std::string>(arena, resolved_name),
params));
}

if (type_spec.has_type()) {
Expand Down
Loading
Loading