blob: 04c432e540382c65b6be90a4214a78e90219fa90 [file] [log] [blame]
[email protected]1791e6c92014-04-11 08:29:011// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]5e212ed2012-03-21 23:29:152// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
avia2f4804a2015-12-24 23:11:135#include <stddef.h>
6
dchengc963c7142016-04-08 03:55:227#include <memory>
[email protected]5e212ed2012-03-21 23:29:158#include <string>
vabr9984ea62017-04-10 11:33:499#include <utility>
[email protected]5e212ed2012-03-21 23:29:1510
lazyboyd6dbb262017-03-30 00:57:3011#include "base/command_line.h"
thestigc9e38a22014-09-13 01:02:1112#include "base/files/file_util.h"
avia2f4804a2015-12-24 23:11:1313#include "base/macros.h"
vabr9984ea62017-04-10 11:33:4914#include "base/memory/ptr_util.h"
fdoraycb32419d2016-06-23 15:52:5515#include "base/run_loop.h"
[email protected]774cebd2013-09-26 04:55:0116#include "base/strings/string_number_conversions.h"
[email protected]00e7bef2013-06-10 20:35:1717#include "base/strings/string_util.h"
lazyboyd6dbb262017-03-30 00:57:3018#include "base/test/test_file_util.h"
[email protected]06492ed2013-03-24 22:13:1419#include "base/values.h"
lazyboyd6dbb262017-03-30 00:57:3020#include "chrome/browser/extensions/chrome_content_verifier_delegate.h"
[email protected]93ac047a2012-12-13 02:53:4921#include "chrome/common/chrome_paths.h"
lazyboyd6dbb262017-03-30 00:57:3022#include "chrome/common/chrome_switches.h"
23#include "chrome/test/base/testing_profile.h"
24#include "components/crx_file/id_util.h"
[email protected]5e212ed2012-03-21 23:29:1525#include "content/public/browser/resource_request_info.h"
jam7e588d6b2016-10-21 16:56:0626#include "content/public/common/browser_side_navigation_policy.h"
megjabloncaf312f2017-01-12 18:47:4927#include "content/public/common/previews_state.h"
[email protected]08a932d52012-06-03 21:42:1228#include "content/public/test/mock_resource_context.h"
[email protected]ec04d3f2013-06-06 21:31:3929#include "content/public/test/test_browser_thread_bundle.h"
lazyboyd6dbb262017-03-30 00:57:3030#include "content/public/test/test_utils.h"
31#include "extensions/browser/content_verifier.h"
[email protected]1791e6c92014-04-11 08:29:0132#include "extensions/browser/extension_protocols.h"
[email protected]38427a12013-11-09 17:34:2033#include "extensions/browser/info_map.h"
[email protected]885c0e92012-11-13 20:27:4234#include "extensions/common/constants.h"
[email protected]e4452d32013-11-15 23:07:4135#include "extensions/common/extension.h"
lazyboyd6dbb262017-03-30 00:57:3036#include "extensions/common/extension_builder.h"
asargenta093ec32016-02-13 01:36:4337#include "extensions/common/file_util.h"
[email protected]2ca01e52013-10-31 22:05:1938#include "net/base/request_priority.h"
rhalavati04b93382017-04-07 19:00:5439#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
[email protected]5e212ed2012-03-21 23:29:1540#include "net/url_request/url_request.h"
[email protected]9d5730b2012-08-24 17:42:4941#include "net/url_request/url_request_job_factory_impl.h"
[email protected]5e212ed2012-03-21 23:29:1542#include "net/url_request/url_request_status.h"
43#include "net/url_request/url_request_test_util.h"
44#include "testing/gtest/include/gtest/gtest.h"
45
[email protected]7491ad02014-07-05 19:10:0746using content::ResourceType;
47
[email protected]702d8b42013-02-27 20:55:5048namespace extensions {
jamescook8816ae52014-09-05 17:02:3749namespace {
[email protected]5e212ed2012-03-21 23:29:1550
asargenta093ec32016-02-13 01:36:4351base::FilePath GetTestPath(const std::string& name) {
52 base::FilePath path;
53 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path));
54 return path.AppendASCII("extensions").AppendASCII(name);
55}
56
lazyboyd6dbb262017-03-30 00:57:3057// Helper function that creates a file at |relative_path| within |directory|
58// and fills it with |content|.
59bool AddFileToDirectory(const base::FilePath& directory,
60 const base::FilePath& relative_path,
61 const std::string& content) {
62 base::FilePath full_path = directory.Append(relative_path);
63 int result = base::WriteFile(full_path, content.data(), content.size());
64 return static_cast<size_t>(result) == content.size();
65}
66
[email protected]5e212ed2012-03-21 23:29:1567scoped_refptr<Extension> CreateTestExtension(const std::string& name,
68 bool incognito_split_mode) {
[email protected]023b3d12013-12-23 18:46:4969 base::DictionaryValue manifest;
[email protected]5e212ed2012-03-21 23:29:1570 manifest.SetString("name", name);
71 manifest.SetString("version", "1");
[email protected]b109bdd2013-11-04 18:08:4372 manifest.SetInteger("manifest_version", 2);
[email protected]5e212ed2012-03-21 23:29:1573 manifest.SetString("incognito", incognito_split_mode ? "split" : "spanning");
74
asargenta093ec32016-02-13 01:36:4375 base::FilePath path = GetTestPath("response_headers");
[email protected]5e212ed2012-03-21 23:29:1576
77 std::string error;
78 scoped_refptr<Extension> extension(
[email protected]1d5e58b2013-01-31 08:41:4079 Extension::Create(path, Manifest::INTERNAL, manifest,
[email protected]ed3b9b12012-05-31 18:37:5180 Extension::NO_FLAGS, &error));
[email protected]5e212ed2012-03-21 23:29:1581 EXPECT_TRUE(extension.get()) << error;
82 return extension;
83}
84
[email protected]93ac047a2012-12-13 02:53:4985scoped_refptr<Extension> CreateWebStoreExtension() {
[email protected]023b3d12013-12-23 18:46:4986 base::DictionaryValue manifest;
[email protected]93ac047a2012-12-13 02:53:4987 manifest.SetString("name", "WebStore");
88 manifest.SetString("version", "1");
89 manifest.SetString("icons.16", "webstore_icon_16.png");
90
[email protected]650b2d52013-02-10 03:41:4591 base::FilePath path;
[email protected]93ac047a2012-12-13 02:53:4992 EXPECT_TRUE(PathService::Get(chrome::DIR_RESOURCES, &path));
93 path = path.AppendASCII("web_store");
94
95 std::string error;
96 scoped_refptr<Extension> extension(
[email protected]1d5e58b2013-01-31 08:41:4097 Extension::Create(path, Manifest::COMPONENT, manifest,
[email protected]93ac047a2012-12-13 02:53:4998 Extension::NO_FLAGS, &error));
99 EXPECT_TRUE(extension.get()) << error;
100 return extension;
101}
102
[email protected]6f7d7062013-06-04 03:49:33103scoped_refptr<Extension> CreateTestResponseHeaderExtension() {
[email protected]023b3d12013-12-23 18:46:49104 base::DictionaryValue manifest;
[email protected]6f7d7062013-06-04 03:49:33105 manifest.SetString("name", "An extension with web-accessible resources");
106 manifest.SetString("version", "2");
107
vabr9984ea62017-04-10 11:33:49108 auto web_accessible_list = base::MakeUnique<base::ListValue>();
[email protected]6f7d7062013-06-04 03:49:33109 web_accessible_list->AppendString("test.dat");
vabr9984ea62017-04-10 11:33:49110 manifest.Set("web_accessible_resources", std::move(web_accessible_list));
[email protected]6f7d7062013-06-04 03:49:33111
asargenta093ec32016-02-13 01:36:43112 base::FilePath path = GetTestPath("response_headers");
[email protected]6f7d7062013-06-04 03:49:33113
114 std::string error;
115 scoped_refptr<Extension> extension(
116 Extension::Create(path, Manifest::UNPACKED, manifest,
117 Extension::NO_FLAGS, &error));
118 EXPECT_TRUE(extension.get()) << error;
119 return extension;
120}
121
lazyboyd6dbb262017-03-30 00:57:30122// A ContentVerifyJob::TestDelegate that observes DoneReading().
123class JobDelegate : public ContentVerifyJob::TestDelegate {
124 public:
125 explicit JobDelegate(const std::string& expected_contents)
126 : expected_contents_(expected_contents), run_loop_(new base::RunLoop()) {
127 ContentVerifyJob::SetDelegateForTests(this);
128 }
129 ~JobDelegate() override { ContentVerifyJob::SetDelegateForTests(nullptr); }
130
131 ContentVerifyJob::FailureReason BytesRead(const ExtensionId& id,
132 int count,
133 const char* data) override {
134 read_contents_.append(data, count);
135 return ContentVerifyJob::NONE;
136 }
137
138 ContentVerifyJob::FailureReason DoneReading(const ExtensionId& id) override {
139 seen_done_reading_extension_ids_.insert(id);
140 if (waiting_for_extension_id_ == id)
141 run_loop_->Quit();
142
143 if (!base::StartsWith(expected_contents_, read_contents_,
144 base::CompareCase::SENSITIVE)) {
145 ADD_FAILURE() << "Unexpected read, expected: " << expected_contents_
146 << ", but found: " << read_contents_;
147 }
148 return ContentVerifyJob::NONE;
149 }
150
151 void WaitForDoneReading(const ExtensionId& id) {
152 ASSERT_FALSE(waiting_for_extension_id_);
153 if (seen_done_reading_extension_ids_.count(id))
154 return;
155 waiting_for_extension_id_ = id;
156 run_loop_->Run();
157 }
158
159 void Reset() {
160 read_contents_.clear();
161 waiting_for_extension_id_.reset();
162 seen_done_reading_extension_ids_.clear();
163 run_loop_ = base::MakeUnique<base::RunLoop>();
164 }
165
166 private:
167 std::string expected_contents_;
168 std::string read_contents_;
169 std::set<ExtensionId> seen_done_reading_extension_ids_;
170 base::Optional<ExtensionId> waiting_for_extension_id_;
171 std::unique_ptr<base::RunLoop> run_loop_;
172
173 DISALLOW_COPY_AND_ASSIGN(JobDelegate);
174};
175
jamescook8816ae52014-09-05 17:02:37176} // namespace
177
178// This test lives in src/chrome instead of src/extensions because it tests
179// functionality delegated back to Chrome via ChromeExtensionsBrowserClient.
lfg048201a2014-09-16 19:09:36180// See chrome/browser/extensions/chrome_url_request_util.cc.
lazyboyd6dbb262017-03-30 00:57:30181class ExtensionProtocolsTest : public testing::Test {
[email protected]5e212ed2012-03-21 23:29:15182 public:
lazyboyd6dbb262017-03-30 00:57:30183 ExtensionProtocolsTest()
[email protected]1791e6c92014-04-11 08:29:01184 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
185 old_factory_(NULL),
186 resource_context_(&test_url_request_context_) {}
[email protected]5e212ed2012-03-21 23:29:15187
dcheng72191812014-10-28 20:49:56188 void SetUp() override {
[email protected]06492ed2013-03-24 22:13:14189 testing::Test::SetUp();
lazyboyd6dbb262017-03-30 00:57:30190 testing_profile_ = TestingProfile::Builder().Build();
[email protected]38427a12013-11-09 17:34:20191 extension_info_map_ = new InfoMap();
[email protected]5e212ed2012-03-21 23:29:15192 net::URLRequestContext* request_context =
193 resource_context_.GetRequestContext();
194 old_factory_ = request_context->job_factory();
lazyboyd6dbb262017-03-30 00:57:30195
196 // Set up content verification.
197 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
198 command_line->AppendSwitchASCII(
199 switches::kExtensionContentVerification,
200 switches::kExtensionContentVerificationEnforce);
201 content_verifier_ = new ContentVerifier(
lazyboy528dedac2017-03-30 17:33:32202 testing_profile_.get(), base::MakeUnique<ChromeContentVerifierDelegate>(
203 testing_profile_.get()));
lazyboyd6dbb262017-03-30 00:57:30204 extension_info_map_->SetContentVerifier(content_verifier_.get());
[email protected]5e212ed2012-03-21 23:29:15205 }
206
dcheng72191812014-10-28 20:49:56207 void TearDown() override {
[email protected]5e212ed2012-03-21 23:29:15208 net::URLRequestContext* request_context =
209 resource_context_.GetRequestContext();
210 request_context->set_job_factory(old_factory_);
lazyboyd6dbb262017-03-30 00:57:30211 content_verifier_->Shutdown();
[email protected]5e212ed2012-03-21 23:29:15212 }
213
[email protected]1791e6c92014-04-11 08:29:01214 void SetProtocolHandler(bool is_incognito) {
[email protected]93ac047a2012-12-13 02:53:49215 net::URLRequestContext* request_context =
216 resource_context_.GetRequestContext();
217 job_factory_.SetProtocolHandler(
[email protected]702d8b42013-02-27 20:55:50218 kExtensionScheme,
[email protected]1791e6c92014-04-11 08:29:01219 CreateExtensionProtocolHandler(is_incognito,
[email protected]7b7e0b32014-03-17 16:09:23220 extension_info_map_.get()));
[email protected]93ac047a2012-12-13 02:53:49221 request_context->set_job_factory(&job_factory_);
222 }
223
[email protected]5e212ed2012-03-21 23:29:15224 void StartRequest(net::URLRequest* request,
[email protected]6c1e05212014-07-31 00:59:40225 ResourceType resource_type) {
gabf9d15582014-11-13 16:40:15226 content::ResourceRequestInfo::AllocateForTesting(
megjabloncaf312f2017-01-12 18:47:49227 request, resource_type, &resource_context_,
228 /*render_process_id=*/-1,
229 /*render_view_id=*/-1,
230 /*render_frame_id=*/-1,
231 /*is_main_frame=*/resource_type == content::RESOURCE_TYPE_MAIN_FRAME,
megjabloncaf312f2017-01-12 18:47:49232 /*allow_download=*/true,
Jian Li18173422017-11-08 03:00:02233 /*is_async=*/false, content::PREVIEWS_OFF,
234 /*navigation_ui_data*/ nullptr);
[email protected]5e212ed2012-03-21 23:29:15235 request->Start();
fdoraycb32419d2016-06-23 15:52:55236 base::RunLoop().Run();
[email protected]5e212ed2012-03-21 23:29:15237 }
238
asargenta093ec32016-02-13 01:36:43239 // Helper method to create a URLRequest, call StartRequest on it, and return
240 // the result. If |extension| hasn't already been added to
241 // |extension_info_map_|, this will add it.
maksim.sisov1b83bb72016-10-07 06:07:23242 int DoRequest(const Extension& extension, const std::string& relative_path) {
asargenta093ec32016-02-13 01:36:43243 if (!extension_info_map_->extensions().Contains(extension.id())) {
244 extension_info_map_->AddExtension(&extension,
245 base::Time::Now(),
246 false, // incognito_enabled
247 false); // notifications_disabled
248 }
dchengc963c7142016-04-08 03:55:22249 std::unique_ptr<net::URLRequest> request(
asargenta093ec32016-02-13 01:36:43250 resource_context_.GetRequestContext()->CreateRequest(
dchengc963c7142016-04-08 03:55:22251 extension.GetResourceURL(relative_path), net::DEFAULT_PRIORITY,
rhalavati04b93382017-04-07 19:00:54252 &test_delegate_, TRAFFIC_ANNOTATION_FOR_TESTS));
asargenta093ec32016-02-13 01:36:43253 StartRequest(request.get(), content::RESOURCE_TYPE_MAIN_FRAME);
maksim.sisov1b83bb72016-10-07 06:07:23254 return test_delegate_.request_status();
asargenta093ec32016-02-13 01:36:43255 }
256
[email protected]5e212ed2012-03-21 23:29:15257 protected:
[email protected]ec04d3f2013-06-06 21:31:39258 content::TestBrowserThreadBundle thread_bundle_;
[email protected]38427a12013-11-09 17:34:20259 scoped_refptr<InfoMap> extension_info_map_;
[email protected]9d5730b2012-08-24 17:42:49260 net::URLRequestJobFactoryImpl job_factory_;
[email protected]5e212ed2012-03-21 23:29:15261 const net::URLRequestJobFactory* old_factory_;
[email protected]2086a3d2012-11-13 17:49:20262 net::TestDelegate test_delegate_;
[email protected]37ac95b2013-07-23 23:39:35263 net::TestURLRequestContext test_url_request_context_;
[email protected]5e212ed2012-03-21 23:29:15264 content::MockResourceContext resource_context_;
lazyboyd6dbb262017-03-30 00:57:30265 scoped_refptr<ContentVerifier> content_verifier_;
266 std::unique_ptr<TestingProfile> testing_profile_;
[email protected]5e212ed2012-03-21 23:29:15267};
268
269// Tests that making a chrome-extension request in an incognito context is
270// only allowed under the right circumstances (if the extension is allowed
271// in incognito, and it's either a non-main-frame request or a split-mode
272// extension).
lazyboyd6dbb262017-03-30 00:57:30273TEST_F(ExtensionProtocolsTest, IncognitoRequest) {
[email protected]93ac047a2012-12-13 02:53:49274 // Register an incognito extension protocol handler.
[email protected]1791e6c92014-04-11 08:29:01275 SetProtocolHandler(true);
[email protected]93ac047a2012-12-13 02:53:49276
[email protected]5e212ed2012-03-21 23:29:15277 struct TestCase {
278 // Inputs.
279 std::string name;
280 bool incognito_split_mode;
281 bool incognito_enabled;
282
283 // Expected results.
284 bool should_allow_main_frame_load;
285 bool should_allow_sub_frame_load;
286 } cases[] = {
287 {"spanning disabled", false, false, false, false},
288 {"split disabled", true, false, false, false},
nasko5cf9d452016-06-01 05:34:56289 {"spanning enabled", false, true, false, false},
290 {"split enabled", true, true, true, false},
[email protected]5e212ed2012-03-21 23:29:15291 };
292
viettrungluu9e65ad12014-10-16 04:22:26293 for (size_t i = 0; i < arraysize(cases); ++i) {
[email protected]5e212ed2012-03-21 23:29:15294 scoped_refptr<Extension> extension =
295 CreateTestExtension(cases[i].name, cases[i].incognito_split_mode);
296 extension_info_map_->AddExtension(
[email protected]9afacd22013-11-13 20:23:31297 extension.get(), base::Time::Now(), cases[i].incognito_enabled, false);
[email protected]5e212ed2012-03-21 23:29:15298
299 // First test a main frame request.
300 {
301 // It doesn't matter that the resource doesn't exist. If the resource
naskob9164c42016-06-07 01:21:35302 // is blocked, we should see BLOCKED_BY_CLIENT. Otherwise, the request
[email protected]5e212ed2012-03-21 23:29:15303 // should just fail because the file doesn't exist.
dchengc963c7142016-04-08 03:55:22304 std::unique_ptr<net::URLRequest> request(
[email protected]f7022f32014-08-21 16:32:19305 resource_context_.GetRequestContext()->CreateRequest(
dchengc963c7142016-04-08 03:55:22306 extension->GetResourceURL("404.html"), net::DEFAULT_PRIORITY,
rhalavati04b93382017-04-07 19:00:54307 &test_delegate_, TRAFFIC_ANNOTATION_FOR_TESTS));
[email protected]f7022f32014-08-21 16:32:19308 StartRequest(request.get(), content::RESOURCE_TYPE_MAIN_FRAME);
[email protected]5e212ed2012-03-21 23:29:15309
310 if (cases[i].should_allow_main_frame_load) {
maksim.sisov1b83bb72016-10-07 06:07:23311 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, test_delegate_.request_status())
312 << cases[i].name;
[email protected]5e212ed2012-03-21 23:29:15313 } else {
maksim.sisov1b83bb72016-10-07 06:07:23314 EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, test_delegate_.request_status())
naskob9164c42016-06-07 01:21:35315 << cases[i].name;
[email protected]5e212ed2012-03-21 23:29:15316 }
317 }
318
319 // Now do a subframe request.
320 {
jam7e588d6b2016-10-21 16:56:06321 // With PlzNavigate, the subframe navigation requests are blocked in
322 // ExtensionNavigationThrottle which isn't added in this unit test. This
323 // is tested in an integration test in
324 // ExtensionResourceRequestPolicyTest.IframeNavigateToInaccessible.
325 if (!content::IsBrowserSideNavigationEnabled()) {
326 std::unique_ptr<net::URLRequest> request(
327 resource_context_.GetRequestContext()->CreateRequest(
328 extension->GetResourceURL("404.html"), net::DEFAULT_PRIORITY,
rhalavati04b93382017-04-07 19:00:54329 &test_delegate_, TRAFFIC_ANNOTATION_FOR_TESTS));
jam7e588d6b2016-10-21 16:56:06330 StartRequest(request.get(), content::RESOURCE_TYPE_SUB_FRAME);
[email protected]5e212ed2012-03-21 23:29:15331
jam7e588d6b2016-10-21 16:56:06332 if (cases[i].should_allow_sub_frame_load) {
333 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, test_delegate_.request_status())
334 << cases[i].name;
335 } else {
336 EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, test_delegate_.request_status())
337 << cases[i].name;
338 }
[email protected]5e212ed2012-03-21 23:29:15339 }
340 }
341 }
342}
343
[email protected]774cebd2013-09-26 04:55:01344void CheckForContentLengthHeader(net::URLRequest* request) {
345 std::string content_length;
346 request->GetResponseHeaderByName(net::HttpRequestHeaders::kContentLength,
347 &content_length);
348 EXPECT_FALSE(content_length.empty());
349 int length_value = 0;
350 EXPECT_TRUE(base::StringToInt(content_length, &length_value));
351 EXPECT_GT(length_value, 0);
352}
353
[email protected]93ac047a2012-12-13 02:53:49354// Tests getting a resource for a component extension works correctly, both when
355// the extension is enabled and when it is disabled.
lazyboyd6dbb262017-03-30 00:57:30356TEST_F(ExtensionProtocolsTest, ComponentResourceRequest) {
[email protected]93ac047a2012-12-13 02:53:49357 // Register a non-incognito extension protocol handler.
[email protected]1791e6c92014-04-11 08:29:01358 SetProtocolHandler(false);
[email protected]93ac047a2012-12-13 02:53:49359
360 scoped_refptr<Extension> extension = CreateWebStoreExtension();
[email protected]9afacd22013-11-13 20:23:31361 extension_info_map_->AddExtension(extension.get(),
362 base::Time::Now(),
363 false,
364 false);
[email protected]93ac047a2012-12-13 02:53:49365
366 // First test it with the extension enabled.
367 {
dchengc963c7142016-04-08 03:55:22368 std::unique_ptr<net::URLRequest> request(
[email protected]f7022f32014-08-21 16:32:19369 resource_context_.GetRequestContext()->CreateRequest(
370 extension->GetResourceURL("webstore_icon_16.png"),
rhalavati04b93382017-04-07 19:00:54371 net::DEFAULT_PRIORITY, &test_delegate_,
372 TRAFFIC_ANNOTATION_FOR_TESTS));
[email protected]f7022f32014-08-21 16:32:19373 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA);
maksim.sisov1b83bb72016-10-07 06:07:23374 EXPECT_EQ(net::OK, test_delegate_.request_status());
[email protected]f7022f32014-08-21 16:32:19375 CheckForContentLengthHeader(request.get());
Chris Mumford9f87e392017-12-15 20:44:55376 std::string mime_type;
377 request->GetResponseHeaderByName(net::HttpRequestHeaders::kContentType,
378 &mime_type);
379 EXPECT_EQ("image/png", mime_type);
[email protected]93ac047a2012-12-13 02:53:49380 }
381
382 // And then test it with the extension disabled.
383 extension_info_map_->RemoveExtension(extension->id(),
limasdf0deef2042017-05-03 19:17:17384 UnloadedExtensionReason::DISABLE);
[email protected]93ac047a2012-12-13 02:53:49385 {
dchengc963c7142016-04-08 03:55:22386 std::unique_ptr<net::URLRequest> request(
[email protected]f7022f32014-08-21 16:32:19387 resource_context_.GetRequestContext()->CreateRequest(
388 extension->GetResourceURL("webstore_icon_16.png"),
rhalavati04b93382017-04-07 19:00:54389 net::DEFAULT_PRIORITY, &test_delegate_,
390 TRAFFIC_ANNOTATION_FOR_TESTS));
[email protected]f7022f32014-08-21 16:32:19391 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA);
maksim.sisov1b83bb72016-10-07 06:07:23392 EXPECT_EQ(net::OK, test_delegate_.request_status());
[email protected]f7022f32014-08-21 16:32:19393 CheckForContentLengthHeader(request.get());
Chris Mumford9f87e392017-12-15 20:44:55394 std::string mime_type;
395 request->GetResponseHeaderByName(net::HttpRequestHeaders::kContentType,
396 &mime_type);
397 EXPECT_EQ("image/png", mime_type);
[email protected]93ac047a2012-12-13 02:53:49398 }
399}
400
[email protected]6f7d7062013-06-04 03:49:33401// Tests that a URL request for resource from an extension returns a few
402// expected response headers.
lazyboyd6dbb262017-03-30 00:57:30403TEST_F(ExtensionProtocolsTest, ResourceRequestResponseHeaders) {
[email protected]6f7d7062013-06-04 03:49:33404 // Register a non-incognito extension protocol handler.
[email protected]1791e6c92014-04-11 08:29:01405 SetProtocolHandler(false);
[email protected]6f7d7062013-06-04 03:49:33406
407 scoped_refptr<Extension> extension = CreateTestResponseHeaderExtension();
[email protected]9afacd22013-11-13 20:23:31408 extension_info_map_->AddExtension(extension.get(),
409 base::Time::Now(),
410 false,
411 false);
[email protected]6f7d7062013-06-04 03:49:33412
413 {
dchengc963c7142016-04-08 03:55:22414 std::unique_ptr<net::URLRequest> request(
[email protected]f7022f32014-08-21 16:32:19415 resource_context_.GetRequestContext()->CreateRequest(
dchengc963c7142016-04-08 03:55:22416 extension->GetResourceURL("test.dat"), net::DEFAULT_PRIORITY,
rhalavati04b93382017-04-07 19:00:54417 &test_delegate_, TRAFFIC_ANNOTATION_FOR_TESTS));
[email protected]f7022f32014-08-21 16:32:19418 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA);
maksim.sisov1b83bb72016-10-07 06:07:23419 EXPECT_EQ(net::OK, test_delegate_.request_status());
[email protected]6f7d7062013-06-04 03:49:33420
421 // Check that cache-related headers are set.
422 std::string etag;
[email protected]f7022f32014-08-21 16:32:19423 request->GetResponseHeaderByName("ETag", &etag);
brettw66d1b81b2015-07-06 19:29:40424 EXPECT_TRUE(base::StartsWith(etag, "\"", base::CompareCase::SENSITIVE));
425 EXPECT_TRUE(base::EndsWith(etag, "\"", base::CompareCase::SENSITIVE));
[email protected]6f7d7062013-06-04 03:49:33426
427 std::string revalidation_header;
[email protected]f7022f32014-08-21 16:32:19428 request->GetResponseHeaderByName("cache-control", &revalidation_header);
[email protected]6f7d7062013-06-04 03:49:33429 EXPECT_EQ("no-cache", revalidation_header);
430
431 // We set test.dat as web-accessible, so it should have a CORS header.
432 std::string access_control;
[email protected]f7022f32014-08-21 16:32:19433 request->GetResponseHeaderByName("Access-Control-Allow-Origin",
[email protected]6f7d7062013-06-04 03:49:33434 &access_control);
435 EXPECT_EQ("*", access_control);
436 }
437}
438
[email protected]b109bdd2013-11-04 18:08:43439// Tests that a URL request for main frame or subframe from an extension
440// succeeds, but subresources fail. See http://crbug.com/312269.
lazyboyd6dbb262017-03-30 00:57:30441TEST_F(ExtensionProtocolsTest, AllowFrameRequests) {
[email protected]b109bdd2013-11-04 18:08:43442 // Register a non-incognito extension protocol handler.
[email protected]1791e6c92014-04-11 08:29:01443 SetProtocolHandler(false);
[email protected]b109bdd2013-11-04 18:08:43444
445 scoped_refptr<Extension> extension = CreateTestExtension("foo", false);
[email protected]9afacd22013-11-13 20:23:31446 extension_info_map_->AddExtension(extension.get(),
447 base::Time::Now(),
448 false,
449 false);
[email protected]b109bdd2013-11-04 18:08:43450
nasko5cf9d452016-06-01 05:34:56451 // All MAIN_FRAME requests should succeed. SUB_FRAME requests that are not
452 // explicitly listed in web_accesible_resources or same-origin to the parent
453 // should not succeed.
[email protected]b109bdd2013-11-04 18:08:43454 {
dchengc963c7142016-04-08 03:55:22455 std::unique_ptr<net::URLRequest> request(
[email protected]f7022f32014-08-21 16:32:19456 resource_context_.GetRequestContext()->CreateRequest(
dchengc963c7142016-04-08 03:55:22457 extension->GetResourceURL("test.dat"), net::DEFAULT_PRIORITY,
rhalavati04b93382017-04-07 19:00:54458 &test_delegate_, TRAFFIC_ANNOTATION_FOR_TESTS));
[email protected]f7022f32014-08-21 16:32:19459 StartRequest(request.get(), content::RESOURCE_TYPE_MAIN_FRAME);
maksim.sisov1b83bb72016-10-07 06:07:23460 EXPECT_EQ(net::OK, test_delegate_.request_status());
[email protected]b109bdd2013-11-04 18:08:43461 }
462 {
jam7e588d6b2016-10-21 16:56:06463 // With PlzNavigate, the subframe navigation requests are blocked in
464 // ExtensionNavigationThrottle which isn't added in this unit test. This is
465 // tested in an integration test in
466 // ExtensionResourceRequestPolicyTest.IframeNavigateToInaccessible.
467 if (!content::IsBrowserSideNavigationEnabled()) {
468 std::unique_ptr<net::URLRequest> request(
469 resource_context_.GetRequestContext()->CreateRequest(
470 extension->GetResourceURL("test.dat"), net::DEFAULT_PRIORITY,
rhalavati04b93382017-04-07 19:00:54471 &test_delegate_, TRAFFIC_ANNOTATION_FOR_TESTS));
jam7e588d6b2016-10-21 16:56:06472 StartRequest(request.get(), content::RESOURCE_TYPE_SUB_FRAME);
473 EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, test_delegate_.request_status());
474 }
[email protected]b109bdd2013-11-04 18:08:43475 }
476
477 // And subresource types, such as media, should fail.
478 {
dchengc963c7142016-04-08 03:55:22479 std::unique_ptr<net::URLRequest> request(
[email protected]f7022f32014-08-21 16:32:19480 resource_context_.GetRequestContext()->CreateRequest(
dchengc963c7142016-04-08 03:55:22481 extension->GetResourceURL("test.dat"), net::DEFAULT_PRIORITY,
rhalavati04b93382017-04-07 19:00:54482 &test_delegate_, TRAFFIC_ANNOTATION_FOR_TESTS));
[email protected]f7022f32014-08-21 16:32:19483 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA);
maksim.sisov1b83bb72016-10-07 06:07:23484 EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, test_delegate_.request_status());
[email protected]b109bdd2013-11-04 18:08:43485 }
486}
487
lazyboyd6dbb262017-03-30 00:57:30488TEST_F(ExtensionProtocolsTest, MetadataFolder) {
asargenta093ec32016-02-13 01:36:43489 SetProtocolHandler(false);
490
491 base::FilePath extension_dir = GetTestPath("metadata_folder");
492 std::string error;
493 scoped_refptr<Extension> extension =
494 file_util::LoadExtension(extension_dir, Manifest::INTERNAL,
495 Extension::NO_FLAGS, &error);
496 ASSERT_NE(extension.get(), nullptr) << "error: " << error;
497
498 // Loading "/test.html" should succeed.
maksim.sisov1b83bb72016-10-07 06:07:23499 EXPECT_EQ(net::OK, DoRequest(*extension, "test.html"));
asargenta093ec32016-02-13 01:36:43500
501 // Loading "/_metadata/verified_contents.json" should fail.
502 base::FilePath relative_path =
503 base::FilePath(kMetadataFolder).Append(kVerifiedContentsFilename);
504 EXPECT_TRUE(base::PathExists(extension_dir.Append(relative_path)));
maksim.sisov1b83bb72016-10-07 06:07:23505 EXPECT_EQ(net::ERR_FAILED,
asargenta093ec32016-02-13 01:36:43506 DoRequest(*extension, relative_path.AsUTF8Unsafe()));
507
508 // Loading "/_metadata/a.txt" should also fail.
509 relative_path = base::FilePath(kMetadataFolder).AppendASCII("a.txt");
510 EXPECT_TRUE(base::PathExists(extension_dir.Append(relative_path)));
maksim.sisov1b83bb72016-10-07 06:07:23511 EXPECT_EQ(net::ERR_FAILED,
asargenta093ec32016-02-13 01:36:43512 DoRequest(*extension, relative_path.AsUTF8Unsafe()));
513}
514
lazyboyd6dbb262017-03-30 00:57:30515// Tests that unreadable files and deleted files correctly go through
516// ContentVerifyJob.
517TEST_F(ExtensionProtocolsTest, VerificationSeenForFileAccessErrors) {
518 const char kFooJsContents[] = "hello world.";
519 JobDelegate test_job_delegate(kFooJsContents);
520 SetProtocolHandler(false);
521
522 const std::string kFooJs("foo.js");
523 // Create a temporary directory that a fake extension will live in and fill
524 // it with some test files.
525 base::ScopedTempDir temp_dir;
526 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
527 base::FilePath foo_js(FILE_PATH_LITERAL("foo.js"));
528 ASSERT_TRUE(AddFileToDirectory(temp_dir.GetPath(), foo_js, kFooJsContents))
529 << "Failed to write " << temp_dir.GetPath().value() << "/"
530 << foo_js.value();
531
532 ExtensionBuilder builder;
533 builder
534 .SetManifest(DictionaryBuilder()
535 .Set("name", "Foo")
536 .Set("version", "1.0")
537 .Set("manifest_version", 2)
538 .Set("update_url",
539 "https://clients2.google.com/service/update2/crx")
540 .Build())
541 .SetID(crx_file::id_util::GenerateId("whatever"))
542 .SetPath(temp_dir.GetPath())
543 .SetLocation(Manifest::INTERNAL);
544 scoped_refptr<Extension> extension(builder.Build());
545
546 ASSERT_TRUE(extension.get());
547 content_verifier_->OnExtensionLoaded(testing_profile_.get(), extension.get());
548 // Wait for PostTask to ContentVerifierIOData::AddData() to finish.
549 content::RunAllPendingInMessageLoop();
550
551 // Valid and readable foo.js.
552 EXPECT_EQ(net::OK, DoRequest(*extension, kFooJs));
553 test_job_delegate.WaitForDoneReading(extension->id());
554
555 // chmod -r foo.js.
556 base::FilePath foo_path = temp_dir.GetPath().AppendASCII(kFooJs);
557 ASSERT_TRUE(base::MakeFileUnreadable(foo_path));
558 test_job_delegate.Reset();
559 EXPECT_EQ(net::ERR_ACCESS_DENIED, DoRequest(*extension, kFooJs));
560 test_job_delegate.WaitForDoneReading(extension->id());
561
562 // Delete foo.js.
563 ASSERT_TRUE(base::DieFileDie(foo_path, false));
564 test_job_delegate.Reset();
565 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, DoRequest(*extension, kFooJs));
566 test_job_delegate.WaitForDoneReading(extension->id());
567}
568
lazyboye83ab9c62017-03-30 03:18:26569// Tests that zero byte files correctly go through ContentVerifyJob.
570TEST_F(ExtensionProtocolsTest, VerificationSeenForZeroByteFile) {
571 const char kFooJsContents[] = ""; // Empty.
572 JobDelegate test_job_delegate(kFooJsContents);
573 SetProtocolHandler(false);
574
575 const std::string kFooJs("foo.js");
576 // Create a temporary directory that a fake extension will live in and fill
577 // it with some test files.
578 base::ScopedTempDir temp_dir;
579 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
580 base::FilePath foo_js(FILE_PATH_LITERAL("foo.js"));
581 ASSERT_TRUE(AddFileToDirectory(temp_dir.GetPath(), foo_js, kFooJsContents))
582 << "Failed to write " << temp_dir.GetPath().value() << "/"
583 << foo_js.value();
584
585 // Sanity check foo.js.
586 base::FilePath foo_path = temp_dir.GetPath().AppendASCII(kFooJs);
587 int64_t foo_file_size = -1;
588 ASSERT_TRUE(base::GetFileSize(foo_path, &foo_file_size));
589 ASSERT_EQ(0, foo_file_size);
590
591 ExtensionBuilder builder;
592 builder
593 .SetManifest(DictionaryBuilder()
594 .Set("name", "Foo")
595 .Set("version", "1.0")
596 .Set("manifest_version", 2)
597 .Set("update_url",
598 "https://clients2.google.com/service/update2/crx")
599 .Build())
600 .SetID(crx_file::id_util::GenerateId("whatever"))
601 .SetPath(temp_dir.GetPath())
602 .SetLocation(Manifest::INTERNAL);
603 scoped_refptr<Extension> extension(builder.Build());
604
605 ASSERT_TRUE(extension.get());
606 content_verifier_->OnExtensionLoaded(testing_profile_.get(), extension.get());
607 // Wait for PostTask to ContentVerifierIOData::AddData() to finish.
608 content::RunAllPendingInMessageLoop();
609
610 // Request foo.js.
611 EXPECT_EQ(net::OK, DoRequest(*extension, kFooJs));
612 test_job_delegate.WaitForDoneReading(extension->id());
613}
614
[email protected]702d8b42013-02-27 20:55:50615} // namespace extensions