Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(518)

Side by Side Diff: chrome/browser/pdf/pdf_extension_test.cc

Issue 1316803003: Prevent leaking PDF data cross-origin (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@2454
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/browser/resources/pdf/pdf.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/base_paths.h" 5 #include "base/base_paths.h"
6 #include "base/files/file_enumerator.h" 6 #include "base/files/file_enumerator.h"
7 #include "base/files/file_util.h" 7 #include "base/files/file_util.h"
8 #include "base/hash.h" 8 #include "base/hash.h"
9 #include "base/path_service.h" 9 #include "base/path_service.h"
10 #include "chrome/browser/extensions/component_loader.h" 10 #include "chrome/browser/extensions/component_loader.h"
11 #include "chrome/browser/extensions/extension_apitest.h" 11 #include "chrome/browser/extensions/extension_apitest.h"
12 #include "chrome/browser/extensions/extension_service.h" 12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model.h" 13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
14 #include "chrome/common/chrome_paths.h" 14 #include "chrome/common/chrome_paths.h"
15 #include "chrome/common/chrome_switches.h" 15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/test/base/ui_test_utils.h" 16 #include "chrome/test/base/ui_test_utils.h"
17 #include "content/public/browser/browser_plugin_guest_manager.h" 17 #include "content/public/browser/browser_plugin_guest_manager.h"
18 #include "content/public/browser/plugin_service.h" 18 #include "content/public/browser/plugin_service.h"
19 #include "content/public/test/browser_test_utils.h" 19 #include "content/public/test/browser_test_utils.h"
20 #include "extensions/browser/extension_registry.h" 20 #include "extensions/browser/extension_registry.h"
21 #include "extensions/common/manifest_handlers/mime_types_handler.h" 21 #include "extensions/common/manifest_handlers/mime_types_handler.h"
22 #include "extensions/test/result_catcher.h" 22 #include "extensions/test/result_catcher.h"
23 #include "grit/component_extension_resources.h" 23 #include "grit/component_extension_resources.h"
24 #include "net/test/embedded_test_server/embedded_test_server.h" 24 #include "net/test/embedded_test_server/embedded_test_server.h"
25 #include "ui/base/resource/resource_bundle.h" 25 #include "ui/base/resource/resource_bundle.h"
26 26
27 const int kNumberLoadTestParts = 10; 27 const int kNumberLoadTestParts = 10;
28 28
29 bool GetGuestCallback(content::WebContents** guest_out,
30 content::WebContents* guest) {
31 EXPECT_FALSE(*guest_out);
32 *guest_out = guest;
33 // Return false so that we iterate through all the guests and verify there is
34 // only one.
35 return false;
36 }
37
29 class PDFExtensionTest : public ExtensionApiTest, 38 class PDFExtensionTest : public ExtensionApiTest,
30 public testing::WithParamInterface<int> { 39 public testing::WithParamInterface<int> {
31 public: 40 public:
32 ~PDFExtensionTest() override {} 41 ~PDFExtensionTest() override {}
33 42
34 void SetUpCommandLine(base::CommandLine* command_line) override { 43 void SetUpCommandLine(base::CommandLine* command_line) override {
35 command_line->AppendSwitch(switches::kDisablePdfMaterialUI); 44 command_line->AppendSwitch(switches::kDisablePdfMaterialUI);
36 } 45 }
37 46
38 void SetUpOnMainThread() override { 47 void SetUpOnMainThread() override {
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 LOG(INFO) << "Loading: " << pdf_file; 158 LOG(INFO) << "Loading: " << pdf_file;
150 bool success = LoadPdf(embedded_test_server()->GetURL("/" + pdf_file)); 159 bool success = LoadPdf(embedded_test_server()->GetURL("/" + pdf_file));
151 EXPECT_EQ(!PdfIsExpectedToFailLoad(pdf_file), success); 160 EXPECT_EQ(!PdfIsExpectedToFailLoad(pdf_file), success);
152 } 161 }
153 ++count; 162 ++count;
154 } 163 }
155 // Assume that there is at least 1 pdf in the directory to guard against 164 // Assume that there is at least 1 pdf in the directory to guard against
156 // someone deleting the directory and silently making the test pass. 165 // someone deleting the directory and silently making the test pass.
157 ASSERT_GE(count, 1u); 166 ASSERT_GE(count, 1u);
158 } 167 }
168
169 void TestGetSelectedTextReply(GURL url, bool expect_success) {
170 ui_test_utils::NavigateToURL(browser(), url);
171 content::WebContents* web_contents =
172 browser()->tab_strip_model()->GetActiveWebContents();
173 ASSERT_TRUE(pdf_extension_test_util::EnsurePDFHasLoaded(web_contents));
174
175 // Reach into the guest and hook into it such that it posts back a 'flush'
176 // message after every getSelectedTextReply message sent.
177 content::BrowserPluginGuestManager* guest_manager =
178 web_contents->GetBrowserContext()->GetGuestManager();
179 content::WebContents* guest_contents = nullptr;
180 ASSERT_NO_FATAL_FAILURE(guest_manager->ForEachGuest(
181 web_contents, base::Bind(&GetGuestCallback, &guest_contents)));
182 ASSERT_TRUE(guest_contents);
183 ASSERT_TRUE(content::ExecuteScript(
184 guest_contents,
185 "var oldSendScriptingMessage = "
186 " PDFViewer.prototype.sendScriptingMessage_;"
187 "PDFViewer.prototype.sendScriptingMessage_ = function(message) {"
188 " oldSendScriptingMessage.bind(this)(message);"
189 " if (message.type == 'getSelectedTextReply')"
190 " this.parentWindow_.postMessage('flush', '*');"
191 "}"));
192
193 // Add an event listener for flush messages and request the selected text.
194 // If we get a flush message without receiving getSelectedText we know that
195 // the message didn't come through.
196 bool success = false;
197 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
198 web_contents,
199 "window.addEventListener('message', function(event) {"
200 " if (event.data == 'flush')"
201 " window.domAutomationController.send(false);"
202 " if (event.data.type == 'getSelectedTextReply')"
203 " window.domAutomationController.send(true);"
204 "});"
205 "document.getElementsByTagName('embed')[0].postMessage("
206 " {type: 'getSelectedText'});",
207 &success));
208 ASSERT_EQ(expect_success, success);
209 }
159 }; 210 };
160 211
161 IN_PROC_BROWSER_TEST_P(PDFExtensionTest, Load) { 212 IN_PROC_BROWSER_TEST_P(PDFExtensionTest, Load) {
162 #if defined(GOOGLE_CHROME_BUILD) 213 #if defined(GOOGLE_CHROME_BUILD)
163 // Load private PDFs. 214 // Load private PDFs.
164 LoadAllPdfsTest("pdf_private", GetParam()); 215 LoadAllPdfsTest("pdf_private", GetParam());
165 #endif 216 #endif
166 // Load public PDFs. 217 // Load public PDFs.
167 LoadAllPdfsTest("pdf", GetParam()); 218 LoadAllPdfsTest("pdf", GetParam());
168 } 219 }
(...skipping 24 matching lines...) Expand all
193 } 244 }
194 245
195 IN_PROC_BROWSER_TEST_F(PDFExtensionTest, ParamsParser) { 246 IN_PROC_BROWSER_TEST_F(PDFExtensionTest, ParamsParser) {
196 RunTestsInFile("params_parser_test.js", "test.pdf"); 247 RunTestsInFile("params_parser_test.js", "test.pdf");
197 } 248 }
198 249
199 IN_PROC_BROWSER_TEST_F(PDFExtensionTest, ZoomManager) { 250 IN_PROC_BROWSER_TEST_F(PDFExtensionTest, ZoomManager) {
200 RunTestsInFile("zoom_manager_test.js", "test.pdf"); 251 RunTestsInFile("zoom_manager_test.js", "test.pdf");
201 } 252 }
202 253
254 // Ensure that the internal PDF plugin application/x-google-chrome-pdf won't be
255 // loaded if it's not loaded in the chrome extension page.
256 IN_PROC_BROWSER_TEST_F(PDFExtensionTest, EnsureInternalPluginDisabled) {
257 std::string url = embedded_test_server()->GetURL("/pdf/test.pdf").spec();
258 std::string data_url =
259 "data:text/html,"
260 "<html><body>"
261 "<embed type=\"application/x-google-chrome-pdf\" src=\"" +
262 url +
263 "\">"
264 "</body></html>";
265 ui_test_utils::NavigateToURL(browser(), GURL(data_url));
266 content::WebContents* web_contents =
267 browser()->tab_strip_model()->GetActiveWebContents();
268 bool plugin_loaded = false;
269 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
270 web_contents,
271 "var plugin_loaded = "
272 " document.getElementsByTagName('embed')[0].postMessage !== undefined;"
273 "window.domAutomationController.send(plugin_loaded);",
274 &plugin_loaded));
275 ASSERT_FALSE(plugin_loaded);
276 }
277
278 // Ensure cross-origin replies won't work for getSelectedText.
279 IN_PROC_BROWSER_TEST_F(PDFExtensionTest, EnsureCrossOriginRepliesBlocked) {
280 std::string url = embedded_test_server()->GetURL("/pdf/test.pdf").spec();
281 std::string data_url =
282 "data:text/html,"
283 "<html><body>"
284 "<embed type=\"application/pdf\" src=\"" +
285 url +
286 "\">"
287 "</body></html>";
288 TestGetSelectedTextReply(GURL(data_url), false);
289 }
290
291 // Ensure same-origin replies do work for getSelectedText.
292 IN_PROC_BROWSER_TEST_F(PDFExtensionTest, EnsureSameOriginRepliesAllowed) {
293 TestGetSelectedTextReply(embedded_test_server()->GetURL("/pdf/test.pdf"),
294 true);
295 }
296
203 class MaterialPDFExtensionTest : public PDFExtensionTest { 297 class MaterialPDFExtensionTest : public PDFExtensionTest {
204 void SetUpCommandLine(base::CommandLine* command_line) override { 298 void SetUpCommandLine(base::CommandLine* command_line) override {
205 command_line->AppendSwitch(switches::kEnablePdfMaterialUI); 299 command_line->AppendSwitch(switches::kEnablePdfMaterialUI);
206 } 300 }
207 }; 301 };
208 302
209 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest, Basic) { 303 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest, Basic) {
210 RunTestsInFile("basic_test_material.js", "test.pdf"); 304 RunTestsInFile("basic_test_material.js", "test.pdf");
211 } 305 }
212 306
(...skipping 13 matching lines...) Expand all
226 RunTestsInFile("navigator_test.js", "test.pdf"); 320 RunTestsInFile("navigator_test.js", "test.pdf");
227 } 321 }
228 322
229 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest, ParamsParser) { 323 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest, ParamsParser) {
230 RunTestsInFile("params_parser_test.js", "test.pdf"); 324 RunTestsInFile("params_parser_test.js", "test.pdf");
231 } 325 }
232 326
233 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest, ZoomManager) { 327 IN_PROC_BROWSER_TEST_F(MaterialPDFExtensionTest, ZoomManager) {
234 RunTestsInFile("zoom_manager_test.js", "test.pdf"); 328 RunTestsInFile("zoom_manager_test.js", "test.pdf");
235 } 329 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/resources/pdf/pdf.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698