blob: 8d857d2c00ecd09dc8a9a48571370bf3274a68b9 [file] [log] [blame]
kalman30a92f962015-09-03 18:08:201// Copyright 2015 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Yutaka Hirano54b166eb2019-12-05 20:59:525#include "base/bind.h"
kalman30a92f962015-09-03 18:08:206#include "base/files/file_path.h"
7#include "base/strings/stringprintf.h"
8#include "chrome/browser/extensions/extension_apitest.h"
thestige80821242015-09-30 23:46:089#include "chrome/browser/ui/browser_navigator_params.h"
kalman30a92f962015-09-03 18:08:2010#include "chrome/browser/ui/tabs/tab_strip_model.h"
11#include "chrome/test/base/ui_test_utils.h"
12#include "content/public/browser/web_contents.h"
13#include "content/public/test/browser_test_utils.h"
14#include "extensions/common/extension.h"
Devlin Cronin4f455a22018-01-25 01:36:4515#include "extensions/test/test_extension_dir.h"
kalman30a92f962015-09-03 18:08:2016#include "net/dns/mock_host_resolver.h"
17#include "net/test/embedded_test_server/embedded_test_server.h"
Yutaka Hirano54b166eb2019-12-05 20:59:5218#include "net/test/embedded_test_server/http_request.h"
19#include "net/test/embedded_test_server/http_response.h"
20#include "services/network/public/cpp/features.h"
kalman30a92f962015-09-03 18:08:2021
22namespace extensions {
23
24namespace {
25
Yutaka Hirano54b166eb2019-12-05 20:59:5226// Returns a response whose body is request's origin.
27std::unique_ptr<net::test_server::HttpResponse> HandleEchoOrigin(
28 const net::test_server::HttpRequest& request) {
29 if (request.relative_url != "/echo-origin")
30 return nullptr;
31
32 auto response = std::make_unique<net::test_server::BasicHttpResponse>();
33 response->set_code(net::HTTP_OK);
34 response->set_content_type("text/plain");
35 auto it = request.headers.find("origin");
36 if (it != request.headers.end()) {
37 response->set_content(it->second);
38 } else {
39 response->set_content("<no origin attached>");
40 }
41 response->AddCustomHeader("access-control-allow-origin", "*");
42
43 return response;
44}
45
kalman30a92f962015-09-03 18:08:2046// JavaScript snippet which performs a fetch given a URL expression to be
47// substituted as %s, then sends back the fetched content using the
48// domAutomationController.
49const char* kFetchScript =
50 "fetch(%s).then(function(result) {\n"
51 " return result.text();\n"
52 "}).then(function(text) {\n"
53 " window.domAutomationController.send(text);\n"
54 "}).catch(function(err) {\n"
55 " window.domAutomationController.send(String(err));\n"
56 "});\n";
57
Yutaka Hirano54b166eb2019-12-05 20:59:5258constexpr char kFetchPostScript[] = R"(
59 fetch($1, {method: 'POST'}).then((result) => {
60 return result.text();
61 }).then((text) => {
62 window.domAutomationController.send(text);
63 }).catch((error) => {
64 window.domAutomationController.send(String(err));
65 });
66)";
67
kalman30a92f962015-09-03 18:08:2068class ExtensionFetchTest : public ExtensionApiTest {
69 protected:
70 // Writes an empty background page and a text file called "text" with content
71 // "text content", then loads and returns the extension. |dir| must already
72 // have a manifest.
73 const Extension* WriteFilesAndLoadTestExtension(TestExtensionDir* dir) {
74 dir->WriteFile(FILE_PATH_LITERAL("text"), "text content");
75 dir->WriteFile(FILE_PATH_LITERAL("bg.js"), "");
vabr9142fe22016-09-08 13:19:2276 return LoadExtension(dir->UnpackedPath());
kalman30a92f962015-09-03 18:08:2077 }
78
79 // Returns |kFetchScript| with |url_expression| substituted as its test URL.
80 std::string GetFetchScript(const std::string& url_expression) {
81 return base::StringPrintf(kFetchScript, url_expression.c_str());
82 }
83
84 // Returns |url| as a string surrounded by single quotes, for passing to
85 // JavaScript as a string literal.
86 std::string GetQuotedURL(const GURL& url) {
87 return base::StringPrintf("'%s'", url.spec().c_str());
88 }
89
90 // Like GetQuotedURL(), but fetching the URL from the test server's |host|
91 // and |path|.
92 std::string GetQuotedTestServerURL(const std::string& host,
93 const std::string& path) {
94 return GetQuotedURL(embedded_test_server()->GetURL(host, path));
95 }
96
97 // Opens a tab, puts it in the foreground, navigates it to |url| then returns
98 // its WebContents.
99 content::WebContents* CreateAndNavigateTab(const GURL& url) {
cm.sanchi2522bc92017-12-04 08:04:13100 NavigateParams params(browser(), url, ui::PAGE_TRANSITION_LINK);
nick3b04f322016-08-31 19:29:19101 params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
kalman30a92f962015-09-03 18:08:20102 ui_test_utils::NavigateToURL(&params);
103 return browser()->tab_strip_model()->GetActiveWebContents();
104 }
105
106 private:
107 void SetUpOnMainThread() override {
108 ExtensionApiTest::SetUpOnMainThread();
109 host_resolver()->AddRule("*", "127.0.0.1");
Yutaka Hirano54b166eb2019-12-05 20:59:52110
111 embedded_test_server()->RegisterRequestHandler(
112 base::BindRepeating(HandleEchoOrigin));
kalman30a92f962015-09-03 18:08:20113 ASSERT_TRUE(StartEmbeddedTestServer());
114 }
115};
116
117IN_PROC_BROWSER_TEST_F(ExtensionFetchTest, ExtensionCanFetchExtensionResource) {
118 TestExtensionDir dir;
Devlin Cronin57344652019-12-27 20:51:51119 constexpr char kManifest[] =
120 R"({
121 "background": {"scripts": ["bg.js"]},
122 "manifest_version": 2,
123 "name": "ExtensionCanFetchExtensionResource",
124 "version": "1"
125 })";
126 dir.WriteManifest(kManifest);
kalman30a92f962015-09-03 18:08:20127 const Extension* extension = WriteFilesAndLoadTestExtension(&dir);
128 ASSERT_TRUE(extension);
129
130 EXPECT_EQ(
131 "text content",
132 ExecuteScriptInBackgroundPage(
133 extension->id(), GetFetchScript("chrome.runtime.getURL('text')")));
134}
135
136IN_PROC_BROWSER_TEST_F(ExtensionFetchTest,
137 ExtensionCanFetchHostedResourceWithHostPermissions) {
138 TestExtensionDir dir;
Devlin Cronin57344652019-12-27 20:51:51139 constexpr char kManifest[] =
140 R"({
141 "background": {"scripts": ["bg.js"]},
142 "manifest_version": 2,
143 "name": "ExtensionCanFetchHostedResourceWithHostPermissions",
144 "permissions": ["http://example.com/*"],
145 "version": "1"
146 })";
147 dir.WriteManifest(kManifest);
kalman30a92f962015-09-03 18:08:20148 const Extension* extension = WriteFilesAndLoadTestExtension(&dir);
149 ASSERT_TRUE(extension);
150
151 EXPECT_EQ("Hello!", ExecuteScriptInBackgroundPage(
152 extension->id(),
153 GetFetchScript(GetQuotedTestServerURL(
154 "example.com", "/extensions/test_file.txt"))));
155}
156
Xiaocheng Huc11718bb2019-12-28 00:34:40157// TODO(crbug.com/1038156): Test is flaky.
kalman30a92f962015-09-03 18:08:20158IN_PROC_BROWSER_TEST_F(
159 ExtensionFetchTest,
Xiaocheng Huc11718bb2019-12-28 00:34:40160 DISABLED_ExtensionCannotFetchHostedResourceWithoutHostPermissions) {
kalman30a92f962015-09-03 18:08:20161 TestExtensionDir dir;
Devlin Cronin57344652019-12-27 20:51:51162 constexpr char kManifest[] =
163 R"({
164 "background": {"scripts": ["bg.js"]},
165 "manifest_version": 2,
166 "name": "ExtensionCannotFetchHostedResourceWithoutHostPermissions",
167 "version": "1"
168 })";
169 dir.WriteManifest(kManifest);
kalman30a92f962015-09-03 18:08:20170 const Extension* extension = WriteFilesAndLoadTestExtension(&dir);
171 ASSERT_TRUE(extension);
172
173 // TODO(kalman): Another test would be to configure the test server to work
174 // with CORS, and test that the fetch succeeds.
175 EXPECT_EQ(
176 "TypeError: Failed to fetch",
177 ExecuteScriptInBackgroundPage(
178 extension->id(), GetFetchScript(GetQuotedTestServerURL(
179 "example.com", "/extensions/test_file.txt"))));
180}
181
182IN_PROC_BROWSER_TEST_F(ExtensionFetchTest,
183 HostCanFetchWebAccessibleExtensionResource) {
184 TestExtensionDir dir;
Devlin Cronin57344652019-12-27 20:51:51185 constexpr char kManifest[] =
186 R"({
187 "background": {"scripts": ["bg.js"]},
188 "manifest_version": 2,
189 "name": "HostCanFetchWebAccessibleExtensionResource",
190 "version": "1",
191 "web_accessible_resources": ["text"]
192 })";
193 dir.WriteManifest(kManifest);
kalman30a92f962015-09-03 18:08:20194 const Extension* extension = WriteFilesAndLoadTestExtension(&dir);
195 ASSERT_TRUE(extension);
196
197 content::WebContents* empty_tab = CreateAndNavigateTab(
198 embedded_test_server()->GetURL("example.com", "/empty.html"));
199
200 // TODO(kalman): Test this from a content script too.
201 std::string fetch_result;
202 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
203 empty_tab,
204 GetFetchScript(GetQuotedURL(extension->GetResourceURL("text"))),
205 &fetch_result));
206 EXPECT_EQ("text content", fetch_result);
207}
208
Makoto Shimazub8a7f58f2018-11-07 19:09:31209// Calling fetch() from a http(s) service worker context to a
210// chrome-extensions:// URL since the loading path in a service worker is
211// different from pages.
212// This is a regression test for https://crbug.com/901443.
213IN_PROC_BROWSER_TEST_F(
214 ExtensionFetchTest,
215 HostCanFetchWebAccessibleExtensionResource_FetchFromServiceWorker) {
216 TestExtensionDir dir;
Devlin Cronin57344652019-12-27 20:51:51217 constexpr char kManifest[] =
218 R"({
219 "background": {"scripts": ["bg.js"]},
220 "manifest_version": 2,
221 "name": "FetchFromServiceWorker",
222 "version": "1",
223 "web_accessible_resources": ["text"]
224 })";
225 dir.WriteManifest(kManifest);
Makoto Shimazub8a7f58f2018-11-07 19:09:31226 const Extension* extension = WriteFilesAndLoadTestExtension(&dir);
227 ASSERT_TRUE(extension);
228
229 content::WebContents* tab =
230 CreateAndNavigateTab(embedded_test_server()->GetURL(
231 "/workers/fetch_from_service_worker.html"));
232 EXPECT_EQ("ready", content::EvalJs(tab, "setup();"));
233 EXPECT_EQ("text content",
234 content::EvalJs(
235 tab, base::StringPrintf(
236 "fetch_from_service_worker('%s');",
237 extension->GetResourceURL("text").spec().c_str())));
238}
239
kalman30a92f962015-09-03 18:08:20240IN_PROC_BROWSER_TEST_F(ExtensionFetchTest,
241 HostCannotFetchNonWebAccessibleExtensionResource) {
242 TestExtensionDir dir;
Devlin Cronin57344652019-12-27 20:51:51243 constexpr char kManifest[] =
244 R"({
245 "background": {"scripts": ["bg.js"]},
246 "manifest_version": 2,
247 "name": "HostCannotFetchNonWebAccessibleExtensionResource",
248 "version": "1"
249 })";
250 dir.WriteManifest(kManifest);
kalman30a92f962015-09-03 18:08:20251 const Extension* extension = WriteFilesAndLoadTestExtension(&dir);
252 ASSERT_TRUE(extension);
253
254 content::WebContents* empty_tab = CreateAndNavigateTab(
255 embedded_test_server()->GetURL("example.com", "/empty.html"));
256
257 // TODO(kalman): Test this from a content script too.
258 std::string fetch_result;
259 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
260 empty_tab,
261 GetFetchScript(GetQuotedURL(extension->GetResourceURL("text"))),
262 &fetch_result));
263 EXPECT_EQ("TypeError: Failed to fetch", fetch_result);
264}
265
Yutaka Hirano7938c712019-10-15 06:00:42266IN_PROC_BROWSER_TEST_F(ExtensionFetchTest, FetchResponseType) {
267 const std::string script = base::StringPrintf(
268 "fetch(%s).then(function(response) {\n"
269 " window.domAutomationController.send(response.type);\n"
270 "}).catch(function(err) {\n"
271 " window.domAutomationController.send(String(err));\n"
272 "});\n",
273 GetQuotedTestServerURL("example.com", "/extensions/test_file.txt")
274 .data());
275 TestExtensionDir dir;
Devlin Cronin57344652019-12-27 20:51:51276 constexpr char kManifest[] =
277 R"({
278 "background": {"scripts": ["bg.js"]},
279 "manifest_version": 2,
280 "name": "FetchResponseType",
281 "permissions": ["http://example.com/*"],
282 "version": "1"
283 })";
284 dir.WriteManifest(kManifest);
Yutaka Hirano7938c712019-10-15 06:00:42285 const Extension* extension = WriteFilesAndLoadTestExtension(&dir);
286 ASSERT_TRUE(extension);
287
288 EXPECT_EQ("basic", ExecuteScriptInBackgroundPage(extension->id(), script));
289}
290
Yutaka Hirano54b166eb2019-12-05 20:59:52291IN_PROC_BROWSER_TEST_F(ExtensionFetchTest, OriginOnPostWithPermissions) {
292 TestExtensionDir dir;
293 dir.WriteManifest(R"JSON(
294 {
295 "background": {"scripts": ["bg.js"]},
296 "manifest_version": 2,
297 "name": "FetchResponseType",
298 "permissions": ["http://example.com/*"],
299 "version": "1"
300 })JSON");
301 const Extension* extension = WriteFilesAndLoadTestExtension(&dir);
302 ASSERT_TRUE(extension);
303
304 // Extension scripts will have the origin of the destination URL only when
305 // OOR-CORS is enabled.
306 GURL destination_url =
307 embedded_test_server()->GetURL("example.com", "/echo-origin");
308 std::string script = content::JsReplace(kFetchPostScript, destination_url);
309 std::string origin_string =
310 network::features::ShouldEnableOutOfBlinkCorsForTesting()
311 ? url::Origin::Create(destination_url).Serialize()
312 : url::Origin::Create(extension->url()).Serialize();
313 EXPECT_EQ(origin_string,
314 ExecuteScriptInBackgroundPage(extension->id(), script));
315}
316
317IN_PROC_BROWSER_TEST_F(ExtensionFetchTest, OriginOnPostWithoutPermissions) {
318 TestExtensionDir dir;
319 dir.WriteManifest(R"JSON(
320 {
321 "background": {"scripts": ["bg.js"]},
322 "manifest_version": 2,
323 "name": "FetchResponseType",
324 "permissions": [],
325 "version": "1"
326 })JSON");
327 const Extension* extension = WriteFilesAndLoadTestExtension(&dir);
328 ASSERT_TRUE(extension);
329
330 const std::string script = content::JsReplace(
331 kFetchPostScript,
332 embedded_test_server()->GetURL("example.com", "/echo-origin"));
333 EXPECT_EQ(url::Origin::Create(extension->url()).Serialize(),
334 ExecuteScriptInBackgroundPage(extension->id(), script));
335}
336
Yutaka Hirano5a605872020-01-08 05:09:07337// An extension background script should be able to fetch resources contained in
338// the extension, and those resources should not be opaque.
339IN_PROC_BROWSER_TEST_F(ExtensionFetchTest, ExtensionResourceShouldNotBeOpaque) {
340 // We use a script to test this feature. Ideally testing with fetch() and
341 // response type is better, but some logic in blink (see the manual
342 // response type handling in blink::FetchManager) would hide potential
343 // breakages, which is why we are using a script.
344 const std::string script = base::StringPrintf(R"(
345 const script = document.createElement('script');
346 window.onerror = (message) => {
347 window.domAutomationController.send('onerror: ' + message);
348 }
349 script.src = 'error.js'
350 document.body.appendChild(script);)");
351 TestExtensionDir dir;
352 dir.WriteManifest(R"JSON(
353 {
354 "background": {"scripts": ["bg.js"]},
355 "manifest_version": 2,
356 "name": "FetchResponseType",
357 "permissions": [],
358 "version": "1"
359 })JSON");
360 dir.WriteFile(FILE_PATH_LITERAL("error.js"), "throw TypeError('hi!')");
361 const Extension* extension = WriteFilesAndLoadTestExtension(&dir);
362 ASSERT_TRUE(extension);
363
364 // We expect that we can read the content of the error here. Otherwise
365 // "onerror: Script error." will be seen.
366 EXPECT_EQ("onerror: Uncaught TypeError: hi!",
367 ExecuteScriptInBackgroundPage(extension->id(), script));
368}
369
kalman30a92f962015-09-03 18:08:20370} // namespace
371
372} // namespace extensions