|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 3 | + * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +#include "base/path_service.h" |
| 6 | +#include "brave/common/brave_paths.h" |
| 7 | +#include "brave/components/brave_shields/common/brave_shield_constants.h" |
| 8 | +#include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 9 | +#include "chrome/browser/profiles/profile.h" |
| 10 | +#include "chrome/browser/ui/browser.h" |
| 11 | +#include "chrome/test/base/in_process_browser_test.h" |
| 12 | +#include "chrome/test/base/ui_test_utils.h" |
| 13 | +#include "components/content_settings/core/browser/host_content_settings_map.h" |
| 14 | +#include "content/public/test/browser_test_utils.h" |
| 15 | +#include "net/dns/mock_host_resolver.h" |
| 16 | +#include "url/gurl.h" |
| 17 | + |
| 18 | +class BraveNetworkDelegateBrowserTest : public InProcessBrowserTest { |
| 19 | + public: |
| 20 | + void SetUpOnMainThread() override { |
| 21 | + InProcessBrowserTest::SetUpOnMainThread(); |
| 22 | + |
| 23 | + host_resolver()->AddRule("*", "127.0.0.1"); |
| 24 | + content::SetupCrossSiteRedirector(embedded_test_server()); |
| 25 | + |
| 26 | + brave::RegisterPathProvider(); |
| 27 | + base::FilePath test_data_dir; |
| 28 | + base::PathService::Get(brave::DIR_TEST_DATA, &test_data_dir); |
| 29 | + embedded_test_server()->ServeFilesFromDirectory(test_data_dir); |
| 30 | + |
| 31 | + ASSERT_TRUE(embedded_test_server()->Start()); |
| 32 | + |
| 33 | + url_ = embedded_test_server()->GetURL("a.com", "/nested_iframe.html"); |
| 34 | + nested_iframe_script_url_ = |
| 35 | + embedded_test_server()->GetURL("a.com", "/nested_iframe_script.html"); |
| 36 | + |
| 37 | + top_level_page_pattern_ = |
| 38 | + ContentSettingsPattern::FromString("http://a.com/*"); |
| 39 | + first_party_pattern_ = |
| 40 | + ContentSettingsPattern::FromString("https://firstParty/*"); |
| 41 | + } |
| 42 | + |
| 43 | + HostContentSettingsMap* content_settings() { |
| 44 | + return HostContentSettingsMapFactory::GetForProfile(browser()->profile()); |
| 45 | + } |
| 46 | + |
| 47 | + void AllowCookies() { |
| 48 | + content_settings()->SetContentSettingCustomScope( |
| 49 | + top_level_page_pattern_, ContentSettingsPattern::Wildcard(), |
| 50 | + CONTENT_SETTINGS_TYPE_PLUGINS, brave_shields::kCookies, |
| 51 | + CONTENT_SETTING_ALLOW); |
| 52 | + content_settings()->SetContentSettingCustomScope( |
| 53 | + top_level_page_pattern_, first_party_pattern_, |
| 54 | + CONTENT_SETTINGS_TYPE_PLUGINS, brave_shields::kCookies, |
| 55 | + CONTENT_SETTING_ALLOW); |
| 56 | + } |
| 57 | + |
| 58 | + protected: |
| 59 | + GURL url_; |
| 60 | + GURL nested_iframe_script_url_; |
| 61 | + |
| 62 | + private: |
| 63 | + ContentSettingsPattern top_level_page_pattern_; |
| 64 | + ContentSettingsPattern first_party_pattern_; |
| 65 | + ContentSettingsPattern iframe_pattern_; |
| 66 | +}; |
| 67 | + |
| 68 | +// It is important that cookies in following tests are set by response headers, |
| 69 | +// not by javascript. Fetching such cookies is controlled by NetworkDelegate. |
| 70 | +IN_PROC_BROWSER_TEST_F(BraveNetworkDelegateBrowserTest, Iframe3PCookieBlocked) { |
| 71 | + ui_test_utils::NavigateToURL(browser(), url_); |
| 72 | + const std::string cookie = |
| 73 | + content::GetCookies(browser()->profile(), GURL("http://c.com/")); |
| 74 | + EXPECT_TRUE(cookie.empty()) << "Actual cookie: " << cookie; |
| 75 | +} |
| 76 | + |
| 77 | +IN_PROC_BROWSER_TEST_F(BraveNetworkDelegateBrowserTest, Iframe3PCookieAllowed) { |
| 78 | + AllowCookies(); |
| 79 | + ui_test_utils::NavigateToURL(browser(), url_); |
| 80 | + const std::string cookie = |
| 81 | + content::GetCookies(browser()->profile(), GURL("http://c.com/")); |
| 82 | + EXPECT_FALSE(cookie.empty()); |
| 83 | +} |
| 84 | + |
| 85 | +// Fetching not just a frame, but some other resource. |
| 86 | +IN_PROC_BROWSER_TEST_F(BraveNetworkDelegateBrowserTest, |
| 87 | + IframeJs3PCookieBlocked) { |
| 88 | + ui_test_utils::NavigateToURL(browser(), nested_iframe_script_url_); |
| 89 | + const std::string cookie = |
| 90 | + content::GetCookies(browser()->profile(), GURL("http://c.com/")); |
| 91 | + EXPECT_TRUE(cookie.empty()) << "Actual cookie: " << cookie; |
| 92 | +} |
| 93 | + |
| 94 | +IN_PROC_BROWSER_TEST_F(BraveNetworkDelegateBrowserTest, |
| 95 | + IframeJs3PCookieAllowed) { |
| 96 | + AllowCookies(); |
| 97 | + ui_test_utils::NavigateToURL(browser(), nested_iframe_script_url_); |
| 98 | + const std::string cookie = |
| 99 | + content::GetCookies(browser()->profile(), GURL("http://c.com/")); |
| 100 | + EXPECT_FALSE(cookie.empty()); |
| 101 | +} |
0 commit comments