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 malformed scoped API key better

Jason Bosco1e1f7371f2cf
diff --git a/src/auth_manager.cpp b/src/auth_manager.cpp
index a98476b7..4f8bca94 100644
--- a/src/auth_manager.cpp
+++ b/src/auth_manager.cpp
@@ -221,6 +221,11 @@ Option<bool> AuthManager::authenticate_parse_params(const std::string& scoped_ap
const std::string& key_payload = StringUtils::base64_decode(scoped_api_key);
+ if(key_payload.size() < HMAC_BASE64_LEN + api_key_t::PREFIX_LEN) {
+ LOG(ERROR) << "Malformed scoped API key.";
+ return Option<bool>(403, "Forbidden.");
+ }
+
// FORMAT:
// <DIGEST><PARENT_KEY_PREFIX><PARAMS>
const std::string& hmacSHA256 = key_payload.substr(0, HMAC_BASE64_LEN);
diff --git a/src/raft_server.cpp b/src/raft_server.cpp
index 8e69c35a..3dfd92fa 100644
--- a/src/raft_server.cpp
+++ b/src/raft_server.cpp
@@ -172,7 +172,7 @@ void ReplicationState::write_to_leader(http_req *request, http_res *response) co
}
const std::string & leader_addr = node->leader_id().to_string();
- LOG(INFO) << "Redirecting write to leader at: " << leader_addr;
+ //LOG(INFO) << "Redirecting write to leader at: " << leader_addr;
h2o_custom_generator_t* custom_generator = reinterpret_cast<h2o_custom_generator_t *>(response->generator);
HttpServer* server = custom_generator->h2o_handler->http_server;
diff --git a/test/auth_manager_test.cpp b/test/auth_manager_test.cpp
index 8ecf45c3..bbc9f34d 100644
--- a/test/auth_manager_test.cpp
+++ b/test/auth_manager_test.cpp
@@ -296,6 +296,11 @@ TEST_F(AuthManagerTest, ScopedAPIKeys) {
"Nlcl9pZDoxMDgwIiwgImV4cGlyZXNfYXQiOiA2NDcyMzM2MzIwMH0=";
ASSERT_FALSE(auth_manager.authenticate(scoped_key5, "documents:search", "coll1", empty_params));
+
+ // bad scoped API key
+ ASSERT_FALSE(auth_manager.authenticate(" XhsdBdhehdDheruyhvbdhwjhHdhgyeHbfheR", "documents:search", "coll1", empty_params));
+ ASSERT_FALSE(auth_manager.authenticate("cXYPvkNKRlQrBzVTEgY4a3FrZfZ2MEs4kFJ6all3eldwM GhKZnRId3Y3TT1RZmxZeYJmaWx0ZXJfYnkiOkJ1aWQ6OElVm1lUVm15SG9ZOHM4NUx2VFk4S2drNHJIMiJ9", "documents:search", "coll1", empty_params));
+ ASSERT_FALSE(auth_manager.authenticate("SXZqcVdOWjVNNUVsY3ZiTW9YajQ1QnhrUXJaRzRaS0VhTlFvUmlvQ3gycz1LZXlWeyJmaWx0ZXJfYnkiOiAidXNlcl9pZDoxMDgw In0=", "documents:search", "coll1", empty_params));
}
TEST_F(AuthManagerTest, ValidateBadKeyProperties) {