component_extension_url_pattern_unittest.cc 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright (c) 2018 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 "content/public/test/test_utils.h"
  5. #include "extensions/common/constants.h"
  6. #include "extensions/common/extension.h"
  7. #include "extensions/common/extension_builder.h"
  8. #include "extensions/common/manifest.h"
  9. #include "extensions/common/permissions/permissions_data.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. #include "url/gurl.h"
  12. namespace extensions {
  13. namespace {
  14. constexpr int kTabId = 42;
  15. }
  16. TEST(ComponentExtensionUrlPattern, AllUrls) {
  17. // Component extensions do not have access to "chrome" scheme URLs through
  18. // the "<all_urls>" meta-pattern.
  19. auto all_urls = ExtensionBuilder("all urls")
  20. .AddPermission("<all_urls>")
  21. .SetLocation(mojom::ManifestLocation::kComponent)
  22. .Build();
  23. std::string error;
  24. EXPECT_FALSE(all_urls->permissions_data()->CanAccessPage(
  25. content::GetWebUIURL("settings"), kTabId, &error))
  26. << error;
  27. // Non-chrome scheme should be fine.
  28. EXPECT_TRUE(all_urls->permissions_data()->CanAccessPage(
  29. GURL("https://example.com"), kTabId, &error))
  30. << error;
  31. }
  32. TEST(ComponentExtensionUrlPattern, ChromeVoxExtension) {
  33. // The ChromeVox extension has access to "chrome" scheme URLs through the
  34. // "<all_urls>" meta-pattern because it's allowlisted.
  35. auto all_urls = ExtensionBuilder("all urls")
  36. .AddPermission("<all_urls>")
  37. .SetLocation(mojom::ManifestLocation::kComponent)
  38. .SetID(extension_misc::kChromeVoxExtensionId)
  39. .Build();
  40. std::string error;
  41. EXPECT_TRUE(all_urls->permissions_data()->CanAccessPage(
  42. content::GetWebUIURL("settings"), kTabId, &error))
  43. << error;
  44. }
  45. TEST(ComponentExtensionUrlPattern, ExplicitChromeUrl) {
  46. // Explicitly specifying a pattern that allows access to the chrome
  47. // scheme is OK.
  48. auto chrome_urls = ExtensionBuilder("chrome urls")
  49. .AddPermission(content::GetWebUIURLString("*/*"))
  50. .SetLocation(mojom::ManifestLocation::kComponent)
  51. .Build();
  52. std::string error;
  53. EXPECT_TRUE(chrome_urls->permissions_data()->CanAccessPage(
  54. content::GetWebUIURL("settings"), kTabId, &error))
  55. << error;
  56. }
  57. } // namespace extensions