blob: a3ddd331941465b2914f61db1b04c8ac78c0367a [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());
[email protected]93ac047a2012-12-13 02:53:49376 }
377
378 // And then test it with the extension disabled.
379 extension_info_map_->RemoveExtension(extension->id(),
limasdf0deef2042017-05-03 19:17:17380 UnloadedExtensionReason::DISABLE);
[email protected]93ac047a2012-12-13 02:53:49381 {
dchengc963c7142016-04-08 03:55:22382 std::unique_ptr<net::URLRequest> request(
[email protected]f7022f32014-08-21 16:32:19383 resource_context_.GetRequestContext()->CreateRequest(
384 extension->GetResourceURL("webstore_icon_16.png"),
rhalavati04b93382017-04-07 19:00:54385 net::DEFAULT_PRIORITY, &test_delegate_,
386 TRAFFIC_ANNOTATION_FOR_TESTS));
[email protected]f7022f32014-08-21 16:32:19387 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA);
maksim.sisov1b83bb72016-10-07 06:07:23388 EXPECT_EQ(net::OK, test_delegate_.request_status());
[email protected]f7022f32014-08-21 16:32:19389 CheckForContentLengthHeader(request.get());
[email protected]93ac047a2012-12-13 02:53:49390 }
391}
392
[email protected]6f7d7062013-06-04 03:49:33393// Tests that a URL request for resource from an extension returns a few
394// expected response headers.
lazyboyd6dbb262017-03-30 00:57:30395TEST_F(ExtensionProtocolsTest, ResourceRequestResponseHeaders) {
[email protected]6f7d7062013-06-04 03:49:33396 // Register a non-incognito extension protocol handler.
[email protected]1791e6c92014-04-11 08:29:01397 SetProtocolHandler(false);
[email protected]6f7d7062013-06-04 03:49:33398
399 scoped_refptr<Extension> extension = CreateTestResponseHeaderExtension();
[email protected]9afacd22013-11-13 20:23:31400 extension_info_map_->AddExtension(extension.get(),
401 base::Time::Now(),
402 false,
403 false);
[email protected]6f7d7062013-06-04 03:49:33404
405 {
dchengc963c7142016-04-08 03:55:22406 std::unique_ptr<net::URLRequest> request(
[email protected]f7022f32014-08-21 16:32:19407 resource_context_.GetRequestContext()->CreateRequest(
dchengc963c7142016-04-08 03:55:22408 extension->GetResourceURL("test.dat"), net::DEFAULT_PRIORITY,
rhalavati04b93382017-04-07 19:00:54409 &test_delegate_, TRAFFIC_ANNOTATION_FOR_TESTS));
[email protected]f7022f32014-08-21 16:32:19410 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA);
maksim.sisov1b83bb72016-10-07 06:07:23411 EXPECT_EQ(net::OK, test_delegate_.request_status());
[email protected]6f7d7062013-06-04 03:49:33412
413 // Check that cache-related headers are set.
414 std::string etag;
[email protected]f7022f32014-08-21 16:32:19415 request->GetResponseHeaderByName("ETag", &etag);
brettw66d1b81b2015-07-06 19:29:40416 EXPECT_TRUE(base::StartsWith(etag, "\"", base::CompareCase::SENSITIVE));
417 EXPECT_TRUE(base::EndsWith(etag, "\"", base::CompareCase::SENSITIVE));
[email protected]6f7d7062013-06-04 03:49:33418
419 std::string revalidation_header;
[email protected]f7022f32014-08-21 16:32:19420 request->GetResponseHeaderByName("cache-control", &revalidation_header);
[email protected]6f7d7062013-06-04 03:49:33421 EXPECT_EQ("no-cache", revalidation_header);
422
423 // We set test.dat as web-accessible, so it should have a CORS header.
424 std::string access_control;
[email protected]f7022f32014-08-21 16:32:19425 request->GetResponseHeaderByName("Access-Control-Allow-Origin",
[email protected]6f7d7062013-06-04 03:49:33426 &access_control);
427 EXPECT_EQ("*", access_control);
428 }
429}
430
[email protected]b109bdd2013-11-04 18:08:43431// Tests that a URL request for main frame or subframe from an extension
432// succeeds, but subresources fail. See http://crbug.com/312269.
lazyboyd6dbb262017-03-30 00:57:30433TEST_F(ExtensionProtocolsTest, AllowFrameRequests) {
[email protected]b109bdd2013-11-04 18:08:43434 // Register a non-incognito extension protocol handler.
[email protected]1791e6c92014-04-11 08:29:01435 SetProtocolHandler(false);
[email protected]b109bdd2013-11-04 18:08:43436
437 scoped_refptr<Extension> extension = CreateTestExtension("foo", false);
[email protected]9afacd22013-11-13 20:23:31438 extension_info_map_->AddExtension(extension.get(),
439 base::Time::Now(),
440 false,
441 false);
[email protected]b109bdd2013-11-04 18:08:43442
nasko5cf9d452016-06-01 05:34:56443 // All MAIN_FRAME requests should succeed. SUB_FRAME requests that are not
444 // explicitly listed in web_accesible_resources or same-origin to the parent
445 // should not succeed.
[email protected]b109bdd2013-11-04 18:08:43446 {
dchengc963c7142016-04-08 03:55:22447 std::unique_ptr<net::URLRequest> request(
[email protected]f7022f32014-08-21 16:32:19448 resource_context_.GetRequestContext()->CreateRequest(
dchengc963c7142016-04-08 03:55:22449 extension->GetResourceURL("test.dat"), net::DEFAULT_PRIORITY,
rhalavati04b93382017-04-07 19:00:54450 &test_delegate_, TRAFFIC_ANNOTATION_FOR_TESTS));
[email protected]f7022f32014-08-21 16:32:19451 StartRequest(request.get(), content::RESOURCE_TYPE_MAIN_FRAME);
maksim.sisov1b83bb72016-10-07 06:07:23452 EXPECT_EQ(net::OK, test_delegate_.request_status());
[email protected]b109bdd2013-11-04 18:08:43453 }
454 {
jam7e588d6b2016-10-21 16:56:06455 // With PlzNavigate, the subframe navigation requests are blocked in
456 // ExtensionNavigationThrottle which isn't added in this unit test. This is
457 // tested in an integration test in
458 // ExtensionResourceRequestPolicyTest.IframeNavigateToInaccessible.
459 if (!content::IsBrowserSideNavigationEnabled()) {
460 std::unique_ptr<net::URLRequest> request(
461 resource_context_.GetRequestContext()->CreateRequest(
462 extension->GetResourceURL("test.dat"), net::DEFAULT_PRIORITY,
rhalavati04b93382017-04-07 19:00:54463 &test_delegate_, TRAFFIC_ANNOTATION_FOR_TESTS));
jam7e588d6b2016-10-21 16:56:06464 StartRequest(request.get(), content::RESOURCE_TYPE_SUB_FRAME);
465 EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, test_delegate_.request_status());
466 }
[email protected]b109bdd2013-11-04 18:08:43467 }
468
469 // And subresource types, such as media, should fail.
470 {
dchengc963c7142016-04-08 03:55:22471 std::unique_ptr<net::URLRequest> request(
[email protected]f7022f32014-08-21 16:32:19472 resource_context_.GetRequestContext()->CreateRequest(
dchengc963c7142016-04-08 03:55:22473 extension->GetResourceURL("test.dat"), net::DEFAULT_PRIORITY,
rhalavati04b93382017-04-07 19:00:54474 &test_delegate_, TRAFFIC_ANNOTATION_FOR_TESTS));
[email protected]f7022f32014-08-21 16:32:19475 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA);
maksim.sisov1b83bb72016-10-07 06:07:23476 EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, test_delegate_.request_status());
[email protected]b109bdd2013-11-04 18:08:43477 }
478}
479
lazyboyd6dbb262017-03-30 00:57:30480TEST_F(ExtensionProtocolsTest, MetadataFolder) {
asargenta093ec32016-02-13 01:36:43481 SetProtocolHandler(false);
482
483 base::FilePath extension_dir = GetTestPath("metadata_folder");
484 std::string error;
485 scoped_refptr<Extension> extension =
486 file_util::LoadExtension(extension_dir, Manifest::INTERNAL,
487 Extension::NO_FLAGS, &error);
488 ASSERT_NE(extension.get(), nullptr) << "error: " << error;
489
490 // Loading "/test.html" should succeed.
maksim.sisov1b83bb72016-10-07 06:07:23491 EXPECT_EQ(net::OK, DoRequest(*extension, "test.html"));
asargenta093ec32016-02-13 01:36:43492
493 // Loading "/_metadata/verified_contents.json" should fail.
494 base::FilePath relative_path =
495 base::FilePath(kMetadataFolder).Append(kVerifiedContentsFilename);
496 EXPECT_TRUE(base::PathExists(extension_dir.Append(relative_path)));
maksim.sisov1b83bb72016-10-07 06:07:23497 EXPECT_EQ(net::ERR_FAILED,
asargenta093ec32016-02-13 01:36:43498 DoRequest(*extension, relative_path.AsUTF8Unsafe()));
499
500 // Loading "/_metadata/a.txt" should also fail.
501 relative_path = base::FilePath(kMetadataFolder).AppendASCII("a.txt");
502 EXPECT_TRUE(base::PathExists(extension_dir.Append(relative_path)));
maksim.sisov1b83bb72016-10-07 06:07:23503 EXPECT_EQ(net::ERR_FAILED,
asargenta093ec32016-02-13 01:36:43504 DoRequest(*extension, relative_path.AsUTF8Unsafe()));
505}
506
lazyboyd6dbb262017-03-30 00:57:30507// Tests that unreadable files and deleted files correctly go through
508// ContentVerifyJob.
509TEST_F(ExtensionProtocolsTest, VerificationSeenForFileAccessErrors) {
510 const char kFooJsContents[] = "hello world.";
511 JobDelegate test_job_delegate(kFooJsContents);
512 SetProtocolHandler(false);
513
514 const std::string kFooJs("foo.js");
515 // Create a temporary directory that a fake extension will live in and fill
516 // it with some test files.
517 base::ScopedTempDir temp_dir;
518 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
519 base::FilePath foo_js(FILE_PATH_LITERAL("foo.js"));
520 ASSERT_TRUE(AddFileToDirectory(temp_dir.GetPath(), foo_js, kFooJsContents))
521 << "Failed to write " << temp_dir.GetPath().value() << "/"
522 << foo_js.value();
523
524 ExtensionBuilder builder;
525 builder
526 .SetManifest(DictionaryBuilder()
527 .Set("name", "Foo")
528 .Set("version", "1.0")
529 .Set("manifest_version", 2)
530 .Set("update_url",
531 "https://clients2.google.com/service/update2/crx")
532 .Build())
533 .SetID(crx_file::id_util::GenerateId("whatever"))
534 .SetPath(temp_dir.GetPath())
535 .SetLocation(Manifest::INTERNAL);
536 scoped_refptr<Extension> extension(builder.Build());
537
538 ASSERT_TRUE(extension.get());
539 content_verifier_->OnExtensionLoaded(testing_profile_.get(), extension.get());
540 // Wait for PostTask to ContentVerifierIOData::AddData() to finish.
541 content::RunAllPendingInMessageLoop();
542
543 // Valid and readable foo.js.
544 EXPECT_EQ(net::OK, DoRequest(*extension, kFooJs));
545 test_job_delegate.WaitForDoneReading(extension->id());
546
547 // chmod -r foo.js.
548 base::FilePath foo_path = temp_dir.GetPath().AppendASCII(kFooJs);
549 ASSERT_TRUE(base::MakeFileUnreadable(foo_path));
550 test_job_delegate.Reset();
551 EXPECT_EQ(net::ERR_ACCESS_DENIED, DoRequest(*extension, kFooJs));
552 test_job_delegate.WaitForDoneReading(extension->id());
553
554 // Delete foo.js.
555 ASSERT_TRUE(base::DieFileDie(foo_path, false));
556 test_job_delegate.Reset();
557 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, DoRequest(*extension, kFooJs));
558 test_job_delegate.WaitForDoneReading(extension->id());
559}
560
lazyboye83ab9c62017-03-30 03:18:26561// Tests that zero byte files correctly go through ContentVerifyJob.
562TEST_F(ExtensionProtocolsTest, VerificationSeenForZeroByteFile) {
563 const char kFooJsContents[] = ""; // Empty.
564 JobDelegate test_job_delegate(kFooJsContents);
565 SetProtocolHandler(false);
566
567 const std::string kFooJs("foo.js");
568 // Create a temporary directory that a fake extension will live in and fill
569 // it with some test files.
570 base::ScopedTempDir temp_dir;
571 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
572 base::FilePath foo_js(FILE_PATH_LITERAL("foo.js"));
573 ASSERT_TRUE(AddFileToDirectory(temp_dir.GetPath(), foo_js, kFooJsContents))
574 << "Failed to write " << temp_dir.GetPath().value() << "/"
575 << foo_js.value();
576
577 // Sanity check foo.js.
578 base::FilePath foo_path = temp_dir.GetPath().AppendASCII(kFooJs);
579 int64_t foo_file_size = -1;
580 ASSERT_TRUE(base::GetFileSize(foo_path, &foo_file_size));
581 ASSERT_EQ(0, foo_file_size);
582
583 ExtensionBuilder builder;
584 builder
585 .SetManifest(DictionaryBuilder()
586 .Set("name", "Foo")
587 .Set("version", "1.0")
588 .Set("manifest_version", 2)
589 .Set("update_url",
590 "https://clients2.google.com/service/update2/crx")
591 .Build())
592 .SetID(crx_file::id_util::GenerateId("whatever"))
593 .SetPath(temp_dir.GetPath())
594 .SetLocation(Manifest::INTERNAL);
595 scoped_refptr<Extension> extension(builder.Build());
596
597 ASSERT_TRUE(extension.get());
598 content_verifier_->OnExtensionLoaded(testing_profile_.get(), extension.get());
599 // Wait for PostTask to ContentVerifierIOData::AddData() to finish.
600 content::RunAllPendingInMessageLoop();
601
602 // Request foo.js.
603 EXPECT_EQ(net::OK, DoRequest(*extension, kFooJs));
604 test_job_delegate.WaitForDoneReading(extension->id());
605}
606
[email protected]702d8b42013-02-27 20:55:50607} // namespace extensions