Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/channels/channel_http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,12 @@ void channel_http::dispatch(const request_ptr& request) NOEXCEPT
claimed_ = false;

// Basic authorization carried by http headers. ws frames and tcp messages
// carry none, so authorization is dispatched to protocol.
if (!websocket() && !downgraded() && !authorized())
// carry none, so authorization is dispatched to protocol. A cors preflight
// carries no credentials.
const auto preflight = request->method() == verb::options &&
to_bool(request->count(field::access_control_request_method));

if (!websocket() && !downgraded() && !authorized() && !preflight)
{
unauthorized(*request);
return;
Expand Down Expand Up @@ -516,6 +520,15 @@ void channel_http::unauthorized(const request& request) NOEXCEPT
BC_ASSERT(stranded());
response out{ status::unauthorized, request.version() };
out.set(field::www_authenticate, challenge);

// An allowed origin may read the status of its cross-origin request.
if (to_bool(request.count(field::origin)) &&
is_allowed_origin(request, request.version()))
{
out.set(field::access_control_allow_origin, request[field::origin]);
out.set(field::vary, "origin");
}

send(std::move(out),
std::bind(&channel_http::handle_rejected,
shared_from_base<channel_http>(), _1, error::unauthorized));
Expand Down
3 changes: 2 additions & 1 deletion src/protocols/protocol_http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,8 @@ void protocol_http::add_access_control_headers(fields& fields,
if (origin)
fields.set(field::access_control_allow_origin, request[field::origin]);
if (headers)
fields.set(field::access_control_allow_headers, "*");
fields.set(field::access_control_allow_headers,
request[field::access_control_request_headers]);
if (method)
fields.set(field::access_control_allow_methods, "*");

Expand Down
96 changes: 96 additions & 0 deletions test/channels/channel_http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,39 @@ static const std::string http_get_request
"\r\n"
};

static const std::string http_preflight_request
{
"OPTIONS / HTTP/1.1\r\n"
"Host: example.com\r\n"
"Origin: http://example.org\r\n"
"Access-Control-Request-Method: POST\r\n"
"Access-Control-Request-Headers: authorization, content-type\r\n"
"\r\n"
};

static const std::string http_options_request
{
"OPTIONS / HTTP/1.1\r\n"
"Host: example.com\r\n"
"\r\n"
};

static const std::string http_originated_get_request
{
"GET /index HTTP/1.1\r\n"
"Host: example.com\r\n"
"Origin: http://example.org\r\n"
"\r\n"
};

static const std::string http_foreign_get_request
{
"GET /index HTTP/1.1\r\n"
"Host: example.com\r\n"
"Origin: http://example.net\r\n"
"\r\n"
};

static const std::string http_authorized_get_request
{
"GET /index HTTP/1.1\r\n"
Expand Down Expand Up @@ -957,6 +990,18 @@ struct originated_options
}
};

struct credentialed_originated_options
: channel_http::options_t
{
credentialed_originated_options() NOEXCEPT
: channel_http::options_t("test")
{
credentials.emplace_back("test:123\xC2\xA3:getinfo");
origins.emplace_back("example.org");
initialize_context();
}
};

struct opaque_options
: channel_http::options_t
{
Expand All @@ -973,6 +1018,7 @@ static const credentialed_options credentialed{};
static const restricted_options restricted{};
static const originated_options originated{};
static const opaque_options opaque{};
static const credentialed_originated_options credentialed_originated{};
static const network::settings http_settings{ test::network_settings() };
static const std::string rfc7617_authorization{ "Basic dGVzdDoxMjPCow==" };

Expand Down Expand Up @@ -1061,6 +1107,12 @@ struct http_channel_fixture
if (!ec) respond(self, *request);
});

self->subscribe<method::options>([=](const code& ec,
const method::options::cptr& request) NOEXCEPT
{
if (!ec) respond(self, *request);
});

self->subscribe<method::unknown>([=](const code& ec,
const method::unknown::cptr& request) NOEXCEPT
{
Expand Down Expand Up @@ -1099,6 +1151,15 @@ struct credentialed_channel_fixture
}
};

struct credentialed_originated_channel_fixture
: http_channel_fixture
{
credentialed_originated_channel_fixture() NOEXCEPT
: http_channel_fixture(credentialed_originated)
{
}
};

struct restricted_channel_fixture
: http_channel_fixture
{
Expand Down Expand Up @@ -1483,6 +1544,41 @@ BC_FIXTURE_TEST_CASE(channel_http__resume__unauthorized_get__unauthorized, crede
BC_REQUIRE_EQUAL(http_await(stopped), error::unauthorized);
}

BC_FIXTURE_TEST_CASE(channel_http__resume__unauthorized_preflight__dispatched, credentialed_channel_fixture)
{
start(http_ok);
send(http_preflight_request);
BC_REQUIRE_EQUAL(receive().result_int(), 200u);
}

BC_FIXTURE_TEST_CASE(channel_http__resume__unauthorized_options__unauthorized, credentialed_channel_fixture)
{
start(http_ok);
send(http_options_request);
BC_REQUIRE_EQUAL(receive().result_int(), 401u);
BC_REQUIRE_EQUAL(http_await(stopped), error::unauthorized);
}

BC_FIXTURE_TEST_CASE(channel_http__resume__unauthorized_allowed_origin__allow_origin, credentialed_originated_channel_fixture)
{
start(http_ok);
send(http_originated_get_request);
const auto response = receive();
BC_REQUIRE_EQUAL(response.result_int(), 401u);
BC_REQUIRE_EQUAL(response[field::access_control_allow_origin], "http://example.org");
BC_REQUIRE_EQUAL(http_await(stopped), error::unauthorized);
}

BC_FIXTURE_TEST_CASE(channel_http__resume__unauthorized_foreign_origin__no_allow_origin, credentialed_originated_channel_fixture)
{
start(http_ok);
send(http_foreign_get_request);
const auto response = receive();
BC_REQUIRE_EQUAL(response.result_int(), 401u);
BC_REQUIRE(!response.count(field::access_control_allow_origin));
BC_REQUIRE_EQUAL(http_await(stopped), error::unauthorized);
}

BC_FIXTURE_TEST_CASE(channel_http__resume__conflicting_content_length__bad_request_and_stopped, http_channel_fixture)
{
start(http_ok);
Expand Down
Loading