Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Handle bad regexp in allowed collection API key.

Kishore Nalland05c51c5361a
diff --git a/include/auth_manager.h b/include/auth_manager.h
index 94c8704a..eb7380ce 100644
--- a/include/auth_manager.h
+++ b/include/auth_manager.h
@@ -117,6 +117,8 @@ private:
bool auth_against_key(const std::string& req_collection, const std::string& action,
const api_key_t &api_key, const bool search_only) const;
+ static bool regexp_match(const std::string& value, const std::string& regexp);
+
public:
static const size_t GENERATED_KEY_LEN = 32;
diff --git a/src/auth_manager.cpp b/src/auth_manager.cpp
index 4b073365..c763694e 100644
--- a/src/auth_manager.cpp
+++ b/src/auth_manager.cpp
@@ -176,6 +176,15 @@ bool AuthManager::authenticate(const std::string& action,
return (num_keys_matched == collection_keys.size());
}
+bool AuthManager::regexp_match(const std::string& value, const std::string& regexp) {
+ try {
+ return std::regex_match (value, std::regex(regexp));
+ } catch(const std::exception& e) {
+ LOG(ERROR) << "Error while matching regexp " << regexp << " against value " << value;
+ return false;
+ }
+}
+
bool AuthManager::auth_against_key(const std::string& req_collection, const std::string& action,
const api_key_t& api_key, const bool search_only) const {
@@ -221,7 +230,7 @@ bool AuthManager::auth_against_key(const std::string& req_collection, const std:
for(const std::string& allowed_collection: api_key.collections) {
if(allowed_collection == "*" || (allowed_collection == req_collection) || req_collection.empty() ||
- std::regex_match (req_collection, std::regex(allowed_collection))) {
+ regexp_match(req_collection, allowed_collection)) {
coll_allowed = true;
break;
}
diff --git a/test/auth_manager_test.cpp b/test/auth_manager_test.cpp
index 31b2915a..5db876b3 100644
--- a/test/auth_manager_test.cpp
+++ b/test/auth_manager_test.cpp
@@ -251,6 +251,13 @@ TEST_F(AuthManagerTest, VerifyAuthentication) {
{collection_key_t("collection1", coll_a_key.value),
collection_key_t("collectionB", coll_b_key.value)},
sparams, embedded_params));
+
+ // bad collection allow regexp
+ api_key_t coll_c_key = api_key_t("coll_c", "one action key", {"documents:search"}, {"*coll_c"}, FUTURE_TS);
+ auth_manager.create_key(coll_c_key);
+ ASSERT_FALSE(auth_manager.authenticate("documents:search",
+ {collection_key_t("coll_c", coll_c_key.value),},
+ sparams, embedded_params));
}
TEST_F(AuthManagerTest, GenerationOfAPIAction) {