blob: f76f99bdbbc3d65b0d49e62c3c6bb44e97b626fd [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
157IN_PROC_BROWSER_TEST_F(
158 ExtensionFetchTest,
159 ExtensionCannotFetchHostedResourceWithoutHostPermissions) {
160 TestExtensionDir dir;
Devlin Cronin57344652019-12-27 20:51:51161 constexpr char kManifest[] =
162 R"({
163 "background": {"scripts": ["bg.js"]},
164 "manifest_version": 2,
165 "name": "ExtensionCannotFetchHostedResourceWithoutHostPermissions",
166 "version": "1"
167 })";
168 dir.WriteManifest(kManifest);
kalman30a92f962015-09-03 18:08:20169 const Extension* extension = WriteFilesAndLoadTestExtension(&dir);
170 ASSERT_TRUE(extension);
171
172 // TODO(kalman): Another test would be to configure the test server to work
173 // with CORS, and test that the fetch succeeds.
174 EXPECT_EQ(
175 "TypeError: Failed to fetch",
176 ExecuteScriptInBackgroundPage(
177 extension->id(), GetFetchScript(GetQuotedTestServerURL(
178 "example.com", "/extensions/test_file.txt"))));
179}
180
181IN_PROC_BROWSER_TEST_F(ExtensionFetchTest,
182 HostCanFetchWebAccessibleExtensionResource) {
183 TestExtensionDir dir;
Devlin Cronin57344652019-12-27 20:51:51184 constexpr char kManifest[] =
185 R"({
186 "background": {"scripts": ["bg.js"]},
187 "manifest_version": 2,
188 "name": "HostCanFetchWebAccessibleExtensionResource",
189 "version": "1",
190 "web_accessible_resources": ["text"]
191 })";
192 dir.WriteManifest(kManifest);
kalman30a92f962015-09-03 18:08:20193 const Extension* extension = WriteFilesAndLoadTestExtension(&dir);
194 ASSERT_TRUE(extension);
195
196 content::WebContents* empty_tab = CreateAndNavigateTab(
197 embedded_test_server()->GetURL("example.com", "/empty.html"));
198
199 // TODO(kalman): Test this from a content script too.
200 std::string fetch_result;
201 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
202 empty_tab,
203 GetFetchScript(GetQuotedURL(extension->GetResourceURL("text"))),
204 &fetch_result));
205 EXPECT_EQ("text content", fetch_result);
206}
207
Makoto Shimazub8a7f58f2018-11-07 19:09:31208// Calling fetch() from a http(s) service worker context to a
209// chrome-extensions:// URL since the loading path in a service worker is
210// different from pages.
211// This is a regression test for https://crbug.com/901443.
212IN_PROC_BROWSER_TEST_F(
213 ExtensionFetchTest,
214 HostCanFetchWebAccessibleExtensionResource_FetchFromServiceWorker) {
215 TestExtensionDir dir;
Devlin Cronin57344652019-12-27 20:51:51216 constexpr char kManifest[] =
217 R"({
218 "background": {"scripts": ["bg.js"]},
219 "manifest_version": 2,
220 "name": "FetchFromServiceWorker",
221 "version": "1",
222 "web_accessible_resources": ["text"]
223 })";
224 dir.WriteManifest(kManifest);
Makoto Shimazub8a7f58f2018-11-07 19:09:31225 const Extension* extension = WriteFilesAndLoadTestExtension(&dir);
226 ASSERT_TRUE(extension);
227
228 content::WebContents* tab =
229 CreateAndNavigateTab(embedded_test_server()->GetURL(
230 "/workers/fetch_from_service_worker.html"));
231 EXPECT_EQ("ready", content::EvalJs(tab, "setup();"));
232 EXPECT_EQ("text content",
233 content::EvalJs(
234 tab, base::StringPrintf(
235 "fetch_from_service_worker('%s');",
236 extension->GetResourceURL("text").spec().c_str())));
237}
238
kalman30a92f962015-09-03 18:08:20239IN_PROC_BROWSER_TEST_F(ExtensionFetchTest,
240 HostCannotFetchNonWebAccessibleExtensionResource) {
241 TestExtensionDir dir;
Devlin Cronin57344652019-12-27 20:51:51242 constexpr char kManifest[] =
243 R"({
244 "background": {"scripts": ["bg.js"]},
245 "manifest_version": 2,
246 "name": "HostCannotFetchNonWebAccessibleExtensionResource",
247 "version": "1"
248 })";
249 dir.WriteManifest(kManifest);
kalman30a92f962015-09-03 18:08:20250 const Extension* extension = WriteFilesAndLoadTestExtension(&dir);
251 ASSERT_TRUE(extension);
252
253 content::WebContents* empty_tab = CreateAndNavigateTab(
254 embedded_test_server()->GetURL("example.com", "/empty.html"));
255
256 // TODO(kalman): Test this from a content script too.
257 std::string fetch_result;
258 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
259 empty_tab,
260 GetFetchScript(GetQuotedURL(extension->GetResourceURL("text"))),
261 &fetch_result));
262 EXPECT_EQ("TypeError: Failed to fetch", fetch_result);
263}
264
Yutaka Hirano7938c712019-10-15 06:00:42265IN_PROC_BROWSER_TEST_F(ExtensionFetchTest, FetchResponseType) {
266 const std::string script = base::StringPrintf(
267 "fetch(%s).then(function(response) {\n"
268 " window.domAutomationController.send(response.type);\n"
269 "}).catch(function(err) {\n"
270 " window.domAutomationController.send(String(err));\n"
271 "});\n",
272 GetQuotedTestServerURL("example.com", "/extensions/test_file.txt")
273 .data());
274 TestExtensionDir dir;
Devlin Cronin57344652019-12-27 20:51:51275 constexpr char kManifest[] =
276 R"({
277 "background": {"scripts": ["bg.js"]},
278 "manifest_version": 2,
279 "name": "FetchResponseType",
280 "permissions": ["http://example.com/*"],
281 "version": "1"
282 })";
283 dir.WriteManifest(kManifest);
Yutaka Hirano7938c712019-10-15 06:00:42284 const Extension* extension = WriteFilesAndLoadTestExtension(&dir);
285 ASSERT_TRUE(extension);
286
287 EXPECT_EQ("basic", ExecuteScriptInBackgroundPage(extension->id(), script));
288}
289
Yutaka Hirano54b166eb2019-12-05 20:59:52290IN_PROC_BROWSER_TEST_F(ExtensionFetchTest, OriginOnPostWithPermissions) {
291 TestExtensionDir dir;
292 dir.WriteManifest(R"JSON(
293 {
294 "background": {"scripts": ["bg.js"]},
295 "manifest_version": 2,
296 "name": "FetchResponseType",
297 "permissions": ["http://example.com/*"],
298 "version": "1"
299 })JSON");
300 const Extension* extension = WriteFilesAndLoadTestExtension(&dir);
301 ASSERT_TRUE(extension);
302
303 // Extension scripts will have the origin of the destination URL only when
304 // OOR-CORS is enabled.
305 GURL destination_url =
306 embedded_test_server()->GetURL("example.com", "/echo-origin");
307 std::string script = content::JsReplace(kFetchPostScript, destination_url);
308 std::string origin_string =
309 network::features::ShouldEnableOutOfBlinkCorsForTesting()
310 ? url::Origin::Create(destination_url).Serialize()
311 : url::Origin::Create(extension->url()).Serialize();
312 EXPECT_EQ(origin_string,
313 ExecuteScriptInBackgroundPage(extension->id(), script));
314}
315
316IN_PROC_BROWSER_TEST_F(ExtensionFetchTest, OriginOnPostWithoutPermissions) {
317 TestExtensionDir dir;
318 dir.WriteManifest(R"JSON(
319 {
320 "background": {"scripts": ["bg.js"]},
321 "manifest_version": 2,
322 "name": "FetchResponseType",
323 "permissions": [],
324 "version": "1"
325 })JSON");
326 const Extension* extension = WriteFilesAndLoadTestExtension(&dir);
327 ASSERT_TRUE(extension);
328
329 const std::string script = content::JsReplace(
330 kFetchPostScript,
331 embedded_test_server()->GetURL("example.com", "/echo-origin"));
332 EXPECT_EQ(url::Origin::Create(extension->url()).Serialize(),
333 ExecuteScriptInBackgroundPage(extension->id(), script));
334}
335
kalman30a92f962015-09-03 18:08:20336} // namespace
337
338} // namespace extensions