Commit 6ca3b1b2 authored by Tommy Steimel's avatar Tommy Steimel Committed by Commit Bot

Fix Access-Control-Allow-Origin for chrome-untrusted://resources

This CL fixes a bug in chrome-untrusted://resources where the
Access-Control-Allow-Origin policy would only allow for chrome://
origins. This changes it so that chrome-untrusted://resources only
allows for chrome-untrusted:// origins and chrome://resources only
allows for chrome:// origins.

Bug: 1086327
Change-Id: Icbf05aa66e587ed3cb5d5f26ef2b46c0628641c1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2216282
Commit-Queue: Tommy Steimel <steimel@chromium.org>
Reviewed-by: default avatardpapad <dpapad@chromium.org>
Cr-Commit-Position: refs/heads/master@{#772326}
parent 9e5def3b
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/memory/ref_counted_memory.h" #include "base/memory/ref_counted_memory.h"
#include "base/notreached.h" #include "base/notreached.h"
#include "base/strings/strcat.h"
#include "base/strings/string_piece.h" #include "base/strings/string_piece.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/task/post_task.h" #include "base/task/post_task.h"
...@@ -295,25 +296,35 @@ int GetIdrForPath(const std::string& path) { ...@@ -295,25 +296,35 @@ int GetIdrForPath(const std::string& path) {
std::unique_ptr<SharedResourcesDataSource> std::unique_ptr<SharedResourcesDataSource>
SharedResourcesDataSource::CreateForChromeScheme() { SharedResourcesDataSource::CreateForChromeScheme() {
return std::make_unique<SharedResourcesDataSource>(PassKey(), return std::make_unique<SharedResourcesDataSource>(PassKey(),
kChromeUIResourcesHost); kChromeUIScheme);
} }
// static // static
std::unique_ptr<SharedResourcesDataSource> std::unique_ptr<SharedResourcesDataSource>
SharedResourcesDataSource::CreateForChromeUntrustedScheme() { SharedResourcesDataSource::CreateForChromeUntrustedScheme() {
return std::make_unique<SharedResourcesDataSource>( return std::make_unique<SharedResourcesDataSource>(PassKey(),
PassKey(), kChromeUIUntrustedResourcesURL); kChromeUIUntrustedScheme);
} }
SharedResourcesDataSource::SharedResourcesDataSource( SharedResourcesDataSource::SharedResourcesDataSource(PassKey,
PassKey, const std::string& scheme)
const std::string& source_name) : scheme_(scheme) {}
: source_name_(source_name) {}
SharedResourcesDataSource::~SharedResourcesDataSource() = default; SharedResourcesDataSource::~SharedResourcesDataSource() = default;
std::string SharedResourcesDataSource::GetSource() { std::string SharedResourcesDataSource::GetSource() {
return source_name_; // URLDataManagerBackend assumes that chrome:// data sources return just the
// hostname for GetSource().
if (scheme_ == kChromeUIScheme)
return kChromeUIResourcesHost;
// We only expect chrome-untrusted:// scheme at this point.
DCHECK_EQ(kChromeUIUntrustedScheme, scheme_);
// Other schemes (i.e. chrome-untrusted://) return the scheme and host
// together.
return base::StrCat(
{scheme_, url::kStandardSchemeSeparator, kChromeUIResourcesHost});
} }
void SharedResourcesDataSource::StartDataRequest( void SharedResourcesDataSource::StartDataRequest(
...@@ -401,12 +412,12 @@ bool SharedResourcesDataSource::ShouldServeMimeTypeAsContentTypeHeader() { ...@@ -401,12 +412,12 @@ bool SharedResourcesDataSource::ShouldServeMimeTypeAsContentTypeHeader() {
std::string SharedResourcesDataSource::GetAccessControlAllowOriginForOrigin( std::string SharedResourcesDataSource::GetAccessControlAllowOriginForOrigin(
const std::string& origin) { const std::string& origin) {
// For now we give access only for "chrome://*" origins. // For now we give access only for origins with the allowed scheme.
// According to CORS spec, Access-Control-Allow-Origin header doesn't support // According to CORS spec, Access-Control-Allow-Origin header doesn't support
// wildcards, so we need to set its value explicitly by passing the |origin| // wildcards, so we need to set its value explicitly by passing the |origin|
// back. // back.
std::string allowed_origin_prefix = kChromeUIScheme; const std::string allowed_origin_prefix =
allowed_origin_prefix += "://"; base::StrCat({scheme_, url::kStandardSchemeSeparator});
if (!base::StartsWith(origin, allowed_origin_prefix, if (!base::StartsWith(origin, allowed_origin_prefix,
base::CompareCase::SENSITIVE)) { base::CompareCase::SENSITIVE)) {
return "null"; return "null";
......
...@@ -31,7 +31,7 @@ class SharedResourcesDataSource : public URLDataSource { ...@@ -31,7 +31,7 @@ class SharedResourcesDataSource : public URLDataSource {
static std::unique_ptr<SharedResourcesDataSource> static std::unique_ptr<SharedResourcesDataSource>
CreateForChromeUntrustedScheme(); CreateForChromeUntrustedScheme();
explicit SharedResourcesDataSource(PassKey, const std::string& source_name); SharedResourcesDataSource(PassKey, const std::string& scheme);
SharedResourcesDataSource(const SharedResourcesDataSource&) = delete; SharedResourcesDataSource(const SharedResourcesDataSource&) = delete;
SharedResourcesDataSource& operator=(const SharedResourcesDataSource&) = SharedResourcesDataSource& operator=(const SharedResourcesDataSource&) =
delete; delete;
...@@ -59,7 +59,9 @@ class SharedResourcesDataSource : public URLDataSource { ...@@ -59,7 +59,9 @@ class SharedResourcesDataSource : public URLDataSource {
bool IsPolymer2DisabledForPage(const WebContents::Getter& wc_getter); bool IsPolymer2DisabledForPage(const WebContents::Getter& wc_getter);
#endif // defined (OS_CHROMEOS) #endif // defined (OS_CHROMEOS)
const std::string source_name_; // The URL scheme this data source is accessed from, e.g. "chrome" or
// "chrome-untrusted".
const std::string scheme_;
}; };
} // namespace content } // namespace content
......
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