Skip to content

Commit

Permalink
Match exact host in autoplay whitelist before matching eTLD+1
Browse files Browse the repository at this point in the history
tests
  • Loading branch information
pilgrim-brave committed Feb 26, 2020
1 parent 9a76aba commit e7c2198
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 7 deletions.
65 changes: 58 additions & 7 deletions browser/autoplay/autoplay_permission_context_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,15 @@ class AutoplayWhitelistServiceTest : public BaseLocalDataFilesBrowserTest {

void SetUpOnMainThread() override {
BaseLocalDataFilesBrowserTest::SetUpOnMainThread();
whitelist_autoplay_url_ =
embedded_test_server()->GetURL("example.com", "/autoplay_by_attr.html");
// exact host match
whitelist_autoplay_urls_.push_back(embedded_test_server()->GetURL(
"example.com", "/autoplay_by_attr.html"));
// eTLD+1 match
whitelist_autoplay_urls_.push_back(embedded_test_server()->GetURL(
"sub.example.com", "/autoplay_by_attr.html"));
// exact host match with subdomain
whitelist_autoplay_urls_.push_back(embedded_test_server()->GetURL(
"sandbox.uphold.com", "/autoplay_by_attr.html"));
}

// BaseLocalDataFilesBrowserTest overrides
Expand Down Expand Up @@ -179,10 +186,12 @@ class AutoplayWhitelistServiceTest : public BaseLocalDataFilesBrowserTest {
ASSERT_EQ("PLAYING", msg_from_renderer);
}

const GURL& whitelist_autoplay_url() { return whitelist_autoplay_url_; }
const GURL& whitelist_autoplay_url(int index) {
return whitelist_autoplay_urls_[index];
}

private:
GURL whitelist_autoplay_url_;
std::vector<GURL> whitelist_autoplay_urls_;
};

// Autoplay blocks by default, no bubble is shown
Expand Down Expand Up @@ -553,16 +562,58 @@ IN_PROC_BROWSER_TEST_F(AutoplayPermissionContextBrowserTest, FileAutoplay) {
EXPECT_EQ(result, kVideoPlaying);
}

// Default allow autoplay on URLs in whitelist
IN_PROC_BROWSER_TEST_F(AutoplayWhitelistServiceTest, Allow) {
// Default allow autoplay on URLs in whitelist if host matches exactly
IN_PROC_BROWSER_TEST_F(AutoplayWhitelistServiceTest, AllowIfExactHostMatch) {
ASSERT_TRUE(InstallMockExtension());
std::string result;
PermissionRequestManager* manager =
PermissionRequestManager::FromWebContents(contents());
auto popup_prompt_factory =
std::make_unique<MockPermissionPromptFactory>(manager);

NavigateToURLUntilLoadStop(whitelist_autoplay_url(0));
EXPECT_FALSE(popup_prompt_factory->is_visible());
EXPECT_FALSE(popup_prompt_factory->RequestTypeSeen(
PermissionRequestType::PERMISSION_AUTOPLAY));
EXPECT_EQ(0, popup_prompt_factory->TotalRequestCount());
WaitForPlaying();
EXPECT_TRUE(
ExecuteScriptAndExtractString(contents(), kVideoPlayingDetect, &result));
EXPECT_EQ(result, kVideoPlaying);
}

// Default allow autoplay on URLs in whitelist if eTLD+1 matches
IN_PROC_BROWSER_TEST_F(AutoplayWhitelistServiceTest, AllowIfETLDPlusOneMatch) {
ASSERT_TRUE(InstallMockExtension());
std::string result;
PermissionRequestManager* manager =
PermissionRequestManager::FromWebContents(contents());
auto popup_prompt_factory =
std::make_unique<MockPermissionPromptFactory>(manager);

NavigateToURLUntilLoadStop(whitelist_autoplay_url(1));
EXPECT_FALSE(popup_prompt_factory->is_visible());
EXPECT_FALSE(popup_prompt_factory->RequestTypeSeen(
PermissionRequestType::PERMISSION_AUTOPLAY));
EXPECT_EQ(0, popup_prompt_factory->TotalRequestCount());
WaitForPlaying();
EXPECT_TRUE(
ExecuteScriptAndExtractString(contents(), kVideoPlayingDetect, &result));
EXPECT_EQ(result, kVideoPlaying);
}

// Default allow autoplay on URLs in whitelist if multi-domain host matches
// exactly
IN_PROC_BROWSER_TEST_F(AutoplayWhitelistServiceTest,
AllowIfExactSubdomainAndHostMatch) {
ASSERT_TRUE(InstallMockExtension());
std::string result;
PermissionRequestManager* manager =
PermissionRequestManager::FromWebContents(contents());
auto popup_prompt_factory =
std::make_unique<MockPermissionPromptFactory>(manager);

NavigateToURLUntilLoadStop(whitelist_autoplay_url());
NavigateToURLUntilLoadStop(whitelist_autoplay_url(2));
EXPECT_FALSE(popup_prompt_factory->is_visible());
EXPECT_FALSE(popup_prompt_factory->RequestTypeSeen(
PermissionRequestType::PERMISSION_AUTOPLAY));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ AutoplayWhitelistService::~AutoplayWhitelistService() {

bool AutoplayWhitelistService::ShouldAllowAutoplay(const GURL& url) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// look for exact host match (this is the only way that subdomains
// listed in the autoplay whitelist will match)
if (url.has_host() &&
autoplay_whitelist_client_->matchesHost(url.host().c_str()))
return true;
// look for match of eTLD+1
std::string etld_plus_one =
net::registry_controlled_domains::GetDomainAndRegistry(
url, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
Expand Down
Binary file modified test/data/autoplay-whitelist-data/1/AutoplayWhitelist.dat
Binary file not shown.

0 comments on commit e7c2198

Please sign in to comment.