-
Notifications
You must be signed in to change notification settings - Fork 888
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue 2095: use the proper tab origin. #942
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
#include "base/path_service.h" | ||
#include "brave/common/brave_paths.h" | ||
#include "brave/components/brave_shields/common/brave_shield_constants.h" | ||
#include "chrome/browser/content_settings/host_content_settings_map_factory.h" | ||
#include "chrome/browser/profiles/profile.h" | ||
#include "chrome/browser/ui/browser.h" | ||
#include "chrome/test/base/in_process_browser_test.h" | ||
#include "chrome/test/base/ui_test_utils.h" | ||
#include "components/content_settings/core/browser/host_content_settings_map.h" | ||
#include "content/public/test/browser_test_utils.h" | ||
#include "net/dns/mock_host_resolver.h" | ||
#include "url/gurl.h" | ||
|
||
class BraveNetworkDelegateBrowserTest : public InProcessBrowserTest { | ||
public: | ||
void SetUpOnMainThread() override { | ||
InProcessBrowserTest::SetUpOnMainThread(); | ||
|
||
host_resolver()->AddRule("*", "127.0.0.1"); | ||
content::SetupCrossSiteRedirector(embedded_test_server()); | ||
|
||
brave::RegisterPathProvider(); | ||
base::FilePath test_data_dir; | ||
base::PathService::Get(brave::DIR_TEST_DATA, &test_data_dir); | ||
embedded_test_server()->ServeFilesFromDirectory(test_data_dir); | ||
|
||
ASSERT_TRUE(embedded_test_server()->Start()); | ||
|
||
url_ = embedded_test_server()->GetURL("a.com", "/nested_iframe.html"); | ||
nested_iframe_script_url_ = | ||
embedded_test_server()->GetURL("a.com", "/nested_iframe_script.html"); | ||
|
||
top_level_page_pattern_ = | ||
ContentSettingsPattern::FromString("http://a.com/*"); | ||
first_party_pattern_ = | ||
ContentSettingsPattern::FromString("https://firstParty/*"); | ||
} | ||
|
||
HostContentSettingsMap* content_settings() { | ||
return HostContentSettingsMapFactory::GetForProfile(browser()->profile()); | ||
} | ||
|
||
void AllowCookies() { | ||
content_settings()->SetContentSettingCustomScope( | ||
top_level_page_pattern_, ContentSettingsPattern::Wildcard(), | ||
CONTENT_SETTINGS_TYPE_PLUGINS, brave_shields::kCookies, | ||
CONTENT_SETTING_ALLOW); | ||
content_settings()->SetContentSettingCustomScope( | ||
top_level_page_pattern_, first_party_pattern_, | ||
CONTENT_SETTINGS_TYPE_PLUGINS, brave_shields::kCookies, | ||
CONTENT_SETTING_ALLOW); | ||
} | ||
|
||
protected: | ||
GURL url_; | ||
GURL nested_iframe_script_url_; | ||
|
||
private: | ||
ContentSettingsPattern top_level_page_pattern_; | ||
ContentSettingsPattern first_party_pattern_; | ||
ContentSettingsPattern iframe_pattern_; | ||
}; | ||
|
||
// It is important that cookies in following tests are set by response headers, | ||
// not by javascript. Fetching such cookies is controlled by NetworkDelegate. | ||
IN_PROC_BROWSER_TEST_F(BraveNetworkDelegateBrowserTest, Iframe3PCookieBlocked) { | ||
ui_test_utils::NavigateToURL(browser(), url_); | ||
const std::string cookie = | ||
content::GetCookies(browser()->profile(), GURL("http://c.com/")); | ||
EXPECT_TRUE(cookie.empty()) << "Actual cookie: " << cookie; | ||
} | ||
|
||
IN_PROC_BROWSER_TEST_F(BraveNetworkDelegateBrowserTest, Iframe3PCookieAllowed) { | ||
AllowCookies(); | ||
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) { | ||
ui_test_utils::NavigateToURL(browser(), nested_iframe_script_url_); | ||
const std::string cookie = | ||
content::GetCookies(browser()->profile(), GURL("http://c.com/")); | ||
EXPECT_TRUE(cookie.empty()) << "Actual cookie: " << cookie; | ||
} | ||
|
||
IN_PROC_BROWSER_TEST_F(BraveNetworkDelegateBrowserTest, | ||
IframeJs3PCookieAllowed) { | ||
AllowCookies(); | ||
ui_test_utils::NavigateToURL(browser(), nested_iframe_script_url_); | ||
const std::string cookie = | ||
content::GetCookies(browser()->profile(), GURL("http://c.com/")); | ||
EXPECT_FALSE(cookie.empty()); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,9 +107,9 @@ void GetRenderFrameInfo(const URLRequest* request, | |
if (request_info) { | ||
*frame_tree_node_id = request_info->GetFrameTreeNodeId(); | ||
} | ||
extensions::ExtensionApiFrameIdMap::FrameData frame_data; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dead code |
||
if (!content::ResourceRequestInfo::GetRenderFrameForRequest( | ||
request, render_process_id, render_frame_id)) { | ||
|
||
const content::WebSocketHandshakeRequestInfo* websocket_info = | ||
content::WebSocketHandshakeRequestInfo::ForRequest(request); | ||
if (websocket_info) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,7 +94,9 @@ namespace brave_shields { | |
|
||
base::Lock BraveShieldsWebContentsObserver::frame_data_map_lock_; | ||
std::map<BraveShieldsWebContentsObserver::RenderFrameIdKey, GURL> | ||
BraveShieldsWebContentsObserver::render_frame_key_to_tab_url; | ||
BraveShieldsWebContentsObserver::frame_key_to_tab_url_; | ||
std::map<int, GURL> | ||
BraveShieldsWebContentsObserver::frame_tree_node_id_to_tab_url_; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Additional map for tracking frame tree node ids. They are available on the IO thread for frames/subframes instead of the pair procces_id/frame_id |
||
|
||
BraveShieldsWebContentsObserver::RenderFrameIdKey::RenderFrameIdKey() | ||
: render_process_id(content::ChildProcessHost::kInvalidUniqueID), | ||
|
@@ -136,27 +138,21 @@ void BraveShieldsWebContentsObserver::RenderFrameCreated( | |
WebContents* web_contents = WebContents::FromRenderFrameHost(rfh); | ||
if (web_contents) { | ||
UpdateContentSettingsToRendererFrames(web_contents); | ||
|
||
base::AutoLock lock(frame_data_map_lock_); | ||
const RenderFrameIdKey key(rfh->GetProcess()->GetID(), rfh->GetRoutingID()); | ||
std::map<RenderFrameIdKey, GURL>::iterator iter = | ||
render_frame_key_to_tab_url.find(key); | ||
if (iter != render_frame_key_to_tab_url.end()) { | ||
render_frame_key_to_tab_url.erase(key); | ||
} | ||
render_frame_key_to_tab_url.insert( | ||
std::pair<RenderFrameIdKey, GURL>(key, web_contents->GetURL())); | ||
frame_key_to_tab_url_[key] = web_contents->GetURL(); | ||
frame_tree_node_id_to_tab_url_[rfh->GetFrameTreeNodeId()] = | ||
web_contents->GetURL(); | ||
} | ||
} | ||
|
||
void BraveShieldsWebContentsObserver::RenderFrameDeleted( | ||
RenderFrameHost* rfh) { | ||
base::AutoLock lock(frame_data_map_lock_); | ||
const RenderFrameIdKey key(rfh->GetProcess()->GetID(), rfh->GetRoutingID()); | ||
std::map<RenderFrameIdKey, GURL>::iterator iter = | ||
render_frame_key_to_tab_url.find(key); | ||
if (iter != render_frame_key_to_tab_url.end()) { | ||
render_frame_key_to_tab_url.erase(key); | ||
} | ||
frame_key_to_tab_url_.erase(key); | ||
frame_tree_node_id_to_tab_url_.erase(rfh->GetFrameTreeNodeId()); | ||
} | ||
|
||
void BraveShieldsWebContentsObserver::RenderFrameHostChanged( | ||
|
@@ -169,15 +165,37 @@ void BraveShieldsWebContentsObserver::RenderFrameHostChanged( | |
} | ||
} | ||
|
||
void BraveShieldsWebContentsObserver::DidFinishNavigation( | ||
content::NavigationHandle* navigation_handle) { | ||
RenderFrameHost* main_frame = web_contents()->GetMainFrame(); | ||
if (!web_contents() || !main_frame) { | ||
return; | ||
} | ||
int process_id = main_frame->GetProcess()->GetID(); | ||
int routing_id = main_frame->GetRoutingID(); | ||
int tree_node_id = main_frame->GetFrameTreeNodeId(); | ||
|
||
base::AutoLock lock(frame_data_map_lock_); | ||
frame_key_to_tab_url_[{process_id, routing_id}] = web_contents()->GetURL(); | ||
frame_tree_node_id_to_tab_url_[tree_node_id] = web_contents()->GetURL(); | ||
} | ||
|
||
// static | ||
GURL BraveShieldsWebContentsObserver::GetTabURLFromRenderFrameInfo( | ||
int render_process_id, int render_frame_id) { | ||
int render_process_id, int render_frame_id, int render_frame_tree_node_id) { | ||
base::AutoLock lock(frame_data_map_lock_); | ||
const RenderFrameIdKey key(render_process_id, render_frame_id); | ||
std::map<RenderFrameIdKey, GURL>::iterator iter = | ||
render_frame_key_to_tab_url.find(key); | ||
if (iter != render_frame_key_to_tab_url.end()) { | ||
return iter->second; | ||
if (-1 != render_process_id && -1 != render_frame_id) { | ||
auto iter = frame_key_to_tab_url_.find({render_process_id, | ||
render_frame_id}); | ||
if (iter != frame_key_to_tab_url_.end()) { | ||
return iter->second; | ||
} | ||
} | ||
if (-1 != render_frame_tree_node_id) { | ||
auto iter2 = frame_tree_node_id_to_tab_url_.find(render_frame_tree_node_id); | ||
if (iter2 != frame_tree_node_id_to_tab_url_.end()) { | ||
return iter2->second; | ||
} | ||
} | ||
return GURL(); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo - wrong order of
render_process_id
andrender_frame_id