nsScriptSecurityManager.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* -*- Mode: C++; tab-width: 8; 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. #ifndef nsScriptSecurityManager_h__
  6. #define nsScriptSecurityManager_h__
  7. #include "nsIScriptSecurityManager.h"
  8. #include "nsIAddonPolicyService.h"
  9. #include "mozilla/Maybe.h"
  10. #include "nsIAddonPolicyService.h"
  11. #include "nsIPrincipal.h"
  12. #include "nsCOMPtr.h"
  13. #include "nsIObserver.h"
  14. #include "nsServiceManagerUtils.h"
  15. #include "plstr.h"
  16. #include "js/TypeDecls.h"
  17. #include <stdint.h>
  18. class nsCString;
  19. class nsIIOService;
  20. class nsIStringBundle;
  21. class nsSystemPrincipal;
  22. namespace mozilla {
  23. class PrincipalOriginAttributes;
  24. } // namespace mozilla
  25. /////////////////////////////
  26. // nsScriptSecurityManager //
  27. /////////////////////////////
  28. #define NS_SCRIPTSECURITYMANAGER_CID \
  29. { 0x7ee2a4c0, 0x4b93, 0x17d3, \
  30. { 0xba, 0x18, 0x00, 0x60, 0xb0, 0xf1, 0x99, 0xa2 }}
  31. class nsScriptSecurityManager final : public nsIScriptSecurityManager,
  32. public nsIObserver
  33. {
  34. public:
  35. static void Shutdown();
  36. NS_DEFINE_STATIC_CID_ACCESSOR(NS_SCRIPTSECURITYMANAGER_CID)
  37. NS_DECL_ISUPPORTS
  38. NS_DECL_NSISCRIPTSECURITYMANAGER
  39. NS_DECL_NSIOBSERVER
  40. static nsScriptSecurityManager*
  41. GetScriptSecurityManager();
  42. // Invoked exactly once, by XPConnect.
  43. static void InitStatics();
  44. static nsSystemPrincipal*
  45. SystemPrincipalSingletonConstructor();
  46. /**
  47. * Utility method for comparing two URIs. For security purposes, two URIs
  48. * are equivalent if their schemes, hosts, and ports (if any) match. This
  49. * method returns true if aSubjectURI and aObjectURI have the same origin,
  50. * false otherwise.
  51. */
  52. static bool SecurityCompareURIs(nsIURI* aSourceURI, nsIURI* aTargetURI);
  53. static uint32_t SecurityHashURI(nsIURI* aURI);
  54. static uint16_t AppStatusForPrincipal(nsIPrincipal *aPrin);
  55. static nsresult
  56. ReportError(JSContext* cx, const nsAString& messageTag,
  57. nsIURI* aSource, nsIURI* aTarget);
  58. static uint32_t
  59. HashPrincipalByOrigin(nsIPrincipal* aPrincipal);
  60. static bool
  61. GetStrictFileOriginPolicy()
  62. {
  63. return sStrictFileOriginPolicy;
  64. }
  65. void DeactivateDomainPolicy();
  66. private:
  67. // GetScriptSecurityManager is the only call that can make one
  68. nsScriptSecurityManager();
  69. virtual ~nsScriptSecurityManager();
  70. // Decides, based on CSP, whether or not eval() and stuff can be executed.
  71. static bool
  72. ContentSecurityPolicyPermitsJSAction(JSContext *cx);
  73. static bool
  74. JSPrincipalsSubsume(JSPrincipals *first, JSPrincipals *second);
  75. // Returns null if a principal cannot be found; generally callers
  76. // should error out at that point.
  77. static nsIPrincipal* doGetObjectPrincipal(JSObject* obj);
  78. nsresult
  79. Init();
  80. nsresult
  81. InitPrefs();
  82. inline void
  83. ScriptSecurityPrefChanged();
  84. inline void
  85. AddSitesToFileURIWhitelist(const nsCString& aSiteList);
  86. // If aURI is a moz-extension:// URI, set mAddonId to the associated addon.
  87. nsresult MaybeSetAddonIdFromURI(mozilla::PrincipalOriginAttributes& aAttrs, nsIURI* aURI);
  88. nsresult GetChannelResultPrincipal(nsIChannel* aChannel,
  89. nsIPrincipal** aPrincipal,
  90. bool aIgnoreSandboxing);
  91. nsresult
  92. CheckLoadURIFlags(nsIURI* aSourceURI, nsIURI* aTargetURI, nsIURI* aSourceBaseURI,
  93. nsIURI* aTargetBaseURI, uint32_t aFlags);
  94. // Returns the file URI whitelist, initializing it if it has not been
  95. // initialized.
  96. const nsTArray<nsCOMPtr<nsIURI>>& EnsureFileURIWhitelist();
  97. nsCOMPtr<nsIPrincipal> mSystemPrincipal;
  98. bool mPrefInitialized;
  99. bool mIsJavaScriptEnabled;
  100. // List of URIs whose domains and sub-domains are whitelisted to allow
  101. // access to file: URIs. Lazily initialized; isNothing() when not yet
  102. // initialized.
  103. mozilla::Maybe<nsTArray<nsCOMPtr<nsIURI>>> mFileURIWhitelist;
  104. // This machinery controls new-style domain policies. The old-style
  105. // policy machinery will be removed soon.
  106. nsCOMPtr<nsIDomainPolicy> mDomainPolicy;
  107. // Cached addon policy service. We can't generate this in Init() because
  108. // that's too early to get a service.
  109. mozilla::Maybe<nsCOMPtr<nsIAddonPolicyService>> mAddonPolicyService;
  110. nsIAddonPolicyService* GetAddonPolicyService()
  111. {
  112. if (mAddonPolicyService.isNothing()) {
  113. mAddonPolicyService.emplace(do_GetService("@mozilla.org/addons/policy-service;1"));
  114. }
  115. return mAddonPolicyService.ref();
  116. }
  117. static bool sStrictFileOriginPolicy;
  118. static nsIIOService *sIOService;
  119. static nsIStringBundle *sStrBundle;
  120. static JSContext *sContext;
  121. };
  122. #endif // nsScriptSecurityManager_h__