Skip to content

Commit

Permalink
engine: Don't follow redirects an infinite number of times
Browse files Browse the repository at this point in the history
  • Loading branch information
robinlinden committed Nov 16, 2023
1 parent 4123e2c commit 0fd4d8d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions engine/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,16 @@ void Engine::on_navigation_success() {
}

Engine::LoadResult Engine::load(uri::Uri uri) {
static constexpr int kMaxRedirects = 10;

auto is_redirect = [](int status_code) {
return status_code == 301 || status_code == 302 || status_code == 307 || status_code == 308;
};

int redirect_count = 0;
protocol::Response response = protocol_handler_->handle(uri);
while (response.err == protocol::Error::Ok && is_redirect(response.status_line.status_code)) {
++redirect_count;
auto location = response.headers.get("Location");
if (!location) {
response.err = protocol::Error::InvalidResponse;
Expand All @@ -148,6 +152,10 @@ Engine::LoadResult Engine::load(uri::Uri uri) {
spdlog::info("Following {} redirect from {} to {}", response.status_line.status_code, uri.uri, *location);
uri = uri::Uri::parse(std::string(*location), uri);
response = protocol_handler_->handle(uri);
if (redirect_count > kMaxRedirects) {
response.err = protocol::Error::RedirectLimit;
return {std::move(response), std::move(uri)};
}
}

return {std::move(response), std::move(uri)};
Expand Down
11 changes: 11 additions & 0 deletions engine/engine_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,5 +418,16 @@ int main() {
expect(contains(e.stylesheet().rules, {.selectors{"p"}, .declarations{{css::PropertyId::Color, "green"}}}));
});

etest::test("redirect loop", [] {
std::map<std::string, Response> responses;
responses["hax://example.com"s] = Response{
.err = Error::Ok,
.status_line = {.status_code = 301},
.headers = {{"Location", "hax://example.com"}},
};
engine::Engine e{std::make_unique<FakeProtocolHandler>(std::move(responses))};
expect_eq(e.navigate(uri::Uri::parse("hax://example.com")), protocol::Error::RedirectLimit);
});

return etest::run_all_tests();
}

0 comments on commit 0fd4d8d

Please sign in to comment.