2022-06-25 01:55:54 +02:00
|
|
|
#include "csrf_token.h"
|
|
|
|
|
2022-06-26 19:43:31 +02:00
|
|
|
#include "core/crypto/crypto_core.h"
|
|
|
|
#include "core/os/os.h"
|
|
|
|
#include "http_server_enums.h"
|
2022-06-25 01:55:54 +02:00
|
|
|
#include "http_session.h"
|
2022-06-26 19:43:31 +02:00
|
|
|
#include "web_server_request.h"
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-26 20:54:26 +02:00
|
|
|
PoolStringArray CSRFTokenWebServerMiddleware::get_ignored_urls() {
|
|
|
|
return ignored_urls;
|
|
|
|
}
|
|
|
|
void CSRFTokenWebServerMiddleware::set_ignored_urls(const PoolStringArray &val) {
|
|
|
|
ignored_urls = val;
|
|
|
|
}
|
|
|
|
|
2022-06-26 19:43:31 +02:00
|
|
|
bool CSRFTokenWebServerMiddleware::_on_before_handle_request_main(Ref<WebServerRequest> request) {
|
2022-06-25 01:55:54 +02:00
|
|
|
switch (request->get_method()) {
|
2022-06-26 19:43:31 +02:00
|
|
|
case HTTPServerEnums::HTTP_METHOD_POST:
|
|
|
|
case HTTPServerEnums::HTTP_METHOD_DELETE:
|
|
|
|
case HTTPServerEnums::HTTP_METHOD_PATCH:
|
|
|
|
case HTTPServerEnums::HTTP_METHOD_PUT: {
|
2022-06-25 01:55:54 +02:00
|
|
|
if (shold_ignore(request)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!request->session.is_valid()) {
|
2022-06-26 19:43:31 +02:00
|
|
|
request->send_error(HTTPServerEnums::HTTP_STATUS_CODE_401_UNAUTHORIZED);
|
2022-06-25 01:55:54 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!request->validate_csrf_token()) {
|
2022-06-26 19:43:31 +02:00
|
|
|
request->send_error(HTTPServerEnums::HTTP_STATUS_CODE_401_UNAUTHORIZED);
|
2022-06-25 01:55:54 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't create the session itself
|
|
|
|
if (!request->session.is_valid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!request->has_csrf_token()) {
|
|
|
|
request->set_csrf_token(create_token());
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-26 19:43:31 +02:00
|
|
|
bool CSRFTokenWebServerMiddleware::shold_ignore(Ref<WebServerRequest> request) {
|
|
|
|
String path = request->get_path_full();
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < ignored_urls.size(); ++i) {
|
2022-06-26 19:43:31 +02:00
|
|
|
if (path.begins_with(ignored_urls[i])) {
|
2022-06-25 01:55:54 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-26 15:00:25 +02:00
|
|
|
String CSRFTokenWebServerMiddleware::create_token() {
|
2022-06-26 19:43:31 +02:00
|
|
|
String s = String::num(OS::get_singleton()->get_unix_time());
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-26 19:43:31 +02:00
|
|
|
return s.sha256_text().substr(0, 10);
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
2022-06-26 15:00:25 +02:00
|
|
|
CSRFTokenWebServerMiddleware::CSRFTokenWebServerMiddleware() {
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
2022-06-26 15:00:25 +02:00
|
|
|
CSRFTokenWebServerMiddleware::~CSRFTokenWebServerMiddleware() {
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
2022-06-26 19:43:31 +02:00
|
|
|
|
|
|
|
void CSRFTokenWebServerMiddleware::_bind_methods() {
|
2022-06-26 20:54:26 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_ignored_urls"), &CSRFTokenWebServerMiddleware::get_ignored_urls);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_ignored_urls", "val"), &CSRFTokenWebServerMiddleware::set_ignored_urls);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::POOL_STRING_ARRAY, "ignored_urls"), "set_ignored_urls", "get_ignored_urls");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("create_token"), &CSRFTokenWebServerMiddleware::create_token);
|
2022-06-26 19:43:31 +02:00
|
|
|
}
|