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
1 change: 1 addition & 0 deletions libs/common/include/launchdarkly/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ enum class Error : std::uint32_t {
kConfig_Events_ZeroCapacity = 300,

kConfig_SDKKey_Empty = 400,
kConfig_SDKKey_InvalidCharacters = 401,
/* Client-side errors: 10000-19999 */
/* Server-side errors: 20000-29999 */
kConfig_DataSystem_LazyLoad_MissingSource = 20000,
Expand Down
5 changes: 5 additions & 0 deletions libs/common/src/config/config_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ ConfigBuilder<SDK>::Build() const {
if (sdk_key.empty()) {
return tl::make_unexpected(Error::kConfig_SDKKey_Empty);
}
// A line break in the key can end the header that carries it and add
// other headers to the request. Do not send such a key.
if (sdk_key.find_first_of("\r\n") != std::string::npos) {
return tl::make_unexpected(Error::kConfig_SDKKey_InvalidCharacters);
}
auto offline = offline_.value_or(Defaults<SDK>::Offline());
auto endpoints_config = service_endpoints_builder_.Build();
if (!endpoints_config) {
Expand Down
2 changes: 2 additions & 0 deletions libs/common/src/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ char const* ErrorToString(Error err) {
return "events: capacity must be non-zero";
case Error::kConfig_SDKKey_Empty:
return "sdk key: cannot be empty";
case Error::kConfig_SDKKey_InvalidCharacters:
return "sdk key: cannot contain a carriage return or line feed";
case Error::kConfig_DataSystem_LazyLoad_MissingSource:
return "data system: lazy load config requires a source";
case Error::kConfig_BigSegments_NullStore:
Expand Down
12 changes: 12 additions & 0 deletions libs/common/tests/config_builder_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
: public ::testing::
Test { // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes)
protected:
launchdarkly::Logger logger;

Check warning on line 10 in libs/common/tests/config_builder_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/common/tests/config_builder_test.cpp:10:26 [cppcoreguidelines-non-private-member-variables-in-classes]

member variable 'logger' has protected visibility
ConfigBuilderTest() : logger(launchdarkly::logging::NullLogger()) {}
};

Expand All @@ -19,6 +19,18 @@
ASSERT_EQ(cfg->SdkKey(), "sdk-123");
}

TEST_F(ConfigBuilderTest, ClientConfig_MobileKeyWithLineBreakFailsBuild) {
using namespace launchdarkly::client_side;
for (auto const* mobile_key :
{"mob-123\n", "mob-123\r", "mob-123\r\nX-Injected: injected"}) {
auto cfg = ConfigBuilder(mobile_key).Build();

ASSERT_FALSE(cfg);
EXPECT_EQ(cfg.error(),
launchdarkly::Error::kConfig_SDKKey_InvalidCharacters);
}
}

TEST_F(ConfigBuilderTest,
DefaultConstruction_UsesDefaultEndpointsIfNotSupplied) {
using namespace launchdarkly::client_side;
Expand Down
5 changes: 5 additions & 0 deletions libs/server-sdk/src/config/config_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ tl::expected<Config, Error> ConfigBuilder::Build() const {
if (sdk_key.empty()) {
return tl::make_unexpected(Error::kConfig_SDKKey_Empty);
}
// A line break in the key can end the header that carries it and add
// other headers to the request. Do not send such a key.
if (sdk_key.find_first_of("\r\n") != std::string::npos) {
return tl::make_unexpected(Error::kConfig_SDKKey_InvalidCharacters);
}

auto endpoints_config = service_endpoints_builder_.Build();
if (!endpoints_config) {
Expand Down
10 changes: 10 additions & 0 deletions libs/server-sdk/tests/config_builder_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
// Minimal store so a non-null pointer can be threaded through the config.
class StubBigSegmentStore : public server_side::integrations::IBigSegmentStore {
public:
GetMembershipResult GetMembership(

Check warning on line 21 in libs/server-sdk/tests/config_builder_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/server-sdk/tests/config_builder_test.cpp:21:5 [modernize-use-nodiscard]

function 'GetMembership' should be marked [[nodiscard]]
std::string const&) const noexcept override {

Check warning on line 22 in libs/server-sdk/tests/config_builder_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/server-sdk/tests/config_builder_test.cpp:22:27 [readability-named-parameter]

all parameters should be named in a function
return server_side::integrations::Membership::FromSegmentRefs({}, {});
}
GetMetadataResult GetMetadata() const noexcept override {

Check warning on line 25 in libs/server-sdk/tests/config_builder_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/server-sdk/tests/config_builder_test.cpp:25:5 [modernize-use-nodiscard]

function 'GetMetadata' should be marked [[nodiscard]]
return std::optional<server_side::integrations::StoreMetadata>{
std::nullopt};
}
Expand All @@ -32,7 +32,7 @@
class ConfigBuilderTest : public ::testing::Test {
// NOLINT(cppcoreguidelines-non-private-member-variables-in-classes)
protected:
Logger logger;

Check warning on line 35 in libs/server-sdk/tests/config_builder_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/server-sdk/tests/config_builder_test.cpp:35:12 [cppcoreguidelines-non-private-member-variables-in-classes]

member variable 'logger' has protected visibility

ConfigBuilderTest() : logger(logging::NullLogger()) {}
};
Expand Down Expand Up @@ -131,11 +131,11 @@
auto const fdv2_config =
std::get<built::FDv2Config>(cfg->DataSystemConfig().system_);

ASSERT_EQ(fdv2_config.initializers.size(), 1u);

Check warning on line 134 in libs/server-sdk/tests/config_builder_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/server-sdk/tests/config_builder_test.cpp:134:48 [readability-uppercase-literal-suffix]

integer literal has suffix 'u', which is not uppercase
EXPECT_EQ(fdv2_config.initializers[0].poll_interval,
Defaults::FDv2PollingConfig().poll_interval);

ASSERT_EQ(fdv2_config.synchronizers.size(), 2u);

Check warning on line 138 in libs/server-sdk/tests/config_builder_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/server-sdk/tests/config_builder_test.cpp:138:49 [readability-uppercase-literal-suffix]

integer literal has suffix 'u', which is not uppercase
ASSERT_TRUE(std::holds_alternative<built::FDv2Config::StreamingConfig>(
fdv2_config.synchronizers[0]));
EXPECT_EQ(std::get<built::FDv2Config::StreamingConfig>(
Expand Down Expand Up @@ -214,7 +214,7 @@
auto const fdv2_config =
std::get<built::FDv2Config>(cfg->DataSystemConfig().system_);

ASSERT_EQ(fdv2_config.synchronizers.size(), 2u);

Check warning on line 217 in libs/server-sdk/tests/config_builder_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/server-sdk/tests/config_builder_test.cpp:217:49 [readability-uppercase-literal-suffix]

integer literal has suffix 'u', which is not uppercase
ASSERT_TRUE(std::holds_alternative<built::FDv2Config::PollingConfig>(
fdv2_config.synchronizers[0]));
ASSERT_TRUE(std::holds_alternative<built::FDv2Config::StreamingConfig>(
Expand All @@ -238,7 +238,7 @@
auto const fdv2_config =
std::get<built::FDv2Config>(cfg->DataSystemConfig().system_);

ASSERT_EQ(fdv2_config.initializers.size(), 1u);

Check warning on line 241 in libs/server-sdk/tests/config_builder_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/server-sdk/tests/config_builder_test.cpp:241:48 [readability-uppercase-literal-suffix]

integer literal has suffix 'u', which is not uppercase
/* filter_key field removed from public PollingConfig
EXPECT_EQ(fdv2_config.initializers[0].filter_key, "flag-subset");
*/
Expand All @@ -255,7 +255,7 @@
auto const fdv2_config =
std::get<built::FDv2Config>(cfg->DataSystemConfig().system_);

ASSERT_EQ(fdv2_config.synchronizers.size(), 1u);

Check warning on line 258 in libs/server-sdk/tests/config_builder_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/server-sdk/tests/config_builder_test.cpp:258:49 [readability-uppercase-literal-suffix]

integer literal has suffix 'u', which is not uppercase
auto const& sync = std::get<built::FDv2Config::StreamingConfig>(
fdv2_config.synchronizers[0]);
ASSERT_TRUE(sync.base_url_override.has_value());
Expand Down Expand Up @@ -351,6 +351,16 @@
EXPECT_EQ(cfg.error(), Error::kConfig_BigSegments_NullStore);
}

TEST_F(ConfigBuilderTest, SdkKeyWithLineBreakFailsBuild) {
for (auto const* sdk_key :
{"sdk-123\n", "sdk-123\r", "sdk-123\r\nX-Injected: injected"}) {
auto cfg = ConfigBuilder(sdk_key).Build();

ASSERT_FALSE(cfg);
EXPECT_EQ(cfg.error(), Error::kConfig_SDKKey_InvalidCharacters);
}
}

TEST_F(ConfigBuilderTest, CanDisableDataSystem) {
ConfigBuilder builder("sdk-123");

Expand Down
Loading