blob: 9824d27e5997de179398fec45ded6e08c49e1316 [file] [log] [blame]
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/threading/thread_restrictions.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "content/public/test/browser_test.h"
#include "extensions/common/constants.h"
#include "extensions/common/file_util.h"
namespace extensions {
enum class ManifestVersion { TWO, THREE };
class SandboxedPagesTest
: public ExtensionApiTest,
public ::testing::WithParamInterface<ManifestVersion> {
public:
SandboxedPagesTest() = default;
bool RunTest(const char* extension_name,
const char* manifest,
const RunOptions& run_options,
const LoadOptions& load_options) WARN_UNUSED_RESULT {
const char* kCustomArg =
GetParam() == ManifestVersion::TWO ? "manifest_v2" : "manifest_v3";
SetCustomArg(kCustomArg);
base::ScopedAllowBlockingForTesting scoped_allow_blocking;
// Load the extension with the given `manifest`.
if (!temp_dir_.CreateUniqueTempDir()) {
ADD_FAILURE() << "Could not create temporary dir for test";
return false;
}
base::FilePath source_extension_path =
test_data_dir_.AppendASCII(extension_name);
base::FilePath destination_extension_path =
temp_dir_.GetPath().AppendASCII(extension_name);
if (!base::CopyDirectory(source_extension_path, destination_extension_path,
true /* recursive */)) {
ADD_FAILURE() << source_extension_path.value()
<< " could not be copied to "
<< destination_extension_path.value();
return false;
}
test_data_dir_ = temp_dir_.GetPath();
base::FilePath manifest_path =
destination_extension_path.Append(kManifestFilename);
if (!base::WriteFile(manifest_path, manifest)) {
ADD_FAILURE() << "Could not write manifest file to "
<< manifest_path.value();
return false;
}
return RunExtensionTest(extension_name, run_options, load_options);
}
private:
base::ScopedTempDir temp_dir_;
};
IN_PROC_BROWSER_TEST_P(SandboxedPagesTest, SandboxedPages) {
const char* kManifestV2 = R"(
{
"name": "Extension with sandboxed pages",
"manifest_version": 2,
"version": "0.1",
"sandbox": {
"pages": ["sandboxed.html"]
}
}
)";
const char* kManifestV3 = R"(
{
"name": "Extension with sandboxed pages",
"manifest_version": 3,
"version": "0.1",
"sandbox": {
"pages": ["sandboxed.html"]
}
}
)";
const char* kManifest =
GetParam() == ManifestVersion::TWO ? kManifestV2 : kManifestV3;
EXPECT_TRUE(
RunTest("sandboxed_pages", kManifest, {.page_url = "main.html"}, {}))
<< message_;
}
IN_PROC_BROWSER_TEST_P(SandboxedPagesTest, SandboxedPagesCSP) {
ASSERT_TRUE(StartEmbeddedTestServer());
const char* kManifestV2 = R"(
{
"name": "Tests that loading web content fails inside sandboxed pages",
"manifest_version": 2,
"version": "0.1",
"web_accessible_resources": ["local_frame.html", "remote_frame.html"],
"sandbox": {
"pages": ["sandboxed.html"],
"content_security_policy": "sandbox allow-scripts; child-src *;"
}
}
)";
const char* kManifestV3 = R"(
{
"name": "Tests that loading web content fails inside sandboxed pages",
"manifest_version": 3,
"version": "0.1",
"web_accessible_resources": [{
"resources" : ["local_frame.html", "remote_frame.html"],
"matches": ["<all_urls>"]
}],
"sandbox": {
"pages": ["sandboxed.html"]
},
"content_security_policy": {
"sandbox": "sandbox allow-scripts; child-src *;"
}
}
)";
const char* kManifest =
GetParam() == ManifestVersion::TWO ? kManifestV2 : kManifestV3;
// This extension attempts to load remote web content inside a sandboxed page.
// Loading web content will fail because of CSP. In addition to that we will
// show manifest warnings, hence ignore_manifest_warnings is set to true.
ASSERT_TRUE(RunTest("sandboxed_pages_csp", kManifest,
{.page_url = "main.html"},
{.ignore_manifest_warnings = true}))
<< message_;
}
INSTANTIATE_TEST_SUITE_P(,
SandboxedPagesTest,
::testing::Values(ManifestVersion::TWO,
ManifestVersion::THREE));
} // namespace extensions