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
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/push_messaging/push_messaging_app_identifier.h"
#include <string.h>
#include "base/check_op.h"
#include "base/notreached.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/uuid.h"
#include "base/values.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/pref_names.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
constexpr char kPushMessagingAppIdentifierPrefix[] = "wp:";
constexpr char kInstanceIDGuidSuffix[] = "-V2";
namespace {
// sizeof is strlen + 1 since it's null-terminated.
constexpr size_t kPrefixLength = sizeof(kPushMessagingAppIdentifierPrefix) - 1;
constexpr size_t kGuidSuffixLength = sizeof(kInstanceIDGuidSuffix) - 1;
// Ok to use '#' as separator since only the origin of the url is used.
constexpr char kPrefValueSeparator = '#';
constexpr size_t kGuidLength = 36; // "%08X-%04X-%04X-%04X-%012llX"
std::string FromTimeToString(base::Time time) {
DCHECK(!time.is_null());
return base::NumberToString(time.ToDeltaSinceWindowsEpoch().InMilliseconds());
}
bool FromStringToTime(const std::string& time_string,
std::optional<base::Time>* time) {
DCHECK(!time_string.empty());
int64_t milliseconds;
if (base::StringToInt64(time_string, &milliseconds) && milliseconds > 0) {
*time = std::make_optional(base::Time::FromDeltaSinceWindowsEpoch(
base::Milliseconds(milliseconds)));
return true;
}
return false;
}
std::string MakePrefValue(
const GURL& origin,
int64_t service_worker_registration_id,
const std::optional<base::Time>& expiration_time = std::nullopt) {
std::string result = origin.spec() + kPrefValueSeparator +
base::NumberToString(service_worker_registration_id);
if (expiration_time)
result += kPrefValueSeparator + FromTimeToString(*expiration_time);
return result;
}
bool DisassemblePrefValue(const std::string& pref_value,
GURL* origin,
int64_t* service_worker_registration_id,
std::optional<base::Time>* expiration_time) {
std::vector<std::string> parts =
base::SplitString(pref_value, std::string(1, kPrefValueSeparator),
base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
if (parts.size() < 2 || parts.size() > 3)
return false;
if (!base::StringToInt64(parts[1], service_worker_registration_id))
return false;
*origin = GURL(parts[0]);
if (!origin->is_valid())
return false;
if (parts.size() == 3)
return FromStringToTime(parts[2], expiration_time);
return true;
}
} // namespace
// static
void PushMessagingAppIdentifier::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
// TODO(johnme): If push becomes enabled in incognito, be careful that this
// pref is read from the right profile, as prefs defined in a regular profile
// are visible in the corresponding incognito profile unless overridden.
// TODO(johnme): Make sure this pref doesn't get out of sync after crashes.
registry->RegisterDictionaryPref(prefs::kPushMessagingAppIdentifierMap);
}
// static
bool PushMessagingAppIdentifier::UseInstanceID(const std::string& app_id) {
return base::EndsWith(app_id, kInstanceIDGuidSuffix,
base::CompareCase::SENSITIVE);
}
// static
PushMessagingAppIdentifier PushMessagingAppIdentifier::Generate(
const GURL& origin,
int64_t service_worker_registration_id,
const std::optional<base::Time>& expiration_time) {
// All new push subscriptions use Instance ID tokens.
return GenerateInternal(origin, service_worker_registration_id,
true /* use_instance_id */, expiration_time);
}
// static
PushMessagingAppIdentifier PushMessagingAppIdentifier::LegacyGenerateForTesting(
const GURL& origin,
int64_t service_worker_registration_id,
const std::optional<base::Time>& expiration_time) {
return GenerateInternal(origin, service_worker_registration_id,
false /* use_instance_id */, expiration_time);
}
// static
PushMessagingAppIdentifier PushMessagingAppIdentifier::GenerateInternal(
const GURL& origin,
int64_t service_worker_registration_id,
bool use_instance_id,
const std::optional<base::Time>& expiration_time) {
// Use uppercase GUID for consistency with GUIDs Push has already sent to GCM.
// Also allows detecting case mangling; see code commented "crbug.com/461867".
std::string guid =
base::ToUpperASCII(base::Uuid::GenerateRandomV4().AsLowercaseString());
if (use_instance_id) {
guid.replace(guid.size() - kGuidSuffixLength, kGuidSuffixLength,
kInstanceIDGuidSuffix);
}
CHECK(!guid.empty());
std::string app_id = kPushMessagingAppIdentifierPrefix + origin.spec() +
kPrefValueSeparator + guid;
PushMessagingAppIdentifier app_identifier(
app_id, origin, service_worker_registration_id, expiration_time);
app_identifier.DCheckValid();
return app_identifier;
}
// static
PushMessagingAppIdentifier PushMessagingAppIdentifier::FindByAppId(
Profile* profile, const std::string& app_id) {
if (!base::StartsWith(app_id, kPushMessagingAppIdentifierPrefix,
base::CompareCase::INSENSITIVE_ASCII)) {
return PushMessagingAppIdentifier();
}
// Since we now know this is a Push Messaging app_id, check the case hasn't
// been mangled (crbug.com/461867).
DCHECK_EQ(kPushMessagingAppIdentifierPrefix, app_id.substr(0, kPrefixLength));
DCHECK_GE(app_id.size(), kPrefixLength + kGuidLength);
DCHECK_EQ(app_id.substr(app_id.size() - kGuidLength),
base::ToUpperASCII(app_id.substr(app_id.size() - kGuidLength)));
const base::Value::Dict& map =
profile->GetPrefs()->GetDict(prefs::kPushMessagingAppIdentifierMap);
const std::string* map_value = map.FindString(app_id);
if (!map_value || map_value->empty())
return PushMessagingAppIdentifier();
GURL origin;
int64_t service_worker_registration_id;
std::optional<base::Time> expiration_time;
// Try disassemble the pref value, return an invalid app identifier if the
// pref value is corrupted
if (!DisassemblePrefValue(*map_value, &origin,
&service_worker_registration_id,
&expiration_time)) {
NOTREACHED();
}
PushMessagingAppIdentifier app_identifier(
app_id, origin, service_worker_registration_id, expiration_time);
app_identifier.DCheckValid();
return app_identifier;
}
// static
PushMessagingAppIdentifier PushMessagingAppIdentifier::FindByServiceWorker(
Profile* profile,
const GURL& origin,
int64_t service_worker_registration_id) {
const std::string base_pref_value =
MakePrefValue(origin, service_worker_registration_id);
const base::Value::Dict& map =
profile->GetPrefs()->GetDict(prefs::kPushMessagingAppIdentifierMap);
for (auto entry : map) {
if (entry.second.is_string() &&
base::StartsWith(entry.second.GetString(), base_pref_value,
base::CompareCase::SENSITIVE)) {
return FindByAppId(profile, entry.first);
}
}
return PushMessagingAppIdentifier();
}
// static
std::vector<PushMessagingAppIdentifier> PushMessagingAppIdentifier::GetAll(
Profile* profile) {
std::vector<PushMessagingAppIdentifier> result;
const base::Value::Dict& map =
profile->GetPrefs()->GetDict(prefs::kPushMessagingAppIdentifierMap);
for (auto entry : map) {
result.push_back(FindByAppId(profile, entry.first));
}
return result;
}
// static
void PushMessagingAppIdentifier::DeleteAllFromPrefs(Profile* profile) {
profile->GetPrefs()->SetDict(prefs::kPushMessagingAppIdentifierMap,
base::Value::Dict());
}
// static
size_t PushMessagingAppIdentifier::GetCount(Profile* profile) {
return profile->GetPrefs()
->GetDict(prefs::kPushMessagingAppIdentifierMap)
.size();
}
PushMessagingAppIdentifier::PushMessagingAppIdentifier(
const PushMessagingAppIdentifier& other) = default;
PushMessagingAppIdentifier::PushMessagingAppIdentifier()
: origin_(GURL()), service_worker_registration_id_(-1) {}
PushMessagingAppIdentifier::PushMessagingAppIdentifier(
const std::string& app_id,
const GURL& origin,
int64_t service_worker_registration_id,
const std::optional<base::Time>& expiration_time)
: app_id_(app_id),
origin_(origin),
service_worker_registration_id_(service_worker_registration_id),
expiration_time_(expiration_time) {}
PushMessagingAppIdentifier::~PushMessagingAppIdentifier() = default;
bool PushMessagingAppIdentifier::IsExpired() const {
return (expiration_time_) ? *expiration_time_ < base::Time::Now() : false;
}
void PushMessagingAppIdentifier::PersistToPrefs(Profile* profile) const {
DCheckValid();
ScopedDictPrefUpdate update(profile->GetPrefs(),
prefs::kPushMessagingAppIdentifierMap);
base::Value::Dict& map = update.Get();
// Delete any stale entry with the same origin and Service Worker
// registration id (hence we ensure there is a 1:1 not 1:many mapping).
PushMessagingAppIdentifier old =
FindByServiceWorker(profile, origin_, service_worker_registration_id_);
if (!old.is_null())
map.Remove(old.app_id_);
map.Set(app_id_, MakePrefValue(origin_, service_worker_registration_id_,
expiration_time_));
}
void PushMessagingAppIdentifier::DeleteFromPrefs(Profile* profile) const {
DCheckValid();
ScopedDictPrefUpdate update(profile->GetPrefs(),
prefs::kPushMessagingAppIdentifierMap);
base::Value::Dict& map = update.Get();
map.Remove(app_id_);
}
void PushMessagingAppIdentifier::DCheckValid() const {
#if DCHECK_IS_ON()
DCHECK_GE(service_worker_registration_id_, 0);
DCHECK(origin_.is_valid());
DCHECK_EQ(origin_.DeprecatedGetOriginAsURL(), origin_);
// "wp:"
DCHECK_EQ(kPushMessagingAppIdentifierPrefix,
app_id_.substr(0, kPrefixLength));
// Optional (origin.spec() + '#')
if (app_id_.size() != kPrefixLength + kGuidLength) {
constexpr size_t suffix_length = 1 /* kPrefValueSeparator */ + kGuidLength;
DCHECK_GT(app_id_.size(), kPrefixLength + suffix_length);
DCHECK_EQ(origin_, GURL(app_id_.substr(
kPrefixLength,
app_id_.size() - kPrefixLength - suffix_length)));
DCHECK_EQ(std::string(1, kPrefValueSeparator),
app_id_.substr(app_id_.size() - suffix_length, 1));
}
// GUID. In order to distinguish them, an app_id created for an InstanceID
// based subscription has the last few characters of the GUID overwritten with
// kInstanceIDGuidSuffix (which contains non-hex characters invalid in GUIDs).
std::string guid = app_id_.substr(app_id_.size() - kGuidLength);
if (UseInstanceID(app_id_)) {
DCHECK(!base::Uuid::ParseCaseInsensitive(guid).is_valid());
// Replace suffix with valid hex so we can validate the rest of the string.
guid = guid.replace(guid.size() - kGuidSuffixLength, kGuidSuffixLength,
kGuidSuffixLength, 'C');
}
DCHECK(base::Uuid::ParseCaseInsensitive(guid).is_valid());
#endif // DCHECK_IS_ON()
}
|