enterprise_authentication_app_link_policy_handler.cc 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2022 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/enterprise_authentication_app_link_policy_handler.h"
  5. #include <memory>
  6. #include "base/strings/string_util.h"
  7. #include "components/policy/core/common/policy_pref_names.h"
  8. #include "components/policy/policy_constants.h"
  9. #include "components/strings/grit/components_strings.h"
  10. #include "url/gurl.h"
  11. namespace policy {
  12. EnterpriseAuthenticationAppLinkPolicyHandler::
  13. EnterpriseAuthenticationAppLinkPolicyHandler(const char* policy_name,
  14. const char* pref_path)
  15. : TypeCheckingPolicyHandler(policy_name, base::Value::Type::LIST),
  16. pref_path_(pref_path) {}
  17. EnterpriseAuthenticationAppLinkPolicyHandler::
  18. ~EnterpriseAuthenticationAppLinkPolicyHandler() = default;
  19. bool EnterpriseAuthenticationAppLinkPolicyHandler::CheckPolicySettings(
  20. const PolicyMap& policies,
  21. PolicyErrorMap* errors) {
  22. if (!TypeCheckingPolicyHandler::CheckPolicySettings(policies, errors))
  23. return false;
  24. const base::Value* value =
  25. policies.GetValue(policy_name(), base::Value::Type::LIST);
  26. if (!value || value->GetList().empty())
  27. return true;
  28. // Filters more than |url_util::kMaxFiltersPerPolicy| are ignored, add a
  29. // warning message.
  30. if (value->GetList().size() > policy::kMaxUrlFiltersPerPolicy) {
  31. errors->AddError(policy_name(),
  32. IDS_POLICY_URL_ALLOW_BLOCK_LIST_MAX_FILTERS_LIMIT_WARNING,
  33. base::NumberToString(policy::kMaxUrlFiltersPerPolicy));
  34. }
  35. std::vector<std::string> invalid_policies;
  36. for (const auto& entry : value->GetList()) {
  37. const std::string* url = entry.FindStringKey("url");
  38. if (!url) {
  39. invalid_policies.push_back(
  40. "Invalid policy: Required key 'url' does not exists");
  41. } else if (!ValidatePolicyEntry(url)) {
  42. invalid_policies.push_back("Invalid url: " + *url);
  43. }
  44. }
  45. if (!invalid_policies.empty()) {
  46. errors->AddError(policy_name(), IDS_POLICY_PROTO_PARSING_ERROR,
  47. base::JoinString(invalid_policies, ","));
  48. }
  49. return invalid_policies.size() < value->GetList().size();
  50. }
  51. void EnterpriseAuthenticationAppLinkPolicyHandler::ApplyPolicySettings(
  52. const PolicyMap& policies,
  53. PrefValueMap* prefs) {
  54. const base::Value* value =
  55. policies.GetValue(policy_name(), base::Value::Type::LIST);
  56. if (!value)
  57. return;
  58. base::Value::List filtered_values;
  59. for (const auto& entry : value->GetList()) {
  60. const std::string* url = entry.FindStringKey("url");
  61. if (ValidatePolicyEntry(url))
  62. filtered_values.Append(*url);
  63. }
  64. if (filtered_values.size() > policy::kMaxUrlFiltersPerPolicy) {
  65. filtered_values.erase(
  66. filtered_values.begin() + policy::kMaxUrlFiltersPerPolicy,
  67. filtered_values.end());
  68. }
  69. prefs->SetValue(pref_path_, base::Value(std::move(filtered_values)));
  70. }
  71. // Validates that policy follows official pattern
  72. // https://www.chromium.org/administrators/url-blocklist-filter-format
  73. bool EnterpriseAuthenticationAppLinkPolicyHandler::ValidatePolicyEntry(
  74. const std::string* policy) {
  75. url_matcher::util::FilterComponents components;
  76. return policy && url_matcher::util::FilterToComponents(
  77. *policy, &components.scheme, &components.host,
  78. &components.match_subdomains, &components.port,
  79. &components.path, &components.query);
  80. }
  81. } // namespace policy