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 request body for key creation.

kishorenc2d169df65f56
diff --git a/include/auth_manager.h b/include/auth_manager.h
index fceee6e1..13262931 100644
--- a/include/auth_manager.h
+++ b/include/auth_manager.h
@@ -55,6 +55,26 @@ struct api_key_t {
}
}
+ if(!key_obj["actions"].is_array() || key_obj["actions"].empty()) {
+ return Option<uint32_t>(400,"Wrong format for `actions`. It should be an array of string.");
+ }
+
+ if(!key_obj["collections"].is_array() || key_obj["collections"].empty()) {
+ return Option<uint32_t>(400,"Wrong format for `collections`. It should be an array of string.");
+ }
+
+ for(const nlohmann::json & item: key_obj["actions"]) {
+ if(!item.is_string()) {
+ return Option<uint32_t>(400,"Wrong format for `actions`. It should be an array of string.");
+ }
+ }
+
+ for(const nlohmann::json & item: key_obj["collections"]) {
+ if(!item.is_string()) {
+ return Option<uint32_t>(400,"Wrong format for `collections`. It should be an array of string.");
+ }
+ }
+
return Option<uint32_t>(200);
}
diff --git a/src/core_api.cpp b/src/core_api.cpp
index 8f9deb9a..8a9175ca 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -911,10 +911,10 @@ bool post_create_key(http_req &req, http_res &res) {
const std::string &rand_key = StringUtils::randstring(AuthManager::KEY_LEN, req.seed);
api_key_t api_key(
- rand_key,
- req_json["description"].get<std::string>(),
- req_json["actions"].get<std::vector<std::string>>(),
- req_json["collections"].get<std::vector<std::string>>()
+ rand_key,
+ req_json["description"].get<std::string>(),
+ req_json["actions"].get<std::vector<std::string>>(),
+ req_json["collections"].get<std::vector<std::string>>()
);
const Option<api_key_t>& api_key_op = auth_manager.create_key(api_key);
diff --git a/test/auth_manager_test.cpp b/test/auth_manager_test.cpp
index 73cf6afe..7582cf11 100644
--- a/test/auth_manager_test.cpp
+++ b/test/auth_manager_test.cpp
@@ -220,3 +220,26 @@ TEST_F(AuthManagerTest, ScopedAPIKeys) {
);
ASSERT_FALSE(auth_manager.authenticate(scoped_key2, "documents:search", "coll", empty_params));
}
+
+TEST_F(AuthManagerTest, ValidateBadKeyProperties) {
+ nlohmann::json key_obj1;
+ key_obj1["description"] = "desc";
+ key_obj1["actions"].push_back("*");
+ key_obj1["collections"].push_back(1);
+
+ Option<uint32_t> validate_op = api_key_t::validate(key_obj1);
+ ASSERT_FALSE(validate_op.ok());
+ ASSERT_STREQ("Wrong format for `collections`. It should be an array of string.", validate_op.error().c_str());
+
+ key_obj1["actions"].push_back(1);
+ key_obj1["collections"].push_back("*");
+ validate_op = api_key_t::validate(key_obj1);
+ ASSERT_FALSE(validate_op.ok());
+ ASSERT_STREQ("Wrong format for `actions`. It should be an array of string.", validate_op.error().c_str());
+
+ key_obj1["actions"] = 1;
+ key_obj1["collections"].push_back("*");
+ validate_op = api_key_t::validate(key_obj1);
+ ASSERT_FALSE(validate_op.ok());
+ ASSERT_STREQ("Wrong format for `actions`. It should be an array of string.", validate_op.error().c_str());
+}
\ No newline at end of file