Commit bdd2b573 authored by Karan Bhatia's avatar Karan Bhatia Committed by Commit Bot

Extensions: Always use CSP dictionary on Manifest V3.

Currently the "content_security_policy" manifest key is restricted to trunk.
However it is mandatory for Mv3 extensions. Hence, always allow it if the
manifest version is 3.

Also, make some other minor cosmetic changes.

BUG=993530

Change-Id: Ifca0c588d923a36644002e865f853d74533d7356
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1815277
Commit-Queue: Karan Bhatia <karandeepb@chromium.org>
Reviewed-by: default avatarIstiaque Ahmed <lazyboy@chromium.org>
Cr-Commit-Position: refs/heads/master@{#698986}
parent 3594437b
...@@ -125,8 +125,8 @@ const std::string& CSPInfo::GetExtensionPagesCSP(const Extension* extension) { ...@@ -125,8 +125,8 @@ const std::string& CSPInfo::GetExtensionPagesCSP(const Extension* extension) {
// static // static
const std::string* CSPInfo::GetIsolatedWorldCSP(const Extension& extension) { const std::string* CSPInfo::GetIsolatedWorldCSP(const Extension& extension) {
// TODO(crbug.com/914224): This should be only called for extensions which can // TODO(crbug.com/1005978): This should be only called for extensions which
// have isolated worlds. Figure out the case of TYPE_USER_SCRIPT and add // can have isolated worlds. Figure out the case of TYPE_USER_SCRIPT and add
// DCHECK(csp_info). // DCHECK(csp_info).
CSPInfo* csp_info = static_cast<CSPInfo*>( CSPInfo* csp_info = static_cast<CSPInfo*>(
extension.GetManifestData(keys::kContentSecurityPolicy)); extension.GetManifestData(keys::kContentSecurityPolicy));
...@@ -163,19 +163,24 @@ bool CSPHandler::Parse(Extension* extension, base::string16* error) { ...@@ -163,19 +163,24 @@ bool CSPHandler::Parse(Extension* extension, base::string16* error) {
// The "content_security_policy" manifest key can either be a string or a // The "content_security_policy" manifest key can either be a string or a
// dictionary of the format // dictionary of the format
// "content_security_policy" : { // "content_security_policy" : {
// "extension_pages" : "" // "extension_pages": "",
// "sandbox": "",
// "isolated_world": ""
// } // }
const base::Value* csp = GetManifestPath(extension, key); const base::Value* csp = GetManifestPath(extension, key);
const int kManifestVersion3 = 3;
// TODO(crbug.com/914224): Remove the channel check once the support for the // TODO(crbug.com/914224): Remove the channel check once support for isolated
// dictionary key is launched to other channels. // world CSP is implemenented.
bool csp_dictionary_supported = bool csp_dictionary_supported =
extension->GetType() == Manifest::TYPE_EXTENSION && extension->GetType() == Manifest::TYPE_EXTENSION &&
GetCurrentChannel() == version_info::Channel::UNKNOWN; (extension->manifest_version() >= kManifestVersion3 ||
GetCurrentChannel() == version_info::Channel::UNKNOWN);
if (csp_dictionary_supported) { if (csp_dictionary_supported) {
// CSP key as dictionary is mandatory for manifest v3 extensions. // CSP key as dictionary is mandatory for manifest v3 (and above)
if (extension->manifest_version() == 3) { // extensions.
if (extension->manifest_version() >= kManifestVersion3) {
if (csp && !csp->is_dict()) { if (csp && !csp->is_dict()) {
*error = GetInvalidManifestKeyError(key); *error = GetInvalidManifestKeyError(key);
return false; return false;
...@@ -204,14 +209,6 @@ bool CSPHandler::Parse(Extension* extension, base::string16* error) { ...@@ -204,14 +209,6 @@ bool CSPHandler::Parse(Extension* extension, base::string16* error) {
bool CSPHandler::ParseCSPDictionary(Extension* extension, bool CSPHandler::ParseCSPDictionary(Extension* extension,
base::string16* error) { base::string16* error) {
if (!ParseExtensionPagesCSP(
extension, error, keys::kContentSecurityPolicy_ExtensionPagesPath,
true /* secure_only */,
GetManifestPath(extension,
keys::kContentSecurityPolicy_ExtensionPagesPath))) {
return false;
}
// keys::kSandboxedPagesCSP shouldn't be used when using // keys::kSandboxedPagesCSP shouldn't be used when using
// keys::kContentSecurityPolicy as a dictionary. // keys::kContentSecurityPolicy as a dictionary.
if (extension->manifest()->HasPath(keys::kSandboxedPagesCSP)) { if (extension->manifest()->HasPath(keys::kSandboxedPagesCSP)) {
...@@ -219,7 +216,12 @@ bool CSPHandler::ParseCSPDictionary(Extension* extension, ...@@ -219,7 +216,12 @@ bool CSPHandler::ParseCSPDictionary(Extension* extension,
return false; return false;
} }
return ParseSandboxCSP( return ParseExtensionPagesCSP(
extension, error, keys::kContentSecurityPolicy_ExtensionPagesPath,
true /* secure_only */,
GetManifestPath(
extension, keys::kContentSecurityPolicy_ExtensionPagesPath)) &&
ParseSandboxCSP(
extension, error, keys::kContentSecurityPolicy_SandboxedPagesPath, extension, error, keys::kContentSecurityPolicy_SandboxedPagesPath,
GetManifestPath( GetManifestPath(
extension, keys::kContentSecurityPolicy_SandboxedPagesPath)) && extension, keys::kContentSecurityPolicy_SandboxedPagesPath)) &&
...@@ -376,7 +378,8 @@ void CSPHandler::SetSandboxCSP(Extension* extension, std::string sandbox_csp) { ...@@ -376,7 +378,8 @@ void CSPHandler::SetSandboxCSP(Extension* extension, std::string sandbox_csp) {
} }
bool CSPHandler::AlwaysParseForType(Manifest::Type type) const { bool CSPHandler::AlwaysParseForType(Manifest::Type type) const {
// TODO(karandeepb): Check if TYPE_USER_SCRIPT needs to be included here. // TODO(crbug.com/1005978): Check if TYPE_USER_SCRIPT needs to be included
// here.
return type == Manifest::TYPE_PLATFORM_APP || return type == Manifest::TYPE_PLATFORM_APP ||
type == Manifest::TYPE_EXTENSION || type == Manifest::TYPE_EXTENSION ||
type == Manifest::TYPE_LEGACY_PACKAGED_APP; type == Manifest::TYPE_LEGACY_PACKAGED_APP;
......
...@@ -64,8 +64,8 @@ class CSPHandler : public ManifestHandler { ...@@ -64,8 +64,8 @@ class CSPHandler : public ManifestHandler {
CSPHandler(); CSPHandler();
~CSPHandler() override; ~CSPHandler() override;
// ManifestHandler override:
bool Parse(Extension* extension, base::string16* error) override; bool Parse(Extension* extension, base::string16* error) override;
bool AlwaysParseForType(Manifest::Type type) const override;
private: private:
// Parses the "content_security_policy" dictionary in the manifest. // Parses the "content_security_policy" dictionary in the manifest.
...@@ -103,6 +103,8 @@ class CSPHandler : public ManifestHandler { ...@@ -103,6 +103,8 @@ class CSPHandler : public ManifestHandler {
// Helper to set the sandbox content security policy manifest data. // Helper to set the sandbox content security policy manifest data.
void SetSandboxCSP(Extension* extension, std::string sandbox_csp); void SetSandboxCSP(Extension* extension, std::string sandbox_csp);
// ManifestHandler overrides:
bool AlwaysParseForType(Manifest::Type type) const override;
base::span<const char* const> Keys() const override; base::span<const char* const> Keys() const override;
DISALLOW_COPY_AND_ASSIGN(CSPHandler); DISALLOW_COPY_AND_ASSIGN(CSPHandler);
......
...@@ -266,8 +266,6 @@ TEST_F(CSPInfoUnitTest, CSPDictionary_IsolatedWorlds) { ...@@ -266,8 +266,6 @@ TEST_F(CSPInfoUnitTest, CSPDictionary_IsolatedWorlds) {
// key is mandatory for manifest v3 extensions and that defaults are applied // key is mandatory for manifest v3 extensions and that defaults are applied
// correctly. // correctly.
TEST_F(CSPInfoUnitTest, CSPDictionaryMandatoryForV3) { TEST_F(CSPInfoUnitTest, CSPDictionaryMandatoryForV3) {
ScopedCurrentChannel channel(version_info::Channel::UNKNOWN);
LoadAndExpectError("csp_invalid_type_v3.json", LoadAndExpectError("csp_invalid_type_v3.json",
GetInvalidManifestKeyError(keys::kContentSecurityPolicy)); GetInvalidManifestKeyError(keys::kContentSecurityPolicy));
......
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