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
C++ algoliadatastoreelasticsearchenterprise-searchfacetingfull-text-searchfuzzy-searchgeosearchin-memoryinstantsearchmerchandisingpineconesearchsearch-enginesemantic-searchsimilarity-searchsite-searchsynonymstypo-tolerancevector-search
Fix scoped API searching on prefix-sharing keys.
Kishore Nallan0341e693d18a
diff --git a/src/auth_manager.cpp b/src/auth_manager.cppindex b82c038c..929660e4 100644--- a/src/auth_manager.cpp+++ b/src/auth_manager.cpp@@ -255,7 +255,8 @@ Option<bool> AuthManager::authenticate_parse_params(const collection_key_t& scopconst std::string& key_prefix = key_payload.substr(HMAC_BASE64_LEN, api_key_t::PREFIX_LEN);const std::string& custom_params = key_payload.substr(HMAC_BASE64_LEN + api_key_t::PREFIX_LEN);- // calculate and verify hmac against matching api key+ // Calculate and verify hmac against matching api key.+ // There could be several matching keys since we look up only on a 4-char prefix.auto prefix_range = api_keys.equal_prefix_range(key_prefix);for(auto it = prefix_range.first; it != prefix_range.second; ++it) {@@ -265,8 +266,7 @@ Option<bool> AuthManager::authenticate_parse_params(const collection_key_t& scopbool auth_success = auth_against_key(scoped_api_key.collection, action, root_api_key, true);if(!auth_success) {- LOG(ERROR) << fmt_error("Parent key does not allow queries against queried collection.", root_api_key.value);- return Option<bool>(403, "Forbidden.");+ continue;}// finally verify hmac@@ -276,27 +276,23 @@ Option<bool> AuthManager::authenticate_parse_params(const collection_key_t& scoptry {embedded_params = nlohmann::json::parse(custom_params);} catch(const std::exception& e) {- LOG(ERROR) << "JSON error: " << e.what();- return Option<bool>(403, "Forbidden.");+ continue;}if(!embedded_params.is_object()) {- LOG(ERROR) << fmt_error("Scoped API key contains invalid search parameters.", root_api_key.value);- return Option<bool>(403, "Forbidden.");+ continue;}if(embedded_params.count("expires_at") != 0) {if(!embedded_params["expires_at"].is_number_integer() || embedded_params["expires_at"].get<int64_t>() < 0) {- LOG(ERROR) << fmt_error("Wrong format for `expires_at`. It should be an unsigned integer.", root_api_key.value);- return Option<bool>(403, "Forbidden.");+ continue;}// if parent key's expiry timestamp is smaller, it takes precedenceuint64_t expiry_ts = std::min(root_api_key.expires_at, embedded_params["expires_at"].get<uint64_t>());if(uint64_t(std::time(0)) > expiry_ts) {- LOG(ERROR) << fmt_error("Scoped API key has expired. ", root_api_key.value);- return Option<bool>(403, "Forbidden.");+ continue;}}diff --git a/test/auth_manager_test.cpp b/test/auth_manager_test.cppindex 7e15fd75..4b93e00a 100644--- a/test/auth_manager_test.cpp+++ b/test/auth_manager_test.cpp@@ -319,10 +319,17 @@ TEST_F(AuthManagerTest, ScopedAPIKeys) {ASSERT_EQ("user_id:1080", embedded_params[0]["filter_by"].get<std::string>());// when more than a single key prefix matches, must pick the correct underlying key+api_key_t key_search_coll2("KeyVal2", "test key", {"documents:search"}, {"coll2"}, FUTURE_TS);auth_manager.create_key(key_search_coll2);+ ASSERT_TRUE(auth_manager.authenticate("documents:search", {collection_key_t("coll1", scoped_key)}, empty_params, embedded_params));ASSERT_FALSE(auth_manager.authenticate("documents:search", {collection_key_t("coll2", scoped_key)}, empty_params, embedded_params));+ // scoped key generated from key_search_coll2+ std::string scoped_key_prefix2 = "QmNlNXdkUThaeDJFZXNiOXB4VUFCT1BmN01GSEJnRUdiMng2aTJESjJqND1LZXlWeyJmaWx0ZXJfYnkiOiAidXNlcl9pZDoxMDgwIn0=";+ ASSERT_TRUE(auth_manager.authenticate("documents:search", {collection_key_t("coll2", scoped_key_prefix2)}, empty_params, embedded_params));+ ASSERT_FALSE(auth_manager.authenticate("documents:search", {collection_key_t("coll1", scoped_key_prefix2)}, empty_params, embedded_params));+// should only allow scoped API keys derived from parent key with documents:search actionapi_key_t key_search_admin("AdminKey", "admin key", {"*"}, {"*"}, FUTURE_TS);auth_manager.create_key(key_search_admin);