[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 5 | #include <stddef.h> |
| 6 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 7 | #include <memory> |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 8 | #include <string> |
vabr | 9984ea6 | 2017-04-10 11:33:49 | [diff] [blame] | 9 | #include <utility> |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 10 | |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 11 | #include "base/command_line.h" |
thestig | c9e38a2 | 2014-09-13 01:02:11 | [diff] [blame] | 12 | #include "base/files/file_util.h" |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 13 | #include "base/macros.h" |
fdoray | cb32419d | 2016-06-23 15:52:55 | [diff] [blame] | 14 | #include "base/run_loop.h" |
[email protected] | 774cebd | 2013-09-26 04:55:01 | [diff] [blame] | 15 | #include "base/strings/string_number_conversions.h" |
[email protected] | 00e7bef | 2013-06-10 20:35:17 | [diff] [blame] | 16 | #include "base/strings/string_util.h" |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 17 | #include "base/test/test_file_util.h" |
Devlin Cronin | c3f8807 | 2018-01-30 02:10:11 | [diff] [blame] | 18 | #include "base/test/values_test_util.h" |
[email protected] | 06492ed | 2013-03-24 22:13:14 | [diff] [blame] | 19 | #include "base/values.h" |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 20 | #include "chrome/browser/extensions/chrome_content_verifier_delegate.h" |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 21 | #include "chrome/common/chrome_paths.h" |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 22 | #include "chrome/common/chrome_switches.h" |
| 23 | #include "chrome/test/base/testing_profile.h" |
| 24 | #include "components/crx_file/id_util.h" |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 25 | #include "content/public/browser/resource_request_info.h" |
megjablon | caf312f | 2017-01-12 18:47:49 | [diff] [blame] | 26 | #include "content/public/common/previews_state.h" |
[email protected] | 08a932d5 | 2012-06-03 21:42:12 | [diff] [blame] | 27 | #include "content/public/test/mock_resource_context.h" |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 28 | #include "content/public/test/test_browser_thread_bundle.h" |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 29 | #include "content/public/test/test_renderer_host.h" |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 30 | #include "content/public/test/test_utils.h" |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 31 | #include "content/public/test/web_contents_tester.h" |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 32 | #include "extensions/browser/content_verifier.h" |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 33 | #include "extensions/browser/content_verifier/test_utils.h" |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 34 | #include "extensions/browser/extension_prefs.h" |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 35 | #include "extensions/browser/extension_protocols.h" |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 36 | #include "extensions/browser/extension_registry.h" |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 37 | #include "extensions/browser/info_map.h" |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame] | 38 | #include "extensions/common/constants.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 39 | #include "extensions/common/extension.h" |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 40 | #include "extensions/common/extension_builder.h" |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 41 | #include "extensions/common/extension_paths.h" |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 42 | #include "extensions/common/file_util.h" |
Devlin Cronin | c3f8807 | 2018-01-30 02:10:11 | [diff] [blame] | 43 | #include "extensions/test/test_extension_dir.h" |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 44 | #include "net/base/request_priority.h" |
rhalavati | 04b9338 | 2017-04-07 19:00:54 | [diff] [blame] | 45 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 46 | #include "net/url_request/url_request.h" |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 47 | #include "net/url_request/url_request_job_factory_impl.h" |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 48 | #include "net/url_request/url_request_status.h" |
| 49 | #include "net/url_request/url_request_test_util.h" |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 50 | #include "services/network/test/test_url_loader_client.h" |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 51 | #include "testing/gtest/include/gtest/gtest.h" |
| 52 | |
[email protected] | 7491ad0 | 2014-07-05 19:10:07 | [diff] [blame] | 53 | using content::ResourceType; |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 54 | using extensions::ExtensionRegistry; |
| 55 | using network::mojom::URLLoader; |
[email protected] | 7491ad0 | 2014-07-05 19:10:07 | [diff] [blame] | 56 | |
[email protected] | 702d8b4 | 2013-02-27 20:55:50 | [diff] [blame] | 57 | namespace extensions { |
jamescook | 8816ae5 | 2014-09-05 17:02:37 | [diff] [blame] | 58 | namespace { |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 59 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 60 | enum class RequestHandlerType { |
| 61 | kURLLoader, |
| 62 | kURLRequest, |
| 63 | }; |
| 64 | |
| 65 | const RequestHandlerType kTestModes[] = {RequestHandlerType::kURLLoader, |
| 66 | RequestHandlerType::kURLRequest}; |
| 67 | |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 68 | base::FilePath GetTestPath(const std::string& name) { |
| 69 | base::FilePath path; |
| 70 | EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path)); |
| 71 | return path.AppendASCII("extensions").AppendASCII(name); |
| 72 | } |
| 73 | |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 74 | base::FilePath GetContentVerifierTestPath() { |
| 75 | base::FilePath path; |
| 76 | EXPECT_TRUE(PathService::Get(extensions::DIR_TEST_DATA, &path)); |
| 77 | return path.AppendASCII("content_hash_fetcher") |
| 78 | .AppendASCII("different_sized_files"); |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 79 | } |
| 80 | |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 81 | scoped_refptr<Extension> CreateTestExtension(const std::string& name, |
| 82 | bool incognito_split_mode) { |
[email protected] | 023b3d1 | 2013-12-23 18:46:49 | [diff] [blame] | 83 | base::DictionaryValue manifest; |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 84 | manifest.SetString("name", name); |
| 85 | manifest.SetString("version", "1"); |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 86 | manifest.SetInteger("manifest_version", 2); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 87 | manifest.SetString("incognito", incognito_split_mode ? "split" : "spanning"); |
| 88 | |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 89 | base::FilePath path = GetTestPath("response_headers"); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 90 | |
| 91 | std::string error; |
| 92 | scoped_refptr<Extension> extension( |
[email protected] | 1d5e58b | 2013-01-31 08:41:40 | [diff] [blame] | 93 | Extension::Create(path, Manifest::INTERNAL, manifest, |
[email protected] | ed3b9b1 | 2012-05-31 18:37:51 | [diff] [blame] | 94 | Extension::NO_FLAGS, &error)); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 95 | EXPECT_TRUE(extension.get()) << error; |
| 96 | return extension; |
| 97 | } |
| 98 | |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 99 | scoped_refptr<Extension> CreateWebStoreExtension() { |
Devlin Cronin | f43c331 | 2018-03-27 19:54:17 | [diff] [blame] | 100 | std::unique_ptr<base::DictionaryValue> manifest = |
| 101 | DictionaryBuilder() |
| 102 | .Set("name", "WebStore") |
| 103 | .Set("version", "1") |
| 104 | .Set("manifest_version", 2) |
| 105 | .Set("icons", |
| 106 | DictionaryBuilder().Set("16", "webstore_icon_16.png").Build()) |
| 107 | .Set("web_accessible_resources", |
| 108 | ListBuilder().Append("webstore_icon_16.png").Build()) |
| 109 | .Build(); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 110 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 111 | base::FilePath path; |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 112 | EXPECT_TRUE(PathService::Get(chrome::DIR_RESOURCES, &path)); |
| 113 | path = path.AppendASCII("web_store"); |
| 114 | |
| 115 | std::string error; |
Devlin Cronin | f43c331 | 2018-03-27 19:54:17 | [diff] [blame] | 116 | scoped_refptr<Extension> extension(Extension::Create( |
| 117 | path, Manifest::COMPONENT, *manifest, Extension::NO_FLAGS, &error)); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 118 | EXPECT_TRUE(extension.get()) << error; |
| 119 | return extension; |
| 120 | } |
| 121 | |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 122 | scoped_refptr<Extension> CreateTestResponseHeaderExtension() { |
[email protected] | 023b3d1 | 2013-12-23 18:46:49 | [diff] [blame] | 123 | base::DictionaryValue manifest; |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 124 | manifest.SetString("name", "An extension with web-accessible resources"); |
| 125 | manifest.SetString("version", "2"); |
| 126 | |
Jinho Bang | b5216cec | 2018-01-17 19:43:11 | [diff] [blame] | 127 | auto web_accessible_list = std::make_unique<base::ListValue>(); |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 128 | web_accessible_list->AppendString("test.dat"); |
vabr | 9984ea6 | 2017-04-10 11:33:49 | [diff] [blame] | 129 | manifest.Set("web_accessible_resources", std::move(web_accessible_list)); |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 130 | |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 131 | base::FilePath path = GetTestPath("response_headers"); |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 132 | |
| 133 | std::string error; |
| 134 | scoped_refptr<Extension> extension( |
| 135 | Extension::Create(path, Manifest::UNPACKED, manifest, |
| 136 | Extension::NO_FLAGS, &error)); |
| 137 | EXPECT_TRUE(extension.get()) << error; |
| 138 | return extension; |
| 139 | } |
| 140 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 141 | // Helper function to create a |ResourceRequest| for testing purposes. |
| 142 | network::ResourceRequest CreateResourceRequest(const std::string& method, |
| 143 | ResourceType resource_type, |
| 144 | const GURL& url) { |
| 145 | network::ResourceRequest request; |
| 146 | request.method = method; |
| 147 | request.url = url; |
| 148 | request.site_for_cookies = url; // bypass third-party cookie blocking. |
| 149 | request.request_initiator = |
| 150 | url::Origin::Create(url); // ensure initiator set. |
| 151 | request.referrer_policy = content::Referrer::GetDefaultReferrerPolicy(); |
| 152 | request.resource_type = resource_type; |
| 153 | request.is_main_frame = resource_type == content::RESOURCE_TYPE_MAIN_FRAME; |
| 154 | request.allow_download = true; |
| 155 | return request; |
| 156 | } |
| 157 | |
| 158 | // The result of either a URLRequest of a URLLoader response (but not both) |
| 159 | // depending on the on test type. |
| 160 | class GetResult { |
| 161 | public: |
| 162 | GetResult(std::unique_ptr<net::URLRequest> request, int result) |
| 163 | : request_(std::move(request)), result_(result) {} |
| 164 | GetResult(const network::ResourceResponseHead& response, int result) |
| 165 | : resource_response_(response), result_(result) {} |
| 166 | GetResult(GetResult&& other) |
| 167 | : request_(std::move(other.request_)), result_(other.result_) {} |
| 168 | ~GetResult() = default; |
| 169 | |
| 170 | std::string GetResponseHeaderByName(const std::string& name) const { |
| 171 | std::string value; |
| 172 | if (request_) |
| 173 | request_->GetResponseHeaderByName(name, &value); |
| 174 | else if (resource_response_.headers) |
| 175 | resource_response_.headers->GetNormalizedHeader(name, &value); |
| 176 | return value; |
| 177 | } |
| 178 | |
| 179 | int result() const { return result_; } |
| 180 | |
| 181 | private: |
| 182 | std::unique_ptr<net::URLRequest> request_; |
| 183 | const network::ResourceResponseHead resource_response_; |
| 184 | int result_; |
| 185 | |
| 186 | DISALLOW_COPY_AND_ASSIGN(GetResult); |
| 187 | }; |
| 188 | |
jamescook | 8816ae5 | 2014-09-05 17:02:37 | [diff] [blame] | 189 | } // namespace |
| 190 | |
| 191 | // This test lives in src/chrome instead of src/extensions because it tests |
| 192 | // functionality delegated back to Chrome via ChromeExtensionsBrowserClient. |
lfg | 048201a | 2014-09-16 19:09:36 | [diff] [blame] | 193 | // See chrome/browser/extensions/chrome_url_request_util.cc. |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 194 | class ExtensionProtocolsTest |
| 195 | : public testing::Test, |
| 196 | public testing::WithParamInterface<RequestHandlerType> { |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 197 | public: |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 198 | ExtensionProtocolsTest() |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 199 | : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 200 | rvh_test_enabler_(new content::RenderViewHostTestEnabler()), |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 201 | old_factory_(NULL), |
| 202 | resource_context_(&test_url_request_context_) {} |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 203 | |
dcheng | 7219181 | 2014-10-28 20:49:56 | [diff] [blame] | 204 | void SetUp() override { |
[email protected] | 06492ed | 2013-03-24 22:13:14 | [diff] [blame] | 205 | testing::Test::SetUp(); |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 206 | testing_profile_ = TestingProfile::Builder().Build(); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 207 | contents_.reset(CreateTestWebContents()); |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 208 | extension_info_map_ = new InfoMap(); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 209 | old_factory_ = resource_context_.GetRequestContext()->job_factory(); |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 210 | |
| 211 | // Set up content verification. |
| 212 | base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 213 | command_line->AppendSwitchASCII( |
| 214 | switches::kExtensionContentVerification, |
| 215 | switches::kExtensionContentVerificationEnforce); |
| 216 | content_verifier_ = new ContentVerifier( |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 217 | browser_context(), |
| 218 | std::make_unique<ChromeContentVerifierDelegate>(browser_context())); |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 219 | extension_info_map_->SetContentVerifier(content_verifier_.get()); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 220 | } |
| 221 | |
dcheng | 7219181 | 2014-10-28 20:49:56 | [diff] [blame] | 222 | void TearDown() override { |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 223 | loader_factory_.reset(); |
| 224 | resource_context_.GetRequestContext()->set_job_factory(old_factory_); |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 225 | content_verifier_->Shutdown(); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 226 | } |
| 227 | |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 228 | void SetProtocolHandler(bool is_incognito) { |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 229 | switch (request_handler()) { |
| 230 | case RequestHandlerType::kURLLoader: |
| 231 | loader_factory_ = extensions::CreateExtensionNavigationURLLoaderFactory( |
| 232 | main_rfh(), extension_info_map_.get()); |
| 233 | break; |
| 234 | case RequestHandlerType::kURLRequest: |
| 235 | job_factory_.SetProtocolHandler( |
| 236 | kExtensionScheme, CreateExtensionProtocolHandler( |
| 237 | is_incognito, extension_info_map_.get())); |
| 238 | resource_context_.GetRequestContext()->set_job_factory(&job_factory_); |
| 239 | break; |
| 240 | } |
| 241 | testing_profile_->ForceIncognito(is_incognito); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 242 | } |
| 243 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 244 | GetResult RequestOrLoad(const GURL& url, ResourceType resource_type) { |
| 245 | switch (request_handler()) { |
| 246 | case RequestHandlerType::kURLLoader: |
| 247 | return LoadURL(url, resource_type); |
| 248 | case RequestHandlerType::kURLRequest: |
| 249 | return RequestURL(url, resource_type); |
| 250 | } |
| 251 | NOTREACHED(); |
| 252 | return GetResult(nullptr, net::ERR_FAILED); |
| 253 | } |
| 254 | |
| 255 | void AddExtension(const scoped_refptr<const Extension>& extension, |
| 256 | bool incognito_enabled, |
| 257 | bool notifications_disabled) { |
| 258 | extension_info_map_->AddExtension(extension.get(), base::Time::Now(), |
| 259 | incognito_enabled, |
| 260 | notifications_disabled); |
| 261 | if (request_handler() == RequestHandlerType::kURLLoader) { |
| 262 | EXPECT_TRUE(extension_registry()->AddEnabled(extension)); |
| 263 | ExtensionPrefs::Get(browser_context()) |
| 264 | ->SetIsIncognitoEnabled(extension->id(), incognito_enabled); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | void RemoveExtension(const scoped_refptr<const Extension>& extension, |
| 269 | const UnloadedExtensionReason reason) { |
| 270 | extension_info_map_->RemoveExtension(extension->id(), reason); |
| 271 | if (request_handler() == RequestHandlerType::kURLLoader) { |
| 272 | EXPECT_TRUE(extension_registry()->RemoveEnabled(extension->id())); |
| 273 | if (reason == UnloadedExtensionReason::DISABLE) |
| 274 | EXPECT_TRUE(extension_registry()->AddDisabled(extension)); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | // Helper method to create a URL request/loader, call RequestOrLoad on it, and |
| 279 | // return the result. If |extension| hasn't already been added to |
| 280 | // |extension_info_map_|, this will add it. |
| 281 | GetResult DoRequestOrLoad(const scoped_refptr<Extension> extension, |
| 282 | const std::string& relative_path) { |
| 283 | if (!extension_info_map_->extensions().Contains(extension->id())) { |
| 284 | AddExtension(extension.get(), |
| 285 | /*incognito_enabled=*/false, |
| 286 | /*notifications_disabled=*/false); |
| 287 | } |
| 288 | return RequestOrLoad(extension->GetResourceURL(relative_path), |
| 289 | content::RESOURCE_TYPE_MAIN_FRAME); |
| 290 | } |
| 291 | |
| 292 | ExtensionRegistry* extension_registry() { |
| 293 | return ExtensionRegistry::Get(browser_context()); |
| 294 | } |
| 295 | |
| 296 | content::BrowserContext* browser_context() { return testing_profile_.get(); } |
| 297 | |
| 298 | protected: |
| 299 | scoped_refptr<ContentVerifier> content_verifier_; |
| 300 | |
| 301 | private: |
| 302 | GetResult LoadURL(const GURL& url, ResourceType resource_type) { |
| 303 | constexpr int32_t kRoutingId = 81; |
| 304 | constexpr int32_t kRequestId = 28; |
| 305 | |
| 306 | network::mojom::URLLoaderPtr loader; |
| 307 | network::TestURLLoaderClient client; |
| 308 | loader_factory_->CreateLoaderAndStart( |
| 309 | mojo::MakeRequest(&loader), kRoutingId, kRequestId, |
| 310 | network::mojom::kURLLoadOptionNone, |
| 311 | CreateResourceRequest("GET", resource_type, url), |
| 312 | client.CreateInterfacePtr(), |
| 313 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 314 | |
| 315 | client.RunUntilComplete(); |
| 316 | return GetResult(client.response_head(), |
| 317 | client.completion_status().error_code); |
| 318 | } |
| 319 | |
| 320 | GetResult RequestURL(const GURL& url, ResourceType resource_type) { |
| 321 | auto request = resource_context_.GetRequestContext()->CreateRequest( |
| 322 | url, net::DEFAULT_PRIORITY, &test_delegate_, |
| 323 | TRAFFIC_ANNOTATION_FOR_TESTS); |
| 324 | |
gab | f9d1558 | 2014-11-13 16:40:15 | [diff] [blame] | 325 | content::ResourceRequestInfo::AllocateForTesting( |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 326 | request.get(), resource_type, &resource_context_, |
megjablon | caf312f | 2017-01-12 18:47:49 | [diff] [blame] | 327 | /*render_process_id=*/-1, |
| 328 | /*render_view_id=*/-1, |
| 329 | /*render_frame_id=*/-1, |
| 330 | /*is_main_frame=*/resource_type == content::RESOURCE_TYPE_MAIN_FRAME, |
megjablon | caf312f | 2017-01-12 18:47:49 | [diff] [blame] | 331 | /*allow_download=*/true, |
Jian Li | 1817342 | 2017-11-08 03:00:02 | [diff] [blame] | 332 | /*is_async=*/false, content::PREVIEWS_OFF, |
| 333 | /*navigation_ui_data*/ nullptr); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 334 | request->Start(); |
fdoray | cb32419d | 2016-06-23 15:52:55 | [diff] [blame] | 335 | base::RunLoop().Run(); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 336 | return GetResult(std::move(request), test_delegate_.request_status()); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 337 | } |
| 338 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 339 | content::WebContents* CreateTestWebContents() { |
| 340 | auto site_instance = content::SiteInstance::Create(browser_context()); |
| 341 | return content::WebContentsTester::CreateTestWebContents( |
| 342 | browser_context(), std::move(site_instance)); |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 343 | } |
| 344 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 345 | content::WebContents* web_contents() { return contents_.get(); } |
| 346 | |
| 347 | content::RenderFrameHost* main_rfh() { |
| 348 | return web_contents()->GetMainFrame(); |
| 349 | } |
| 350 | |
| 351 | RequestHandlerType request_handler() const { return GetParam(); } |
| 352 | |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 353 | content::TestBrowserThreadBundle thread_bundle_; |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 354 | std::unique_ptr<content::RenderViewHostTestEnabler> rvh_test_enabler_; |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 355 | net::URLRequestJobFactoryImpl job_factory_; |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 356 | const net::URLRequestJobFactory* old_factory_; |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 357 | std::unique_ptr<network::mojom::URLLoaderFactory> loader_factory_; |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 358 | net::TestURLRequestContext test_url_request_context_; |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 359 | content::MockResourceContext resource_context_; |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 360 | std::unique_ptr<TestingProfile> testing_profile_; |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 361 | net::TestDelegate test_delegate_; |
| 362 | std::unique_ptr<content::WebContents> contents_; |
| 363 | scoped_refptr<InfoMap> extension_info_map_; |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 364 | }; |
| 365 | |
| 366 | // Tests that making a chrome-extension request in an incognito context is |
| 367 | // only allowed under the right circumstances (if the extension is allowed |
| 368 | // in incognito, and it's either a non-main-frame request or a split-mode |
| 369 | // extension). |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 370 | TEST_P(ExtensionProtocolsTest, IncognitoRequest) { |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 371 | // Register an incognito extension protocol handler. |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 372 | SetProtocolHandler(true); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 373 | |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 374 | struct TestCase { |
| 375 | // Inputs. |
| 376 | std::string name; |
| 377 | bool incognito_split_mode; |
| 378 | bool incognito_enabled; |
| 379 | |
| 380 | // Expected results. |
| 381 | bool should_allow_main_frame_load; |
| 382 | bool should_allow_sub_frame_load; |
| 383 | } cases[] = { |
| 384 | {"spanning disabled", false, false, false, false}, |
| 385 | {"split disabled", true, false, false, false}, |
nasko | 5cf9d45 | 2016-06-01 05:34:56 | [diff] [blame] | 386 | {"spanning enabled", false, true, false, false}, |
| 387 | {"split enabled", true, true, true, false}, |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 388 | }; |
| 389 | |
viettrungluu | 9e65ad1 | 2014-10-16 04:22:26 | [diff] [blame] | 390 | for (size_t i = 0; i < arraysize(cases); ++i) { |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 391 | scoped_refptr<Extension> extension = |
| 392 | CreateTestExtension(cases[i].name, cases[i].incognito_split_mode); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 393 | AddExtension(extension, cases[i].incognito_enabled, false); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 394 | |
| 395 | // First test a main frame request. |
| 396 | { |
| 397 | // It doesn't matter that the resource doesn't exist. If the resource |
nasko | b9164c4 | 2016-06-07 01:21:35 | [diff] [blame] | 398 | // is blocked, we should see BLOCKED_BY_CLIENT. Otherwise, the request |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 399 | // should just fail because the file doesn't exist. |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 400 | auto get_result = RequestOrLoad(extension->GetResourceURL("404.html"), |
| 401 | content::RESOURCE_TYPE_MAIN_FRAME); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 402 | |
| 403 | if (cases[i].should_allow_main_frame_load) { |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 404 | EXPECT_EQ(net::ERR_FILE_NOT_FOUND, get_result.result()) |
maksim.sisov | 1b83bb7 | 2016-10-07 06:07:23 | [diff] [blame] | 405 | << cases[i].name; |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 406 | } else { |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 407 | EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, get_result.result()) |
nasko | b9164c4 | 2016-06-07 01:21:35 | [diff] [blame] | 408 | << cases[i].name; |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
John Abd-El-Malek | 2305cdc | 2018-02-14 20:26:28 | [diff] [blame] | 412 | // Subframe navigation requests are blocked in ExtensionNavigationThrottle |
| 413 | // which isn't added in this unit test. This is tested in an integration |
| 414 | // test in ExtensionResourceRequestPolicyTest.IframeNavigateToInaccessible. |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 415 | RemoveExtension(extension, UnloadedExtensionReason::UNINSTALL); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 419 | void CheckForContentLengthHeader(const GetResult& get_result) { |
| 420 | std::string content_length = get_result.GetResponseHeaderByName( |
| 421 | net::HttpRequestHeaders::kContentLength); |
| 422 | |
[email protected] | 774cebd | 2013-09-26 04:55:01 | [diff] [blame] | 423 | EXPECT_FALSE(content_length.empty()); |
| 424 | int length_value = 0; |
| 425 | EXPECT_TRUE(base::StringToInt(content_length, &length_value)); |
| 426 | EXPECT_GT(length_value, 0); |
| 427 | } |
| 428 | |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 429 | // Tests getting a resource for a component extension works correctly, both when |
| 430 | // the extension is enabled and when it is disabled. |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 431 | TEST_P(ExtensionProtocolsTest, ComponentResourceRequest) { |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 432 | // Register a non-incognito extension protocol handler. |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 433 | SetProtocolHandler(false); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 434 | |
| 435 | scoped_refptr<Extension> extension = CreateWebStoreExtension(); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 436 | AddExtension(extension, false, false); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 437 | |
| 438 | // First test it with the extension enabled. |
| 439 | { |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 440 | auto get_result = |
| 441 | RequestOrLoad(extension->GetResourceURL("webstore_icon_16.png"), |
| 442 | content::RESOURCE_TYPE_MEDIA); |
| 443 | EXPECT_EQ(net::OK, get_result.result()); |
| 444 | CheckForContentLengthHeader(get_result); |
| 445 | EXPECT_EQ("image/png", get_result.GetResponseHeaderByName( |
| 446 | net::HttpRequestHeaders::kContentType)); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | // And then test it with the extension disabled. |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 450 | RemoveExtension(extension, UnloadedExtensionReason::DISABLE); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 451 | { |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 452 | auto get_result = |
| 453 | RequestOrLoad(extension->GetResourceURL("webstore_icon_16.png"), |
| 454 | content::RESOURCE_TYPE_MEDIA); |
| 455 | EXPECT_EQ(net::OK, get_result.result()); |
| 456 | CheckForContentLengthHeader(get_result); |
| 457 | EXPECT_EQ("image/png", get_result.GetResponseHeaderByName( |
| 458 | net::HttpRequestHeaders::kContentType)); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 462 | // Tests that a URL request for resource from an extension returns a few |
| 463 | // expected response headers. |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 464 | TEST_P(ExtensionProtocolsTest, ResourceRequestResponseHeaders) { |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 465 | // Register a non-incognito extension protocol handler. |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 466 | SetProtocolHandler(false); |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 467 | |
| 468 | scoped_refptr<Extension> extension = CreateTestResponseHeaderExtension(); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 469 | AddExtension(extension, false, false); |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 470 | |
| 471 | { |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 472 | auto get_result = RequestOrLoad(extension->GetResourceURL("test.dat"), |
| 473 | content::RESOURCE_TYPE_MEDIA); |
| 474 | EXPECT_EQ(net::OK, get_result.result()); |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 475 | |
| 476 | // Check that cache-related headers are set. |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 477 | std::string etag = get_result.GetResponseHeaderByName("ETag"); |
brettw | 66d1b81b | 2015-07-06 19:29:40 | [diff] [blame] | 478 | EXPECT_TRUE(base::StartsWith(etag, "\"", base::CompareCase::SENSITIVE)); |
| 479 | EXPECT_TRUE(base::EndsWith(etag, "\"", base::CompareCase::SENSITIVE)); |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 480 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 481 | std::string revalidation_header = |
| 482 | get_result.GetResponseHeaderByName("cache-control"); |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 483 | EXPECT_EQ("no-cache", revalidation_header); |
| 484 | |
| 485 | // We set test.dat as web-accessible, so it should have a CORS header. |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 486 | std::string access_control = |
| 487 | get_result.GetResponseHeaderByName("Access-Control-Allow-Origin"); |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 488 | EXPECT_EQ("*", access_control); |
| 489 | } |
| 490 | } |
| 491 | |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 492 | // Tests that a URL request for main frame or subframe from an extension |
| 493 | // succeeds, but subresources fail. See http://crbug.com/312269. |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 494 | TEST_P(ExtensionProtocolsTest, AllowFrameRequests) { |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 495 | // Register a non-incognito extension protocol handler. |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 496 | SetProtocolHandler(false); |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 497 | |
| 498 | scoped_refptr<Extension> extension = CreateTestExtension("foo", false); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 499 | AddExtension(extension, false, false); |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 500 | |
nasko | 5cf9d45 | 2016-06-01 05:34:56 | [diff] [blame] | 501 | // All MAIN_FRAME requests should succeed. SUB_FRAME requests that are not |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 502 | // explicitly listed in web_accessible_resources or same-origin to the parent |
nasko | 5cf9d45 | 2016-06-01 05:34:56 | [diff] [blame] | 503 | // should not succeed. |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 504 | { |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 505 | auto get_result = RequestOrLoad(extension->GetResourceURL("test.dat"), |
| 506 | content::RESOURCE_TYPE_MAIN_FRAME); |
| 507 | EXPECT_EQ(net::OK, get_result.result()); |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 508 | } |
John Abd-El-Malek | 2305cdc | 2018-02-14 20:26:28 | [diff] [blame] | 509 | |
| 510 | // Subframe navigation requests are blocked in ExtensionNavigationThrottle |
| 511 | // which isn't added in this unit test. This is tested in an integration test |
| 512 | // in ExtensionResourceRequestPolicyTest.IframeNavigateToInaccessible. |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 513 | |
| 514 | // And subresource types, such as media, should fail. |
| 515 | { |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 516 | auto get_result = RequestOrLoad(extension->GetResourceURL("test.dat"), |
| 517 | content::RESOURCE_TYPE_MEDIA); |
| 518 | EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, get_result.result()); |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 522 | TEST_P(ExtensionProtocolsTest, MetadataFolder) { |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 523 | SetProtocolHandler(false); |
| 524 | |
| 525 | base::FilePath extension_dir = GetTestPath("metadata_folder"); |
| 526 | std::string error; |
| 527 | scoped_refptr<Extension> extension = |
| 528 | file_util::LoadExtension(extension_dir, Manifest::INTERNAL, |
| 529 | Extension::NO_FLAGS, &error); |
| 530 | ASSERT_NE(extension.get(), nullptr) << "error: " << error; |
| 531 | |
| 532 | // Loading "/test.html" should succeed. |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 533 | EXPECT_EQ(net::OK, DoRequestOrLoad(extension, "test.html").result()); |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 534 | |
| 535 | // Loading "/_metadata/verified_contents.json" should fail. |
| 536 | base::FilePath relative_path = |
| 537 | base::FilePath(kMetadataFolder).Append(kVerifiedContentsFilename); |
| 538 | EXPECT_TRUE(base::PathExists(extension_dir.Append(relative_path))); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 539 | EXPECT_NE(net::OK, |
| 540 | DoRequestOrLoad(extension, relative_path.AsUTF8Unsafe()).result()); |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 541 | |
| 542 | // Loading "/_metadata/a.txt" should also fail. |
| 543 | relative_path = base::FilePath(kMetadataFolder).AppendASCII("a.txt"); |
| 544 | EXPECT_TRUE(base::PathExists(extension_dir.Append(relative_path))); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 545 | EXPECT_NE(net::OK, |
| 546 | DoRequestOrLoad(extension, relative_path.AsUTF8Unsafe()).result()); |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 547 | } |
| 548 | |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 549 | // Tests that unreadable files and deleted files correctly go through |
| 550 | // ContentVerifyJob. |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 551 | TEST_P(ExtensionProtocolsTest, VerificationSeenForFileAccessErrors) { |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 552 | SetProtocolHandler(false); |
| 553 | |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 554 | // Unzip extension containing verification hashes to a temporary directory. |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 555 | base::ScopedTempDir temp_dir; |
| 556 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 557 | base::FilePath unzipped_path = temp_dir.GetPath(); |
| 558 | scoped_refptr<Extension> extension = |
| 559 | content_verifier_test_utils::UnzipToDirAndLoadExtension( |
| 560 | GetContentVerifierTestPath().AppendASCII("source.zip"), |
| 561 | unzipped_path); |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 562 | ASSERT_TRUE(extension.get()); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 563 | ExtensionId extension_id = extension->id(); |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 564 | |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 565 | const std::string kJs("1024.js"); |
| 566 | base::FilePath kRelativePath(FILE_PATH_LITERAL("1024.js")); |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 567 | |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 568 | // Valid and readable 1024.js. |
| 569 | { |
Istiaque Ahmed | 584fe14 | 2018-03-13 09:19:04 | [diff] [blame] | 570 | TestContentVerifySingleJobObserver observer(extension_id, kRelativePath); |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 571 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 572 | content_verifier_->OnExtensionLoaded(browser_context(), extension.get()); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 573 | // Wait for PostTask to ContentVerifierIOData::AddData() to finish. |
| 574 | content::RunAllPendingInMessageLoop(); |
| 575 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 576 | EXPECT_EQ(net::OK, DoRequestOrLoad(extension, kJs).result()); |
Istiaque Ahmed | 1dec1d4 | 2018-02-02 00:01:36 | [diff] [blame] | 577 | EXPECT_EQ(ContentVerifyJob::NONE, observer.WaitForJobFinished()); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | // chmod -r 1024.js. |
| 581 | { |
Istiaque Ahmed | 584fe14 | 2018-03-13 09:19:04 | [diff] [blame] | 582 | TestContentVerifySingleJobObserver observer(extension->id(), kRelativePath); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 583 | base::FilePath file_path = unzipped_path.AppendASCII(kJs); |
| 584 | ASSERT_TRUE(base::MakeFileUnreadable(file_path)); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 585 | EXPECT_EQ(net::ERR_ACCESS_DENIED, DoRequestOrLoad(extension, kJs).result()); |
Istiaque Ahmed | 1dec1d4 | 2018-02-02 00:01:36 | [diff] [blame] | 586 | EXPECT_EQ(ContentVerifyJob::HASH_MISMATCH, observer.WaitForJobFinished()); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 587 | // NOTE: In production, hash mismatch would have disabled |extension|, but |
| 588 | // since UnzipToDirAndLoadExtension() doesn't add the extension to |
| 589 | // ExtensionRegistry, ChromeContentVerifierDelegate won't disable it. |
| 590 | // TODO(lazyboy): We may want to update this to more closely reflect the |
| 591 | // real flow. |
| 592 | } |
| 593 | |
| 594 | // Delete 1024.js. |
| 595 | { |
Istiaque Ahmed | 584fe14 | 2018-03-13 09:19:04 | [diff] [blame] | 596 | TestContentVerifySingleJobObserver observer(extension_id, kRelativePath); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 597 | base::FilePath file_path = unzipped_path.AppendASCII(kJs); |
| 598 | ASSERT_TRUE(base::DieFileDie(file_path, false)); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 599 | EXPECT_EQ(net::ERR_FILE_NOT_FOUND, |
| 600 | DoRequestOrLoad(extension, kJs).result()); |
Istiaque Ahmed | 1dec1d4 | 2018-02-02 00:01:36 | [diff] [blame] | 601 | EXPECT_EQ(ContentVerifyJob::HASH_MISMATCH, observer.WaitForJobFinished()); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 602 | } |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 603 | } |
| 604 | |
lazyboy | e83ab9c6 | 2017-03-30 03:18:26 | [diff] [blame] | 605 | // Tests that zero byte files correctly go through ContentVerifyJob. |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 606 | TEST_P(ExtensionProtocolsTest, VerificationSeenForZeroByteFile) { |
lazyboy | e83ab9c6 | 2017-03-30 03:18:26 | [diff] [blame] | 607 | SetProtocolHandler(false); |
| 608 | |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 609 | const std::string kEmptyJs("empty.js"); |
lazyboy | e83ab9c6 | 2017-03-30 03:18:26 | [diff] [blame] | 610 | base::ScopedTempDir temp_dir; |
| 611 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 612 | base::FilePath unzipped_path = temp_dir.GetPath(); |
lazyboy | e83ab9c6 | 2017-03-30 03:18:26 | [diff] [blame] | 613 | |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 614 | scoped_refptr<Extension> extension = |
| 615 | content_verifier_test_utils::UnzipToDirAndLoadExtension( |
| 616 | GetContentVerifierTestPath().AppendASCII("source.zip"), |
| 617 | unzipped_path); |
| 618 | ASSERT_TRUE(extension.get()); |
| 619 | |
| 620 | base::FilePath kRelativePath(FILE_PATH_LITERAL("empty.js")); |
| 621 | ExtensionId extension_id = extension->id(); |
| 622 | |
| 623 | // Sanity check empty.js. |
| 624 | base::FilePath file_path = unzipped_path.AppendASCII(kEmptyJs); |
lazyboy | e83ab9c6 | 2017-03-30 03:18:26 | [diff] [blame] | 625 | int64_t foo_file_size = -1; |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 626 | ASSERT_TRUE(base::GetFileSize(file_path, &foo_file_size)); |
lazyboy | e83ab9c6 | 2017-03-30 03:18:26 | [diff] [blame] | 627 | ASSERT_EQ(0, foo_file_size); |
| 628 | |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 629 | // Request empty.js. |
| 630 | { |
Istiaque Ahmed | 584fe14 | 2018-03-13 09:19:04 | [diff] [blame] | 631 | TestContentVerifySingleJobObserver observer(extension_id, kRelativePath); |
lazyboy | e83ab9c6 | 2017-03-30 03:18:26 | [diff] [blame] | 632 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 633 | content_verifier_->OnExtensionLoaded(browser_context(), extension.get()); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 634 | // Wait for PostTask to ContentVerifierIOData::AddData() to finish. |
| 635 | content::RunAllPendingInMessageLoop(); |
lazyboy | e83ab9c6 | 2017-03-30 03:18:26 | [diff] [blame] | 636 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 637 | EXPECT_EQ(net::OK, DoRequestOrLoad(extension, kEmptyJs).result()); |
Istiaque Ahmed | 1dec1d4 | 2018-02-02 00:01:36 | [diff] [blame] | 638 | EXPECT_EQ(ContentVerifyJob::NONE, observer.WaitForJobFinished()); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | // chmod -r empty.js. |
| 642 | // Unreadable empty file doesn't generate hash mismatch. Note that this is the |
| 643 | // current behavior of ContentVerifyJob. |
| 644 | // TODO(lazyboy): The behavior is probably incorrect. |
| 645 | { |
Istiaque Ahmed | 584fe14 | 2018-03-13 09:19:04 | [diff] [blame] | 646 | TestContentVerifySingleJobObserver observer(extension->id(), kRelativePath); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 647 | base::FilePath file_path = unzipped_path.AppendASCII(kEmptyJs); |
| 648 | ASSERT_TRUE(base::MakeFileUnreadable(file_path)); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 649 | EXPECT_EQ(net::ERR_ACCESS_DENIED, |
| 650 | DoRequestOrLoad(extension, kEmptyJs).result()); |
Istiaque Ahmed | 1dec1d4 | 2018-02-02 00:01:36 | [diff] [blame] | 651 | EXPECT_EQ(ContentVerifyJob::NONE, observer.WaitForJobFinished()); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | // rm empty.js. |
| 655 | // Deleted empty file doesn't generate hash mismatch. Note that this is the |
| 656 | // current behavior of ContentVerifyJob. |
| 657 | // TODO(lazyboy): The behavior is probably incorrect. |
| 658 | { |
Istiaque Ahmed | 584fe14 | 2018-03-13 09:19:04 | [diff] [blame] | 659 | TestContentVerifySingleJobObserver observer(extension_id, kRelativePath); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 660 | base::FilePath file_path = unzipped_path.AppendASCII(kEmptyJs); |
| 661 | ASSERT_TRUE(base::DieFileDie(file_path, false)); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 662 | EXPECT_EQ(net::ERR_FILE_NOT_FOUND, |
| 663 | DoRequestOrLoad(extension, kEmptyJs).result()); |
Istiaque Ahmed | 1dec1d4 | 2018-02-02 00:01:36 | [diff] [blame] | 664 | EXPECT_EQ(ContentVerifyJob::NONE, observer.WaitForJobFinished()); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 665 | } |
lazyboy | e83ab9c6 | 2017-03-30 03:18:26 | [diff] [blame] | 666 | } |
| 667 | |
proberge | 4f9644c | 2018-03-27 23:01:54 | [diff] [blame^] | 668 | TEST_P(ExtensionProtocolsTest, VerifyScriptListedAsIcon) { |
| 669 | SetProtocolHandler(false); |
| 670 | |
| 671 | const std::string kBackgroundJs("background.js"); |
| 672 | base::ScopedTempDir temp_dir; |
| 673 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 674 | base::FilePath unzipped_path = temp_dir.GetPath(); |
| 675 | |
| 676 | base::FilePath path; |
| 677 | EXPECT_TRUE(PathService::Get(extensions::DIR_TEST_DATA, &path)); |
| 678 | |
| 679 | scoped_refptr<Extension> extension = |
| 680 | content_verifier_test_utils::UnzipToDirAndLoadExtension( |
| 681 | path.AppendASCII("content_hash_fetcher") |
| 682 | .AppendASCII("manifest_mislabeled_script") |
| 683 | .AppendASCII("source.zip"), |
| 684 | unzipped_path); |
| 685 | ASSERT_TRUE(extension.get()); |
| 686 | |
| 687 | base::FilePath kRelativePath(FILE_PATH_LITERAL("background.js")); |
| 688 | ExtensionId extension_id = extension->id(); |
| 689 | |
| 690 | // Request background.js. |
| 691 | { |
| 692 | TestContentVerifySingleJobObserver observer(extension_id, kRelativePath); |
| 693 | |
| 694 | content_verifier_->OnExtensionLoaded(browser_context(), extension.get()); |
| 695 | // Wait for PostTask to ContentVerifierIOData::AddData() to finish. |
| 696 | base::RunLoop().RunUntilIdle(); |
| 697 | |
| 698 | EXPECT_EQ(net::OK, DoRequestOrLoad(extension, kBackgroundJs).result()); |
| 699 | EXPECT_EQ(ContentVerifyJob::NONE, observer.WaitForJobFinished()); |
| 700 | } |
| 701 | |
| 702 | // Modify background.js and request it. |
| 703 | { |
| 704 | base::FilePath file_path = unzipped_path.AppendASCII("background.js"); |
| 705 | const std::string content = "new content"; |
| 706 | EXPECT_NE(base::WriteFile(file_path, content.c_str(), content.size()), -1); |
| 707 | TestContentVerifySingleJobObserver observer(extension_id, kRelativePath); |
| 708 | |
| 709 | content_verifier_->OnExtensionLoaded(browser_context(), extension.get()); |
| 710 | // Wait for PostTask to ContentVerifierIOData::AddData() to finish. |
| 711 | base::RunLoop().RunUntilIdle(); |
| 712 | |
| 713 | EXPECT_EQ(net::OK, DoRequestOrLoad(extension, kBackgroundJs).result()); |
| 714 | EXPECT_EQ(ContentVerifyJob::HASH_MISMATCH, observer.WaitForJobFinished()); |
| 715 | } |
| 716 | } |
| 717 | |
Devlin Cronin | c3f8807 | 2018-01-30 02:10:11 | [diff] [blame] | 718 | // Tests that mime types are properly set for returned extension resources. |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 719 | TEST_P(ExtensionProtocolsTest, MimeTypesForKnownFiles) { |
Devlin Cronin | c3f8807 | 2018-01-30 02:10:11 | [diff] [blame] | 720 | // Register a non-incognito extension protocol handler. |
| 721 | SetProtocolHandler(false); |
| 722 | |
| 723 | TestExtensionDir test_dir; |
| 724 | constexpr char kManifest[] = R"( |
| 725 | { |
| 726 | "name": "Test Ext", |
| 727 | "description": "A test extension", |
| 728 | "manifest_version": 2, |
| 729 | "version": "0.1", |
| 730 | "web_accessible_resources": ["*"] |
| 731 | })"; |
| 732 | test_dir.WriteManifest(kManifest); |
| 733 | std::unique_ptr<base::DictionaryValue> manifest = |
| 734 | base::DictionaryValue::From(base::test::ParseJson(kManifest)); |
| 735 | ASSERT_TRUE(manifest); |
| 736 | |
| 737 | test_dir.WriteFile(FILE_PATH_LITERAL("json_file.json"), "{}"); |
| 738 | test_dir.WriteFile(FILE_PATH_LITERAL("js_file.js"), "function() {}"); |
| 739 | |
| 740 | base::FilePath unpacked_path = test_dir.UnpackedPath(); |
| 741 | ASSERT_TRUE(base::PathExists(unpacked_path.AppendASCII("json_file.json"))); |
| 742 | std::string error; |
| 743 | scoped_refptr<const Extension> extension = |
| 744 | ExtensionBuilder() |
| 745 | .SetManifest(std::move(manifest)) |
| 746 | .SetPath(unpacked_path) |
| 747 | .SetLocation(Manifest::INTERNAL) |
| 748 | .Build(); |
| 749 | ASSERT_TRUE(extension); |
| 750 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 751 | AddExtension(extension.get(), false, false); |
Devlin Cronin | c3f8807 | 2018-01-30 02:10:11 | [diff] [blame] | 752 | |
| 753 | struct { |
| 754 | const char* file_name; |
| 755 | const char* expected_mime_type; |
| 756 | } test_cases[] = { |
| 757 | {"json_file.json", "application/json"}, |
| 758 | {"js_file.js", "application/javascript"}, |
| 759 | }; |
| 760 | |
| 761 | for (const auto& test_case : test_cases) { |
| 762 | SCOPED_TRACE(test_case.file_name); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 763 | auto result = RequestOrLoad(extension->GetResourceURL(test_case.file_name), |
| 764 | content::RESOURCE_TYPE_SUB_RESOURCE); |
| 765 | EXPECT_EQ( |
| 766 | test_case.expected_mime_type, |
| 767 | result.GetResponseHeaderByName(net::HttpRequestHeaders::kContentType)); |
Devlin Cronin | c3f8807 | 2018-01-30 02:10:11 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 771 | INSTANTIATE_TEST_CASE_P(Extensions, |
| 772 | ExtensionProtocolsTest, |
| 773 | ::testing::ValuesIn(kTestModes)); |
| 774 | |
[email protected] | 702d8b4 | 2013-02-27 20:55:50 | [diff] [blame] | 775 | } // namespace extensions |