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

Add validation for embedded key type.

Kishore Nallandba316a7fbf1
diff --git a/src/core_api.cpp b/src/core_api.cpp
index c0db0f8f..766b6dad 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -74,13 +74,14 @@ void get_collections_for_auth(std::map<std::string, std::string>& req_params, co
for(auto& el : obj["searches"]) {
if(el.is_object()) {
std::string coll_name;
- if(el.count("collection") != 0) {
+ if(el.count("collection") != 0 && el["collection"].is_string()) {
coll_name = el["collection"].get<std::string>();
} else if(req_params.count("collection") != 0) {
coll_name = req_params["collection"];
}
- const std::string& access_key = (el.count("x-typesense-api-key") != 0) ?
+ const std::string& access_key = (el.count("x-typesense-api-key") != 0 &&
+ el["x-typesense-api-key"].is_string()) ?
el["x-typesense-api-key"].get<std::string>() :
req_auth_key;
diff --git a/test/core_api_utils_test.cpp b/test/core_api_utils_test.cpp
index e1314716..e9685924 100644
--- a/test/core_api_utils_test.cpp
+++ b/test/core_api_utils_test.cpp
@@ -283,6 +283,43 @@ TEST_F(CoreAPIUtilsTest, ExtractCollectionsFromRequestBody) {
ASSERT_EQ("products", collections[0].collection);
ASSERT_EQ("bar", collections[0].api_key);
+ // when api key type is bad
+ collections.clear();
+ embedded_params_vec.clear();
+ rpath = route_path("POST", {"collections"}, post_multi_search, false, false);
+ body = R"(
+ {"searches":[
+ {
+ "query_by": "concat",
+ "collection": "products",
+ "q": "battery",
+ "x-typesense-api-key": 123
+ }
+ ]
+ }
+ )";
+
+ get_collections_for_auth(req_params, body, rpath, "foo", collections, embedded_params_vec);
+ ASSERT_EQ("foo", collections[0].api_key);
+
+ // when collection name is bad
+ collections.clear();
+ embedded_params_vec.clear();
+ rpath = route_path("POST", {"collections"}, post_multi_search, false, false);
+ body = R"(
+ {"searches":[
+ {
+ "query_by": "concat",
+ "collection": 123,
+ "q": "battery"
+ }
+ ]
+ }
+ )";
+
+ get_collections_for_auth(req_params, body, rpath, "foo", collections, embedded_params_vec);
+ ASSERT_EQ("", collections[0].collection);
+
// get collection for multi-search
collections.clear();
embedded_params_vec.clear();