nsIPrincipal.idl (25197B)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 /* Defines the abstract interface for a principal. */ 7 8 #include "nsIContentSecurityPolicy.idl" 9 #include "nsISerializable.idl" 10 #include "nsIAboutModule.idl" 11 #include "nsIReferrerInfo.idl" 12 interface nsIChannel; 13 #include "mozIDOMWindow.idl" 14 15 %{C++ 16 struct JSPrincipals; 17 #include "nsCOMPtr.h" 18 #include "nsTArray.h" 19 #include "nsString.h" 20 #include "mozilla/DebugOnly.h" 21 namespace mozilla { 22 class OriginAttributes; 23 } 24 25 /** 26 * Some methods have a fast path for the case when we're comparing a principal 27 * to itself. The situation may happen for example with about:blank documents. 28 */ 29 30 #define DECL_FAST_INLINE_HELPER(method_) \ 31 inline bool method_(nsIPrincipal* aOther) \ 32 { \ 33 mozilla::DebugOnly<bool> val = false; \ 34 MOZ_ASSERT_IF(this == aOther, \ 35 NS_SUCCEEDED(method_(aOther, &val)) && val); \ 36 \ 37 bool retVal = false; \ 38 return \ 39 this == aOther || \ 40 (NS_SUCCEEDED(method_(aOther, &retVal)) && retVal); \ 41 } 42 43 %} 44 45 interface nsIURI; 46 47 webidl WebExtensionPolicy; 48 49 [ptr] native JSContext(JSContext); 50 [ptr] native JSPrincipals(JSPrincipals); 51 [ref] native PrincipalArray(const nsTArray<nsCOMPtr<nsIPrincipal>>); 52 [ref] native const_OriginAttributes(const mozilla::OriginAttributes); 53 native ReferrerPolicy(mozilla::dom::ReferrerPolicy); 54 55 [scriptable, builtinclass, uuid(f75f502d-79fd-48be-a079-e5a7b8f80c8b)] 56 interface nsIPrincipal : nsISupports 57 { 58 /** 59 * Returns whether the other principal is equivalent to this principal. 60 * Principals are considered equal if they are the same principal, or 61 * they have the same origin. 62 * 63 * May be called from any thread. 64 */ 65 boolean equals(in nsIPrincipal other); 66 67 /** 68 * Returns whether the other principal is equivalent to this principal 69 * for permission purposes 70 * Matches {originAttributes ,equalsURIForPermission} 71 * 72 * May be called from any thread. 73 */ 74 75 boolean equalsForPermission(in nsIPrincipal other, in boolean aExactHost); 76 77 /** 78 * Like equals, but takes document.domain changes into account. 79 * 80 * May be called from any thread, though document.domain may racily change 81 * during the comparison when called from off-main-thread. 82 */ 83 boolean equalsConsideringDomain(in nsIPrincipal other); 84 85 %{C++ 86 DECL_FAST_INLINE_HELPER(Equals) 87 DECL_FAST_INLINE_HELPER(EqualsConsideringDomain) 88 %} 89 90 /* 91 * Returns whether the Principals URI is equal to the other URI 92 * 93 * May be called from any thread. 94 */ 95 boolean equalsURI(in nsIURI aOtherURI); 96 97 /** 98 * Returns a hash value for the principal. Guaranteed to be equal 99 * for two principals which are Equals(...). 100 * 101 * Implemented in BasePrincipal.cpp. 102 * 103 * May be called from any thread. 104 */ 105 %{C++ 106 uint32_t GetHashValue() const; 107 %} 108 109 /** 110 * The principal URI to which this principal pertains. This is 111 * generally the document URI. 112 * 113 * May be called from any thread. 114 */ 115 [infallible] readonly attribute nsIURI URI; 116 117 /** 118 * The domain URI to which this principal pertains. 119 * This is null unless script successfully sets document.domain to our URI 120 * or a superdomain of our URI. 121 * Setting this has no effect on the URI. 122 * See https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy#Changing_origin 123 * 124 * The getter may be called from any thread, but may only be set on the main thread. 125 */ 126 [noscript] attribute nsIURI domain; 127 128 /** 129 * Returns whether the other principal is equal to or weaker than this 130 * principal. Principals are equal if they are the same object or they 131 * have the same origin. 132 * 133 * Thus a principal always subsumes itself. 134 * 135 * The system principal subsumes itself and all other principals. 136 * 137 * A null principal (corresponding to an unknown, hence assumed minimally 138 * privileged, security context) is not equal to any other principal 139 * (including other null principals), and therefore does not subsume 140 * anything but itself. 141 * 142 * May be called from any thread. 143 */ 144 boolean subsumes(in nsIPrincipal other); 145 146 /** 147 * Same as the previous method, subsumes(), but takes document.domain into 148 * account. 149 * 150 * May be called from any thread, though document.domain may racily change 151 * during the comparison when called from off-main-thread. 152 */ 153 boolean subsumesConsideringDomain(in nsIPrincipal other); 154 155 /** 156 * Same as the subsumesConsideringDomain(), but ignores the first party 157 * domain in its originAttributes. 158 * 159 * May be called from any thread, though document.domain may racily change 160 * during the comparison when called from off-main-thread. 161 */ 162 boolean subsumesConsideringDomainIgnoringFPD(in nsIPrincipal other); 163 164 %{C++ 165 DECL_FAST_INLINE_HELPER(Subsumes) 166 DECL_FAST_INLINE_HELPER(SubsumesConsideringDomain) 167 DECL_FAST_INLINE_HELPER(SubsumesConsideringDomainIgnoringFPD) 168 #undef DECL_FAST_INLINE_HELPER 169 %} 170 171 /** 172 * Checks whether this principal is allowed to load the network resource 173 * located at the given URI under the same-origin policy. This means that 174 * content principals are only allowed to load resources from the same 175 * domain, the system principal is allowed to load anything, and null 176 * principals can only load URIs where they are the principal. This is 177 * changed by the optional flag allowIfInheritsPrincipal (which defaults to 178 * false) which allows URIs that inherit their loader's principal. 179 * 180 * If the load is allowed this function does nothing. If the load is not 181 * allowed the function throws NS_ERROR_DOM_BAD_URI. 182 * 183 * May be called from any thread. 184 * 185 * NOTE: Other policies might override this, such as the Access-Control 186 * specification. 187 * NOTE: The 'domain' attribute has no effect on the behaviour of this 188 * function. 189 * 190 * 191 * @param uri The URI about to be loaded. 192 * @param allowIfInheritsPrincipal If true, the load is allowed if the 193 * loadee inherits the principal of the 194 * loader. 195 * @throws NS_ERROR_DOM_BAD_URI if the load is not allowed. 196 */ 197 void checkMayLoad(in nsIURI uri, 198 in boolean allowIfInheritsPrincipal); 199 200 /** 201 * Like checkMayLoad, but if returning an error will also report that error 202 * to the console, using the provided window id. The window id may be 0 to 203 * report to just the browser console, not web consoles. 204 * 205 * NOTE: Main-Thread Only. 206 */ 207 void checkMayLoadWithReporting(in nsIURI uri, 208 in boolean allowIfInheritsPrincipal, 209 in unsigned long long innerWindowID); 210 211 /** 212 * Checks if the provided URI is considered third-party to the 213 * URI of the principal. 214 * Returns true if the URI is third-party. 215 * 216 * May be called from any thread. 217 * 218 * @param uri - The URI to check 219 */ 220 boolean isThirdPartyURI(in nsIURI uri); 221 222 /** 223 * Checks if the provided principal is considered third-party to the 224 * URI of the Principal. 225 * Returns true if the principal is third-party. 226 * 227 * May be called from any thread. 228 * 229 * @param principal - The principal to check 230 */ 231 boolean isThirdPartyPrincipal(in nsIPrincipal principal); 232 233 /** 234 * Checks if the provided channel is considered third-party to the 235 * URI of the principal. 236 * Returns true if the channel is third-party. 237 * Returns false if the Principal is a System Principal 238 * 239 * NOTE: Main-Thread Only. 240 * 241 * @param channel - The Channel to check 242 */ 243 boolean isThirdPartyChannel(in nsIChannel channel); 244 245 /** 246 * A dictionary of the non-default origin attributes associated with this 247 * nsIPrincipal. 248 * 249 * Attributes are tokens that are taken into account when determining whether 250 * two principals are same-origin - if any attributes differ, the principals 251 * are cross-origin, even if the scheme, host, and port are the same. 252 * Attributes should also be considered for all security and bucketing decisions, 253 * even those which make non-standard comparisons (like cookies, which ignore 254 * scheme, or quotas, which ignore subdomains). 255 * 256 * If you're looking for an easy-to-use canonical stringification of the origin 257 * attributes, see |originSuffix| below. 258 */ 259 [implicit_jscontext] 260 readonly attribute jsval originAttributes; 261 262 // May be called from any thread. 263 [noscript, notxpcom, nostdcall, binaryname(OriginAttributesRef)] 264 const_OriginAttributes OriginAttributesRef(); 265 266 /** 267 * A canonical representation of the origin for this principal. This 268 * consists of a base string (which, for content principals, is of the 269 * format scheme://host:port), concatenated with |originAttributes| (see 270 * below). 271 * 272 * We maintain the invariant that principalA.equals(principalB) if and only 273 * if principalA.origin == principalB.origin. 274 * 275 * May be called from any thread. 276 */ 277 readonly attribute ACString origin; 278 279 /** 280 * Returns an ASCII compatible serialization of the principal's origin, as 281 * specified by the whatwg HTML specification. If the principal does not 282 * have a host, the origin will be "null". 283 * 284 * https://html.spec.whatwg.org/multipage/browsers.html#ascii-serialisation-of-an-origin 285 * 286 * Note that this is different from `origin`, does not contain 287 * gecko-specific metadata like origin attributes, and should not be used 288 * for permissions or security checks. 289 * 290 * May be called from any thread. 291 */ 292 [noscript] readonly attribute ACString webExposedOriginSerialization; 293 294 /** 295 * Returns the "host:port" portion of the 296 * Principals URI, if any. 297 * 298 * May be called from any thread. 299 */ 300 readonly attribute ACString hostPort; 301 302 /** 303 * Returns the "host:port" portion of the 304 * Principals URI, if any. 305 * 306 * May be called from any thread. 307 */ 308 readonly attribute ACString asciiHost; 309 310 /** 311 * Returns the "host" portion of the 312 * Principals URI, if any. 313 * 314 * May be called from any thread. 315 */ 316 readonly attribute ACString host; 317 318 /** 319 * Returns the prePath of the principals uri 320 * follows the format scheme: 321 * "scheme://username:password@hostname:portnumber/" 322 * 323 * May be called from any thread. 324 */ 325 readonly attribute ACString prePath; 326 327 /** 328 * Returns the filePath of the principals uri. See nsIURI. 329 * 330 * May be called from any thread. 331 */ 332 readonly attribute ACString filePath; 333 334 /** 335 * Returns the ASCII Spec from the Principals URI. 336 * Might return the empty string, e.g. for the case of 337 * a SystemPrincipal or an EpxandedPrincipal. 338 * 339 * May be called from any thread. 340 * 341 * WARNING: DO NOT USE FOR SECURITY CHECKS. 342 * just for logging purposes! 343 */ 344 readonly attribute ACString asciiSpec; 345 346 /** 347 * Returns the Spec from the Principals URI. 348 * Might return the empty string, e.g. for the case of 349 * a SystemPrincipal or an EpxandedPrincipal. 350 * 351 * May be called from any thread. 352 * 353 * WARNING: Do not land new Code using, as this will be removed soon 354 */ 355 readonly attribute ACString spec; 356 357 /** 358 * Returns the Pre Path of the Principals URI with 359 * user:pass stripped for privacy and spoof prevention 360 * 361 * May be called from any thread. 362 */ 363 readonly attribute AUTF8String exposablePrePath; 364 365 /** 366 * Returns the Spec of the Principals URI with 367 * user/pass/ref/query stripped for privacy and spoof prevention 368 * 369 * May be called from any thread. 370 */ 371 readonly attribute AUTF8String exposableSpec; 372 373 /** 374 * Return the scheme of the principals URI 375 * 376 * May be called from any thread. 377 */ 378 readonly attribute ACString scheme; 379 380 /** 381 * Checks if the Principal's URI Scheme matches with the parameter 382 * 383 * May be called from any thread. 384 * 385 * @param scheme The scheme to be checked 386 */ 387 [infallible] 388 boolean schemeIs(in string scheme); 389 390 /* 391 * Checks if the Principal's URI is contained in the given Pref 392 * 393 * NOTE: Main-Thread Only. 394 * 395 * @param pref The pref to be checked 396 */ 397 [infallible] 398 boolean isURIInPrefList(in string pref); 399 400 /** 401 * Check if the Principal's URI is contained in the given list 402 * 403 * May be called from any thread. 404 * 405 * @param list The list to be checked 406 */ 407 [infallible] 408 boolean isURIInList(in ACString list); 409 410 /** 411 * Check if the Principal's URI is a content-accessible about: page 412 * 413 * May be called from any thread. 414 */ 415 [infallible] 416 boolean isContentAccessibleAboutURI(); 417 418 /** 419 * Uses NS_Security Compare to determine if the 420 * other URI is same-origin as the uri of the Principal 421 * 422 * May be called from any thread. 423 */ 424 [infallible] 425 boolean isSameOrigin(in nsIURI otherURI); 426 427 428 /* 429 * Generates a Cache-Key for the Cors-Preflight Cache 430 * 431 * May be called from any thread. 432 */ 433 [noscript] 434 ACString getPrefLightCacheKey(in nsIURI aURI ,in boolean aWithCredentials, 435 in const_OriginAttributes aOriginAttributes); 436 437 438 /* 439 * Checks if the Principals URI has first party storage access 440 * when loaded inside the provided 3rd party resource window. 441 * See also: ContentBlocking::ShouldAllowAccessFor 442 * 443 * NOTE: Main-Thread Only. 444 */ 445 boolean hasFirstpartyStorageAccess(in mozIDOMWindow aWindow, out uint32_t rejectedReason); 446 447 448 /* 449 * Returns a Key for the LocalStorage Manager, used to 450 * check the Principals Origin Storage usage. 451 * 452 * May be called from any thread. 453 */ 454 readonly attribute ACString localStorageQuotaKey; 455 456 /** 457 * Implementation of 458 * https://w3c.github.io/webappsec-secure-contexts/#is-origin-trustworthy 459 * 460 * The value returned by this method feeds into the the Secure Context 461 * algorithm that determins the value of Window.isSecureContext and 462 * WorkerGlobalScope.isSecureContext. 463 * 464 * This method returns false instead of throwing upon errors. 465 * 466 * NOTE: Main-Thread Only. 467 */ 468 [infallible] 469 readonly attribute boolean isOriginPotentiallyTrustworthy; 470 471 /** 472 * NOTE: Main-Thread Only. 473 */ 474 [infallible] 475 readonly attribute boolean isLoopbackHost; 476 477 /** 478 * Returns the Flags of the Principals 479 * associated AboutModule, in case there is one. 480 * 481 * NOTE: Main-Thread Only. 482 */ 483 uint32_t getAboutModuleFlags(); 484 485 /** 486 * Returns the Key to access the Principals 487 * Origin Local/Session Storage 488 * 489 * May be called from any thread. 490 */ 491 readonly attribute ACString storageOriginKey; 492 493 /** 494 * Creates and Returns a new ReferrerInfo with the 495 * Principals URI 496 * 497 * May be called from any thread. 498 */ 499 [noscript] nsIReferrerInfo createReferrerInfo(in ReferrerPolicy aReferrerPolicy); 500 501 /** 502 * The base part of |origin| without the concatenation with |originSuffix|. 503 * This doesn't have the important invariants described above with |origin|, 504 * and as such should only be used for legacy situations. 505 * 506 * May be called from any thread. 507 */ 508 readonly attribute ACString originNoSuffix; 509 510 /** 511 * A string of the form ^key1=value1&key2=value2, where each pair represents 512 * an attribute with a non-default value. If all attributes have default 513 * values, this is the empty string. 514 * 515 * The value of .originSuffix is automatically serialized into .origin, so any 516 * consumers using that are automatically origin-attribute-aware. Consumers with 517 * special requirements must inspect and compare .originSuffix manually. 518 * 519 * May be called from any thread. 520 */ 521 readonly attribute AUTF8String originSuffix; 522 523 /** 524 * A canonical representation of the site-origin for this principal. 525 * This string has the same format as |origin| (see above). Two principals 526 * with differing |siteOrigin| values will never compare equal, even when 527 * considering domain mutations. 528 * 529 * For most principals, |siteOrigin| matches |origin| precisely. Only 530 * principals which allow mutating |domain|, such as ContentPrincipal, 531 * override the default implementation in BasePrincipal. 532 * 533 * May be called from any thread. 534 */ 535 readonly attribute ACString siteOrigin; 536 537 /** 538 * The base part of |siteOrigin| without the concatenation with 539 * |originSuffix|. 540 * 541 * May be called from any thread. 542 */ 543 readonly attribute ACString siteOriginNoSuffix; 544 545 /** 546 * The base domain of the principal URI to which this principal pertains 547 * (generally the document URI), handling null principals and 548 * non-hierarchical schemes correctly. 549 * 550 * May be called from any thread. 551 */ 552 readonly attribute ACString baseDomain; 553 554 /** 555 * Gets the ID of the add-on this principal belongs to. 556 * 557 * May be called from any thread. 558 */ 559 readonly attribute AString addonId; 560 561 /** 562 * Gets the WebExtensionPolicy of the add-on this principal belongs to. 563 * 564 * NOTE: Main-Thread Only. 565 */ 566 readonly attribute WebExtensionPolicy addonPolicy; 567 readonly attribute WebExtensionPolicy contentScriptAddonPolicy; 568 569 /** 570 * Gets the id of the user context this principal is inside. If this 571 * principal is inside the default userContext, this returns 572 * nsIScriptSecurityManager::DEFAULT_USER_CONTEXT_ID. 573 * 574 * May be called from any thread. 575 */ 576 [infallible] readonly attribute unsigned long userContextId; 577 578 /** 579 * Gets the id of the private browsing state of the context containing 580 * this principal. If the principal has a private browsing value of 0, it 581 * is not in private browsing. 582 * 583 * May be called from any thread. 584 */ 585 [infallible] readonly attribute unsigned long privateBrowsingId; 586 587 /** 588 * Retuns true if it is in private browsing based on privateBrowsingId 589 * being non-zero. 590 * 591 * May be called from any thread. 592 */ 593 [infallible] readonly attribute boolean isInPrivateBrowsing; 594 595 /** 596 * Returns true iff this is a null principal (corresponding to an 597 * unknown, hence assumed minimally privileged, security context). 598 * 599 * May be called from any thread. 600 */ 601 [infallible] readonly attribute boolean isNullPrincipal; 602 603 /** 604 * Returns true iff this principal corresponds to a principal origin. 605 * 606 * May be called from any thread. 607 */ 608 [infallible] readonly attribute boolean isContentPrincipal; 609 610 /** 611 * Returns true iff this is an expanded principal. 612 * 613 * May be called from any thread. 614 */ 615 [infallible] readonly attribute boolean isExpandedPrincipal; 616 617 /** 618 * Returns true iff this is the system principal. C++ callers should use 619 * IsSystemPrincipal() instead of this scriptable accessor. 620 * 621 * May be called from any thread. 622 */ 623 readonly attribute boolean isSystemPrincipal; 624 625 /** 626 * Faster and nicer version callable from C++. Callers must include 627 * BasePrincipal.h, where it's implemented. 628 * 629 * May be called from any thread. 630 */ 631 %{C++ 632 inline bool IsSystemPrincipal() const; 633 %} 634 635 /** 636 * Returns true iff the principal is either an addon principal or 637 * an expanded principal, which contains at least one addon principal. 638 * 639 * May be called from any thread. 640 */ 641 [infallible] readonly attribute boolean isAddonOrExpandedAddonPrincipal; 642 643 %{C++ 644 // MOZ_DBG support (threadsafe) 645 friend std::ostream& operator<<(std::ostream& aOut, const nsIPrincipal& aPrincipal) { 646 nsIPrincipal* principal = const_cast<nsIPrincipal*>(&aPrincipal); 647 nsAutoCString origin; 648 mozilla::DebugOnly<nsresult> rv = principal->GetOrigin(origin); 649 MOZ_ASSERT(NS_SUCCEEDED(rv)); 650 return aOut << "nsIPrincipal { " << origin << " }"; 651 } 652 %} 653 654 /** 655 * Returns true if the URI is an Onion URI. 656 * 657 * May be called from any thread. 658 */ 659 [infallible] readonly attribute boolean isOnion; 660 661 /** 662 * Returns true if the Domain Policy allows js execution 663 * for the Principal's URI 664 * 665 * NOTE: Main-Thread Only. 666 */ 667 readonly attribute boolean isScriptAllowedByPolicy; 668 669 670 /** 671 * Returns true if the Principal can acess l10n 672 * features for the Provided DocumentURI 673 * 674 * NOTE: Main-Thread Only. 675 */ 676 boolean isL10nAllowed(in nsIURI aDocumentURI); 677 678 /** 679 * Returns a nsIPrincipal, with one less Subdomain Segment 680 * Returns `nullptr` if there are no more segments to remove. 681 * 682 * May be called from any thread. 683 */ 684 [infallible] readonly attribute nsIPrincipal nextSubDomainPrincipal; 685 686 /** 687 * Returns if the principal is for an IP address. 688 * 689 * May be called from any thread. 690 */ 691 [infallible] readonly attribute boolean isIpAddress; 692 693 /** 694 * Returns if the principal is for a local IP address. 695 * 696 * May be called from any thread. 697 */ 698 [infallible] readonly attribute boolean isLocalIpAddress; 699 700 /** 701 * If this principal is a null principal, reconstruct the precursor 702 * principal which this null principal was derived from. This may be null, 703 * in which case this is not a null principal, there is no known precursor 704 * to this null principal, it was created by a privileged context, or there 705 * was a bugged origin in the precursor string. 706 * 707 * May be called from any thread. 708 * 709 * WARNING: Be careful when using this principal, as it is not part of the 710 * security properties of the null principal, and should NOT be used to 711 * grant a resource with a null principal access to resources from its 712 * precursor origin. This is only to be used for places where tracking how 713 * null principals were created is necessary. 714 */ 715 [infallible] readonly attribute nsIPrincipal precursorPrincipal; 716 }; 717 718 /** 719 * If SystemPrincipal is too risky to use, but we want a principal to access 720 * more than one origin, ExpandedPrincipals letting us define an array of 721 * principals it subsumes. So script with an ExpandedPrincipals will gain 722 * same origin access when at least one of its principals it contains gained 723 * sameorigin acccess. An ExpandedPrincipal will be subsumed by the system 724 * principal, and by another ExpandedPrincipal that has all its principals. 725 * It is added for jetpack content-scripts to let them interact with the 726 * content and a well defined set of other domains, without the risk of 727 * leaking out a system principal to the content. See: Bug 734891 728 */ 729 [uuid(f3e177Df-6a5e-489f-80a7-2dd1481471d8)] 730 interface nsIExpandedPrincipal : nsISupports 731 { 732 /** 733 * An array of principals that the expanded principal subsumes. 734 * 735 * When an expanded principal is used as a triggering principal for a 736 * request that inherits a security context, one of its constitutent 737 * principals is inherited rather than the expanded principal itself. The 738 * last principal in the allowlist is the default principal to inherit. 739 * 740 * Note: this list is not reference counted, it is shared, so 741 * should not be changed and should only be used ephemerally. 742 * 743 * May be called from any thread. 744 */ 745 [noscript, notxpcom, nostdcall] 746 PrincipalArray AllowList(); 747 748 749 /** 750 * Bug 1548468: Move CSP off ExpandedPrincipal. 751 * 752 * A Content Security Policy associated with this principal. Use this function 753 * to query the associated CSP with this principal. 754 * 755 * NOTE: Main-Thread Only. 756 */ 757 readonly attribute nsIContentSecurityPolicy csp; 758 759 %{ C++ 760 inline already_AddRefed<nsIContentSecurityPolicy> GetCsp() 761 { 762 nsCOMPtr<nsIContentSecurityPolicy> result; 763 mozilla::DebugOnly<nsresult> rv = GetCsp(getter_AddRefs(result)); 764 MOZ_ASSERT(NS_SUCCEEDED(rv)); 765 return result.forget(); 766 } 767 %} 768 769 };