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
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "mozilla/dom/CredentialsContainer.h"
#include "mozilla/StaticPrefs_dom.h"
#include "mozilla/StaticPrefs_security.h"
#include "mozilla/dom/Credential.h"
#include "mozilla/dom/FeaturePolicyUtils.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/WebAuthnHandler.h"
#include "mozilla/dom/WebIdentityHandler.h"
#include "mozilla/dom/WindowContext.h"
#include "mozilla/dom/WindowGlobalChild.h"
#include "nsContentUtils.h"
#include "nsIDocShell.h"
namespace mozilla::dom {
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(CredentialsContainer, mParent,
mWebAuthnHandler)
NS_IMPL_CYCLE_COLLECTING_ADDREF(CredentialsContainer)
NS_IMPL_CYCLE_COLLECTING_RELEASE(CredentialsContainer)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CredentialsContainer)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
already_AddRefed<Promise> CreatePromise(nsPIDOMWindowInner* aParent,
ErrorResult& aRv) {
MOZ_ASSERT(aParent);
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aParent);
if (NS_WARN_IF(!global)) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
RefPtr<Promise> promise = Promise::Create(global, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
return promise.forget();
}
already_AddRefed<Promise> CreateAndRejectWithNotAllowed(
nsPIDOMWindowInner* aParent, ErrorResult& aRv) {
MOZ_ASSERT(aParent);
RefPtr<Promise> promise = CreatePromise(aParent, aRv);
if (!promise) {
return nullptr;
}
promise->MaybeRejectWithNotAllowedError(
"CredentialContainer request is not allowed."_ns);
return promise.forget();
}
already_AddRefed<Promise> CreateAndRejectWithNotSupported(
nsPIDOMWindowInner* aParent, ErrorResult& aRv) {
MOZ_ASSERT(aParent);
RefPtr<Promise> promise = CreatePromise(aParent, aRv);
if (!promise) {
return nullptr;
}
promise->MaybeRejectWithNotSupportedError(
"CredentialContainer request is not supported."_ns);
return promise.forget();
}
static bool IsInActiveTab(nsPIDOMWindowInner* aParent) {
// Returns whether aParent is an inner window somewhere in the active tab.
// The active tab is the selected (i.e. visible) tab in the focused window.
MOZ_ASSERT(aParent);
RefPtr<Document> doc = aParent->GetExtantDoc();
if (NS_WARN_IF(!doc)) {
return false;
}
return IsInActiveTab(doc);
}
static bool ConsumeUserActivation(nsPIDOMWindowInner* aParent) {
// Returns whether aParent has transient activation, and consumes the
// activation.
MOZ_ASSERT(aParent);
RefPtr<Document> doc = aParent->GetExtantDoc();
if (NS_WARN_IF(!doc)) {
return false;
}
return doc->ConsumeTransientUserGestureActivation();
}
// static
bool CredentialsContainer::IsSameOriginWithAncestors(
nsPIDOMWindowInner* aParent) {
// This method returns true if aParent is either not in a frame / iframe, or
// is in a frame or iframe and all ancestors for aParent are the same origin.
// This is useful for Credential Management because we need to prohibit
// iframes, but not break mochitests (which use iframes to embed the tests).
MOZ_ASSERT(aParent);
WindowGlobalChild* wgc = aParent->GetWindowGlobalChild();
// If there's no WindowGlobalChild, the inner window has already been
// destroyed, so fail safe and return false.
if (!wgc) {
return false;
}
// Check that all ancestors are the same origin, repeating until we find a
// null parent
for (WindowContext* parentContext =
wgc->WindowContext()->GetParentWindowContext();
parentContext; parentContext = parentContext->GetParentWindowContext()) {
if (!wgc->IsSameOriginWith(parentContext)) {
// same-origin policy is violated
return false;
}
}
return true;
}
CredentialsContainer::CredentialsContainer(nsPIDOMWindowInner* aParent)
: mParent(aParent) {
MOZ_ASSERT(aParent);
}
CredentialsContainer::~CredentialsContainer() = default;
void CredentialsContainer::EnsureWebAuthnHandler() {
MOZ_ASSERT(NS_IsMainThread());
if (!mWebAuthnHandler) {
mWebAuthnHandler = new WebAuthnHandler(mParent);
}
}
already_AddRefed<WebAuthnHandler> CredentialsContainer::GetWebAuthnHandler() {
MOZ_ASSERT(NS_IsMainThread());
EnsureWebAuthnHandler();
RefPtr<WebAuthnHandler> ref = mWebAuthnHandler;
return ref.forget();
}
JSObject* CredentialsContainer::WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return CredentialsContainer_Binding::Wrap(aCx, this, aGivenProto);
}
already_AddRefed<Promise> CredentialsContainer::Get(
const CredentialRequestOptions& aOptions, ErrorResult& aRv) {
uint64_t totalOptions = 0;
if (aOptions.mPublicKey.WasPassed() &&
StaticPrefs::security_webauth_webauthn()) {
totalOptions += 1;
}
if (aOptions.mIdentity.WasPassed() &&
StaticPrefs::dom_security_credentialmanagement_identity_enabled()) {
totalOptions += 1;
}
if (totalOptions > 1) {
return CreateAndRejectWithNotSupported(mParent, aRv);
}
bool conditionallyMediated =
aOptions.mMediation == CredentialMediationRequirement::Conditional;
if (aOptions.mPublicKey.WasPassed() &&
StaticPrefs::security_webauth_webauthn()) {
MOZ_ASSERT(mParent);
if (!FeaturePolicyUtils::IsFeatureAllowed(
mParent->GetExtantDoc(), u"publickey-credentials-get"_ns) ||
!(IsInActiveTab(mParent) || conditionallyMediated)) {
return CreateAndRejectWithNotAllowed(mParent, aRv);
}
if (conditionallyMediated &&
!StaticPrefs::security_webauthn_enable_conditional_mediation()) {
RefPtr<Promise> promise = CreatePromise(mParent, aRv);
if (!promise) {
return nullptr;
}
promise->MaybeRejectWithTypeError<MSG_INVALID_ENUM_VALUE>(
"mediation", "conditional", "CredentialMediationRequirement");
return promise.forget();
}
if (aOptions.mMediation != CredentialMediationRequirement::Conditional &&
aOptions.mMediation != CredentialMediationRequirement::Optional &&
aOptions.mMediation != CredentialMediationRequirement::Required) {
return CreateAndRejectWithNotSupported(mParent, aRv);
}
EnsureWebAuthnHandler();
return mWebAuthnHandler->GetAssertion(aOptions.mPublicKey.Value(),
conditionallyMediated,
aOptions.mSignal, aRv);
}
if (aOptions.mIdentity.WasPassed() &&
StaticPrefs::dom_security_credentialmanagement_identity_enabled()) {
RefPtr<Promise> promise = CreatePromise(mParent, aRv);
if (!promise) {
return nullptr;
}
if (conditionallyMediated) {
promise->MaybeRejectWithTypeError<MSG_INVALID_ENUM_VALUE>(
"mediation", "conditional", "CredentialMediationRequirement");
return promise.forget();
}
WebIdentityHandler* identityHandler =
mParent->GetOrCreateWebIdentityHandler();
if (!identityHandler) {
promise->MaybeRejectWithOperationError("");
return promise.forget();
}
if (aOptions.mSignal.WasPassed()) {
identityHandler->Follow(&aOptions.mSignal.Value());
}
identityHandler->GetCredential(aOptions, IsSameOriginWithAncestors(mParent),
promise);
return promise.forget();
}
return CreateAndRejectWithNotSupported(mParent, aRv);
}
already_AddRefed<Promise> CredentialsContainer::Create(
const CredentialCreationOptions& aOptions, ErrorResult& aRv) {
// Count the types of options provided. Must not be >1.
uint64_t totalOptions = 0;
if (aOptions.mPublicKey.WasPassed() &&
StaticPrefs::security_webauth_webauthn()) {
totalOptions += 1;
}
if (totalOptions > 1) {
return CreateAndRejectWithNotSupported(mParent, aRv);
}
if (aOptions.mPublicKey.WasPassed() &&
StaticPrefs::security_webauth_webauthn()) {
MOZ_ASSERT(mParent);
// In a cross-origin iframe this request consumes user activation, i.e.
// subsequent requests cannot be made without further user interaction.
// See step 2.2 of https://w3c.github.io/webauthn/#sctn-createCredential
bool hasRequiredActivation =
IsInActiveTab(mParent) &&
(IsSameOriginWithAncestors(mParent) || ConsumeUserActivation(mParent));
if (!FeaturePolicyUtils::IsFeatureAllowed(
mParent->GetExtantDoc(), u"publickey-credentials-create"_ns) ||
!hasRequiredActivation) {
return CreateAndRejectWithNotAllowed(mParent, aRv);
}
if (aOptions.mMediation != CredentialMediationRequirement::Optional &&
aOptions.mMediation != CredentialMediationRequirement::Required) {
return CreateAndRejectWithNotSupported(mParent, aRv);
}
EnsureWebAuthnHandler();
return mWebAuthnHandler->MakeCredential(aOptions.mPublicKey.Value(),
aOptions.mSignal, aRv);
}
return CreateAndRejectWithNotSupported(mParent, aRv);
}
already_AddRefed<Promise> CredentialsContainer::Store(
const Credential& aCredential, ErrorResult& aRv) {
nsString type;
aCredential.GetType(type);
if (type.EqualsLiteral("public-key") &&
StaticPrefs::security_webauth_webauthn()) {
if (!IsSameOriginWithAncestors(mParent) || !IsInActiveTab(mParent)) {
return CreateAndRejectWithNotAllowed(mParent, aRv);
}
EnsureWebAuthnHandler();
return mWebAuthnHandler->Store(aCredential, aRv);
}
return CreateAndRejectWithNotSupported(mParent, aRv);
}
already_AddRefed<Promise> CredentialsContainer::PreventSilentAccess(
ErrorResult& aRv) {
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(mParent);
if (NS_WARN_IF(!global)) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
RefPtr<Promise> promise = Promise::Create(global, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
WebIdentityHandler* identityHandler =
mParent->GetOrCreateWebIdentityHandler();
if (!identityHandler) {
promise->MaybeRejectWithOperationError("");
return promise.forget();
}
identityHandler->PreventSilentAccess(promise);
return promise.forget();
}
} // namespace mozilla::dom
|