Skip to content

Commit

Permalink
Issue 6464: Do not forget about shields while calculating cookie rules.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Efremov authored and bridiver committed Oct 15, 2019
1 parent 3f542e1 commit 671efa7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
14 changes: 14 additions & 0 deletions browser/net/brave_network_delegate_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ class BraveNetworkDelegateBrowserTest : public InProcessBrowserTest {
top_level_page_url_);
}

void ShieldsDown() {
brave_shields::SetBraveShieldsEnabled(browser()->profile(),
false,
top_level_page_url_);
}

protected:
GURL url_;
GURL nested_iframe_script_url_;
Expand Down Expand Up @@ -81,6 +87,14 @@ IN_PROC_BROWSER_TEST_F(BraveNetworkDelegateBrowserTest, Iframe3PCookieAllowed) {
EXPECT_FALSE(cookie.empty());
}

IN_PROC_BROWSER_TEST_F(BraveNetworkDelegateBrowserTest, Iframe3PShieldsDown) {
ShieldsDown();
ui_test_utils::NavigateToURL(browser(), url_);
const std::string cookie =
content::GetCookies(browser()->profile(), GURL("http://c.com/"));
EXPECT_FALSE(cookie.empty());
}

// Fetching not just a frame, but some other resource.
IN_PROC_BROWSER_TEST_F(BraveNetworkDelegateBrowserTest,
IframeJs3PCookieBlocked) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ class BraveShieldsRuleIterator : public RuleIterator {


bool IsActive(const Rule& cookie_rule,
const std::vector<Rule>& shield_rules,
bool* shields_down_for_site) {
const std::vector<Rule>& shield_rules) {
// don't include default rules in the iterator
if (cookie_rule.primary_pattern == ContentSettingsPattern::Wildcard() &&
(cookie_rule.secondary_pattern == ContentSettingsPattern::Wildcard() ||
Expand All @@ -86,8 +85,6 @@ bool IsActive(const Rule& cookie_rule,
if (primary_compare == ContentSettingsPattern::IDENTITY ||
primary_compare == ContentSettingsPattern::SUCCESSOR) {
// TODO(bridiver) - move this logic into shields_util for allow/block
*shields_down_for_site =
ValueToContentSetting(&shield_rule.value) == CONTENT_SETTING_BLOCK;
return
ValueToContentSetting(&shield_rule.value) != CONTENT_SETTING_BLOCK;
}
Expand Down Expand Up @@ -224,6 +221,7 @@ void BravePrefProvider::UpdateCookieRules(ContentSettingsType content_type,
while (brave_shields_iterator && brave_shields_iterator->HasNext()) {
shield_rules.push_back(CloneRule(brave_shields_iterator->Next()));
}

brave_shields_iterator.reset();

// add brave cookies after checking shield status
Expand All @@ -234,20 +232,30 @@ void BravePrefProvider::UpdateCookieRules(ContentSettingsType content_type,

auto old_rules = std::move(brave_cookie_rules_[incognito]);

// Matching cookie rules against shield rules.
while (brave_cookies_iterator && brave_cookies_iterator->HasNext()) {
auto rule = brave_cookies_iterator->Next();
bool shields_down_for_site = false;
if (IsActive(rule, shield_rules, &shields_down_for_site)) {
if (IsActive(rule, shield_rules)) {
rules.push_back(CloneRule(rule, true));
brave_cookie_rules_[incognito].push_back(CloneRule(rule, true));
} else if (shields_down_for_site) {
}
}

// Adding shields down rules (they always override cookie rules).
for (const auto& shield_rule : shield_rules) {
// Skip the default rule.
if (shield_rule.primary_pattern.MatchesAllHosts()) {
continue;
}
// Shields down.
if (ValueToContentSetting(&shield_rule.value) == CONTENT_SETTING_BLOCK) {
rules.push_back(
Rule(ContentSettingsPattern::Wildcard(),
rule.primary_pattern,
shield_rule.primary_pattern,
ContentSettingToValue(CONTENT_SETTING_ALLOW)->Clone()));
brave_cookie_rules_[incognito].push_back(
Rule(ContentSettingsPattern::Wildcard(),
rule.primary_pattern,
shield_rule.primary_pattern,
ContentSettingToValue(CONTENT_SETTING_ALLOW)->Clone()));
}
}
Expand Down
13 changes: 12 additions & 1 deletion renderer/brave_content_settings_observer_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ IN_PROC_BROWSER_TEST_F(BraveContentSettingsObserverBrowserTest,
}

IN_PROC_BROWSER_TEST_F(BraveContentSettingsObserverBrowserTest,
ShieldsDownAllowsCookies) {
ShieldsDownAllowBlockedCookies) {
BlockCookies();
ShieldsDown();
NavigateToPageWithIframe();
Expand All @@ -665,6 +665,17 @@ IN_PROC_BROWSER_TEST_F(BraveContentSettingsObserverBrowserTest,
COOKIE_STR);
}

IN_PROC_BROWSER_TEST_F(BraveContentSettingsObserverBrowserTest,
ShieldsDownAllowsCookies) {
ShieldsDown();
NavigateToPageWithIframe();
EXPECT_STREQ(ExecScriptGetStr(kCookieScript, contents()).c_str(), COOKIE_STR);
ASSERT_TRUE(NavigateIframeToURL(contents(), kIframeID, iframe_url()));
ASSERT_EQ(child_frame()->GetLastCommittedURL(), iframe_url());
EXPECT_STREQ(ExecScriptGetStr(kCookieScript, child_frame()).c_str(),
COOKIE_STR);
}

IN_PROC_BROWSER_TEST_F(BraveContentSettingsObserverBrowserTest,
ShieldsUpBlockCookies) {
BlockCookies();
Expand Down

0 comments on commit 671efa7

Please sign in to comment.