Commit f28c0709 authored by Daniel Hosseinian's avatar Daniel Hosseinian Committed by Commit Bot

Show that JS content settings are not respected in PDFs with tests

JavaScript content settings in PDFs can be verified by testing whether
the JavaScript in a PDF can successfully "beep." Add tests that are able
to record a "beep" regardless of the content settings. In doing so,
re-enable PDFExtensionJSTest.NoBeep as
PDFExtensionContentSettingJSTest.NoBeep with the incorrect behavior.

Create parameterized tests that are able to enable/disable the feature
flag that controls whether JS content settings are honored in PDFs.
The current behavior of the tests should not be impacted by the
parameter.

Two of the tests try to alter the content settings between the
initialization of two same-origin PDFs.

Bug: 696650
Change-Id: Ie62abc66cc2ba671f250f98bde7aa9b2fb132980
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2202972
Commit-Queue: Daniel Hosseinian <dhoss@chromium.org>
Reviewed-by: default avatarTom Sepez <tsepez@chromium.org>
Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#773486}
parent 5ae4137b
...@@ -224,14 +224,13 @@ class PDFExtensionTest : public extensions::ExtensionApiTest { ...@@ -224,14 +224,13 @@ class PDFExtensionTest : public extensions::ExtensionApiTest {
// Same as LoadPdf(), but also returns a pointer to the guest WebContents for // Same as LoadPdf(), but also returns a pointer to the guest WebContents for
// the loaded PDF. Returns nullptr if the load fails. // the loaded PDF. Returns nullptr if the load fails.
WebContents* LoadPdfGetGuestContents(const GURL& url) { WebContents* LoadPdfGetGuestContents(const GURL& url) {
if (!LoadPdf(url)) return LoadPdfGetGuestContentsHelper(url, /*new_tab=*/false);
return nullptr; }
WebContents* contents = GetActiveWebContents(); // Same as LoadPdf(), but also returns a pointer to the guest WebContents for
content::BrowserPluginGuestManager* guest_manager = // the loaded PDF in a new tab. Returns nullptr if the load fails.
contents->GetBrowserContext()->GetGuestManager(); WebContents* LoadPdfInNewTabGetGuestContents(const GURL& url) {
WebContents* guest_contents = guest_manager->GetFullPageGuest(contents); return LoadPdfGetGuestContentsHelper(url, /*new_tab=*/true);
return guest_contents;
} }
// Load all the PDFs contained in chrome/test/data/<dir_name>. This only runs // Load all the PDFs contained in chrome/test/data/<dir_name>. This only runs
...@@ -369,6 +368,22 @@ class PDFExtensionTest : public extensions::ExtensionApiTest { ...@@ -369,6 +368,22 @@ class PDFExtensionTest : public extensions::ExtensionApiTest {
base::FilePath(ChromeContentClient::kPDFPluginPath), base::FilePath(ChromeContentClient::kPDFPluginPath),
browser()->profile()->GetPath()); browser()->profile()->GetPath());
} }
private:
WebContents* LoadPdfGetGuestContentsHelper(const GURL& url, bool new_tab) {
if (new_tab) {
if (!LoadPdfInNewTab(url))
return nullptr;
} else if (!LoadPdf(url)) {
return nullptr;
}
WebContents* contents = GetActiveWebContents();
content::BrowserPluginGuestManager* guest_manager =
contents->GetBrowserContext()->GetGuestManager();
WebContents* guest_contents = guest_manager->GetFullPageGuest(contents);
return guest_contents;
}
}; };
class PDFExtensionTestWithTestGuestViewManager : public PDFExtensionTest { class PDFExtensionTestWithTestGuestViewManager : public PDFExtensionTest {
...@@ -700,11 +715,23 @@ class PDFExtensionJSTest : public PDFExtensionTest { ...@@ -700,11 +715,23 @@ class PDFExtensionJSTest : public PDFExtensionTest {
~PDFExtensionJSTest() override = default; ~PDFExtensionJSTest() override = default;
protected: protected:
void RunTestsInJsModule(const std::string& filename,
const std::string& pdf_filename) {
RunTestsInJsModuleHelper(filename, pdf_filename, /*new_tab=*/false);
}
void RunTestsInJsModuleNewTab(const std::string& filename,
const std::string& pdf_filename) {
RunTestsInJsModuleHelper(filename, pdf_filename, /*new_tab=*/true);
}
private:
// Runs the extensions test at chrome/test/data/pdf/<filename> on the PDF file // Runs the extensions test at chrome/test/data/pdf/<filename> on the PDF file
// at chrome/test/data/pdf/<pdf_filename>, where |filename| is loaded as a JS // at chrome/test/data/pdf/<pdf_filename>, where |filename| is loaded as a JS
// module. // module.
void RunTestsInJsModule(const std::string& filename, void RunTestsInJsModuleHelper(const std::string& filename,
const std::string& pdf_filename) { const std::string& pdf_filename,
bool new_tab) {
extensions::ResultCatcher catcher; extensions::ResultCatcher catcher;
GURL url(embedded_test_server()->GetURL("/pdf/" + pdf_filename)); GURL url(embedded_test_server()->GetURL("/pdf/" + pdf_filename));
...@@ -714,7 +741,8 @@ class PDFExtensionJSTest : public PDFExtensionTest { ...@@ -714,7 +741,8 @@ class PDFExtensionJSTest : public PDFExtensionTest {
// being seen due to the BrowserPluginGuest not being available yet (see // being seen due to the BrowserPluginGuest not being available yet (see
// crbug.com/498077). So instead use LoadPdf() which ensures that the PDF is // crbug.com/498077). So instead use LoadPdf() which ensures that the PDF is
// loaded before continuing. // loaded before continuing.
WebContents* guest_contents = LoadPdfGetGuestContents(url); WebContents* guest_contents = new_tab ? LoadPdfInNewTabGetGuestContents(url)
: LoadPdfGetGuestContents(url);
ASSERT_TRUE(guest_contents); ASSERT_TRUE(guest_contents);
constexpr char kModuleLoaderTemplate[] = constexpr char kModuleLoaderTemplate[] =
...@@ -797,30 +825,10 @@ IN_PROC_BROWSER_TEST_F(PDFExtensionJSTest, WhitespaceTitle) { ...@@ -797,30 +825,10 @@ IN_PROC_BROWSER_TEST_F(PDFExtensionJSTest, WhitespaceTitle) {
RunTestsInJsModule("whitespace_title_test.js", "test-whitespace-title.pdf"); RunTestsInJsModule("whitespace_title_test.js", "test-whitespace-title.pdf");
} }
IN_PROC_BROWSER_TEST_F(PDFExtensionJSTest, Beep) {
RunTestsInJsModule("beep_test.js", "test-beep.pdf");
}
IN_PROC_BROWSER_TEST_F(PDFExtensionJSTest, TwoUpViewFeature) { IN_PROC_BROWSER_TEST_F(PDFExtensionJSTest, TwoUpViewFeature) {
RunTestsInJsModule("two_up_view_feature_test.js", "test.pdf"); RunTestsInJsModule("two_up_view_feature_test.js", "test.pdf");
} }
// TODO(tsepez): See https://crbug.com/696650.
IN_PROC_BROWSER_TEST_F(PDFExtensionJSTest, DISABLED_NoBeep) {
// Block the exact query from pdf/main.js while still allowing enough
// JavaScript to run in the extension for this test harness to complete
// its work.
auto* map =
HostContentSettingsMapFactory::GetForProfile(browser()->profile());
map->SetContentSettingCustomScope(
ContentSettingsPattern::Wildcard(),
ContentSettingsPattern::FromString(
"chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai"),
ContentSettingsType::JAVASCRIPT, std::string(), CONTENT_SETTING_BLOCK);
RunTestsInJsModule("nobeep_test.js", "test-beep.pdf");
}
IN_PROC_BROWSER_TEST_F(PDFExtensionJSTest, PageChange) { IN_PROC_BROWSER_TEST_F(PDFExtensionJSTest, PageChange) {
RunTestsInJsModule("page_change_test.js", "test-bookmarks.pdf"); RunTestsInJsModule("page_change_test.js", "test-bookmarks.pdf");
} }
...@@ -851,7 +859,7 @@ class PDFAnnotationsJSTest : public PDFExtensionJSTest { ...@@ -851,7 +859,7 @@ class PDFAnnotationsJSTest : public PDFExtensionJSTest {
protected: protected:
void SetUpCommandLine(base::CommandLine* command_line) override { void SetUpCommandLine(base::CommandLine* command_line) override {
PDFExtensionTest::SetUpCommandLine(command_line); PDFExtensionJSTest::SetUpCommandLine(command_line);
feature_list_.InitAndEnableFeature(chrome_pdf::features::kPDFAnnotations); feature_list_.InitAndEnableFeature(chrome_pdf::features::kPDFAnnotations);
} }
...@@ -870,6 +878,84 @@ IN_PROC_BROWSER_TEST_F(PDFAnnotationsJSTest, MAYBE_AnnotationsFeatureEnabled) { ...@@ -870,6 +878,84 @@ IN_PROC_BROWSER_TEST_F(PDFAnnotationsJSTest, MAYBE_AnnotationsFeatureEnabled) {
} }
#endif // defined(OS_CHROMEOS) #endif // defined(OS_CHROMEOS)
class PDFExtensionContentSettingJSTest
: public PDFExtensionJSTest,
public testing::WithParamInterface<bool> {
public:
~PDFExtensionContentSettingJSTest() override = default;
protected:
void SetUpCommandLine(base::CommandLine* command_line) override {
PDFExtensionJSTest::SetUpCommandLine(command_line);
if (ShouldHonorJsContentSettings()) {
feature_list_.InitAndEnableFeature(
chrome_pdf::features::kPdfHonorJsContentSettings);
}
}
bool ShouldHonorJsContentSettings() const { return GetParam(); }
// When blocking JavaScript, block the exact query from pdf/main.js while
// still allowing enough JavaScript to run in the extension for the test
// harness to complete its work.
void SetPdfJavaScript(bool enabled) {
auto* map =
HostContentSettingsMapFactory::GetForProfile(browser()->profile());
map->SetContentSettingCustomScope(
ContentSettingsPattern::Wildcard(),
ContentSettingsPattern::FromString(
"chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai"),
ContentSettingsType::JAVASCRIPT, std::string(),
enabled ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK);
}
std::string GetDisabledJsTestFile() const {
// TODO(crbug.com/696650): Run nobeep_test.js when
// |honor_js_content_settings_| once JS content settings get respected in
// the PDF extension.
return "beep_test.js";
}
private:
base::test::ScopedFeatureList feature_list_;
};
IN_PROC_BROWSER_TEST_P(PDFExtensionContentSettingJSTest, Beep) {
RunTestsInJsModule("beep_test.js", "test-beep.pdf");
}
IN_PROC_BROWSER_TEST_P(PDFExtensionContentSettingJSTest, NoBeep) {
SetPdfJavaScript(/*enabled=*/false);
RunTestsInJsModule(GetDisabledJsTestFile(), "test-beep.pdf");
}
IN_PROC_BROWSER_TEST_P(PDFExtensionContentSettingJSTest, BeepThenNoBeep) {
RunTestsInJsModule("beep_test.js", "test-beep.pdf");
SetPdfJavaScript(/*enabled=*/false);
RunTestsInJsModuleNewTab(GetDisabledJsTestFile(), "test-beep.pdf");
// Make sure there are two PDFs in the same process.
const int tab_count = browser()->tab_strip_model()->count();
EXPECT_EQ(2, tab_count);
EXPECT_EQ(1, CountPDFProcesses());
}
IN_PROC_BROWSER_TEST_P(PDFExtensionContentSettingJSTest, NoBeepThenBeep) {
SetPdfJavaScript(/*enabled=*/false);
RunTestsInJsModule(GetDisabledJsTestFile(), "test-beep.pdf");
SetPdfJavaScript(/*enabled=*/true);
RunTestsInJsModuleNewTab("beep_test.js", "test-beep.pdf");
// Make sure there are two PDFs in the same process.
const int tab_count = browser()->tab_strip_model()->count();
EXPECT_EQ(2, tab_count);
EXPECT_EQ(1, CountPDFProcesses());
}
INSTANTIATE_TEST_SUITE_P(/* no prefix */,
PDFExtensionContentSettingJSTest,
testing::Bool());
// Service worker tests are regression tests for // Service worker tests are regression tests for
// https://crbug.com/916514. // https://crbug.com/916514.
class PDFExtensionServiceWorkerJSTest : public PDFExtensionJSTest { class PDFExtensionServiceWorkerJSTest : public PDFExtensionJSTest {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment