1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsCookieBannerTelemetryService.h"
#include "cookieBanner.pb.h"
#include "mozilla/Base64.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/glean/GleanMetrics.h"
#include "mozilla/OriginAttributes.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/Logging.h"
#include "Cookie.h"
#include "nsCRT.h"
#include "nsError.h"
#include "nsICookie.h"
#include "nsICookieManager.h"
#include "nsICookieNotification.h"
#include "nsTHashSet.h"
#include "nsIObserverService.h"
#include "nsIScriptSecurityManager.h"
#include "nsISearchService.h"
#include "nsServiceManagerUtils.h"
#include "nsStringFwd.h"
#include "nsTArray.h"
namespace mozilla {
NS_IMPL_ISUPPORTS(nsCookieBannerTelemetryService,
nsICookieBannerTelemetryService, nsIObserver)
static LazyLogModule gCookieBannerTelemetryLog(
"nsCookieBannerTelemetryService");
static StaticRefPtr<nsCookieBannerTelemetryService>
sCookieBannerTelemetryServiceSingleton;
// A hash set used to tell whether a base domain is a Google domain.
static StaticAutoPtr<nsTHashSet<nsCString>> sGoogleDomainsSet;
namespace {
// A helper function that decodes Google's SOCS cookie and returns the GDPR
// choice and the region. The choice and be either "Accept", "Reject", or
// "Custom".
nsresult DecodeSOCSGoogleCookie(const nsACString& aCookie, nsACString& aChoice,
nsACString& aRegion) {
aChoice.Truncate();
aRegion.Truncate();
FallibleTArray<uint8_t> decoded;
nsresult rv =
Base64URLDecode(aCookie, Base64URLDecodePaddingPolicy::Ignore, decoded);
NS_ENSURE_SUCCESS(rv, rv);
std::string buf(reinterpret_cast<const char*>(decoded.Elements()),
decoded.Length());
cookieBanner::GoogleSOCSCookie socs;
if (!socs.ParseFromString(buf)) {
return NS_ERROR_CANNOT_CONVERT_DATA;
}
aRegion.Assign(socs.data().region().c_str(), socs.data().region().length());
// The first field represents the gdpr choice, 1 means "Reject" and 2 means
// either "Accept" or "Custom". We need to check a following field to decide.
if (socs.gdpr_choice() == 1) {
aChoice.AssignLiteral("Reject");
return NS_OK;
}
// The platform field represents where does this GDPR consent is selected. The
// value can be either "gws_*" or "boq_identityfrontenduiserver_*". If the
// field value starts with "gws_", it means the consent is from the Google
// search page. Otherwise, it's from the consent.google.com, which is used for
// the custom setting.
std::string prefix = "gws_";
if (socs.data().platform().compare(0, prefix.length(), prefix) == 0) {
aChoice.AssignLiteral("Accept");
return NS_OK;
}
aChoice.AssignLiteral("Custom");
return NS_OK;
}
} // anonymous namespace
// static
already_AddRefed<nsCookieBannerTelemetryService>
nsCookieBannerTelemetryService::GetSingleton() {
MOZ_LOG(gCookieBannerTelemetryLog, LogLevel::Debug, ("GetSingleton."));
if (!sCookieBannerTelemetryServiceSingleton) {
sCookieBannerTelemetryServiceSingleton =
new nsCookieBannerTelemetryService();
RunOnShutdown([] {
DebugOnly<nsresult> rv =
sCookieBannerTelemetryServiceSingleton->Shutdown();
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"nsCookieBannerTelemetryService::Shutdown failed.");
sCookieBannerTelemetryServiceSingleton = nullptr;
});
}
return do_AddRef(sCookieBannerTelemetryServiceSingleton);
}
nsresult nsCookieBannerTelemetryService::Init() {
MOZ_LOG(gCookieBannerTelemetryLog, LogLevel::Debug, ("Init."));
if (mIsInitialized) {
return NS_OK;
}
mIsInitialized = true;
nsCOMPtr<nsIObserverService> obsSvc = mozilla::services::GetObserverService();
NS_ENSURE_TRUE(obsSvc, NS_ERROR_FAILURE);
nsresult rv = obsSvc->AddObserver(this, "browser-search-service", false);
NS_ENSURE_SUCCESS(rv, rv);
rv = obsSvc->AddObserver(this, "idle-daily", false);
NS_ENSURE_SUCCESS(rv, rv);
rv = obsSvc->AddObserver(this, "cookie-changed", false);
NS_ENSURE_SUCCESS(rv, rv);
rv = obsSvc->AddObserver(this, "private-cookie-changed", false);
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
nsresult nsCookieBannerTelemetryService::Shutdown() {
MOZ_LOG(gCookieBannerTelemetryLog, LogLevel::Debug, ("Shutdown."));
if (!mIsInitialized) {
return NS_OK;
}
mIsInitialized = false;
sGoogleDomainsSet = nullptr;
nsCOMPtr<nsIObserverService> obsSvc = mozilla::services::GetObserverService();
NS_ENSURE_TRUE(obsSvc, NS_ERROR_FAILURE);
nsresult rv = obsSvc->RemoveObserver(this, "browser-search-service");
NS_ENSURE_SUCCESS(rv, rv);
rv = obsSvc->RemoveObserver(this, "idle-daily");
NS_ENSURE_SUCCESS(rv, rv);
rv = obsSvc->RemoveObserver(this, "cookie-changed");
NS_ENSURE_SUCCESS(rv, rv);
rv = obsSvc->RemoveObserver(this, "private-cookie-changed");
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
NS_IMETHODIMP
nsCookieBannerTelemetryService::Observe(nsISupports* aSubject,
const char* aTopic,
const char16_t* aData) {
if (nsCRT::strcmp(aTopic, "profile-after-change") == 0) {
MOZ_LOG(gCookieBannerTelemetryLog, LogLevel::Debug,
("Observe profile-after-change"));
return Init();
}
if (nsCRT::strcmp(aTopic, "idle-daily") == 0) {
MOZ_LOG(gCookieBannerTelemetryLog, LogLevel::Debug, ("idle-daily"));
return MaybeReportGoogleGDPRChoiceTelemetry();
}
if (nsCRT::strcmp(aTopic, "browser-search-service") == 0 &&
nsDependentString(aData).EqualsLiteral("init-complete")) {
MOZ_LOG(gCookieBannerTelemetryLog, LogLevel::Debug,
("Observe browser-search-service::init-complete."));
mIsSearchServiceInitialized = true;
return MaybeReportGoogleGDPRChoiceTelemetry();
}
if (nsCRT::strcmp(aTopic, "cookie-changed") == 0 ||
nsCRT::strcmp(aTopic, "private-cookie-changed") == 0) {
MOZ_LOG(gCookieBannerTelemetryLog, LogLevel::Debug, ("Observe %s", aTopic));
nsCOMPtr<nsICookieNotification> notification = do_QueryInterface(aSubject);
NS_ENSURE_TRUE(notification, NS_ERROR_FAILURE);
if (notification->GetAction() != nsICookieNotification::COOKIE_ADDED &&
notification->GetAction() != nsICookieNotification::COOKIE_CHANGED) {
return NS_OK;
}
nsCOMPtr<nsICookie> cookie;
nsresult rv = notification->GetCookie(getter_AddRefs(cookie));
NS_ENSURE_SUCCESS(rv, rv);
nsAutoCString name;
rv = cookie->GetName(name);
NS_ENSURE_SUCCESS(rv, rv);
// Bail out early if this is not a SOCS cookie.
if (!name.EqualsLiteral("SOCS")) {
return NS_OK;
}
// A "SOCS" cookie is added. We record the GDPR choice as well as an event
// telemetry for recording a choice has been made at the moment.
return MaybeReportGoogleGDPRChoiceTelemetry(cookie, true);
}
return NS_OK;
}
nsresult nsCookieBannerTelemetryService::MaybeReportGoogleGDPRChoiceTelemetry(
nsICookie* aCookie, bool aReportEvent) {
MOZ_ASSERT(mIsInitialized);
nsresult rv;
// Don't report the telemetry if the search service is not yet initialized.
if (!mIsSearchServiceInitialized) {
MOZ_LOG(gCookieBannerTelemetryLog, LogLevel::Debug,
("Search Service is not yet initialized."));
return NS_OK;
}
// We only collect Google GDPR choice if Google is the default search engine.
nsCOMPtr<nsISearchService> searchService(
do_GetService("@mozilla.org/browser/search-service;1", &rv));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsISearchEngine> engine;
rv = searchService->GetDefaultEngine(getter_AddRefs(engine));
NS_ENSURE_SUCCESS(rv, rv);
// Bail out early if no default search engine is available. This could happen
// if no search engine is shipped with the Gecko.
if (!engine) {
MOZ_LOG(gCookieBannerTelemetryLog, LogLevel::Debug,
("No default search engine is available."));
return NS_OK;
}
nsAutoString id;
rv = engine->GetId(id);
NS_ENSURE_SUCCESS(rv, rv);
// Bail out early if the default search engine is not Google.
if (!id.EqualsLiteral("google@search.mozilla.orgdefault")) {
return NS_OK;
}
// Build up the Google domain set from the alternative Google search domains
if (!sGoogleDomainsSet) {
sGoogleDomainsSet = new nsTHashSet<nsCString>();
nsTArray<nsCString> googleDomains;
rv = searchService->GetAlternateDomains("www.google.com"_ns, googleDomains);
NS_ENSURE_SUCCESS(rv, rv);
for (const auto& domain : googleDomains) {
// We need to trim down the preceding "www" to get the host because the
// alternate domains provided by search service always have leading "www".
// However, the "SOCS" cookie is always set for all subdomains, e.g.
// ".google.com". Therefore, we need to trim down "www" to match the host
// of the cookie.
NS_ENSURE_TRUE(domain.Length() > 3, NS_ERROR_FAILURE);
sGoogleDomainsSet->Insert(Substring(domain, 3, domain.Length() - 3));
}
}
nsTArray<RefPtr<nsICookie>> cookies;
if (aCookie) {
const auto& attrs = aCookie->AsCookie().OriginAttributesRef();
// We only report cookies for the default originAttributes or private
// browsing mode.
if (attrs.mPrivateBrowsingId !=
nsIScriptSecurityManager::DEFAULT_PRIVATE_BROWSING_ID ||
attrs == OriginAttributes()) {
cookies.AppendElement(RefPtr<nsICookie>(aCookie));
}
} else {
// If no cookie is given, we will iterate all cookies under Google Search
// domains.
nsCOMPtr<nsICookieManager> cookieManager =
do_GetService("@mozilla.org/cookiemanager;1");
if (!cookieManager) {
return NS_ERROR_FAILURE;
}
for (const auto& domain : *sGoogleDomainsSet) {
// Getting Google Search cookies only for normal windows. We will exclude
// cookies of non-default originAttributes, like cookies from containers.
//
// Note that we need to trim the leading "." from the domain here.
nsTArray<RefPtr<nsICookie>> googleCookies;
rv = cookieManager->GetCookiesWithOriginAttributes(
u"{ \"privateBrowsingId\": 0, \"userContextId\": 0 }"_ns,
Substring(domain, 1, domain.Length() - 1), googleCookies);
NS_ENSURE_SUCCESS(rv, rv);
cookies.AppendElements(googleCookies);
}
}
for (const auto& cookie : cookies) {
nsAutoCString name;
rv = cookie->GetName(name);
NS_ENSURE_SUCCESS(rv, rv);
if (!name.EqualsLiteral("SOCS")) {
continue;
}
nsAutoCString host;
rv = cookie->GetHost(host);
NS_ENSURE_SUCCESS(rv, rv);
if (!sGoogleDomainsSet->Contains(host)) {
continue;
}
nsAutoCString value;
rv = cookie->GetValue(value);
NS_ENSURE_SUCCESS(rv, rv);
nsAutoCString choice;
nsAutoCString region;
rv = DecodeSOCSGoogleCookie(value, choice, region);
NS_ENSURE_SUCCESS(rv, rv);
bool isPrivateBrowsing =
cookie->AsCookie().OriginAttributesRef().mPrivateBrowsingId !=
nsIScriptSecurityManager::DEFAULT_PRIVATE_BROWSING_ID;
MOZ_LOG(gCookieBannerTelemetryLog, LogLevel::Debug,
("Record the Google GDPR choice %s on the host %s in region %s for "
"the %s window",
choice.get(), host.get(), region.get(),
isPrivateBrowsing ? "private" : "normal"));
// We rely on the dynamical labelled string which can send most 16 different
// labels per report. In average cases, 16 labels is sufficient because we
// expect a user might have only 1 or 2 "SOCS" cookies on different Google
// Search domains.
//
// Note that we only report event telemetry for private browsing windows
// because the private session is ephemeral. People can change GDPR
// choice across different private sessions, so it's hard to collect the
// state correctly.
if (!isPrivateBrowsing) {
glean::cookie_banners::google_gdpr_choice_cookie.Get(host).Set(choice);
}
if (aReportEvent) {
if (isPrivateBrowsing) {
glean::cookie_banners::GoogleGdprChoiceCookieEventPbmExtra extra = {
.choice = Some(choice),
};
glean::cookie_banners::google_gdpr_choice_cookie_event_pbm.Record(
Some(extra));
} else {
glean::cookie_banners::GoogleGdprChoiceCookieEventExtra extra = {
.choice = Some(choice),
.region = Some(region),
.searchDomain = Some(host),
};
glean::cookie_banners::google_gdpr_choice_cookie_event.Record(
Some(extra));
}
}
}
return NS_OK;
}
} // namespace mozilla
|