Skip to content

Commit

Permalink
Ensure that we set a text/plain mimetype when a server delivers data …
Browse files Browse the repository at this point in the history
…with no mime_type and requested no sniffing.

[email protected]

(cherry picked from commit 496619c)

Bug: 932767
Change-Id: I83a1af0cd2ac1626492624dbfd6b6010fc70ca0a
Reviewed-on: https://chromium-review.googlesource.com/c/1476176
Reviewed-by: Clark DuVall <[email protected]>
Commit-Queue: John Abd-El-Malek <[email protected]>
Cr-Original-Commit-Position: refs/heads/master@{#632880}
Reviewed-on: https://chromium-review.googlesource.com/c/1477995
Reviewed-by: John Abd-El-Malek <[email protected]>
Cr-Commit-Position: refs/branch-heads/3683@{#497}
Cr-Branched-From: e510299-refs/heads/master@{#625896}
  • Loading branch information
John Abd-El-Malek committed Feb 19, 2019
1 parent b6ee3c5 commit 5a8ac33
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
6 changes: 5 additions & 1 deletion content/browser/loader/loader_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, SniffHTMLWithNoContentType) {
CheckTitleTest(
net::URLRequestMockHTTPJob::GetMockUrl("content-sniffer-test0.html"),
"Content Sniffer Test 0");
EXPECT_EQ("text/html", shell()->web_contents()->GetContentsMimeType());
}

IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, RespectNoSniffDirective) {
Expand All @@ -161,16 +162,18 @@ IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, RespectNoSniffDirective) {

CheckTitleTest(net::URLRequestMockHTTPJob::GetMockUrl("nosniff-test.html"),
"mock.http/nosniff-test.html");
EXPECT_EQ("text/plain", shell()->web_contents()->GetContentsMimeType());
}

IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, DoNotSniffHTMLFromTextPlain) {
// Covered by URLLoaderTest.DoNotSniffHTMLFromTextPlain.
// Covered by URLLoaderTest.SniffTextPlainDoesNotResultInHTML.
if (base::FeatureList::IsEnabled(network::features::kNetworkService))
return;

CheckTitleTest(
net::URLRequestMockHTTPJob::GetMockUrl("content-sniffer-test1.html"),
"mock.http/content-sniffer-test1.html");
EXPECT_EQ("text/plain", shell()->web_contents()->GetContentsMimeType());
}

IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, DoNotSniffHTMLFromImageGIF) {
Expand All @@ -181,6 +184,7 @@ IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, DoNotSniffHTMLFromImageGIF) {
CheckTitleTest(
net::URLRequestMockHTTPJob::GetMockUrl("content-sniffer-test2.html"),
"mock.http/content-sniffer-test2.html");
EXPECT_EQ("image/gif", shell()->web_contents()->GetContentsMimeType());
}

IN_PROC_BROWSER_TEST_F(LoaderBrowserTest, SniffNoContentTypeNoData) {
Expand Down
12 changes: 9 additions & 3 deletions services/network/url_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -842,9 +842,15 @@ void URLLoader::OnResponseStarted(net::URLRequest* url_request, int net_error) {
corb_analyzer_->LogAllowedResponse();
}
}
if ((options_ & mojom::kURLLoadOptionSniffMimeType) &&
ShouldSniffContent(url_request_.get(), response_.get())) {
is_more_mime_sniffing_needed_ = true;
if ((options_ & mojom::kURLLoadOptionSniffMimeType)) {
if (ShouldSniffContent(url_request_.get(), response_.get())) {
is_more_mime_sniffing_needed_ = true;
} else if (response_->head.mime_type.empty()) {
// Ugg. The server told us not to sniff the content but didn't give us
// a mime type. What's a browser to do? Turns out, we're supposed to
// treat the response as "text/plain". This is the most secure option.
response_->head.mime_type.assign("text/plain");
}
}
if (!is_more_mime_sniffing_needed_ && !is_more_corb_sniffing_needed_) {
// Treat feed types as text/plain.
Expand Down
2 changes: 1 addition & 1 deletion services/network/url_loader_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ TEST_F(URLLoaderTest, RespectNoSniff) {
set_sniff();
EXPECT_EQ(net::OK, Load(test_server()->GetURL("/nosniff-test.html")));
EXPECT_FALSE(did_mime_sniff());
ASSERT_TRUE(mime_type().empty());
ASSERT_EQ(std::string("text/plain"), mime_type());
}

TEST_F(URLLoaderTest, SniffTextPlainDoesNotResultInHTML) {
Expand Down

0 comments on commit 5a8ac33

Please sign in to comment.