[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> |
| 9 | |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame^] | 10 | #include "base/command_line.h" |
thestig | c9e38a2 | 2014-09-13 01:02:11 | [diff] [blame] | 11 | #include "base/files/file_util.h" |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 12 | #include "base/macros.h" |
fdoray | cb32419d | 2016-06-23 15:52:55 | [diff] [blame] | 13 | #include "base/run_loop.h" |
[email protected] | 774cebd | 2013-09-26 04:55:01 | [diff] [blame] | 14 | #include "base/strings/string_number_conversions.h" |
[email protected] | 00e7bef | 2013-06-10 20:35:17 | [diff] [blame] | 15 | #include "base/strings/string_util.h" |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame^] | 16 | #include "base/test/test_file_util.h" |
[email protected] | 06492ed | 2013-03-24 22:13:14 | [diff] [blame] | 17 | #include "base/values.h" |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame^] | 18 | #include "chrome/browser/extensions/chrome_content_verifier_delegate.h" |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 19 | #include "chrome/common/chrome_paths.h" |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame^] | 20 | #include "chrome/common/chrome_switches.h" |
| 21 | #include "chrome/test/base/testing_profile.h" |
| 22 | #include "components/crx_file/id_util.h" |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 23 | #include "content/public/browser/resource_request_info.h" |
jam | 7e588d6b | 2016-10-21 16:56:06 | [diff] [blame] | 24 | #include "content/public/common/browser_side_navigation_policy.h" |
megjablon | caf312f | 2017-01-12 18:47:49 | [diff] [blame] | 25 | #include "content/public/common/previews_state.h" |
[email protected] | 08a932d5 | 2012-06-03 21:42:12 | [diff] [blame] | 26 | #include "content/public/test/mock_resource_context.h" |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 27 | #include "content/public/test/test_browser_thread_bundle.h" |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame^] | 28 | #include "content/public/test/test_utils.h" |
| 29 | #include "extensions/browser/content_verifier.h" |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 30 | #include "extensions/browser/extension_protocols.h" |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 31 | #include "extensions/browser/info_map.h" |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame] | 32 | #include "extensions/common/constants.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 33 | #include "extensions/common/extension.h" |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame^] | 34 | #include "extensions/common/extension_builder.h" |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 35 | #include "extensions/common/file_util.h" |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 36 | #include "net/base/request_priority.h" |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 37 | #include "net/url_request/url_request.h" |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 38 | #include "net/url_request/url_request_job_factory_impl.h" |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 39 | #include "net/url_request/url_request_status.h" |
| 40 | #include "net/url_request/url_request_test_util.h" |
| 41 | #include "testing/gtest/include/gtest/gtest.h" |
| 42 | |
[email protected] | 7491ad0 | 2014-07-05 19:10:07 | [diff] [blame] | 43 | using content::ResourceType; |
| 44 | |
[email protected] | 702d8b4 | 2013-02-27 20:55:50 | [diff] [blame] | 45 | namespace extensions { |
jamescook | 8816ae5 | 2014-09-05 17:02:37 | [diff] [blame] | 46 | namespace { |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 47 | |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 48 | base::FilePath GetTestPath(const std::string& name) { |
| 49 | base::FilePath path; |
| 50 | EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path)); |
| 51 | return path.AppendASCII("extensions").AppendASCII(name); |
| 52 | } |
| 53 | |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame^] | 54 | // Helper function that creates a file at |relative_path| within |directory| |
| 55 | // and fills it with |content|. |
| 56 | bool AddFileToDirectory(const base::FilePath& directory, |
| 57 | const base::FilePath& relative_path, |
| 58 | const std::string& content) { |
| 59 | base::FilePath full_path = directory.Append(relative_path); |
| 60 | int result = base::WriteFile(full_path, content.data(), content.size()); |
| 61 | return static_cast<size_t>(result) == content.size(); |
| 62 | } |
| 63 | |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 64 | scoped_refptr<Extension> CreateTestExtension(const std::string& name, |
| 65 | bool incognito_split_mode) { |
[email protected] | 023b3d1 | 2013-12-23 18:46:49 | [diff] [blame] | 66 | base::DictionaryValue manifest; |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 67 | manifest.SetString("name", name); |
| 68 | manifest.SetString("version", "1"); |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 69 | manifest.SetInteger("manifest_version", 2); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 70 | manifest.SetString("incognito", incognito_split_mode ? "split" : "spanning"); |
| 71 | |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 72 | base::FilePath path = GetTestPath("response_headers"); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 73 | |
| 74 | std::string error; |
| 75 | scoped_refptr<Extension> extension( |
[email protected] | 1d5e58b | 2013-01-31 08:41:40 | [diff] [blame] | 76 | Extension::Create(path, Manifest::INTERNAL, manifest, |
[email protected] | ed3b9b1 | 2012-05-31 18:37:51 | [diff] [blame] | 77 | Extension::NO_FLAGS, &error)); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 78 | EXPECT_TRUE(extension.get()) << error; |
| 79 | return extension; |
| 80 | } |
| 81 | |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 82 | scoped_refptr<Extension> CreateWebStoreExtension() { |
[email protected] | 023b3d1 | 2013-12-23 18:46:49 | [diff] [blame] | 83 | base::DictionaryValue manifest; |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 84 | manifest.SetString("name", "WebStore"); |
| 85 | manifest.SetString("version", "1"); |
| 86 | manifest.SetString("icons.16", "webstore_icon_16.png"); |
| 87 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 88 | base::FilePath path; |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 89 | EXPECT_TRUE(PathService::Get(chrome::DIR_RESOURCES, &path)); |
| 90 | path = path.AppendASCII("web_store"); |
| 91 | |
| 92 | std::string error; |
| 93 | scoped_refptr<Extension> extension( |
[email protected] | 1d5e58b | 2013-01-31 08:41:40 | [diff] [blame] | 94 | Extension::Create(path, Manifest::COMPONENT, manifest, |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 95 | Extension::NO_FLAGS, &error)); |
| 96 | EXPECT_TRUE(extension.get()) << error; |
| 97 | return extension; |
| 98 | } |
| 99 | |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 100 | scoped_refptr<Extension> CreateTestResponseHeaderExtension() { |
[email protected] | 023b3d1 | 2013-12-23 18:46:49 | [diff] [blame] | 101 | base::DictionaryValue manifest; |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 102 | manifest.SetString("name", "An extension with web-accessible resources"); |
| 103 | manifest.SetString("version", "2"); |
| 104 | |
[email protected] | aeca23f | 2013-06-21 22:34:41 | [diff] [blame] | 105 | base::ListValue* web_accessible_list = new base::ListValue(); |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 106 | web_accessible_list->AppendString("test.dat"); |
| 107 | manifest.Set("web_accessible_resources", web_accessible_list); |
| 108 | |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 109 | base::FilePath path = GetTestPath("response_headers"); |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 110 | |
| 111 | std::string error; |
| 112 | scoped_refptr<Extension> extension( |
| 113 | Extension::Create(path, Manifest::UNPACKED, manifest, |
| 114 | Extension::NO_FLAGS, &error)); |
| 115 | EXPECT_TRUE(extension.get()) << error; |
| 116 | return extension; |
| 117 | } |
| 118 | |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame^] | 119 | // A ContentVerifyJob::TestDelegate that observes DoneReading(). |
| 120 | class JobDelegate : public ContentVerifyJob::TestDelegate { |
| 121 | public: |
| 122 | explicit JobDelegate(const std::string& expected_contents) |
| 123 | : expected_contents_(expected_contents), run_loop_(new base::RunLoop()) { |
| 124 | ContentVerifyJob::SetDelegateForTests(this); |
| 125 | } |
| 126 | ~JobDelegate() override { ContentVerifyJob::SetDelegateForTests(nullptr); } |
| 127 | |
| 128 | ContentVerifyJob::FailureReason BytesRead(const ExtensionId& id, |
| 129 | int count, |
| 130 | const char* data) override { |
| 131 | read_contents_.append(data, count); |
| 132 | return ContentVerifyJob::NONE; |
| 133 | } |
| 134 | |
| 135 | ContentVerifyJob::FailureReason DoneReading(const ExtensionId& id) override { |
| 136 | seen_done_reading_extension_ids_.insert(id); |
| 137 | if (waiting_for_extension_id_ == id) |
| 138 | run_loop_->Quit(); |
| 139 | |
| 140 | if (!base::StartsWith(expected_contents_, read_contents_, |
| 141 | base::CompareCase::SENSITIVE)) { |
| 142 | ADD_FAILURE() << "Unexpected read, expected: " << expected_contents_ |
| 143 | << ", but found: " << read_contents_; |
| 144 | } |
| 145 | return ContentVerifyJob::NONE; |
| 146 | } |
| 147 | |
| 148 | void WaitForDoneReading(const ExtensionId& id) { |
| 149 | ASSERT_FALSE(waiting_for_extension_id_); |
| 150 | if (seen_done_reading_extension_ids_.count(id)) |
| 151 | return; |
| 152 | waiting_for_extension_id_ = id; |
| 153 | run_loop_->Run(); |
| 154 | } |
| 155 | |
| 156 | void Reset() { |
| 157 | read_contents_.clear(); |
| 158 | waiting_for_extension_id_.reset(); |
| 159 | seen_done_reading_extension_ids_.clear(); |
| 160 | run_loop_ = base::MakeUnique<base::RunLoop>(); |
| 161 | } |
| 162 | |
| 163 | private: |
| 164 | std::string expected_contents_; |
| 165 | std::string read_contents_; |
| 166 | std::set<ExtensionId> seen_done_reading_extension_ids_; |
| 167 | base::Optional<ExtensionId> waiting_for_extension_id_; |
| 168 | std::unique_ptr<base::RunLoop> run_loop_; |
| 169 | |
| 170 | DISALLOW_COPY_AND_ASSIGN(JobDelegate); |
| 171 | }; |
| 172 | |
jamescook | 8816ae5 | 2014-09-05 17:02:37 | [diff] [blame] | 173 | } // namespace |
| 174 | |
| 175 | // This test lives in src/chrome instead of src/extensions because it tests |
| 176 | // functionality delegated back to Chrome via ChromeExtensionsBrowserClient. |
lfg | 048201a | 2014-09-16 19:09:36 | [diff] [blame] | 177 | // See chrome/browser/extensions/chrome_url_request_util.cc. |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame^] | 178 | class ExtensionProtocolsTest : public testing::Test { |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 179 | public: |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame^] | 180 | ExtensionProtocolsTest() |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 181 | : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
| 182 | old_factory_(NULL), |
| 183 | resource_context_(&test_url_request_context_) {} |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 184 | |
dcheng | 7219181 | 2014-10-28 20:49:56 | [diff] [blame] | 185 | void SetUp() override { |
[email protected] | 06492ed | 2013-03-24 22:13:14 | [diff] [blame] | 186 | testing::Test::SetUp(); |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame^] | 187 | testing_profile_ = TestingProfile::Builder().Build(); |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 188 | extension_info_map_ = new InfoMap(); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 189 | net::URLRequestContext* request_context = |
| 190 | resource_context_.GetRequestContext(); |
| 191 | old_factory_ = request_context->job_factory(); |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame^] | 192 | |
| 193 | // Set up content verification. |
| 194 | base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 195 | command_line->AppendSwitchASCII( |
| 196 | switches::kExtensionContentVerification, |
| 197 | switches::kExtensionContentVerificationEnforce); |
| 198 | content_verifier_ = new ContentVerifier( |
| 199 | testing_profile_.get(), |
| 200 | new ChromeContentVerifierDelegate(testing_profile_.get())); |
| 201 | extension_info_map_->SetContentVerifier(content_verifier_.get()); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 202 | } |
| 203 | |
dcheng | 7219181 | 2014-10-28 20:49:56 | [diff] [blame] | 204 | void TearDown() override { |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 205 | net::URLRequestContext* request_context = |
| 206 | resource_context_.GetRequestContext(); |
| 207 | request_context->set_job_factory(old_factory_); |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame^] | 208 | content_verifier_->Shutdown(); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 209 | } |
| 210 | |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 211 | void SetProtocolHandler(bool is_incognito) { |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 212 | net::URLRequestContext* request_context = |
| 213 | resource_context_.GetRequestContext(); |
| 214 | job_factory_.SetProtocolHandler( |
[email protected] | 702d8b4 | 2013-02-27 20:55:50 | [diff] [blame] | 215 | kExtensionScheme, |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 216 | CreateExtensionProtocolHandler(is_incognito, |
[email protected] | 7b7e0b3 | 2014-03-17 16:09:23 | [diff] [blame] | 217 | extension_info_map_.get())); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 218 | request_context->set_job_factory(&job_factory_); |
| 219 | } |
| 220 | |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 221 | void StartRequest(net::URLRequest* request, |
[email protected] | 6c1e0521 | 2014-07-31 00:59:40 | [diff] [blame] | 222 | ResourceType resource_type) { |
gab | f9d1558 | 2014-11-13 16:40:15 | [diff] [blame] | 223 | content::ResourceRequestInfo::AllocateForTesting( |
megjablon | caf312f | 2017-01-12 18:47:49 | [diff] [blame] | 224 | request, resource_type, &resource_context_, |
| 225 | /*render_process_id=*/-1, |
| 226 | /*render_view_id=*/-1, |
| 227 | /*render_frame_id=*/-1, |
| 228 | /*is_main_frame=*/resource_type == content::RESOURCE_TYPE_MAIN_FRAME, |
| 229 | /*parent_is_main_frame=*/false, |
| 230 | /*allow_download=*/true, |
| 231 | /*is_async=*/false, content::PREVIEWS_OFF); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 232 | request->Start(); |
fdoray | cb32419d | 2016-06-23 15:52:55 | [diff] [blame] | 233 | base::RunLoop().Run(); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 234 | } |
| 235 | |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 236 | // Helper method to create a URLRequest, call StartRequest on it, and return |
| 237 | // the result. If |extension| hasn't already been added to |
| 238 | // |extension_info_map_|, this will add it. |
maksim.sisov | 1b83bb7 | 2016-10-07 06:07:23 | [diff] [blame] | 239 | int DoRequest(const Extension& extension, const std::string& relative_path) { |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 240 | if (!extension_info_map_->extensions().Contains(extension.id())) { |
| 241 | extension_info_map_->AddExtension(&extension, |
| 242 | base::Time::Now(), |
| 243 | false, // incognito_enabled |
| 244 | false); // notifications_disabled |
| 245 | } |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 246 | std::unique_ptr<net::URLRequest> request( |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 247 | resource_context_.GetRequestContext()->CreateRequest( |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 248 | extension.GetResourceURL(relative_path), net::DEFAULT_PRIORITY, |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 249 | &test_delegate_)); |
| 250 | StartRequest(request.get(), content::RESOURCE_TYPE_MAIN_FRAME); |
maksim.sisov | 1b83bb7 | 2016-10-07 06:07:23 | [diff] [blame] | 251 | return test_delegate_.request_status(); |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 252 | } |
| 253 | |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 254 | protected: |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 255 | content::TestBrowserThreadBundle thread_bundle_; |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 256 | scoped_refptr<InfoMap> extension_info_map_; |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 257 | net::URLRequestJobFactoryImpl job_factory_; |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 258 | const net::URLRequestJobFactory* old_factory_; |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 259 | net::TestDelegate test_delegate_; |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 260 | net::TestURLRequestContext test_url_request_context_; |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 261 | content::MockResourceContext resource_context_; |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame^] | 262 | scoped_refptr<ContentVerifier> content_verifier_; |
| 263 | std::unique_ptr<TestingProfile> testing_profile_; |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 264 | }; |
| 265 | |
| 266 | // Tests that making a chrome-extension request in an incognito context is |
| 267 | // only allowed under the right circumstances (if the extension is allowed |
| 268 | // in incognito, and it's either a non-main-frame request or a split-mode |
| 269 | // extension). |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame^] | 270 | TEST_F(ExtensionProtocolsTest, IncognitoRequest) { |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 271 | // Register an incognito extension protocol handler. |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 272 | SetProtocolHandler(true); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 273 | |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 274 | struct TestCase { |
| 275 | // Inputs. |
| 276 | std::string name; |
| 277 | bool incognito_split_mode; |
| 278 | bool incognito_enabled; |
| 279 | |
| 280 | // Expected results. |
| 281 | bool should_allow_main_frame_load; |
| 282 | bool should_allow_sub_frame_load; |
| 283 | } cases[] = { |
| 284 | {"spanning disabled", false, false, false, false}, |
| 285 | {"split disabled", true, false, false, false}, |
nasko | 5cf9d45 | 2016-06-01 05:34:56 | [diff] [blame] | 286 | {"spanning enabled", false, true, false, false}, |
| 287 | {"split enabled", true, true, true, false}, |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 288 | }; |
| 289 | |
viettrungluu | 9e65ad1 | 2014-10-16 04:22:26 | [diff] [blame] | 290 | for (size_t i = 0; i < arraysize(cases); ++i) { |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 291 | scoped_refptr<Extension> extension = |
| 292 | CreateTestExtension(cases[i].name, cases[i].incognito_split_mode); |
| 293 | extension_info_map_->AddExtension( |
[email protected] | 9afacd2 | 2013-11-13 20:23:31 | [diff] [blame] | 294 | extension.get(), base::Time::Now(), cases[i].incognito_enabled, false); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 295 | |
| 296 | // First test a main frame request. |
| 297 | { |
| 298 | // It doesn't matter that the resource doesn't exist. If the resource |
nasko | b9164c4 | 2016-06-07 01:21:35 | [diff] [blame] | 299 | // is blocked, we should see BLOCKED_BY_CLIENT. Otherwise, the request |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 300 | // should just fail because the file doesn't exist. |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 301 | std::unique_ptr<net::URLRequest> request( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 302 | resource_context_.GetRequestContext()->CreateRequest( |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 303 | extension->GetResourceURL("404.html"), net::DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 304 | &test_delegate_)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 305 | StartRequest(request.get(), content::RESOURCE_TYPE_MAIN_FRAME); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 306 | |
| 307 | if (cases[i].should_allow_main_frame_load) { |
maksim.sisov | 1b83bb7 | 2016-10-07 06:07:23 | [diff] [blame] | 308 | EXPECT_EQ(net::ERR_FILE_NOT_FOUND, test_delegate_.request_status()) |
| 309 | << cases[i].name; |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 310 | } else { |
maksim.sisov | 1b83bb7 | 2016-10-07 06:07:23 | [diff] [blame] | 311 | EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, test_delegate_.request_status()) |
nasko | b9164c4 | 2016-06-07 01:21:35 | [diff] [blame] | 312 | << cases[i].name; |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
| 316 | // Now do a subframe request. |
| 317 | { |
jam | 7e588d6b | 2016-10-21 16:56:06 | [diff] [blame] | 318 | // With PlzNavigate, the subframe navigation requests are blocked in |
| 319 | // ExtensionNavigationThrottle which isn't added in this unit test. This |
| 320 | // is tested in an integration test in |
| 321 | // ExtensionResourceRequestPolicyTest.IframeNavigateToInaccessible. |
| 322 | if (!content::IsBrowserSideNavigationEnabled()) { |
| 323 | std::unique_ptr<net::URLRequest> request( |
| 324 | resource_context_.GetRequestContext()->CreateRequest( |
| 325 | extension->GetResourceURL("404.html"), net::DEFAULT_PRIORITY, |
| 326 | &test_delegate_)); |
| 327 | StartRequest(request.get(), content::RESOURCE_TYPE_SUB_FRAME); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 328 | |
jam | 7e588d6b | 2016-10-21 16:56:06 | [diff] [blame] | 329 | if (cases[i].should_allow_sub_frame_load) { |
| 330 | EXPECT_EQ(net::ERR_FILE_NOT_FOUND, test_delegate_.request_status()) |
| 331 | << cases[i].name; |
| 332 | } else { |
| 333 | EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, test_delegate_.request_status()) |
| 334 | << cases[i].name; |
| 335 | } |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | |
[email protected] | 774cebd | 2013-09-26 04:55:01 | [diff] [blame] | 341 | void CheckForContentLengthHeader(net::URLRequest* request) { |
| 342 | std::string content_length; |
| 343 | request->GetResponseHeaderByName(net::HttpRequestHeaders::kContentLength, |
| 344 | &content_length); |
| 345 | EXPECT_FALSE(content_length.empty()); |
| 346 | int length_value = 0; |
| 347 | EXPECT_TRUE(base::StringToInt(content_length, &length_value)); |
| 348 | EXPECT_GT(length_value, 0); |
| 349 | } |
| 350 | |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 351 | // Tests getting a resource for a component extension works correctly, both when |
| 352 | // the extension is enabled and when it is disabled. |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame^] | 353 | TEST_F(ExtensionProtocolsTest, ComponentResourceRequest) { |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 354 | // Register a non-incognito extension protocol handler. |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 355 | SetProtocolHandler(false); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 356 | |
| 357 | scoped_refptr<Extension> extension = CreateWebStoreExtension(); |
[email protected] | 9afacd2 | 2013-11-13 20:23:31 | [diff] [blame] | 358 | extension_info_map_->AddExtension(extension.get(), |
| 359 | base::Time::Now(), |
| 360 | false, |
| 361 | false); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 362 | |
| 363 | // First test it with the extension enabled. |
| 364 | { |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 365 | std::unique_ptr<net::URLRequest> request( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 366 | resource_context_.GetRequestContext()->CreateRequest( |
| 367 | extension->GetResourceURL("webstore_icon_16.png"), |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 368 | net::DEFAULT_PRIORITY, &test_delegate_)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 369 | StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA); |
maksim.sisov | 1b83bb7 | 2016-10-07 06:07:23 | [diff] [blame] | 370 | EXPECT_EQ(net::OK, test_delegate_.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 371 | CheckForContentLengthHeader(request.get()); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | // And then test it with the extension disabled. |
| 375 | extension_info_map_->RemoveExtension(extension->id(), |
[email protected] | b0af479 | 2013-10-23 09:12:13 | [diff] [blame] | 376 | UnloadedExtensionInfo::REASON_DISABLE); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 377 | { |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 378 | std::unique_ptr<net::URLRequest> request( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 379 | resource_context_.GetRequestContext()->CreateRequest( |
| 380 | extension->GetResourceURL("webstore_icon_16.png"), |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 381 | net::DEFAULT_PRIORITY, &test_delegate_)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 382 | StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA); |
maksim.sisov | 1b83bb7 | 2016-10-07 06:07:23 | [diff] [blame] | 383 | EXPECT_EQ(net::OK, test_delegate_.request_status()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 384 | CheckForContentLengthHeader(request.get()); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 388 | // Tests that a URL request for resource from an extension returns a few |
| 389 | // expected response headers. |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame^] | 390 | TEST_F(ExtensionProtocolsTest, ResourceRequestResponseHeaders) { |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 391 | // Register a non-incognito extension protocol handler. |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 392 | SetProtocolHandler(false); |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 393 | |
| 394 | scoped_refptr<Extension> extension = CreateTestResponseHeaderExtension(); |
[email protected] | 9afacd2 | 2013-11-13 20:23:31 | [diff] [blame] | 395 | extension_info_map_->AddExtension(extension.get(), |
| 396 | base::Time::Now(), |
| 397 | false, |
| 398 | false); |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 399 | |
| 400 | { |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 401 | std::unique_ptr<net::URLRequest> request( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 402 | resource_context_.GetRequestContext()->CreateRequest( |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 403 | extension->GetResourceURL("test.dat"), net::DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 404 | &test_delegate_)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 405 | StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA); |
maksim.sisov | 1b83bb7 | 2016-10-07 06:07:23 | [diff] [blame] | 406 | EXPECT_EQ(net::OK, test_delegate_.request_status()); |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 407 | |
| 408 | // Check that cache-related headers are set. |
| 409 | std::string etag; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 410 | request->GetResponseHeaderByName("ETag", &etag); |
brettw | 66d1b81b | 2015-07-06 19:29:40 | [diff] [blame] | 411 | EXPECT_TRUE(base::StartsWith(etag, "\"", base::CompareCase::SENSITIVE)); |
| 412 | EXPECT_TRUE(base::EndsWith(etag, "\"", base::CompareCase::SENSITIVE)); |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 413 | |
| 414 | std::string revalidation_header; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 415 | request->GetResponseHeaderByName("cache-control", &revalidation_header); |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 416 | EXPECT_EQ("no-cache", revalidation_header); |
| 417 | |
| 418 | // We set test.dat as web-accessible, so it should have a CORS header. |
| 419 | std::string access_control; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 420 | request->GetResponseHeaderByName("Access-Control-Allow-Origin", |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 421 | &access_control); |
| 422 | EXPECT_EQ("*", access_control); |
| 423 | } |
| 424 | } |
| 425 | |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 426 | // Tests that a URL request for main frame or subframe from an extension |
| 427 | // succeeds, but subresources fail. See http://crbug.com/312269. |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame^] | 428 | TEST_F(ExtensionProtocolsTest, AllowFrameRequests) { |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 429 | // Register a non-incognito extension protocol handler. |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 430 | SetProtocolHandler(false); |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 431 | |
| 432 | scoped_refptr<Extension> extension = CreateTestExtension("foo", false); |
[email protected] | 9afacd2 | 2013-11-13 20:23:31 | [diff] [blame] | 433 | extension_info_map_->AddExtension(extension.get(), |
| 434 | base::Time::Now(), |
| 435 | false, |
| 436 | false); |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 437 | |
nasko | 5cf9d45 | 2016-06-01 05:34:56 | [diff] [blame] | 438 | // All MAIN_FRAME requests should succeed. SUB_FRAME requests that are not |
| 439 | // explicitly listed in web_accesible_resources or same-origin to the parent |
| 440 | // should not succeed. |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 441 | { |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 442 | std::unique_ptr<net::URLRequest> request( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 443 | resource_context_.GetRequestContext()->CreateRequest( |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 444 | extension->GetResourceURL("test.dat"), net::DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 445 | &test_delegate_)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 446 | StartRequest(request.get(), content::RESOURCE_TYPE_MAIN_FRAME); |
maksim.sisov | 1b83bb7 | 2016-10-07 06:07:23 | [diff] [blame] | 447 | EXPECT_EQ(net::OK, test_delegate_.request_status()); |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 448 | } |
| 449 | { |
jam | 7e588d6b | 2016-10-21 16:56:06 | [diff] [blame] | 450 | // With PlzNavigate, the subframe navigation requests are blocked in |
| 451 | // ExtensionNavigationThrottle which isn't added in this unit test. This is |
| 452 | // tested in an integration test in |
| 453 | // ExtensionResourceRequestPolicyTest.IframeNavigateToInaccessible. |
| 454 | if (!content::IsBrowserSideNavigationEnabled()) { |
| 455 | std::unique_ptr<net::URLRequest> request( |
| 456 | resource_context_.GetRequestContext()->CreateRequest( |
| 457 | extension->GetResourceURL("test.dat"), net::DEFAULT_PRIORITY, |
| 458 | &test_delegate_)); |
| 459 | StartRequest(request.get(), content::RESOURCE_TYPE_SUB_FRAME); |
| 460 | EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, test_delegate_.request_status()); |
| 461 | } |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | // And subresource types, such as media, should fail. |
| 465 | { |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 466 | std::unique_ptr<net::URLRequest> request( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 467 | resource_context_.GetRequestContext()->CreateRequest( |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 468 | extension->GetResourceURL("test.dat"), net::DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 469 | &test_delegate_)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 470 | StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA); |
maksim.sisov | 1b83bb7 | 2016-10-07 06:07:23 | [diff] [blame] | 471 | EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, test_delegate_.request_status()); |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame^] | 475 | TEST_F(ExtensionProtocolsTest, MetadataFolder) { |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 476 | SetProtocolHandler(false); |
| 477 | |
| 478 | base::FilePath extension_dir = GetTestPath("metadata_folder"); |
| 479 | std::string error; |
| 480 | scoped_refptr<Extension> extension = |
| 481 | file_util::LoadExtension(extension_dir, Manifest::INTERNAL, |
| 482 | Extension::NO_FLAGS, &error); |
| 483 | ASSERT_NE(extension.get(), nullptr) << "error: " << error; |
| 484 | |
| 485 | // Loading "/test.html" should succeed. |
maksim.sisov | 1b83bb7 | 2016-10-07 06:07:23 | [diff] [blame] | 486 | EXPECT_EQ(net::OK, DoRequest(*extension, "test.html")); |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 487 | |
| 488 | // Loading "/_metadata/verified_contents.json" should fail. |
| 489 | base::FilePath relative_path = |
| 490 | base::FilePath(kMetadataFolder).Append(kVerifiedContentsFilename); |
| 491 | EXPECT_TRUE(base::PathExists(extension_dir.Append(relative_path))); |
maksim.sisov | 1b83bb7 | 2016-10-07 06:07:23 | [diff] [blame] | 492 | EXPECT_EQ(net::ERR_FAILED, |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 493 | DoRequest(*extension, relative_path.AsUTF8Unsafe())); |
| 494 | |
| 495 | // Loading "/_metadata/a.txt" should also fail. |
| 496 | relative_path = base::FilePath(kMetadataFolder).AppendASCII("a.txt"); |
| 497 | EXPECT_TRUE(base::PathExists(extension_dir.Append(relative_path))); |
maksim.sisov | 1b83bb7 | 2016-10-07 06:07:23 | [diff] [blame] | 498 | EXPECT_EQ(net::ERR_FAILED, |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 499 | DoRequest(*extension, relative_path.AsUTF8Unsafe())); |
| 500 | } |
| 501 | |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame^] | 502 | // Tests that unreadable files and deleted files correctly go through |
| 503 | // ContentVerifyJob. |
| 504 | TEST_F(ExtensionProtocolsTest, VerificationSeenForFileAccessErrors) { |
| 505 | const char kFooJsContents[] = "hello world."; |
| 506 | JobDelegate test_job_delegate(kFooJsContents); |
| 507 | SetProtocolHandler(false); |
| 508 | |
| 509 | const std::string kFooJs("foo.js"); |
| 510 | // Create a temporary directory that a fake extension will live in and fill |
| 511 | // it with some test files. |
| 512 | base::ScopedTempDir temp_dir; |
| 513 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 514 | base::FilePath foo_js(FILE_PATH_LITERAL("foo.js")); |
| 515 | ASSERT_TRUE(AddFileToDirectory(temp_dir.GetPath(), foo_js, kFooJsContents)) |
| 516 | << "Failed to write " << temp_dir.GetPath().value() << "/" |
| 517 | << foo_js.value(); |
| 518 | |
| 519 | ExtensionBuilder builder; |
| 520 | builder |
| 521 | .SetManifest(DictionaryBuilder() |
| 522 | .Set("name", "Foo") |
| 523 | .Set("version", "1.0") |
| 524 | .Set("manifest_version", 2) |
| 525 | .Set("update_url", |
| 526 | "https://clients2.google.com/service/update2/crx") |
| 527 | .Build()) |
| 528 | .SetID(crx_file::id_util::GenerateId("whatever")) |
| 529 | .SetPath(temp_dir.GetPath()) |
| 530 | .SetLocation(Manifest::INTERNAL); |
| 531 | scoped_refptr<Extension> extension(builder.Build()); |
| 532 | |
| 533 | ASSERT_TRUE(extension.get()); |
| 534 | content_verifier_->OnExtensionLoaded(testing_profile_.get(), extension.get()); |
| 535 | // Wait for PostTask to ContentVerifierIOData::AddData() to finish. |
| 536 | content::RunAllPendingInMessageLoop(); |
| 537 | |
| 538 | // Valid and readable foo.js. |
| 539 | EXPECT_EQ(net::OK, DoRequest(*extension, kFooJs)); |
| 540 | test_job_delegate.WaitForDoneReading(extension->id()); |
| 541 | |
| 542 | // chmod -r foo.js. |
| 543 | base::FilePath foo_path = temp_dir.GetPath().AppendASCII(kFooJs); |
| 544 | ASSERT_TRUE(base::MakeFileUnreadable(foo_path)); |
| 545 | test_job_delegate.Reset(); |
| 546 | EXPECT_EQ(net::ERR_ACCESS_DENIED, DoRequest(*extension, kFooJs)); |
| 547 | test_job_delegate.WaitForDoneReading(extension->id()); |
| 548 | |
| 549 | // Delete foo.js. |
| 550 | ASSERT_TRUE(base::DieFileDie(foo_path, false)); |
| 551 | test_job_delegate.Reset(); |
| 552 | EXPECT_EQ(net::ERR_FILE_NOT_FOUND, DoRequest(*extension, kFooJs)); |
| 553 | test_job_delegate.WaitForDoneReading(extension->id()); |
| 554 | } |
| 555 | |
[email protected] | 702d8b4 | 2013-02-27 20:55:50 | [diff] [blame] | 556 | } // namespace extensions |