diff --git a/MODULE.bazel b/MODULE.bazel index 3434f1cbe..f2797af40 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -75,7 +75,7 @@ bazel_dep( # TODO(b/582952496): Remove git_override once cel-conformance is published on BCR. git_override( module_name = "cel-conformance", - commit = "8d64ec2a2938204e388fc02e8e5102dc8ca16e83", + commit = "f2745046262d57f8abfa571684093448a5f98f20", remote = "https://github.com/cel-expr/cel-conformance", ) diff --git a/conformance/env/BUILD b/conformance/env/BUILD new file mode 100644 index 000000000..a8e58ed6e --- /dev/null +++ b/conformance/env/BUILD @@ -0,0 +1,90 @@ +# 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. + +load("@rules_cc//cc:cc_library.bzl", "cc_library") +load("@rules_cc//cc:cc_test.bzl", "cc_test") +load( + "//conformance/env:env_conformance_test.bzl", + "cel_env_conformance_test", +) + +package(default_visibility = ["//visibility:public"]) + +cc_library( + name = "yaml_matcher", + testonly = True, + srcs = ["yaml_matcher.cc"], + hdrs = ["yaml_matcher.h"], + copts = [ + "-fexceptions", + ], + features = ["-use_header_modules"], + deps = [ + "//internal:testing_no_main", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/strings", + "@yaml-cpp", + ], +) + +cc_test( + name = "yaml_matcher_test", + srcs = ["yaml_matcher_test.cc"], + deps = [ + ":yaml_matcher", + "//internal:testing", + ], +) + +cc_library( + name = "env_conformance_test_lib", + testonly = True, + srcs = ["env_conformance_test.cc"], + deps = [ + ":yaml_matcher", + "//env:config", + "//env:env_yaml", + "//internal:runfiles", + "//internal:testing_no_main", + "@cel_conformance//proto/cel/expr/conformance/test/env:suite_cc_proto", + "@com_google_absl//absl/flags:flag", + "@com_google_absl//absl/log:absl_check", + "@com_google_absl//absl/status", + "@com_google_absl//absl/status:status_matchers", + "@com_google_absl//absl/status:statusor", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/types:span", + "@com_google_protobuf//:protobuf", + "@com_google_protobuf//src/google/protobuf/io", + ], + alwayslink = True, +) + +_ALL_TESTS = [ + "@cel_conformance//tests/env:testdata/type_signatures.textproto", +] + +cel_env_conformance_test( + name = "env_conformance_test", + test_files = _ALL_TESTS, +) + +cel_env_conformance_test( + name = "env_conformance_skip_test", + skip_tests = [ + "type_signatures/primitives/bool", + "type_signatures/timestamp", + ], + test_files = _ALL_TESTS, +) diff --git a/conformance/env/env_conformance_test.bzl b/conformance/env/env_conformance_test.bzl new file mode 100644 index 000000000..9ec4bd4c2 --- /dev/null +++ b/conformance/env/env_conformance_test.bzl @@ -0,0 +1,46 @@ +# 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. + +""" +This module contains build rules for generating env conformance test targets. +""" + +load("@rules_cc//cc:cc_test.bzl", "cc_test") + +def cel_env_conformance_test(name, test_files, skip_tests = [], **kwargs): + """Generates an env conformance test target. + + Args: + name: Name of the test target. + test_files: List of targets or files representing the test suite textprotos. + skip_tests: List of test cases (or suite/section prefixes) to skip. + **kwargs: Additional arguments passed to the underlying cc_test. + """ + args = [ + ] + paths = ["$(rlocationpath {})".format(f) for f in test_files] + args.append("--test_suites={}".format(",".join(paths))) + + if skip_tests: + args.append("--skip_tests=" + ",".join(skip_tests)) + + cc_test( + name = name, + data = test_files, + deps = [ + "//conformance/env:env_conformance_test_lib", + ], + args = args, + **kwargs + ) diff --git a/conformance/env/env_conformance_test.cc b/conformance/env/env_conformance_test.cc new file mode 100644 index 000000000..07feface4 --- /dev/null +++ b/conformance/env/env_conformance_test.cc @@ -0,0 +1,194 @@ +// 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 +#include +#include +#include +#include +#include + +#include "absl/flags/flag.h" +#include "absl/log/absl_check.h" +#include "absl/status/status.h" +#include "absl/status/status_matchers.h" +#include "absl/status/statusor.h" +#include "absl/strings/match.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" +#include "absl/strings/strip.h" +#include "absl/types/span.h" +#include "cel/expr/conformance/test/env/suite.pb.h" +#include "conformance/env/yaml_matcher.h" +#include "env/config.h" +#include "env/env_yaml.h" +#include "internal/runfiles.h" +#include "internal/testing.h" +#include "google/protobuf/io/zero_copy_stream_impl.h" +#include "google/protobuf/text_format.h" + +ABSL_FLAG(std::vector, test_suites, {}, + "Comma-separated list of test suite textproto runfiles paths."); +ABSL_FLAG(std::vector, skip_tests, {}, + "Comma-separated list of tests to skip."); + +namespace cel { +namespace { + +using absl_testing::IsOk; +using cel::expr::conformance::test::env::TestCase; +using cel::expr::conformance::test::env::TestOutput; +using cel::expr::conformance::test::env::TestSuite; +using testing::HasSubstr; +using testing::Not; + +bool ShouldSkipTest(absl::Span tests_to_skip, + absl::string_view name) { + for (absl::string_view test_to_skip : tests_to_skip) { + absl::string_view consumed_name = name; + if (absl::ConsumePrefix(&consumed_name, test_to_skip) && + (consumed_name.empty() || absl::StartsWith(consumed_name, "/"))) { + return true; + } + } + return false; +} + +class EnvConformanceTest : public testing::Test { + public: + EnvConformanceTest(TestCase test_case, std::string full_test_name, bool skip) + : test_case_(std::move(test_case)), + full_test_name_(std::move(full_test_name)), + skip_(skip) {} + + void TestBody() override { + if (skip_) { + GTEST_SKIP() << "Skipping test: " << full_test_name_; + } + + absl::StatusOr config_or; + switch (test_case_.env_config_case()) { + case TestCase::kEnvYaml: + config_or = EnvConfigFromYaml(test_case_.env_yaml()); + break; + case TestCase::kEnv: + FAIL() << "Proto Environment input is not yet supported in " + << full_test_name_; + case TestCase::ENV_CONFIG_NOT_SET: + FAIL() << "No env_config specified in " << full_test_name_; + } + + const TestOutput& output = test_case_.output(); + const bool has_exported_yaml = !output.exported_env_yaml().empty(); + const bool has_expected_errors = !output.errors().empty(); + + if (!has_exported_yaml && !has_expected_errors) { + FAIL() << "No expected output specified in " << full_test_name_; + } + + if (has_expected_errors) { + ASSERT_THAT(config_or, Not(IsOk())) + << "Expected error in " << full_test_name_ << ", but succeeded."; + for (absl::string_view expected_err : output.errors()) { + if (!expected_err.empty()) { + EXPECT_THAT(config_or.status().message(), HasSubstr(expected_err)); + } + } + return; + } + + ASSERT_THAT(config_or, IsOk()) + << "Failed to parse env_yaml in " << full_test_name_; + + if (has_exported_yaml) { + std::ostringstream actual_stream; + EnvConfigToYamlOptions options; + options.use_type_signatures = true; + EnvConfigToYaml(*config_or, actual_stream, options); + std::string actual_yaml = actual_stream.str(); + + EXPECT_THAT(actual_yaml, EqualsYaml(output.exported_env_yaml())) + << "Env YAML mismatch in " << full_test_name_; + } + } + + private: + TestCase test_case_; + std::string full_test_name_; + bool skip_; +}; + +absl::Status RegisterTestsFromFile(absl::Span skip_tests, + absl::string_view runfile_path) { + std::string abs_path = cel::internal::ResolveRunfilesPath(runfile_path); + if (abs_path.empty()) { + return absl::NotFoundError( + absl::StrCat("could not resolve runfiles path: ", runfile_path)); + } + + TestSuite suite; + { + std::ifstream in(abs_path, std::ios_base::in | std::ios_base::binary); + if (!in.is_open()) { + return absl::UnknownError( + absl::StrCat("failed to open test suite file: ", abs_path)); + } + google::protobuf::io::IstreamInputStream stream(&in); + if (!google::protobuf::TextFormat::Parse(&stream, &suite)) { + return absl::InvalidArgumentError( + absl::StrCat("failed to parse test suite textproto: ", abs_path)); + } + } + + const std::string& suite_name = suite.name(); + for (const auto& section : suite.sections()) { + const std::string& section_name = section.name(); + for (const auto& test : section.tests()) { + const std::string& test_name = test.name(); + std::string full_test_name = + absl::StrCat(suite_name, "/", section_name, "/", test_name); + bool skip = ShouldSkipTest(skip_tests, full_test_name); + + testing::RegisterTest( + suite_name.c_str(), + absl::StrCat(section_name, "/", test_name).c_str(), nullptr, nullptr, + __FILE__, __LINE__, [test, full_test_name, skip]() { + return new EnvConformanceTest(test, full_test_name, skip); + }); + } + } + return absl::OkStatus(); +} + +void RegisterAllTests() { + std::vector suite_paths = absl::GetFlag(FLAGS_test_suites); + std::vector skip_tests = absl::GetFlag(FLAGS_skip_tests); + + ABSL_CHECK(!suite_paths.empty()) + << "No test suites specified in --test_suites flag."; + + for (const std::string& suite_path : suite_paths) { + ABSL_CHECK_OK(RegisterTestsFromFile(skip_tests, suite_path)); + } +} + +} // namespace +} // namespace cel + +int main(int argc, char** argv) { + testing::InitGoogleTest(&argc, argv); + + cel::RegisterAllTests(); + return RUN_ALL_TESTS(); +} diff --git a/conformance/env/yaml_matcher.cc b/conformance/env/yaml_matcher.cc new file mode 100644 index 000000000..610ce26b3 --- /dev/null +++ b/conformance/env/yaml_matcher.cc @@ -0,0 +1,245 @@ +// 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 "conformance/env/yaml_matcher.h" + +#include +#include +#include + +#include "absl/container/flat_hash_set.h" +#include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" +#include "internal/testing.h" +#include "yaml-cpp/exceptions.h" +#include "yaml-cpp/node/node.h" +#include "yaml-cpp/node/parse.h" +#include "yaml-cpp/node/type.h" +#include "yaml-cpp/yaml.h" // IWYU pragma: keep + +namespace cel { +namespace { + +constexpr int kMaxRecursionDepth = 100; + +absl::string_view NodeTypeName(YAML::NodeType::value type) { + switch (type) { + case YAML::NodeType::Undefined: + return "undefined"; + case YAML::NodeType::Null: + return "null"; + case YAML::NodeType::Scalar: + return "scalar"; + case YAML::NodeType::Sequence: + return "sequence"; + case YAML::NodeType::Map: + return "map"; + } + return "unknown"; +} + +bool HasExplicitTag(const YAML::Node& node) { + const std::string& tag = node.Tag(); + return !tag.empty() && tag != "?" && tag != "!"; +} + +bool NodesEqual(const YAML::Node& actual, const YAML::Node& expected, + absl::string_view path, int depth, + testing::MatchResultListener* listener) { + if (depth > kMaxRecursionDepth) { + if (listener != nullptr) { + *listener << "at " << path << ": exceeded maximum nesting depth (" + << kMaxRecursionDepth << ")"; + } + return false; + } + + if (actual.Type() != expected.Type()) { + if (listener != nullptr) { + *listener << "at " << path + << ": type mismatch (actual: " << NodeTypeName(actual.Type()) + << ", expected: " << NodeTypeName(expected.Type()) << ")"; + } + return false; + } + + if ((HasExplicitTag(actual) || HasExplicitTag(expected)) && + actual.Tag() != expected.Tag()) { + if (listener != nullptr) { + *listener << "at " << path << ": tag mismatch (actual: \"" << actual.Tag() + << "\", expected: \"" << expected.Tag() << "\")"; + } + return false; + } + + switch (expected.Type()) { + case YAML::NodeType::Undefined: + case YAML::NodeType::Null: + return true; + + case YAML::NodeType::Scalar: + if (actual.Scalar() != expected.Scalar()) { + if (listener != nullptr) { + *listener << "at " << path << ": scalar mismatch (actual: \"" + << actual.Scalar() << "\", expected: \"" + << expected.Scalar() << "\")"; + } + return false; + } + return true; + + case YAML::NodeType::Sequence: { + if (actual.size() != expected.size()) { + if (listener != nullptr) { + *listener << "at " << path + << ": sequence size mismatch (actual: " << actual.size() + << ", expected: " << expected.size() << ")"; + } + return false; + } + for (size_t i = 0; i < expected.size(); ++i) { + if (!NodesEqual(actual[i], expected[i], absl::StrCat(path, "[", i, "]"), + depth + 1, listener)) { + return false; + } + } + return true; + } + + case YAML::NodeType::Map: { + absl::flat_hash_set expected_keys; + for (const auto& entry : expected) { + const YAML::Node& key = entry.first; + if (!key.IsScalar()) { + if (listener != nullptr) { + *listener << "at " << path + << ": unsupported non-scalar map key in expected YAML" + << " (type: " << NodeTypeName(key.Type()) << ")"; + } + return false; + } + const std::string& key_str = key.Scalar(); + if (!expected_keys.insert(key_str).second) { + if (listener != nullptr) { + *listener << "at " << path << ": duplicate key \"" << key_str + << "\" in expected YAML"; + } + return false; + } + } + + absl::flat_hash_set actual_keys; + for (const auto& entry : actual) { + const YAML::Node& key = entry.first; + if (!key.IsScalar()) { + if (listener != nullptr) { + *listener << "at " << path + << ": unsupported non-scalar map key in actual YAML" + << " (type: " << NodeTypeName(key.Type()) << ")"; + } + return false; + } + const std::string& key_str = key.Scalar(); + if (!actual_keys.insert(key_str).second) { + if (listener != nullptr) { + *listener << "at " << path << ": duplicate key \"" << key_str + << "\" in actual YAML"; + } + return false; + } + } + + for (const auto& entry : expected) { + const std::string& key_str = entry.first.Scalar(); + const YAML::Node& expected_value = entry.second; + const YAML::Node actual_value = actual[key_str]; + if (!actual_value.IsDefined()) { + if (listener != nullptr) { + *listener << "at " << path << ": missing key \"" << key_str << "\""; + } + return false; + } + if (!NodesEqual(actual_value, expected_value, + absl::StrCat(path, ".", key_str), depth + 1, + listener)) { + return false; + } + } + + for (const auto& entry : actual) { + const std::string& key_str = entry.first.Scalar(); + if (!expected_keys.contains(key_str)) { + if (listener != nullptr) { + *listener << "at " << path << ": unexpected key \"" << key_str + << "\""; + } + return false; + } + } + return true; + } + } + return false; +} + +class YamlMatcherImpl : public testing::MatcherInterface { + public: + explicit YamlMatcherImpl(absl::string_view expected_yaml) + : expected_yaml_(expected_yaml) {} + + bool MatchAndExplain(absl::string_view actual_yaml, + testing::MatchResultListener* listener) const override { + YAML::Node expected_node; + try { + expected_node = YAML::Load(expected_yaml_); + } catch (const YAML::Exception& e) { + if (listener != nullptr) { + *listener << "failed to parse expected YAML: " << e.what(); + } + return false; + } + + YAML::Node actual_node; + try { + actual_node = YAML::Load(std::string(actual_yaml)); + } catch (const YAML::Exception& e) { + if (listener != nullptr) { + *listener << "failed to parse actual YAML: " << e.what(); + } + return false; + } + + return NodesEqual(actual_node, expected_node, "$", /*depth=*/0, listener); + } + + void DescribeTo(std::ostream* os) const override { + *os << "equals YAML:\n" << expected_yaml_; + } + + void DescribeNegationTo(std::ostream* os) const override { + *os << "does not equal YAML:\n" << expected_yaml_; + } + + private: + std::string expected_yaml_; +}; + +} // namespace + +testing::Matcher EqualsYaml( + absl::string_view expected_yaml) { + return testing::MakeMatcher(new YamlMatcherImpl(expected_yaml)); +} + +} // namespace cel diff --git a/conformance/env/yaml_matcher.h b/conformance/env/yaml_matcher.h new file mode 100644 index 000000000..c31271e12 --- /dev/null +++ b/conformance/env/yaml_matcher.h @@ -0,0 +1,32 @@ +// 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_CONFORMANCE_ENV_YAML_MATCHER_H_ +#define THIRD_PARTY_CEL_CPP_CONFORMANCE_ENV_YAML_MATCHER_H_ + +#include "absl/strings/string_view.h" +#include "internal/testing.h" + +namespace cel { + +// Returns a matcher that parses both the actual and expected YAML strings and +// recursively compares the resulting YAML node trees for semantic equality +// (ignoring formatting, comments, quoting style, and map key ordering). +// Map keys must be unique scalars; non-scalar or duplicate map keys are +// unsupported and fail to match. +testing::Matcher EqualsYaml(absl::string_view expected_yaml); + +} // namespace cel + +#endif // THIRD_PARTY_CEL_CPP_CONFORMANCE_ENV_YAML_MATCHER_H_ diff --git a/conformance/env/yaml_matcher_test.cc b/conformance/env/yaml_matcher_test.cc new file mode 100644 index 000000000..203430480 --- /dev/null +++ b/conformance/env/yaml_matcher_test.cc @@ -0,0 +1,232 @@ +// 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 "conformance/env/yaml_matcher.h" + +#include +#include + +#include "internal/testing.h" + +namespace cel { +namespace { + +using testing::Not; + +TEST(YamlMatcherTest, MatchesIdenticalYaml) { + EXPECT_THAT( + "variables:\n" + " - name: x\n" + " type: bool\n", + EqualsYaml("variables:\n" + " - name: x\n" + " type: bool\n")); +} + +TEST(YamlMatcherTest, IgnoresWhitespaceLineEndingsCommentsAndIndentation) { + EXPECT_THAT( + "variables: \r\n" + "\r\n" + " # a comment\n" + " - name: x\t \r\n" + " type: bool \n", + EqualsYaml("variables:\n" + " - name: x\n" + " type: bool")); +} + +TEST(YamlMatcherTest, IgnoresQuotingStyleDifferences) { + EXPECT_THAT( + "variables:\n" + " - name: \"x\"\n" + " type: \"map\"\n", + EqualsYaml("variables:\n" + " - name: 'x'\n" + " type: map\n")); +} + +TEST(YamlMatcherTest, MatchesEquivalentDoubleAndSingleQuotedEscapes) { + EXPECT_THAT(R"(type: "~T\\.U")", EqualsYaml(R"(type: '~T\.U')")); + EXPECT_THAT("a: \"it's\"\n", EqualsYaml("a: 'it''s'\n")); +} + +TEST(YamlMatcherTest, IgnoresMapKeyOrdering) { + EXPECT_THAT( + "a: 1\n" + "b:\n" + " x: true\n" + " y: false\n", + EqualsYaml("b:\n" + " y: false\n" + " x: true\n" + "a: 1\n")); +} + +TEST(YamlMatcherTest, MatchesFlowAndBlockStyles) { + EXPECT_THAT( + "params: [int, bool, string]\n" + "meta: {k1: v1, k2: v2}\n", + EqualsYaml("params:\n" + " - int\n" + " - bool\n" + " - string\n" + "meta:\n" + " k2: v2\n" + " k1: v1\n")); +} + +TEST(YamlMatcherTest, MatchesEmptyAndNullDocuments) { + EXPECT_THAT("", EqualsYaml("")); + EXPECT_THAT("# only a comment\n", EqualsYaml("")); + EXPECT_THAT("a:\n", EqualsYaml("a: null\n")); + EXPECT_THAT("a: ~", EqualsYaml("a: null")); +} + +TEST(YamlMatcherTest, DistinguishesEmptyStringFromNull) { + EXPECT_THAT("a: \"\"", Not(EqualsYaml("a: null"))); + EXPECT_THAT("a: ''", Not(EqualsYaml("a:"))); +} + +TEST(YamlMatcherTest, DetectsScalarMismatch) { + EXPECT_THAT( + "variables:\n" + " - name: x\n" + " type: int\n", + Not(EqualsYaml("variables:\n" + " - name: x\n" + " type: bool\n"))); +} + +TEST(YamlMatcherTest, DetectsSequenceOrderAndSizeMismatch) { + EXPECT_THAT( + "params:\n" + " - int\n" + " - bool\n", + Not(EqualsYaml("params:\n" + " - bool\n" + " - int\n"))); + EXPECT_THAT( + "params:\n" + " - int\n", + Not(EqualsYaml("params:\n" + " - int\n" + " - bool\n"))); +} + +TEST(YamlMatcherTest, DetectsMissingAndExtraMapKeys) { + EXPECT_THAT("a: 1\n", Not(EqualsYaml("a: 1\nb: 2\n"))); + EXPECT_THAT("a: 1\nb: 2\n", Not(EqualsYaml("a: 1\n"))); +} + +TEST(YamlMatcherTest, DetectsNodeTypeMismatch) { + testing::StringMatchResultListener seq_vs_map_listener; + EXPECT_FALSE(testing::ExplainMatchResult( + EqualsYaml("a:\n b: 1\n"), "a:\n - 1\n", &seq_vs_map_listener)); + EXPECT_THAT( + seq_vs_map_listener.str(), + testing::HasSubstr("type mismatch (actual: sequence, expected: map)")); + + testing::StringMatchResultListener scalar_vs_seq_listener; + EXPECT_FALSE(testing::ExplainMatchResult(EqualsYaml("a:\n - 1\n"), "a: 1\n", + &scalar_vs_seq_listener)); + EXPECT_THAT( + scalar_vs_seq_listener.str(), + testing::HasSubstr("type mismatch (actual: scalar, expected: sequence)")); + + testing::StringMatchResultListener null_vs_scalar_listener; + EXPECT_FALSE(testing::ExplainMatchResult(EqualsYaml("a: 1\n"), "a: null\n", + &null_vs_scalar_listener)); + EXPECT_THAT( + null_vs_scalar_listener.str(), + testing::HasSubstr("type mismatch (actual: null, expected: scalar)")); +} + +TEST(YamlMatcherTest, DetectsExplicitTagMismatch) { + EXPECT_THAT("data: !!binary SGVsbG8=", EqualsYaml("data: !!binary SGVsbG8=")); + EXPECT_THAT("data: !!binary SGVsbG8=", Not(EqualsYaml("data: SGVsbG8="))); +} + +TEST(YamlMatcherTest, FailsGracefullyOnInvalidYaml) { + EXPECT_THAT("a: [unclosed", Not(EqualsYaml("a: 1"))); + EXPECT_THAT("a: 1", Not(EqualsYaml("a: [unclosed"))); +} + +TEST(YamlMatcherTest, RejectsNonScalarMapKeys) { + testing::StringMatchResultListener expected_listener; + EXPECT_FALSE(testing::ExplainMatchResult( + EqualsYaml("? [a, b]\n: 1\n"), "? [a, b]\n: 1\n", &expected_listener)); + EXPECT_THAT(expected_listener.str(), + testing::HasSubstr("unsupported non-scalar map key")); + + testing::StringMatchResultListener actual_listener; + EXPECT_FALSE(testing::ExplainMatchResult( + EqualsYaml("a: 1\n"), "a: 1\n? [b, c]\n: 2\n", &actual_listener)); + EXPECT_THAT(actual_listener.str(), + testing::HasSubstr("unsupported non-scalar map key")); +} + +TEST(YamlMatcherTest, RejectsDuplicateMapKeys) { + testing::StringMatchResultListener actual_listener; + EXPECT_FALSE(testing::ExplainMatchResult(EqualsYaml("variables:\n" + " - name: x\n" + " type: bool\n"), + "variables:\n" + " - name: x\n" + " type: bool\n" + " type: int\n", + &actual_listener)); + EXPECT_THAT(actual_listener.str(), + testing::HasSubstr("duplicate key \"type\" in actual YAML")); + + testing::StringMatchResultListener expected_listener; + EXPECT_FALSE(testing::ExplainMatchResult(EqualsYaml("variables:\n" + " - name: x\n" + " type: bool\n" + " type: int\n"), + "variables:\n" + " - name: x\n" + " type: bool\n", + &expected_listener)); + EXPECT_THAT(expected_listener.str(), + testing::HasSubstr("duplicate key \"type\" in expected YAML")); + + testing::StringMatchResultListener both_listener; + EXPECT_FALSE(testing::ExplainMatchResult(EqualsYaml("a: 1\na: 1\n"), + "a: 1\na: 2\n", &both_listener)); + EXPECT_THAT(both_listener.str(), testing::HasSubstr("duplicate key \"a\"")); +} + +TEST(YamlMatcherTest, RejectsExcessiveNestingDepth) { + std::string deep_yaml = std::string(102, '[') + std::string(102, ']'); + testing::StringMatchResultListener listener; + EXPECT_FALSE( + testing::ExplainMatchResult(EqualsYaml(deep_yaml), deep_yaml, &listener)); + EXPECT_THAT(listener.str(), + testing::HasSubstr("exceeded maximum nesting depth")); +} + +TEST(YamlMatcherTest, DescribesMatcherAndNegation) { + auto matcher = EqualsYaml("a: 1\n"); + + std::ostringstream os; + matcher.DescribeTo(&os); + EXPECT_THAT(os.str(), testing::HasSubstr("equals YAML:\na: 1\n")); + + std::ostringstream neg_os; + matcher.DescribeNegationTo(&neg_os); + EXPECT_THAT(neg_os.str(), testing::HasSubstr("does not equal YAML:\na: 1\n")); +} + +} // namespace +} // namespace cel diff --git a/env/env_yaml.cc b/env/env_yaml.cc index a0109c158..ad32b592a 100644 --- a/env/env_yaml.cc +++ b/env/env_yaml.cc @@ -80,19 +80,58 @@ std::string FormatYamlErrorMessage(absl::string_view yaml, return message; } +constexpr int kMaxYamlDepth = 100; +constexpr int kMaxYamlNodes = 1000000; + +absl::Status YamlError(absl::string_view yaml, const YAML::Node& node, + absl::string_view error) { + return absl::InvalidArgumentError( + FormatYamlErrorMessage(yaml, error, node.Mark())); +} + +absl::Status ValidateNoDuplicateKeys(absl::string_view yaml, + const YAML::Node& node, int depth, + int& remaining_nodes) { + if (depth > kMaxYamlDepth) { + return YamlError(yaml, node, "YAML nesting depth exceeds maximum"); + } + if (--remaining_nodes < 0) { + return YamlError(yaml, node, "YAML node count exceeds maximum"); + } + if (node.IsMap()) { + absl::flat_hash_set seen_keys; + for (const auto& kv : node) { + if (kv.first.IsScalar()) { + const std::string& key = kv.first.Scalar(); + if (!seen_keys.insert(key).second) { + return YamlError(yaml, kv.first, + absl::StrCat("Duplicate key '", key, "'")); + } + } + CEL_RETURN_IF_ERROR( + ValidateNoDuplicateKeys(yaml, kv.second, depth + 1, remaining_nodes)); + } + } else if (node.IsSequence()) { + for (const YAML::Node& element : node) { + CEL_RETURN_IF_ERROR( + ValidateNoDuplicateKeys(yaml, element, depth + 1, remaining_nodes)); + } + } + return absl::OkStatus(); +} + absl::StatusOr LoadYaml(const std::string& yaml) { + YAML::Node root; try { - return YAML::Load(yaml); + root = YAML::Load(yaml); } catch (YAML::ParserException& e) { return absl::InvalidArgumentError( FormatYamlErrorMessage(yaml, e.msg, e.mark)); } -} - -absl::Status YamlError(absl::string_view yaml, const YAML::Node& node, - absl::string_view error) { - return absl::InvalidArgumentError( - FormatYamlErrorMessage(yaml, error, node.Mark())); + int remaining_nodes = kMaxYamlNodes; + CEL_RETURN_IF_ERROR( + ValidateNoDuplicateKeys(yaml, root, /*depth=*/0, remaining_nodes)); + return root; } std::string GetString(absl::string_view yaml, const YAML::Node& node) { diff --git a/env/env_yaml_test.cc b/env/env_yaml_test.cc index 10c473b31..0ce18fc05 100644 --- a/env/env_yaml_test.cc +++ b/env/env_yaml_test.cc @@ -2199,5 +2199,36 @@ TEST(EnvYamlTest, StrictAcceptsSupportedFields) { IsOk()); } +TEST(EnvYamlTest, RejectsDuplicateKeys) { + EXPECT_THAT(EnvConfigFromYaml("variables:\n" + " - name: x\n" + " type: bool\n" + " type: int\n"), + StatusIs(absl::StatusCode::kInvalidArgument, + HasSubstr("4:5: Duplicate key 'type'"))); + EXPECT_THAT(EnvConfigFromYaml("name: a\n" + "name: b\n"), + StatusIs(absl::StatusCode::kInvalidArgument, + HasSubstr("2:1: Duplicate key 'name'"))); +} + +TEST(EnvYamlTest, RejectsExcessiveNestingDepth) { + std::string deep_yaml = std::string(102, '[') + std::string(102, ']'); + EXPECT_THAT(EnvConfigFromYaml(deep_yaml), + StatusIs(absl::StatusCode::kInvalidArgument, + HasSubstr("YAML nesting depth exceeds maximum"))); +} + +TEST(EnvYamlTest, RejectsExcessiveNodeCount) { + std::string alias_bomb = "a0: &a0 [1, 1]\n"; + for (int i = 1; i <= 20; ++i) { + absl::StrAppendFormat(&alias_bomb, "a%d: &a%d [*a%d, *a%d]\n", i, i, i - 1, + i - 1); + } + EXPECT_THAT(EnvConfigFromYaml(alias_bomb), + StatusIs(absl::StatusCode::kInvalidArgument, + HasSubstr("YAML node count exceeds maximum"))); +} + } // namespace } // namespace cel