Skip to content

Commit

Permalink
Add 'accept' and 'upgrade-insecure-request' headers to prefetch
Browse files Browse the repository at this point in the history
requests.

These headers will be added to all prefetch requests in order to match
the headers added to default mainframe requests.

(cherry picked from commit 7f00fb9)

Bug: 1318193
Change-Id: I9063ee08adcd86e0e13887e6edabbe09fa5c9a1e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3597448
Reviewed-by: Ryan Sturm <ryansturm@chromium.org>
Commit-Queue: Max Curran <curranmax@chromium.org>
Cr-Original-Commit-Position: refs/heads/main@{#994434}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3611762
Commit-Queue: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Auto-Submit: Max Curran <curranmax@chromium.org>
Cr-Commit-Position: refs/branch-heads/5005@{#206}
Cr-Branched-From: 5b4d945-refs/heads/main@{#992738}
  • Loading branch information
Max Curran authored and Chromium LUCI CQ committed Apr 27, 2022
1 parent ef20912 commit f246a99
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Expand Up @@ -43,6 +43,7 @@
#include "components/search_engines/template_url_service.h"
#include "components/version_info/version_info.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/frame_accept_header.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/browser/render_frame_host.h"
Expand Down Expand Up @@ -846,6 +847,10 @@ void PrefetchProxyTabHelper::StartSinglePrefetch() {
prefetch_container->GetPrefetchType().IsProxyRequired()
? prefetch::headers::kSecPurposePrefetchAnonymousClientIpHeaderValue
: prefetch::headers::kSecPurposePrefetchHeaderValue);
request->headers.SetHeader(
net::HttpRequestHeaders::kAccept,
content::FrameAcceptHeaderValue(/*allow_sxg_responses=*/true, profile_));
request->headers.SetHeader("Upgrade-Insecure-Requests", "1");
// Remove the user agent header if it was set so that the network context's
// default is used.
request->headers.RemoveHeader("User-Agent");
Expand Down
Expand Up @@ -27,6 +27,7 @@
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/frame_accept_header.h"
#include "content/public/browser/service_worker_context.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
Expand Down Expand Up @@ -695,7 +696,7 @@ TEST_F(PrefetchProxyTabHelperTest, WrongWebContents) {
"PrefetchProxy.Prefetch.Mainframe.TotalRedirects", 0);
}

TEST_F(PrefetchProxyTabHelperTest, HasPurposePrefetchHeader) {
TEST_F(PrefetchProxyTabHelperTest, HasExplicitHeaders) {
base::HistogramTester histogram_tester;

NavigateSomewhere();
Expand All @@ -704,7 +705,15 @@ TEST_F(PrefetchProxyTabHelperTest, HasPurposePrefetchHeader) {
MakeNavigationPrediction(web_contents(), doc_url, {prediction_url});

VerifyCommonRequestState(prediction_url);

// Checks that headers added explicitly by |PrefetchProxyTabHelper| were
// included.
EXPECT_EQ(RequestHeader("Purpose"), "prefetch");
EXPECT_EQ(RequestHeader("Sec-Purpose"), "prefetch;anonymous-client-ip");
EXPECT_EQ(
RequestHeader("Accept"),
content::FrameAcceptHeaderValue(/*allow_sxg_responses=*/true, profile()));
EXPECT_EQ(RequestHeader("Upgrade-Insecure-Requests"), "1");

EXPECT_EQ(predicted_urls_count(), 1U);
EXPECT_EQ(prefetch_eligible_count(), 1U);
Expand Down

0 comments on commit f246a99

Please sign in to comment.