nsNullPrincipal.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  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. * This is the principal that has no rights and can't be accessed by
  7. * anything other than itself and chrome; null principals are not
  8. * same-origin with anything but themselves.
  9. */
  10. #include "mozilla/ArrayUtils.h"
  11. #include "nsDocShell.h"
  12. #include "nsNullPrincipal.h"
  13. #include "nsNullPrincipalURI.h"
  14. #include "nsMemory.h"
  15. #include "nsIURIWithPrincipal.h"
  16. #include "nsIClassInfoImpl.h"
  17. #include "nsNetCID.h"
  18. #include "nsError.h"
  19. #include "nsIObjectInputStream.h"
  20. #include "nsIObjectOutputStream.h"
  21. #include "nsIScriptSecurityManager.h"
  22. #include "nsPrincipal.h"
  23. #include "nsScriptSecurityManager.h"
  24. #include "pratom.h"
  25. using namespace mozilla;
  26. NS_IMPL_CLASSINFO(nsNullPrincipal, nullptr, nsIClassInfo::MAIN_THREAD_ONLY,
  27. NS_NULLPRINCIPAL_CID)
  28. NS_IMPL_QUERY_INTERFACE_CI(nsNullPrincipal,
  29. nsIPrincipal,
  30. nsISerializable)
  31. NS_IMPL_CI_INTERFACE_GETTER(nsNullPrincipal,
  32. nsIPrincipal,
  33. nsISerializable)
  34. /* static */ already_AddRefed<nsNullPrincipal>
  35. nsNullPrincipal::CreateWithInheritedAttributes(nsIPrincipal* aInheritFrom)
  36. {
  37. RefPtr<nsNullPrincipal> nullPrin = new nsNullPrincipal();
  38. nsresult rv = nullPrin->Init(Cast(aInheritFrom)->OriginAttributesRef());
  39. MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
  40. return nullPrin.forget();
  41. }
  42. /* static */ already_AddRefed<nsNullPrincipal>
  43. nsNullPrincipal::CreateWithInheritedAttributes(nsIDocShell* aDocShell)
  44. {
  45. PrincipalOriginAttributes attrs;
  46. attrs.InheritFromDocShellToDoc(nsDocShell::Cast(aDocShell)->GetOriginAttributes(), nullptr);
  47. RefPtr<nsNullPrincipal> nullPrin = new nsNullPrincipal();
  48. nsresult rv = nullPrin->Init(attrs);
  49. MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
  50. return nullPrin.forget();
  51. }
  52. /* static */ already_AddRefed<nsNullPrincipal>
  53. nsNullPrincipal::Create(const PrincipalOriginAttributes& aOriginAttributes)
  54. {
  55. RefPtr<nsNullPrincipal> nullPrin = new nsNullPrincipal();
  56. nsresult rv = nullPrin->Init(aOriginAttributes);
  57. MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
  58. return nullPrin.forget();
  59. }
  60. nsresult
  61. nsNullPrincipal::Init(const PrincipalOriginAttributes& aOriginAttributes)
  62. {
  63. mOriginAttributes = aOriginAttributes;
  64. mURI = nsNullPrincipalURI::Create();
  65. NS_ENSURE_TRUE(mURI, NS_ERROR_NOT_AVAILABLE);
  66. return NS_OK;
  67. }
  68. nsresult
  69. nsNullPrincipal::GetScriptLocation(nsACString &aStr)
  70. {
  71. return mURI->GetSpec(aStr);
  72. }
  73. /**
  74. * nsIPrincipal implementation
  75. */
  76. NS_IMETHODIMP
  77. nsNullPrincipal::GetHashValue(uint32_t *aResult)
  78. {
  79. *aResult = (NS_PTR_TO_INT32(this) >> 2);
  80. return NS_OK;
  81. }
  82. NS_IMETHODIMP
  83. nsNullPrincipal::SetCsp(nsIContentSecurityPolicy* aCsp) {
  84. // Never destroy an existing CSP on the principal.
  85. // This method should only be called in rare cases.
  86. MOZ_ASSERT(!mCSP, "do not destroy an existing CSP");
  87. if (mCSP) {
  88. return NS_ERROR_ALREADY_INITIALIZED;
  89. }
  90. mCSP = aCsp;
  91. return NS_OK;
  92. }
  93. NS_IMETHODIMP
  94. nsNullPrincipal::GetURI(nsIURI** aURI)
  95. {
  96. return NS_EnsureSafeToReturn(mURI, aURI);
  97. }
  98. NS_IMETHODIMP
  99. nsNullPrincipal::GetDomain(nsIURI** aDomain)
  100. {
  101. return NS_EnsureSafeToReturn(mURI, aDomain);
  102. }
  103. NS_IMETHODIMP
  104. nsNullPrincipal::SetDomain(nsIURI* aDomain)
  105. {
  106. // I think the right thing to do here is to just throw... Silently failing
  107. // seems counterproductive.
  108. return NS_ERROR_NOT_AVAILABLE;
  109. }
  110. nsresult
  111. nsNullPrincipal::GetOriginInternal(nsACString& aOrigin)
  112. {
  113. return mURI->GetSpec(aOrigin);
  114. }
  115. bool
  116. nsNullPrincipal::MayLoadInternal(nsIURI* aURI)
  117. {
  118. // Also allow the load if we are the principal of the URI being checked.
  119. nsCOMPtr<nsIURIWithPrincipal> uriPrinc = do_QueryInterface(aURI);
  120. if (uriPrinc) {
  121. nsCOMPtr<nsIPrincipal> principal;
  122. uriPrinc->GetPrincipal(getter_AddRefs(principal));
  123. if (principal == this) {
  124. return true;
  125. }
  126. }
  127. return false;
  128. }
  129. NS_IMETHODIMP
  130. nsNullPrincipal::GetBaseDomain(nsACString& aBaseDomain)
  131. {
  132. // For a null principal, we use our unique uuid as the base domain.
  133. return mURI->GetPath(aBaseDomain);
  134. }
  135. /**
  136. * nsISerializable implementation
  137. */
  138. NS_IMETHODIMP
  139. nsNullPrincipal::Read(nsIObjectInputStream* aStream)
  140. {
  141. // Note - nsNullPrincipal use NS_GENERIC_FACTORY_CONSTRUCTOR_INIT, which means
  142. // that the Init() method has already been invoked by the time we deserialize.
  143. // This is in contrast to nsPrincipal, which uses NS_GENERIC_FACTORY_CONSTRUCTOR,
  144. // in which case ::Read needs to invoke Init().
  145. nsAutoCString suffix;
  146. nsresult rv = aStream->ReadCString(suffix);
  147. NS_ENSURE_SUCCESS(rv, rv);
  148. bool ok = mOriginAttributes.PopulateFromSuffix(suffix);
  149. NS_ENSURE_TRUE(ok, NS_ERROR_FAILURE);
  150. return NS_OK;
  151. }
  152. NS_IMETHODIMP
  153. nsNullPrincipal::Write(nsIObjectOutputStream* aStream)
  154. {
  155. nsAutoCString suffix;
  156. OriginAttributesRef().CreateSuffix(suffix);
  157. nsresult rv = aStream->WriteStringZ(suffix.get());
  158. NS_ENSURE_SUCCESS(rv, rv);
  159. return NS_OK;
  160. }