Commit aed0e542 authored by Devlin Cronin's avatar Devlin Cronin Committed by Commit Bot

[Extensions] Change Mime-Type deducation in URLRequestExtensionJob

Modify URLRequestExtensionJob to determine the mime type of a requested
file through net::GetWellKnownMimeTypeFromExtension(), rather than
URLRequestFileJob::GetMimeType(). The difference here is that the latter
will include platform-specific modifications. This means that if another
application on the users machine specified a different mime type for an
extension, that type would be used.

This causes problems with modules in extensions, in the cases where a
different application has set a different mime type for .js files, since
modules have strict mime type requirements.

Use GetWellKnownMimeTypeFromExtension(), and add .js to the list of
primary mappings in mime_util.cc.

Bug: 797712

Change-Id: I27fea0479268520abd08fb62a501d213a3a04f52
Reviewed-on: https://chromium-review.googlesource.com/885401Reviewed-by: default avatarAsanka Herath <asanka@chromium.org>
Commit-Queue: Devlin <rdevlin.cronin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#532070}
parent 5d253161
...@@ -72,6 +72,7 @@ ...@@ -72,6 +72,7 @@
#include "mojo/public/cpp/bindings/binding_set.h" #include "mojo/public/cpp/bindings/binding_set.h"
#include "net/base/filename_util.h" #include "net/base/filename_util.h"
#include "net/base/io_buffer.h" #include "net/base/io_buffer.h"
#include "net/base/mime_util.h"
#include "net/base/net_errors.h" #include "net/base/net_errors.h"
#include "net/http/http_request_headers.h" #include "net/http/http_request_headers.h"
#include "net/http/http_response_headers.h" #include "net/http/http_response_headers.h"
...@@ -219,16 +220,6 @@ class URLRequestExtensionJob : public net::URLRequestFileJob { ...@@ -219,16 +220,6 @@ class URLRequestExtensionJob : public net::URLRequestFileJob {
} }
void GetResponseInfo(net::HttpResponseInfo* info) override { void GetResponseInfo(net::HttpResponseInfo* info) override {
// Set the mime type for the request. We do this here (rather than when we
// build the rest of the headers) because the mime type is retrieved only
// after URLRequestFileJob::Start() is called. Using an accurate mime type
// is necessary at least for modules, which enforce strict mime type
// requirements.
std::string mime_type;
bool found_mime_type = GetMimeType(&mime_type);
if (found_mime_type)
response_info_.headers->AddHeader("Content-Type: " + mime_type);
*info = response_info_; *info = response_info_;
} }
...@@ -328,9 +319,31 @@ class URLRequestExtensionJob : public net::URLRequestFileJob { ...@@ -328,9 +319,31 @@ class URLRequestExtensionJob : public net::URLRequestFileJob {
content_security_policy_, content_security_policy_,
send_cors_header_, send_cors_header_,
*last_modified_time); *last_modified_time);
// Set the mime type for the request.
std::string mime_type;
bool found_mime_type = GetMimeType(&mime_type);
if (found_mime_type)
response_info_.headers->AddHeader("Content-Type: " + mime_type);
URLRequestFileJob::Start(); URLRequestFileJob::Start();
} }
bool GetMimeType(std::string* mime_type) const override {
base::FilePath::StringType file_extension = file_path_.Extension();
if (file_extension.empty())
return false;
// We use GetWellKnownMimeTypeFromExtension() to ensure that configurations
// that may have been set by other programs on a user's machine don't affect
// the mime type returned (in particular, JS should always be
// application/javascript). See https://crbug.com/797712. Using an accurate
// mime type is necessary at least for modules, which enforce strict mime
// type requirements.
return net::GetWellKnownMimeTypeFromExtension(
file_extension.substr(1), // Trim leading '.'
mime_type);
}
scoped_refptr<ContentVerifyJob> verify_job_; scoped_refptr<ContentVerifyJob> verify_job_;
std::unique_ptr<base::ElapsedTimer> request_timer_; std::unique_ptr<base::ElapsedTimer> request_timer_;
......
...@@ -79,6 +79,7 @@ static const MimeInfo kPrimaryMappings[] = { ...@@ -79,6 +79,7 @@ static const MimeInfo kPrimaryMappings[] = {
// Must precede audio/webm . // Must precede audio/webm .
{"video/webm", "webm"}, {"video/webm", "webm"},
{"application/javascript", "js"},
{"application/wasm", "wasm"}, {"application/wasm", "wasm"},
{"application/x-chrome-extension", "crx"}, {"application/x-chrome-extension", "crx"},
{"application/xhtml+xml", "xhtml,xht,xhtm"}, {"application/xhtml+xml", "xhtml,xht,xhtm"},
...@@ -107,7 +108,6 @@ static const MimeInfo kSecondaryMappings[] = { ...@@ -107,7 +108,6 @@ static const MimeInfo kSecondaryMappings[] = {
{"application/epub+zip", "epub"}, {"application/epub+zip", "epub"},
{"application/font-woff", "woff"}, {"application/font-woff", "woff"},
{"application/gzip", "gz,tgz"}, {"application/gzip", "gz,tgz"},
{"application/javascript", "js"},
{"application/octet-stream", "bin,exe,com"}, {"application/octet-stream", "bin,exe,com"},
{"application/pdf", "pdf"}, {"application/pdf", "pdf"},
{"application/pkcs7-mime", "p7m,p7c,p7z"}, {"application/pkcs7-mime", "p7m,p7c,p7z"},
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment