diff options
author | yoz@chromium.org <yoz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-02 19:01:37 +0000 |
---|---|---|
committer | yoz@chromium.org <yoz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-02 19:01:37 +0000 |
commit | 8d0e6b8bbaf99a33db774482927ec2daeaba623b (patch) | |
tree | 3167cb1af2848ef3d9fe41816275b98a63621661 | |
parent | 9b99bd9fd5ea25f4bc0509098010fa8621dad59b (diff) | |
download | chromium_src-8d0e6b8bbaf99a33db774482927ec2daeaba623b.zip chromium_src-8d0e6b8bbaf99a33db774482927ec2daeaba623b.tar.gz chromium_src-8d0e6b8bbaf99a33db774482927ec2daeaba623b.tar.bz2 |
Do not inject JS scripts into view source frames.
(Programmatic injection via tabs.executeScript already doesn't work on
view-source. This fixes the other case, scripts from the manifest.)
BUG=39249
TEST=added ExtensionApiTest.ContentScriptViewSource
Review URL: http://codereview.chromium.org/7006030
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@87651 0039d316-1c4b-4281-b951-d872f2087c98
6 files changed, 55 insertions, 1 deletions
diff --git a/chrome/browser/extensions/content_script_apitest.cc b/chrome/browser/extensions/content_script_apitest.cc index 01232eb..821dc48 100644 --- a/chrome/browser/extensions/content_script_apitest.cc +++ b/chrome/browser/extensions/content_script_apitest.cc @@ -48,3 +48,10 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ContentScriptIgnoreHostPermissions) { ASSERT_TRUE(RunExtensionTest( "content_scripts/dont_match_host_permissions")) << message_; } + +// crbug.com/39249 -- content scripts js should not run on view source. +IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ContentScriptViewSource) { + ASSERT_TRUE(StartTestServer()); + host_resolver()->AddRule("c.com", "127.0.0.1"); + ASSERT_TRUE(RunExtensionTest("content_scripts/view_source")) << message_; +} diff --git a/chrome/common/extensions/url_pattern.h b/chrome/common/extensions/url_pattern.h index 1dea772..264da1f 100644 --- a/chrome/common/extensions/url_pattern.h +++ b/chrome/common/extensions/url_pattern.h @@ -182,7 +182,7 @@ class URLPattern { bool IsValidScheme(const std::string& scheme) const; // Returns true if this instance matches the specified URL. - bool MatchesURL(const GURL& url) const; + bool MatchesURL(const GURL& test) const; // Returns true if |test| matches our scheme. bool MatchesScheme(const std::string& test) const; diff --git a/chrome/renderer/extensions/user_script_slave.cc b/chrome/renderer/extensions/user_script_slave.cc index a35681e..b2fa42e 100644 --- a/chrome/renderer/extensions/user_script_slave.cc +++ b/chrome/renderer/extensions/user_script_slave.cc @@ -238,6 +238,10 @@ void UserScriptSlave::InjectScripts(WebFrame* frame, if (frame_url.is_empty()) return; + if (frame->isViewSourceModeEnabled()) + frame_url = GURL(chrome::kViewSourceScheme + std::string(":") + + frame_url.spec()); + PerfTimer timer; int num_css = 0; int num_scripts = 0; diff --git a/chrome/test/data/extensions/api_test/content_scripts/view_source/background.html b/chrome/test/data/extensions/api_test/content_scripts/view_source/background.html new file mode 100644 index 0000000..56ba4b6 --- /dev/null +++ b/chrome/test/data/extensions/api_test/content_scripts/view_source/background.html @@ -0,0 +1,28 @@ +<script> +chrome.test.runTests([ + function noContentScriptsInViewSource() { + + chrome.extension.onRequest.addListener( + function(request, sender, sendResponse) { + chrome.test.fail('Got a content script request from view source mode.'); + }); + + // We rely on content scripts running at document_start to run before we + // receive a tab update with 'complete' status. + + chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { + if (changeInfo.status === 'complete' && + tab.url.indexOf('test_file.html') != -1) { + chrome.test.succeed(); + } + }); + + chrome.test.getConfig(function(config) { + chrome.tabs.create({ + url: 'view-source:http://c.com:' + config.testServer.port + + '/files/extensions/test_file.html'}); + }); + } +]); + +</script> diff --git a/chrome/test/data/extensions/api_test/content_scripts/view_source/manifest.json b/chrome/test/data/extensions/api_test/content_scripts/view_source/manifest.json new file mode 100644 index 0000000..cb73ef5 --- /dev/null +++ b/chrome/test/data/extensions/api_test/content_scripts/view_source/manifest.json @@ -0,0 +1,14 @@ +{ + "name": "View source content scripts test", + "description": "Detect when a view source page opens, checking that content scripts don't run.", + "version": "0.3", + "background_page": "background.html", + "permissions": ["tabs", "http://*/*"], + "content_scripts": [ + { + "matches": ["http://*/*"], + "js": ["request.js"], + "run_at": "document_start" + } + ] +} diff --git a/chrome/test/data/extensions/api_test/content_scripts/view_source/request.js b/chrome/test/data/extensions/api_test/content_scripts/view_source/request.js new file mode 100644 index 0000000..1ecfa42 --- /dev/null +++ b/chrome/test/data/extensions/api_test/content_scripts/view_source/request.js @@ -0,0 +1 @@ +chrome.extension.sendRequest({}); |