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
Allow a list of specific cors domains.
Kishore Nalland20065a48c5d
diff --git a/include/config.h b/include/config.hindex b6960593..c3bb6abd 100644--- a/include/config.h+++ b/include/config.h@@ -32,6 +32,7 @@ private:uint32_t ssl_refresh_interval_seconds;bool enable_cors;+ std::set<std::string> cors_domains;float max_memory_ratio;int snapshot_interval_seconds;@@ -182,6 +183,10 @@ public:return this->enable_cors;}+ std::set<std::string> get_cors_domains() const {+ return this->cors_domains;+ }+std::string get_peering_address() const {return this->peering_address;}@@ -286,9 +291,9 @@ public:this->ssl_certificate = get_env("TYPESENSE_SSL_CERTIFICATE");this->ssl_certificate_key = get_env("TYPESENSE_SSL_CERTIFICATE_KEY");- std::string enable_cors_str = get_env("TYPESENSE_ENABLE_CORS");- StringUtils::toupper(enable_cors_str);- this->enable_cors = ("TRUE" == enable_cors_str) ? true : false;+ std::string cors_value = get_env("TYPESENSE_ENABLE_CORS");+ StringUtils::tolowercase(cors_value);+ set_cors_parameters(cors_value);if(!get_env("TYPESENSE_MAX_MEMORY_RATIO").empty()) {this->max_memory_ratio = std::stof(get_env("TYPESENSE_MAX_MEMORY_RATIO"));@@ -393,7 +398,8 @@ public:}if(reader.Exists("server", "enable-cors")) {- this->enable_cors = reader.GetBoolean("server", "enable-cors", false);+ std::string cors_value = reader.Get("server", "enable-cors", "false");+ set_cors_parameters(cors_value);}if(reader.Exists("server", "peering-address")) {@@ -496,7 +502,8 @@ public:}if(options.exist("enable-cors")) {- this->enable_cors = options.exist("enable-cors");+ std::string cors_value = options.get<std::string>("enable-cors");+ set_cors_parameters(cors_value);}if(options.exist("peering-address")) {@@ -552,6 +559,16 @@ public:}}+ void set_cors_parameters(const std::string& cors_value) {+ this->enable_cors = !(cors_value == "false");++ if(cors_value != "true" && cors_value != "false") {+ std::vector<std::string> cors_values;+ StringUtils::split(cors_value, cors_values, ",");+ cors_domains.insert(cors_values.begin(), cors_values.end());+ }+ }+// validationOption<bool> is_valid() {diff --git a/include/http_server.h b/include/http_server.hindex 64a35627..e1a60be2 100644--- a/include/http_server.h+++ b/include/http_server.h@@ -194,6 +194,8 @@ private:bool cors_enabled;+ std::set<std::string> cors_domains;+ThreadPool* thread_pool;bool (*auth_handler)(std::map<std::string, std::string>& params, const std::string& body, const route_path& rpath,@@ -236,7 +238,8 @@ public:const std::string & ssl_cert_path,const std::string & ssl_cert_key_path,const uint64_t ssl_refresh_interval_ms,- bool cors_enabled, ThreadPool* thread_pool);+ bool cors_enabled, const std::set<std::string>& cors_domains,+ ThreadPool* thread_pool);~HttpServer();diff --git a/src/http_server.cpp b/src/http_server.cppindex 8ddb7a72..388fd3d6 100644--- a/src/http_server.cpp+++ b/src/http_server.cpp@@ -13,11 +13,12 @@HttpServer::HttpServer(const std::string & version, const std::string & listen_address,uint32_t listen_port, const std::string & ssl_cert_path, const std::string & ssl_cert_key_path,- const uint64_t ssl_refresh_interval_ms, bool cors_enabled, ThreadPool* thread_pool):+ const uint64_t ssl_refresh_interval_ms, bool cors_enabled,+ const std::set<std::string>& cors_domains, ThreadPool* thread_pool):SSL_REFRESH_INTERVAL_MS(ssl_refresh_interval_ms),exit_loop(false), version(version), listen_address(listen_address), listen_port(listen_port),ssl_cert_path(ssl_cert_path), ssl_cert_key_path(ssl_cert_key_path),- cors_enabled(cors_enabled), thread_pool(thread_pool) {+ cors_enabled(cors_enabled), cors_domains(cors_domains), thread_pool(thread_pool) {accept_ctx = new h2o_accept_ctx_t();h2o_config_init(&config);hostconf = h2o_config_register_host(&config, h2o_iovec_init(H2O_STRLIT("default")), 65535);@@ -304,8 +305,33 @@ int HttpServer::catch_all_handler(h2o_handler_t *_h2o_handler, h2o_req_t *req) {// Handle CORSif(h2o_handler->http_server->cors_enabled) {+ std::string origin_sent = "*";++ if(!h2o_handler->http_server->cors_domains.empty()) {+ const char* ACL_ORIGIN_HEADER = "origin";+ ssize_t acl_origin_cursor = h2o_find_header_by_str(&req->headers, ACL_ORIGIN_HEADER,+ strlen(ACL_ORIGIN_HEADER), -1);++ // CORS is rejected if cors domains were specified but origin does not match+ bool reject_cors_req = true;++ if(acl_origin_cursor != -1) {+ h2o_iovec_t& acl_origin_header = req->headers.entries[acl_origin_cursor].value;+ origin_sent = std::string(acl_origin_header.base, acl_origin_header.len);+ if(h2o_handler->http_server->cors_domains.count(origin_sent) != 0) {+ reject_cors_req = false;+ }+ }++ if(reject_cors_req) {+ nlohmann::json resp;+ resp["message"] = "Forbidden.";+ return send_response(req, 403, resp.dump());+ }+ }+h2o_add_header_by_str(&req->pool, &req->res.headers, H2O_STRLIT("access-control-allow-origin"),- 0, NULL, H2O_STRLIT("*"));+ 0, NULL, origin_sent.c_str(), origin_sent.size());if(http_method == "OPTIONS") {// locate request access control headersdiff --git a/src/typesense_server_utils.cpp b/src/typesense_server_utils.cppindex ba5f7390..5d272ad3 100644--- a/src/typesense_server_utils.cpp+++ b/src/typesense_server_utils.cpp@@ -79,7 +79,7 @@ void init_cmdline_options(cmdline::parser & options, int argc, char **argv) {options.add<std::string>("ssl-certificate-key", 'k', "Path to the SSL certificate key file.", false, "");options.add<uint32_t>("ssl-refresh-interval-seconds", '\0', "Frequency of automatic reloading of SSL certs from disk.", false, 8 * 60 * 60);- options.add("enable-cors", '\0', "Enable CORS requests.");+ options.add<std::string>("enable-cors", '\0', "Enable CORS requests.", false, "true");options.add<float>("max-memory-ratio", '\0', "Maximum fraction of system memory to be used.", false, 1.0f);options.add<int>("snapshot-interval-seconds", '\0', "Frequency of replication log snapshots.", false, 3600);@@ -412,6 +412,7 @@ int run_server(const Config & config, const std::string & version, void (*masterconfig.get_ssl_cert_key(),config.get_ssl_refresh_interval_seconds() * 1000,config.get_enable_cors(),+ config.get_cors_domains(),&server_thread_pool);