Skip to content

Commit 62d883b

Browse files
authored
Merge pull request #985 from softcom-su/origin-handling-fix
Fixed handling of origin when allow_credentials enabled
2 parents 3c82cda + 9d00070 commit 62d883b

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

include/crow/middlewares/cors.h

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
#include "crow/common.h"
23
#include "crow/http_request.h"
34
#include "crow/http_response.h"
45
#include "crow/routing.h"
@@ -126,12 +127,25 @@ namespace crow
126127
set_header_no_override("Access-Control-Allow-Headers", headers_, res);
127128
set_header_no_override("Access-Control-Expose-Headers", exposed_headers_, res);
128129
set_header_no_override("Access-Control-Max-Age", max_age_, res);
129-
if (allow_credentials_) set_header_no_override("Access-Control-Allow-Credentials", "true", res);
130130

131-
if (allow_credentials_ && origin_ == "*")
132-
set_header_no_override("Access-Control-Allow-Origin", req.get_header_value("Origin"), res);
133-
else
131+
bool origin_set = false;
132+
133+
if (req.method != HTTPMethod::Options)
134+
{
135+
if (allow_credentials_)
136+
{
137+
set_header_no_override("Access-Control-Allow-Credentials", "true", res);
138+
if (origin_ == "*")
139+
{
140+
set_header_no_override("Access-Control-Allow-Origin", req.get_header_value("Origin"), res);
141+
origin_set = true;
142+
}
143+
}
144+
}
145+
146+
if( !origin_set){
134147
set_header_no_override("Access-Control-Allow-Origin", origin_, res);
148+
}
135149
}
136150

137151
bool ignore_ = false;

tests/unittest.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1997,6 +1997,10 @@ TEST_CASE("middleware_cors")
19971997
return "-";
19981998
});
19991999

2000+
CROW_ROUTE(app, "/auth-origin").methods(crow::HTTPMethod::Post)([&](const request&) {
2001+
return "-";
2002+
});
2003+
20002004
CROW_ROUTE(app, "/expose")
20012005
([&](const request&) {
20022006
return "-";
@@ -2024,8 +2028,14 @@ TEST_CASE("middleware_cors")
20242028
CHECK(resp.find("Access-Control-Allow-Origin: test.test") != std::string::npos);
20252029

20262030
resp = HttpClient::request(LOCALHOST_ADDRESS, port,
2027-
"GET /auth-origin\r\nOrigin: test-client\r\n\r\n");
2031+
"GET /auth-origin\r\nOrigin: test-client\r\n\r\n");
20282032
CHECK(resp.find("Access-Control-Allow-Origin: test-client") != std::string::npos);
2033+
CHECK(resp.find("Access-Control-Allow-Credentials: true") != std::string::npos);
2034+
2035+
resp = HttpClient::request(LOCALHOST_ADDRESS, port,
2036+
"OPTIONS /auth-origin / HTTP/1.1 \r\n\r\n");
2037+
CHECK(resp.find("Access-Control-Allow-Origin: *") != std::string::npos);
2038+
CHECK(resp.find("Access-Control-Allow-Credentials: true") == std::string::npos);
20292039

20302040
resp = HttpClient::request(LOCALHOST_ADDRESS, port,
20312041
"GET /expose\r\n\r\n");

0 commit comments

Comments
 (0)