From dfa09f285bfff4c4dddfc99d76a862dd88372cde Mon Sep 17 00:00:00 2001 From: Ryan Lamb <4955475+kinyoklion@users.noreply.github.com> Date: Thu, 19 Nov 2026 00:14:41 +0000 Subject: [PATCH] fix: Evaluate fixed variations of flags and segments that have no salt A flag without a salt returned MALFORMED_FLAG for every rule and fallthrough, even one that serves a fixed variation, and a segment without a salt failed every segment rule. Check for the salt only where bucketing uses it: a rollout without a seed, and a segment rule with a weight. Generated-by: termag --- libs/server-sdk/src/evaluation/bucketing.cpp | 8 +- libs/server-sdk/src/evaluation/bucketing.hpp | 3 +- libs/server-sdk/src/evaluation/rules.cpp | 13 +- libs/server-sdk/src/evaluation/rules.hpp | 3 +- libs/server-sdk/tests/evaluator_tests.cpp | 125 +++++++++++++++++++ 5 files changed, 141 insertions(+), 11 deletions(-) diff --git a/libs/server-sdk/src/evaluation/bucketing.cpp b/libs/server-sdk/src/evaluation/bucketing.cpp index 1333ee1c0..42fcb8008 100644 --- a/libs/server-sdk/src/evaluation/bucketing.cpp +++ b/libs/server-sdk/src/evaluation/bucketing.cpp @@ -154,9 +154,6 @@ tl::expected Variation( std::string const& flag_key, Context const& context, std::optional const& salt) { - if (!salt) { - return tl::make_unexpected(Error::MissingSalt(flag_key)); - } return std::visit( [&](auto&& arg) -> tl::expected { using T = std::decay_t; @@ -175,6 +172,11 @@ tl::expected Variation( bool is_experiment = arg.kind == Flag::Rollout::Kind::kExperiment; + // Only a rollout without a seed uses the salt. + if (!arg.seed && !salt) { + return tl::make_unexpected(Error::MissingSalt(flag_key)); + } + std::optional prefix = arg.seed ? BucketPrefix(*arg.seed) : BucketPrefix(flag_key, *salt); diff --git a/libs/server-sdk/src/evaluation/bucketing.hpp b/libs/server-sdk/src/evaluation/bucketing.hpp index 6a2081136..4e197f856 100644 --- a/libs/server-sdk/src/evaluation/bucketing.hpp +++ b/libs/server-sdk/src/evaluation/bucketing.hpp @@ -112,7 +112,8 @@ class BucketResult { * @param variation_or_rollout Variation or rollout. * @param flag_key Key of flag. * @param context Context to bucket. - * @param salt Salt to use when bucketing. + * @param salt Salt to use when bucketing. Only a rollout without a seed + * needs a salt. * @return A BucketResult on success, or an error if bucketing failed. */ tl::expected Variation( diff --git a/libs/server-sdk/src/evaluation/rules.cpp b/libs/server-sdk/src/evaluation/rules.cpp index 5a5350242..811961423 100644 --- a/libs/server-sdk/src/evaluation/rules.cpp +++ b/libs/server-sdk/src/evaluation/rules.cpp @@ -118,7 +118,7 @@ tl::expected Match(Segment::Rule const& rule, data_interfaces::IStore const& store, EvaluationStack& stack, std::string const& key, - std::string const& salt) { + std::optional const& salt) { for (Clause const& clause : rule.clauses) { auto maybe_match = Match(clause, context, store, stack); if (!maybe_match) { @@ -130,7 +130,11 @@ tl::expected Match(Segment::Rule const& rule, } if (rule.weight && rule.weight >= 0.0) { - BucketPrefix prefix(key, salt); + // Only a rule with a weight uses the salt. + if (!salt) { + return tl::make_unexpected(Error::MissingSalt(key)); + } + BucketPrefix prefix(key, *salt); auto maybe_bucket = Bucket(context, rule.bucketBy, prefix, false, rule.rolloutContextKind); if (!maybe_bucket) { @@ -261,11 +265,8 @@ tl::expected Contains(Segment const& segment, } for (auto const& rule : segment.rules) { - if (!segment.salt) { - return tl::make_unexpected(Error::MissingSalt(segment.key)); - } tl::expected maybe_match = - Match(rule, context, store, stack, segment.key, *segment.salt); + Match(rule, context, store, stack, segment.key, segment.salt); if (!maybe_match) { return tl::make_unexpected(maybe_match.error()); } diff --git a/libs/server-sdk/src/evaluation/rules.hpp b/libs/server-sdk/src/evaluation/rules.hpp index c991c7ac3..d423ad303 100644 --- a/libs/server-sdk/src/evaluation/rules.hpp +++ b/libs/server-sdk/src/evaluation/rules.hpp @@ -10,6 +10,7 @@ #include +#include #include namespace launchdarkly::server_side::evaluation { @@ -31,7 +32,7 @@ namespace launchdarkly::server_side::evaluation { data_interfaces::IStore const& store, EvaluationStack& stack, std::string const& key, - std::string const& salt); + std::optional const& salt); [[nodiscard]] tl::expected MatchSegment( data_model::Clause const&, diff --git a/libs/server-sdk/tests/evaluator_tests.cpp b/libs/server-sdk/tests/evaluator_tests.cpp index 9a824d00a..da73a3ac0 100644 --- a/libs/server-sdk/tests/evaluator_tests.cpp +++ b/libs/server-sdk/tests/evaluator_tests.cpp @@ -1,3 +1,4 @@ +#include "data_components/memory_store/memory_store.hpp" #include "evaluation/evaluator.hpp" #include "test_store.hpp" @@ -276,3 +277,127 @@ TEST_F(EvaluatorTests, ThreadSafeEvaluation) { t.join(); } } + +// Evaluates flags and segments that have no salt. The parameter is the salt +// property of each flag and segment: omitted, or null. +class EvaluatorWithoutSaltTests : public ::testing::TestWithParam { + public: + EvaluatorWithoutSaltTests() + : logger_(logging::NullLogger()), eval_(logger_, store_) { + store_.Init({}); + } + + protected: + // Returns the flag or segment JSON with the salt property of the test + // parameter after the key. + static std::string WithSalt(std::string const& key, + std::string const& rest) { + return R"({"key":")" + key + R"(",)" + GetParam() + rest; + } + + data_components::MemoryStore store_; + + private: + Logger logger_; + + protected: + evaluation::Evaluator eval_; +}; + +TEST_P(EvaluatorWithoutSaltTests, FixedFallthroughIsNotAnError) { + auto const flag = test_store::Flag( + WithSalt("flag", R"("version":1,"on":true,"variations":["a","b"], + "fallthrough":{"variation":1}})") + .c_str()); + + // Evaluate a flag that serves a fixed variation from its fallthrough. + auto const detail = + eval_.Evaluate(*flag.item, ContextBuilder().Kind("user", "u").Build()); + + // The flag serves the variation without a MALFORMED_FLAG error. + ASSERT_FALSE(detail.IsError()); + EXPECT_EQ(*detail, Value("b")); + EXPECT_EQ(detail.Reason(), EvaluationReason::Fallthrough(false)); +} + +TEST_P(EvaluatorWithoutSaltTests, FixedRuleIsNotAnError) { + auto const flag = test_store::Flag( + WithSalt("flag", R"("version":1,"on":true,"variations":["a","b"], + "fallthrough":{"variation":0}, + "rules":[{"id":"rule","variation":1,"clauses":[ + {"attribute":"key","op":"in","values":["u"]}]}]})") + .c_str()); + + // Evaluate a flag whose matching rule serves a fixed variation. + auto const detail = + eval_.Evaluate(*flag.item, ContextBuilder().Kind("user", "u").Build()); + + // The rule serves the variation without a MALFORMED_FLAG error. + ASSERT_FALSE(detail.IsError()); + EXPECT_EQ(*detail, Value("b")); + EXPECT_EQ(detail.Reason(), EvaluationReason::RuleMatch(0, "rule", false)); +} + +TEST_P(EvaluatorWithoutSaltTests, RolloutWithSeedIsNotAnError) { + auto const flag = test_store::Flag( + WithSalt("flag", R"("version":1,"on":true,"variations":["a","b"], + "fallthrough":{"rollout":{"seed":61,"variations":[ + {"variation":1,"weight":100000}]}}})") + .c_str()); + + // Evaluate a flag whose fallthrough is a rollout with a seed. The seed + // replaces the salt when the SDK buckets the context. + auto const detail = + eval_.Evaluate(*flag.item, ContextBuilder().Kind("user", "u").Build()); + + // The rollout serves its variation without a MALFORMED_FLAG error. + ASSERT_FALSE(detail.IsError()); + EXPECT_EQ(*detail, Value("b")); +} + +TEST_P(EvaluatorWithoutSaltTests, RolloutWithoutSeedIsMalformed) { + auto const flag = test_store::Flag( + WithSalt("flag", R"("version":1,"on":true,"variations":["a","b"], + "fallthrough":{"rollout":{"variations":[ + {"variation":1,"weight":100000}]}}})") + .c_str()); + + // Evaluate a flag whose fallthrough is a rollout without a seed. The SDK + // has no salt to bucket the context with. + auto const detail = + eval_.Evaluate(*flag.item, ContextBuilder().Kind("user", "u").Build()); + + // The flag is malformed. + EXPECT_EQ(detail.Reason(), EvaluationReason::MalformedFlag()); +} + +TEST_P(EvaluatorWithoutSaltTests, SegmentRuleIsNotAnError) { + store_.Upsert("segment", + test_store::Segment( + WithSalt("segment", R"("version":1,"rules":[{"clauses":[ + {"attribute":"key","op":"in","values":["u"]}]}]})") + .c_str())); + auto const flag = test_store::Flag(R"({"key":"flag","salt":"salt", + "version":1,"on":true,"variations":["a","b"], + "fallthrough":{"variation":0}, + "rules":[{"id":"rule","variation":1,"clauses":[ + {"attribute":"","op":"segmentMatch","values":["segment"]}]}]})"); + + // Evaluate a flag whose rule matches a segment by the segment's rule. The + // segment rule has no weight. + auto const detail = + eval_.Evaluate(*flag.item, ContextBuilder().Kind("user", "u").Build()); + + // The segment rule matches without a MALFORMED_FLAG error. + ASSERT_FALSE(detail.IsError()); + EXPECT_EQ(*detail, Value("b")); + EXPECT_EQ(detail.Reason(), EvaluationReason::RuleMatch(0, "rule", false)); +} + +INSTANTIATE_TEST_SUITE_P(SaltProperty, + EvaluatorWithoutSaltTests, + ::testing::Values("", R"("salt":null,)"), + [](::testing::TestParamInfo const& info) { + return std::string( + info.param[0] == '\0' ? "Omitted" : "Null"); + });