Skip to content

Commit 963e0e4

Browse files
authored
Merge pull request #20 from duckdb/jray/check-origin-header
check origin header
2 parents 04f136e + 3a5cde3 commit 963e0e4

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/http_server.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ void HttpServer::DoStart(const uint16_t _local_port,
106106
}
107107

108108
local_port = _local_port;
109+
local_url = StringUtil::Format("http://localhost:%d", local_port);
109110
remote_url = _remote_url;
110111
user_agent =
111112
StringUtil::Format("duckdb-ui/%s-%s(%s)", DuckDB::LibraryVersion(),
@@ -211,8 +212,10 @@ void HttpServer::HandleGetLocalEvents(const httplib::Request &req,
211212

212213
void HttpServer::HandleGetLocalToken(const httplib::Request &req,
213214
httplib::Response &res) {
214-
auto sec_fetch_site = req.get_header_value("Sec-Fetch-Site");
215-
if (sec_fetch_site == "cross-site") {
215+
// GET requests don't include Origin, so use Referer instead.
216+
// Referer includes the path, so only compare the start.
217+
auto referer = req.get_header_value("Referer");
218+
if (referer.compare(0, local_url.size(), local_url) != 0) {
216219
res.status = 401;
217220
return;
218221
}
@@ -276,8 +279,8 @@ void HttpServer::HandleGet(const httplib::Request &req,
276279

277280
void HttpServer::HandleInterrupt(const httplib::Request &req,
278281
httplib::Response &res) {
279-
auto sec_fetch_site = req.get_header_value("Sec-Fetch-Site");
280-
if (sec_fetch_site == "cross-site") {
282+
auto origin = req.get_header_value("Origin");
283+
if (origin != local_url) {
281284
res.status = 401;
282285
return;
283286
}
@@ -316,8 +319,8 @@ void HttpServer::HandleRun(const httplib::Request &req, httplib::Response &res,
316319
void HttpServer::DoHandleRun(const httplib::Request &req,
317320
httplib::Response &res,
318321
const httplib::ContentReader &content_reader) {
319-
auto sec_fetch_site = req.get_header_value("Sec-Fetch-Site");
320-
if (sec_fetch_site == "cross-site") {
322+
auto origin = req.get_header_value("Origin");
323+
if (origin != local_url) {
321324
res.status = 401;
322325
return;
323326
}
@@ -438,8 +441,8 @@ void HttpServer::DoHandleRun(const httplib::Request &req,
438441
void HttpServer::HandleTokenize(const httplib::Request &req,
439442
httplib::Response &res,
440443
const httplib::ContentReader &content_reader) {
441-
auto sec_fetch_site = req.get_header_value("Sec-Fetch-Site");
442-
if (sec_fetch_site == "cross-site") {
444+
auto origin = req.get_header_value("Origin");
445+
if (origin != local_url) {
443446
res.status = 401;
444447
return;
445448
}

src/include/http_server.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class HttpServer {
6969
shared_ptr<DatabaseInstance> LockDatabaseInstance();
7070

7171
uint16_t local_port;
72+
std::string local_url;
7273
std::string remote_url;
7374
weak_ptr<DatabaseInstance> ddb_instance;
7475
std::string user_agent;

0 commit comments

Comments
 (0)