From 5b7a35e02bb0ded8755bd4fa54ce73b459cea598 Mon Sep 17 00:00:00 2001 From: Piotr Sarna Date: Fri, 6 Mar 2020 08:00:34 +0100 Subject: [PATCH] network_topology_strategy: validate integers In order to prevent users from creating a network topology strategy instance with invalid inputs, it's not enough to use std::stol() on the input: a string "3abc" still returns the number '3', but will later confuse cqlsh and other drivers, when they ask for topology strategy details. The error message is now more human readable, since for incorrect numeric inputs it used to return a rather cryptic message: ServerError: stol() This commit fixes the issue and comes with a simple test. Fixes #3801 Tests: unit(dev) Message-Id: <7aaae83d003738f047d28727430ca0a5cec6b9c6.1583478000.git.sarna@scylladb.com> --- locator/network_topology_strategy.cc | 4 ++++ test/boost/network_topology_strategy_test.cc | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/locator/network_topology_strategy.cc b/locator/network_topology_strategy.cc index 827e8cd682..43855aade0 100644 --- a/locator/network_topology_strategy.cc +++ b/locator/network_topology_strategy.cc @@ -85,6 +85,10 @@ network_topology_strategy::network_topology_strategy( "NetworkTopologyStrategy"); } + if (val.empty() || std::any_of(val.begin(), val.end(), [] (char c) {return !isdigit(c);})) { + throw exceptions::configuration_exception( + format("Replication factor must be numeric and non-negative, found '{}'", val)); + } _dc_rep_factor.emplace(key, std::stol(val)); _datacenteres.push_back(key); } diff --git a/test/boost/network_topology_strategy_test.cc b/test/boost/network_topology_strategy_test.cc index ffefa3b3be..b85265a634 100644 --- a/test/boost/network_topology_strategy_test.cc +++ b/test/boost/network_topology_strategy_test.cc @@ -37,6 +37,7 @@ #include #include #include "test/lib/log.hh" +#include "test/lib/cql_test_env.hh" using namespace locator; @@ -597,4 +598,13 @@ SEASTAR_TEST_CASE(testCalculateEndpoints) { }); } +SEASTAR_TEST_CASE(test_invalid_dcs) { + return do_with_cql_env_thread([] (auto& e) { + for (auto& incorrect : std::vector{"3\"", "", "!!!", "abcb", "!3", "-5", "0x123"}) { + BOOST_REQUIRE_THROW(e.execute_cql("CREATE KEYSPACE abc WITH REPLICATION " + "= {'class': 'NetworkTopologyStrategy', 'dc1':'" + incorrect + "'}").get(), + exceptions::configuration_exception); + }; + }); +}