aw_safe_browsing_allowlist_manager_unittest.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "android_webview/browser/safe_browsing/aw_safe_browsing_allowlist_manager.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/bind.h"
  8. #include "base/callback.h"
  9. #include "base/run_loop.h"
  10. #include "base/test/task_environment.h"
  11. #include "base/threading/thread_task_runner_handle.h"
  12. #include "base/values.h"
  13. #include "testing/gtest/include/gtest/gtest.h"
  14. #include "url/gurl.h"
  15. namespace android_webview {
  16. class AwSafeBrowsingAllowlistManagerTest : public testing::Test {
  17. protected:
  18. AwSafeBrowsingAllowlistManagerTest() {}
  19. void SetUp() override {
  20. wm_ = std::make_unique<AwSafeBrowsingAllowlistManager>(
  21. base::ThreadTaskRunnerHandle::Get(),
  22. base::ThreadTaskRunnerHandle::Get());
  23. }
  24. void TearDown() override { wm_.reset(); }
  25. void SetAllowlist(std::vector<std::string>&& allowlist, bool expected);
  26. base::test::SingleThreadTaskEnvironment task_environment_{
  27. base::test::SingleThreadTaskEnvironment::MainThreadType::IO};
  28. std::unique_ptr<AwSafeBrowsingAllowlistManager> wm_;
  29. };
  30. void VerifyAllowlistCallback(bool expected, bool success) {
  31. EXPECT_EQ(expected, success);
  32. }
  33. void AwSafeBrowsingAllowlistManagerTest::SetAllowlist(
  34. std::vector<std::string>&& allowlist,
  35. bool expected) {
  36. wm_->SetAllowlistOnUIThread(
  37. std::move(allowlist), base::BindOnce(&VerifyAllowlistCallback, expected));
  38. }
  39. TEST_F(AwSafeBrowsingAllowlistManagerTest, WsSchemeCanBeAllowlisted) {
  40. std::vector<std::string> allowlist;
  41. allowlist.push_back("google.com");
  42. SetAllowlist(std::move(allowlist), true);
  43. base::RunLoop().RunUntilIdle();
  44. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("ws://google.com")));
  45. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("wss://google.com")));
  46. }
  47. TEST_F(AwSafeBrowsingAllowlistManagerTest, HttpSchemeCanBeAllowlisted) {
  48. std::vector<std::string> allowlist;
  49. allowlist.push_back("google.com");
  50. SetAllowlist(std::move(allowlist), true);
  51. base::RunLoop().RunUntilIdle();
  52. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://google.com")));
  53. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("https://google.com")));
  54. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://google.com:80")));
  55. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://google.com:123")));
  56. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://google.com:443")));
  57. }
  58. TEST_F(AwSafeBrowsingAllowlistManagerTest, WsSchemeCanBeAllowlistedExactMatch) {
  59. std::vector<std::string> allowlist;
  60. allowlist.push_back(".google.com");
  61. SetAllowlist(std::move(allowlist), true);
  62. base::RunLoop().RunUntilIdle();
  63. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("ws://google.com")));
  64. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("wss://google.com")));
  65. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("ws://google.com:80")));
  66. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("ws://google.com:123")));
  67. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("ws://google.com:443")));
  68. }
  69. TEST_F(AwSafeBrowsingAllowlistManagerTest, ExactMatchWorks) {
  70. std::vector<std::string> allowlist;
  71. allowlist.push_back(".google.com");
  72. SetAllowlist(std::move(allowlist), true);
  73. base::RunLoop().RunUntilIdle();
  74. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://google.com")));
  75. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("https://google.com")));
  76. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("ws://google.com")));
  77. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("wss://google.com")));
  78. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("ws://a.google.com")));
  79. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("wss://a.google.com")));
  80. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("ws://com")));
  81. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("wss://oogle.com")));
  82. }
  83. TEST_F(AwSafeBrowsingAllowlistManagerTest, SchemeInAllowlistIsInvalid) {
  84. std::vector<std::string> allowlist;
  85. allowlist.push_back("http://google.com");
  86. SetAllowlist(std::move(allowlist), false);
  87. base::RunLoop().RunUntilIdle();
  88. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("http://google.com")));
  89. }
  90. TEST_F(AwSafeBrowsingAllowlistManagerTest,
  91. NonStandardSchemeInAllowlistIsInvalid) {
  92. std::vector<std::string> allowlist;
  93. allowlist.push_back("data:google.com");
  94. allowlist.push_back("mailto:google.com");
  95. SetAllowlist(std::move(allowlist), false);
  96. base::RunLoop().RunUntilIdle();
  97. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("http://google.com")));
  98. }
  99. TEST_F(AwSafeBrowsingAllowlistManagerTest, PortInAllowlistIsInvalid) {
  100. std::vector<std::string> allowlist;
  101. allowlist.push_back("www.google.com:123");
  102. SetAllowlist(std::move(allowlist), false);
  103. base::RunLoop().RunUntilIdle();
  104. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("http://www.google.com")));
  105. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("http://www.google.com:123")));
  106. }
  107. TEST_F(AwSafeBrowsingAllowlistManagerTest, PathInAllowlistIsInvalid) {
  108. std::vector<std::string> allowlist;
  109. allowlist.push_back("www.google.com/123");
  110. SetAllowlist(std::move(allowlist), false);
  111. base::RunLoop().RunUntilIdle();
  112. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("http://www.google.com/123")));
  113. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("http://www.google.com")));
  114. }
  115. TEST_F(AwSafeBrowsingAllowlistManagerTest, PathQueryAndReferenceWorks) {
  116. std::vector<std::string> allowlist;
  117. allowlist.push_back("google.com");
  118. SetAllowlist(std::move(allowlist), true);
  119. base::RunLoop().RunUntilIdle();
  120. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://google.com/a")));
  121. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://google.com/a/b")));
  122. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://google.com?test=1")));
  123. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://google.com/a#a100")));
  124. }
  125. TEST_F(AwSafeBrowsingAllowlistManagerTest, TrailingDotInRuleWorks) {
  126. std::vector<std::string> allowlist;
  127. allowlist.push_back("google.com.");
  128. allowlist.push_back("example.com");
  129. SetAllowlist(std::move(allowlist), true);
  130. base::RunLoop().RunUntilIdle();
  131. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://google.com")));
  132. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://example.com.")));
  133. }
  134. TEST_F(AwSafeBrowsingAllowlistManagerTest, DomainNameEmbeddedInPathIsIgnored) {
  135. std::vector<std::string> allowlist;
  136. allowlist.push_back("google.com");
  137. SetAllowlist(std::move(allowlist), true);
  138. base::RunLoop().RunUntilIdle();
  139. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("http://example.com/google.com")));
  140. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://google.com")));
  141. }
  142. TEST_F(AwSafeBrowsingAllowlistManagerTest, URLsWithEmbeddedUserNamePassword) {
  143. std::vector<std::string> allowlist;
  144. allowlist.push_back("google.com");
  145. SetAllowlist(std::move(allowlist), true);
  146. base::RunLoop().RunUntilIdle();
  147. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://user1:pass@google.com")));
  148. }
  149. TEST_F(AwSafeBrowsingAllowlistManagerTest, AllowlistsWithPunycodeWorks) {
  150. std::vector<std::string> allowlist;
  151. allowlist.push_back("㯙㯜㯙㯟.com");
  152. SetAllowlist(std::move(allowlist), true);
  153. base::RunLoop().RunUntilIdle();
  154. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://xn--domain.com")));
  155. }
  156. TEST_F(AwSafeBrowsingAllowlistManagerTest,
  157. PathQueryAndReferenceWorksWithLeadingDot) {
  158. std::vector<std::string> allowlist;
  159. allowlist.push_back(".google.com");
  160. SetAllowlist(std::move(allowlist), true);
  161. base::RunLoop().RunUntilIdle();
  162. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://google.com/")));
  163. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://google.com/a")));
  164. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://google.com/a/b")));
  165. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://google.com?test=1")));
  166. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://google.com/a#a100")));
  167. }
  168. TEST_F(AwSafeBrowsingAllowlistManagerTest,
  169. SubdomainsAreAllowedWhenNoLeadingDots) {
  170. std::vector<std::string> allowlist;
  171. allowlist.push_back("google.com");
  172. SetAllowlist(std::move(allowlist), true);
  173. base::RunLoop().RunUntilIdle();
  174. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("http://com/")));
  175. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://google.com")));
  176. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://a.google.com/")));
  177. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://b.a.google.com/")));
  178. }
  179. TEST_F(AwSafeBrowsingAllowlistManagerTest,
  180. SubdomainsAreNotAllowedWhenLeadingDots) {
  181. std::vector<std::string> allowlist;
  182. allowlist.push_back(".google.com");
  183. SetAllowlist(std::move(allowlist), true);
  184. base::RunLoop().RunUntilIdle();
  185. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("http://com/")));
  186. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://google.com")));
  187. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("http://a.google.com/")));
  188. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("http://b.a.google.com/")));
  189. }
  190. TEST_F(AwSafeBrowsingAllowlistManagerTest,
  191. MatchSubdomainsInMultipleAllowlists) {
  192. std::vector<std::string> allowlist;
  193. allowlist.push_back("a.google.com");
  194. allowlist.push_back(".google.com");
  195. SetAllowlist(std::move(allowlist), true);
  196. base::RunLoop().RunUntilIdle();
  197. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://a.google.com/")));
  198. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://b.a.google.com/")));
  199. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://google.com/")));
  200. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("http://b.google.com/")));
  201. }
  202. TEST_F(AwSafeBrowsingAllowlistManagerTest, TestLeadingDotInGURL) {
  203. std::vector<std::string> allowlist;
  204. allowlist.push_back("a.google.com");
  205. allowlist.push_back(".google.com");
  206. SetAllowlist(std::move(allowlist), true);
  207. base::RunLoop().RunUntilIdle();
  208. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://.a.google.com/")));
  209. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://.b.a.google.com/")));
  210. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://.google.com/")));
  211. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("http://.b.google.com/")));
  212. }
  213. TEST_F(AwSafeBrowsingAllowlistManagerTest, VerifyTLDsAreNotSpecial) {
  214. std::vector<std::string> allowlist;
  215. allowlist.push_back(".com");
  216. allowlist.push_back("co");
  217. SetAllowlist(std::move(allowlist), true);
  218. base::RunLoop().RunUntilIdle();
  219. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("http://a.google.com/")));
  220. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://b.a.google.co/")));
  221. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://com/")));
  222. }
  223. // It seems GURL is happy to accept "*" in hostname literal. Since we rely
  224. // on GURL host validation, just be consistent on that but make sure
  225. // that does not wildcard all the domains.
  226. TEST_F(AwSafeBrowsingAllowlistManagerTest, VerifyStarDoesNotWildcardDomains) {
  227. std::vector<std::string> allowlist;
  228. allowlist.push_back("*.com");
  229. allowlist.push_back("*co");
  230. allowlist.push_back("b.a.*.co");
  231. allowlist.push_back("b.*.*.co");
  232. allowlist.push_back("b.*");
  233. allowlist.push_back("c*");
  234. SetAllowlist(std::move(allowlist), true);
  235. base::RunLoop().RunUntilIdle();
  236. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("http://a.google.com/")));
  237. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("http://b.a.google.co/")));
  238. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("http://com/")));
  239. allowlist.clear();
  240. allowlist.push_back("*");
  241. SetAllowlist(std::move(allowlist), true);
  242. base::RunLoop().RunUntilIdle();
  243. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("http://a.google.com/")));
  244. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://*/")));
  245. }
  246. TEST_F(AwSafeBrowsingAllowlistManagerTest, VerifyPrefixOrSuffixOfDomains) {
  247. std::vector<std::string> allowlist;
  248. allowlist.push_back("google.com");
  249. SetAllowlist(std::move(allowlist), true);
  250. base::RunLoop().RunUntilIdle();
  251. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("http://ogle.com/")));
  252. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("http://agoogle.com/")));
  253. }
  254. TEST_F(AwSafeBrowsingAllowlistManagerTest, VerifyIPV4CanBeAllowlisted) {
  255. std::vector<std::string> allowlist;
  256. allowlist.push_back("google.com");
  257. allowlist.push_back("192.168.1.1");
  258. SetAllowlist(std::move(allowlist), true);
  259. base::RunLoop().RunUntilIdle();
  260. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://google.com/")));
  261. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://192.168.1.1/")));
  262. }
  263. TEST_F(AwSafeBrowsingAllowlistManagerTest, VerifyIPV4IsNotSegmented) {
  264. std::vector<std::string> allowlist;
  265. allowlist.push_back("192.168.1.1");
  266. SetAllowlist(std::move(allowlist), true);
  267. base::RunLoop().RunUntilIdle();
  268. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://192.168.1.1/")));
  269. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("http://1.192.168.1.1/")));
  270. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("http://192.168.1.0/")));
  271. }
  272. TEST_F(AwSafeBrowsingAllowlistManagerTest, VerifyLeadingDotInIPV4IsNotValid) {
  273. std::vector<std::string> allowlist;
  274. allowlist.push_back(".192.168.1.1");
  275. SetAllowlist(std::move(allowlist), false);
  276. base::RunLoop().RunUntilIdle();
  277. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("http://192.168.1.1/")));
  278. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("http://1.192.168.1.1/")));
  279. }
  280. TEST_F(AwSafeBrowsingAllowlistManagerTest, VerifyMultipleIPV4Works) {
  281. std::vector<std::string> allowlist;
  282. allowlist.push_back("192.168.1.1");
  283. allowlist.push_back("192.168.1.2");
  284. allowlist.push_back("194.168.1.1");
  285. SetAllowlist(std::move(allowlist), true);
  286. base::RunLoop().RunUntilIdle();
  287. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://192.168.1.1/")));
  288. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://192.168.1.2/")));
  289. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://194.168.1.1/")));
  290. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("https://194.168.1.1/")));
  291. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://194.168.1.1:443/")));
  292. }
  293. TEST_F(AwSafeBrowsingAllowlistManagerTest, VerifyIPV6CanBeAllowlisted) {
  294. std::vector<std::string> allowlist;
  295. allowlist.push_back("[10:20:30:40:50:60:70:80]");
  296. SetAllowlist(std::move(allowlist), true);
  297. base::RunLoop().RunUntilIdle();
  298. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://[10:20:30:40:50:60:70:80]")));
  299. }
  300. TEST_F(AwSafeBrowsingAllowlistManagerTest,
  301. VerifyIPV6CannotBeAllowlistedIfBroken) {
  302. std::vector<std::string> allowlist;
  303. allowlist.push_back("[10:20:30:40:50:60:]");
  304. SetAllowlist(std::move(allowlist), false);
  305. base::RunLoop().RunUntilIdle();
  306. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("http://[10:20:30:40:50:60:70:80]")));
  307. }
  308. TEST_F(AwSafeBrowsingAllowlistManagerTest,
  309. VerifyIPV6WithZerosCanBeAllowlisted) {
  310. std::vector<std::string> allowlist;
  311. allowlist.push_back("[20:0:0:0:0:0:0:0]");
  312. SetAllowlist(std::move(allowlist), true);
  313. base::RunLoop().RunUntilIdle();
  314. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://[20:0:0:0:0:0:0:0]")));
  315. }
  316. TEST_F(AwSafeBrowsingAllowlistManagerTest, VerifyCapitalizationDoesNotMatter) {
  317. std::vector<std::string> allowlist;
  318. allowlist.push_back("A.goOGle.Com");
  319. allowlist.push_back(".GOOGLE.COM");
  320. SetAllowlist(std::move(allowlist), true);
  321. base::RunLoop().RunUntilIdle();
  322. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://a.google.com/")));
  323. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://b.a.google.com/")));
  324. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://google.com/")));
  325. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("http://b.google.com/")));
  326. }
  327. TEST_F(AwSafeBrowsingAllowlistManagerTest,
  328. VerifyAllowlistingWorksWhenDomainSuffixesMatch1) {
  329. std::vector<std::string> allowlist;
  330. allowlist.push_back("com");
  331. allowlist.push_back("example.com");
  332. SetAllowlist(std::move(allowlist), true);
  333. base::RunLoop().RunUntilIdle();
  334. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://a.google.com/")));
  335. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://example.com/")));
  336. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://a.example.com/")));
  337. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://com/")));
  338. }
  339. // Same as verifyAllowlistingWorksWhenDomainSuffixesMatch1 but order reversed.
  340. TEST_F(AwSafeBrowsingAllowlistManagerTest,
  341. VerifyAllowlistingWorksWhenDomainSuffixesMatch2) {
  342. std::vector<std::string> allowlist;
  343. allowlist.push_back("example.com");
  344. allowlist.push_back("com");
  345. SetAllowlist(std::move(allowlist), true);
  346. base::RunLoop().RunUntilIdle();
  347. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://a.google.com/")));
  348. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://example.com/")));
  349. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://a.example.com/")));
  350. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://com/")));
  351. }
  352. TEST_F(AwSafeBrowsingAllowlistManagerTest,
  353. VerifyAllowlistingWorksWhenDomainSuffixesMatchWithLeadingDots1) {
  354. std::vector<std::string> allowlist;
  355. allowlist.push_back(".com");
  356. allowlist.push_back("example.com");
  357. SetAllowlist(std::move(allowlist), true);
  358. base::RunLoop().RunUntilIdle();
  359. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("http://a.google.com/")));
  360. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://example.com/")));
  361. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://a.example.com/")));
  362. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://com/")));
  363. }
  364. // Same as VerifyAllowlistingWorksWhenDomainSuffixesMatchWithLeadingDots2
  365. // but order reversed.
  366. TEST_F(AwSafeBrowsingAllowlistManagerTest,
  367. VerifyAllowlistingWorksWhenDomainSuffixesMatchWithLeadingDots2) {
  368. std::vector<std::string> allowlist;
  369. allowlist.push_back("example.com");
  370. allowlist.push_back(".com");
  371. SetAllowlist(std::move(allowlist), true);
  372. base::RunLoop().RunUntilIdle();
  373. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("http://a.google.com/")));
  374. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://example.com/")));
  375. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://a.example.com/")));
  376. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://com/")));
  377. }
  378. // Verify that a more general rule won't be rendered useless by a rule that
  379. // more closely matches. For example if "com" is a rule to allowlist all com
  380. // subdomains, a later rule for .example.com should not make a.example.com a
  381. // no match.
  382. TEST_F(AwSafeBrowsingAllowlistManagerTest,
  383. VerifyAnExactMatchRuleCanBeOverwrittenByAMoreGeneralNonExactMatchRule) {
  384. std::vector<std::string> allowlist;
  385. allowlist.push_back("com");
  386. allowlist.push_back(".example.com");
  387. SetAllowlist(std::move(allowlist), true);
  388. base::RunLoop().RunUntilIdle();
  389. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://a.example.com/")));
  390. }
  391. TEST_F(AwSafeBrowsingAllowlistManagerTest,
  392. VerifySubdomainMatchWinsIfRuleIsEnteredWithAndWithoutSubdomainMatch) {
  393. std::vector<std::string> allowlist;
  394. allowlist.push_back("example.com");
  395. allowlist.push_back(".example.com");
  396. SetAllowlist(std::move(allowlist), true);
  397. base::RunLoop().RunUntilIdle();
  398. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://a.example.com/")));
  399. allowlist = std::vector<std::string>();
  400. allowlist.push_back(".example.com");
  401. allowlist.push_back("example.com");
  402. SetAllowlist(std::move(allowlist), true);
  403. base::RunLoop().RunUntilIdle();
  404. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://a.example.com/")));
  405. }
  406. TEST_F(AwSafeBrowsingAllowlistManagerTest,
  407. VerifyOrderOfRuleEntryDoesNotChangeExpectations) {
  408. std::vector<std::string> allowlist;
  409. allowlist.push_back("b.example.com");
  410. allowlist.push_back(".example.com");
  411. allowlist.push_back("example.com");
  412. allowlist.push_back("a.example.com");
  413. SetAllowlist(std::move(allowlist), true);
  414. base::RunLoop().RunUntilIdle();
  415. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://a.example.com/")));
  416. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://b.example.com/")));
  417. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://example.com/")));
  418. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("http://com/")));
  419. allowlist = std::vector<std::string>();
  420. allowlist.push_back("a.example.com");
  421. allowlist.push_back("example.com");
  422. allowlist.push_back(".example.com");
  423. allowlist.push_back("b.example.com");
  424. SetAllowlist(std::move(allowlist), true);
  425. base::RunLoop().RunUntilIdle();
  426. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://a.example.com/")));
  427. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://b.example.com/")));
  428. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://example.com/")));
  429. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("http://com/")));
  430. }
  431. TEST_F(AwSafeBrowsingAllowlistManagerTest, VerifyInvalidUrlsAreNotAllowlisted) {
  432. std::vector<std::string> allowlist;
  433. allowlist.push_back("google.com");
  434. SetAllowlist(std::move(allowlist), true);
  435. base::RunLoop().RunUntilIdle();
  436. GURL url = GURL("");
  437. EXPECT_FALSE(url.is_valid());
  438. EXPECT_FALSE(wm_->IsUrlAllowed(url));
  439. url = GURL("http;??www.google.com");
  440. EXPECT_FALSE(url.is_valid());
  441. EXPECT_FALSE(wm_->IsUrlAllowed(url));
  442. }
  443. TEST_F(AwSafeBrowsingAllowlistManagerTest,
  444. VerifyUrlsWithoutHostAreNotAllowlisted) {
  445. std::vector<std::string> allowlist;
  446. allowlist.push_back("google.com");
  447. SetAllowlist(std::move(allowlist), true);
  448. base::RunLoop().RunUntilIdle();
  449. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("file:///google.com/test")));
  450. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("mailto:google.com/")));
  451. EXPECT_FALSE(wm_->IsUrlAllowed(GURL("data:google.com/")));
  452. // However FTP, HTTP and WS should work
  453. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("ftp://google.com/")));
  454. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("http://google.com/")));
  455. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("ws://google.com/")));
  456. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("https://google.com/")));
  457. EXPECT_TRUE(wm_->IsUrlAllowed(GURL("wss://google.com/")));
  458. }
  459. } // namespace android_webview