Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2022 The Chromium Authors |
Peter Kotwicz | 90c0dc22 | 2022-03-09 18:24:45 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/webid/federated_identity_api_permission_context.h" |
| 6 | |
Zachary Tan | 3309272 | 2022-11-15 18:59:38 | [diff] [blame] | 7 | #include "chrome/browser/browser_features.h" |
Nicolás Peña Moreno | 0c974faf | 2022-03-10 16:05:12 | [diff] [blame] | 8 | #include "chrome/browser/content_settings/cookie_settings_factory.h" |
Peter Kotwicz | 90c0dc22 | 2022-03-09 18:24:45 | [diff] [blame] | 9 | #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 10 | #include "chrome/browser/permissions/permission_decision_auto_blocker_factory.h" |
Nicolás Peña Moreno | 0c974faf | 2022-03-10 16:05:12 | [diff] [blame] | 11 | #include "chrome/browser/profiles/profile.h" |
Peter Kotwicz | 90c0dc22 | 2022-03-09 18:24:45 | [diff] [blame] | 12 | #include "components/content_settings/core/common/content_settings_types.h" |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 13 | #include "components/permissions/permission_decision_auto_blocker.h" |
| 14 | #include "components/permissions/permission_result.h" |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 15 | #include "content/public/common/content_features.h" |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 16 | #include "url/origin.h" |
Peter Kotwicz | 90c0dc22 | 2022-03-09 18:24:45 | [diff] [blame] | 17 | |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 18 | using PermissionStatus = |
| 19 | content::FederatedIdentityApiPermissionContextDelegate::PermissionStatus; |
| 20 | |
Peter Kotwicz | 90c0dc22 | 2022-03-09 18:24:45 | [diff] [blame] | 21 | FederatedIdentityApiPermissionContext::FederatedIdentityApiPermissionContext( |
| 22 | content::BrowserContext* browser_context) |
| 23 | : host_content_settings_map_( |
Nicolás Peña Moreno | 0c974faf | 2022-03-10 16:05:12 | [diff] [blame] | 24 | HostContentSettingsMapFactory::GetForProfile(browser_context)), |
| 25 | cookie_settings_(CookieSettingsFactory::GetForProfile( |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 26 | Profile::FromBrowserContext(browser_context))), |
| 27 | permission_autoblocker_( |
| 28 | PermissionDecisionAutoBlockerFactory::GetForProfile( |
| 29 | Profile::FromBrowserContext(browser_context))) {} |
Peter Kotwicz | 90c0dc22 | 2022-03-09 18:24:45 | [diff] [blame] | 30 | |
| 31 | FederatedIdentityApiPermissionContext:: |
| 32 | ~FederatedIdentityApiPermissionContext() = default; |
| 33 | |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 34 | content::FederatedIdentityApiPermissionContextDelegate::PermissionStatus |
| 35 | FederatedIdentityApiPermissionContext::GetApiPermissionStatus( |
Peter Kotwicz | 51d5ddf | 2022-08-19 23:20:40 | [diff] [blame] | 36 | const url::Origin& relying_party_embedder) { |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 37 | if (!base::FeatureList::IsEnabled(features::kFedCm)) |
| 38 | return PermissionStatus::BLOCKED_VARIATIONS; |
| 39 | |
Peter Kotwicz | af746d3 | 2022-12-07 02:18:36 | [diff] [blame] | 40 | const GURL rp_embedder_url = relying_party_embedder.GetURL(); |
| 41 | |
Peter Kotwicz | d4900eb8 | 2022-04-22 21:55:59 | [diff] [blame] | 42 | const ContentSetting setting = host_content_settings_map_->GetContentSetting( |
Peter Kotwicz | 51d5ddf | 2022-08-19 23:20:40 | [diff] [blame] | 43 | rp_embedder_url, rp_embedder_url, |
| 44 | ContentSettingsType::FEDERATED_IDENTITY_API); |
Peter Kotwicz | d4900eb8 | 2022-04-22 21:55:59 | [diff] [blame] | 45 | switch (setting) { |
| 46 | case CONTENT_SETTING_ALLOW: |
| 47 | break; |
| 48 | case CONTENT_SETTING_BLOCK: |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 49 | return PermissionStatus::BLOCKED_SETTINGS; |
Peter Kotwicz | d4900eb8 | 2022-04-22 21:55:59 | [diff] [blame] | 50 | default: |
| 51 | NOTREACHED(); |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 52 | return PermissionStatus::BLOCKED_SETTINGS; |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 53 | } |
| 54 | |
Peter Kotwicz | 63559a8 | 2022-06-07 03:46:35 | [diff] [blame] | 55 | if (permission_autoblocker_->IsEmbargoed( |
Peter Kotwicz | 51d5ddf | 2022-08-19 23:20:40 | [diff] [blame] | 56 | rp_embedder_url, ContentSettingsType::FEDERATED_IDENTITY_API)) { |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 57 | return PermissionStatus::BLOCKED_EMBARGO; |
Peter Kotwicz | 63559a8 | 2022-06-07 03:46:35 | [diff] [blame] | 58 | } |
Christian Biesinger | 49ab631 | 2023-08-10 20:07:33 | [diff] [blame^] | 59 | // TODO(npm): FedCM is currently restricted to contexts where third party |
| 60 | // cookies are not blocked unless the FedCmWithoutThirdPartyCookies flag or |
| 61 | // FedCmIdpSigninStatusEnabled flag is enabled. The IDP signin status API |
| 62 | // override is implemented in the caller because it can be enabled through |
| 63 | // origin trials. This block can be removed when the IDP Signin status API |
| 64 | // ships. |
| 65 | // See https://crbug.com/1451396 |
| 66 | if (cookie_settings_->ShouldBlockThirdPartyCookies() && |
| 67 | !cookie_settings_->IsThirdPartyAccessAllowed(rp_embedder_url) && |
| 68 | !base::FeatureList::IsEnabled(features::kFedCmWithoutThirdPartyCookies)) { |
| 69 | return PermissionStatus::BLOCKED_THIRD_PARTY_COOKIES_BLOCKED; |
| 70 | } |
| 71 | |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 72 | return PermissionStatus::GRANTED; |
Nicolás Peña Moreno | 0c974faf | 2022-03-10 16:05:12 | [diff] [blame] | 73 | } |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 74 | |
| 75 | void FederatedIdentityApiPermissionContext::RecordDismissAndEmbargo( |
Peter Kotwicz | 51d5ddf | 2022-08-19 23:20:40 | [diff] [blame] | 76 | const url::Origin& relying_party_embedder) { |
| 77 | const GURL rp_embedder_url = relying_party_embedder.GetURL(); |
| 78 | // If content setting is allowed for `rp_embedder_url`, reset it. |
Peter Kotwicz | 7236eac | 2022-07-11 22:05:04 | [diff] [blame] | 79 | // See crbug.com/1340127 for why the resetting is not conditional on the |
| 80 | // default content setting state. |
| 81 | const ContentSetting setting = host_content_settings_map_->GetContentSetting( |
Peter Kotwicz | 51d5ddf | 2022-08-19 23:20:40 | [diff] [blame] | 82 | rp_embedder_url, rp_embedder_url, |
| 83 | ContentSettingsType::FEDERATED_IDENTITY_API); |
Peter Kotwicz | 7236eac | 2022-07-11 22:05:04 | [diff] [blame] | 84 | if (setting == CONTENT_SETTING_ALLOW) { |
| 85 | host_content_settings_map_->SetContentSettingDefaultScope( |
Peter Kotwicz | 51d5ddf | 2022-08-19 23:20:40 | [diff] [blame] | 86 | rp_embedder_url, rp_embedder_url, |
| 87 | ContentSettingsType::FEDERATED_IDENTITY_API, CONTENT_SETTING_DEFAULT); |
Peter Kotwicz | 7236eac | 2022-07-11 22:05:04 | [diff] [blame] | 88 | } |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 89 | permission_autoblocker_->RecordDismissAndEmbargo( |
Peter Kotwicz | 51d5ddf | 2022-08-19 23:20:40 | [diff] [blame] | 90 | rp_embedder_url, ContentSettingsType::FEDERATED_IDENTITY_API, |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 91 | false /* dismissed_prompt_was_quiet */); |
| 92 | } |
| 93 | |
| 94 | void FederatedIdentityApiPermissionContext::RemoveEmbargoAndResetCounts( |
Peter Kotwicz | 51d5ddf | 2022-08-19 23:20:40 | [diff] [blame] | 95 | const url::Origin& relying_party_embedder) { |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 96 | permission_autoblocker_->RemoveEmbargoAndResetCounts( |
Peter Kotwicz | 51d5ddf | 2022-08-19 23:20:40 | [diff] [blame] | 97 | relying_party_embedder.GetURL(), |
| 98 | ContentSettingsType::FEDERATED_IDENTITY_API); |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 99 | } |