nsJSPrincipals.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. #include "xpcpublic.h"
  6. #include "nsString.h"
  7. #include "nsIObjectOutputStream.h"
  8. #include "nsIObjectInputStream.h"
  9. #include "nsJSPrincipals.h"
  10. #include "plstr.h"
  11. #include "nsXPIDLString.h"
  12. #include "nsCOMPtr.h"
  13. #include "nsIServiceManager.h"
  14. #include "nsMemory.h"
  15. #include "nsStringBuffer.h"
  16. #include "mozilla/ipc/PBackgroundSharedTypes.h"
  17. #include "mozilla/dom/StructuredCloneTags.h"
  18. // for mozilla::dom::workers::kJSPrincipalsDebugToken
  19. #include "mozilla/dom/workers/Workers.h"
  20. #include "mozilla/ipc/BackgroundUtils.h"
  21. using namespace mozilla;
  22. using namespace mozilla::ipc;
  23. using namespace mozilla::dom;
  24. NS_IMETHODIMP_(MozExternalRefCountType)
  25. nsJSPrincipals::AddRef()
  26. {
  27. MOZ_ASSERT(NS_IsMainThread());
  28. NS_PRECONDITION(int32_t(refcount) >= 0, "illegal refcnt");
  29. nsrefcnt count = ++refcount;
  30. NS_LOG_ADDREF(this, count, "nsJSPrincipals", sizeof(*this));
  31. return count;
  32. }
  33. NS_IMETHODIMP_(MozExternalRefCountType)
  34. nsJSPrincipals::Release()
  35. {
  36. MOZ_ASSERT(NS_IsMainThread());
  37. NS_PRECONDITION(0 != refcount, "dup release");
  38. nsrefcnt count = --refcount;
  39. NS_LOG_RELEASE(this, count, "nsJSPrincipals");
  40. if (count == 0) {
  41. delete this;
  42. }
  43. return count;
  44. }
  45. /* static */ bool
  46. nsJSPrincipals::Subsume(JSPrincipals *jsprin, JSPrincipals *other)
  47. {
  48. bool result;
  49. nsresult rv = nsJSPrincipals::get(jsprin)->Subsumes(nsJSPrincipals::get(other), &result);
  50. return NS_SUCCEEDED(rv) && result;
  51. }
  52. /* static */ void
  53. nsJSPrincipals::Destroy(JSPrincipals *jsprin)
  54. {
  55. // The JS runtime can call this method during the last GC when
  56. // nsScriptSecurityManager is destroyed. So we must not assume here that
  57. // the security manager still exists.
  58. nsJSPrincipals *nsjsprin = nsJSPrincipals::get(jsprin);
  59. // We need to destroy the nsIPrincipal. We'll do this by adding
  60. // to the refcount and calling release
  61. #ifdef NS_BUILD_REFCNT_LOGGING
  62. // The refcount logging considers AddRef-to-1 to indicate creation,
  63. // so trick it into thinking it's otherwise, but balance the
  64. // Release() we do below.
  65. nsjsprin->refcount++;
  66. nsjsprin->AddRef();
  67. nsjsprin->refcount--;
  68. #else
  69. nsjsprin->refcount++;
  70. #endif
  71. nsjsprin->Release();
  72. }
  73. #ifdef DEBUG
  74. // Defined here so one can do principals->dump() in the debugger
  75. JS_PUBLIC_API(void)
  76. JSPrincipals::dump()
  77. {
  78. if (debugToken == nsJSPrincipals::DEBUG_TOKEN) {
  79. nsAutoCString str;
  80. nsresult rv = static_cast<nsJSPrincipals *>(this)->GetScriptLocation(str);
  81. fprintf(stderr, "nsIPrincipal (%p) = %s\n", static_cast<void*>(this),
  82. NS_SUCCEEDED(rv) ? str.get() : "(unknown)");
  83. } else if (debugToken == dom::workers::kJSPrincipalsDebugToken) {
  84. fprintf(stderr, "Web Worker principal singleton (%p)\n", this);
  85. } else {
  86. fprintf(stderr,
  87. "!!! JSPrincipals (%p) is not nsJSPrincipals instance - bad token: "
  88. "actual=0x%x expected=0x%x\n",
  89. this, unsigned(debugToken), unsigned(nsJSPrincipals::DEBUG_TOKEN));
  90. }
  91. }
  92. #endif
  93. /* static */ bool
  94. nsJSPrincipals::ReadPrincipals(JSContext* aCx, JSStructuredCloneReader* aReader,
  95. JSPrincipals** aOutPrincipals)
  96. {
  97. uint32_t tag;
  98. uint32_t unused;
  99. if (!JS_ReadUint32Pair(aReader, &tag, &unused)) {
  100. return false;
  101. }
  102. if (!(tag == SCTAG_DOM_NULL_PRINCIPAL ||
  103. tag == SCTAG_DOM_SYSTEM_PRINCIPAL ||
  104. tag == SCTAG_DOM_CONTENT_PRINCIPAL ||
  105. tag == SCTAG_DOM_EXPANDED_PRINCIPAL)) {
  106. xpc::Throw(aCx, NS_ERROR_DOM_DATA_CLONE_ERR);
  107. return false;
  108. }
  109. return ReadKnownPrincipalType(aCx, aReader, tag, aOutPrincipals);
  110. }
  111. static bool
  112. ReadSuffixAndSpec(JSStructuredCloneReader* aReader,
  113. PrincipalOriginAttributes& aAttrs,
  114. nsACString& aSpec)
  115. {
  116. uint32_t suffixLength, specLength;
  117. if (!JS_ReadUint32Pair(aReader, &suffixLength, &specLength)) {
  118. return false;
  119. }
  120. nsAutoCString suffix;
  121. if (!suffix.SetLength(suffixLength, fallible)) {
  122. return false;
  123. }
  124. if (!JS_ReadBytes(aReader, suffix.BeginWriting(), suffixLength)) {
  125. return false;
  126. }
  127. if (!aAttrs.PopulateFromSuffix(suffix)) {
  128. return false;
  129. }
  130. if (!aSpec.SetLength(specLength, fallible)) {
  131. return false;
  132. }
  133. if (!JS_ReadBytes(aReader, aSpec.BeginWriting(), specLength)) {
  134. return false;
  135. }
  136. return true;
  137. }
  138. static bool
  139. ReadPrincipalInfo(JSStructuredCloneReader* aReader,
  140. uint32_t aTag,
  141. PrincipalInfo& aInfo)
  142. {
  143. if (aTag == SCTAG_DOM_SYSTEM_PRINCIPAL) {
  144. aInfo = SystemPrincipalInfo();
  145. } else if (aTag == SCTAG_DOM_NULL_PRINCIPAL) {
  146. PrincipalOriginAttributes attrs;
  147. nsAutoCString dummy;
  148. if (!ReadSuffixAndSpec(aReader, attrs, dummy)) {
  149. return false;
  150. }
  151. aInfo = NullPrincipalInfo(attrs);
  152. } else if (aTag == SCTAG_DOM_EXPANDED_PRINCIPAL) {
  153. uint32_t length, unused;
  154. if (!JS_ReadUint32Pair(aReader, &length, &unused)) {
  155. return false;
  156. }
  157. ExpandedPrincipalInfo expanded;
  158. for (uint32_t i = 0; i < length; i++) {
  159. uint32_t tag;
  160. if (!JS_ReadUint32Pair(aReader, &tag, &unused)) {
  161. return false;
  162. }
  163. PrincipalInfo sub;
  164. if (!ReadPrincipalInfo(aReader, tag, sub)) {
  165. return false;
  166. }
  167. expanded.whitelist().AppendElement(sub);
  168. }
  169. aInfo = expanded;
  170. } else if (aTag == SCTAG_DOM_CONTENT_PRINCIPAL) {
  171. PrincipalOriginAttributes attrs;
  172. nsAutoCString spec;
  173. if (!ReadSuffixAndSpec(aReader, attrs, spec)) {
  174. return false;
  175. }
  176. aInfo = ContentPrincipalInfo(attrs, void_t(), spec);
  177. } else {
  178. MOZ_CRASH("unexpected principal structured clone tag");
  179. }
  180. return true;
  181. }
  182. /* static */ bool
  183. nsJSPrincipals::ReadKnownPrincipalType(JSContext* aCx,
  184. JSStructuredCloneReader* aReader,
  185. uint32_t aTag,
  186. JSPrincipals** aOutPrincipals)
  187. {
  188. MOZ_ASSERT(aTag == SCTAG_DOM_NULL_PRINCIPAL ||
  189. aTag == SCTAG_DOM_SYSTEM_PRINCIPAL ||
  190. aTag == SCTAG_DOM_CONTENT_PRINCIPAL ||
  191. aTag == SCTAG_DOM_EXPANDED_PRINCIPAL);
  192. if (NS_WARN_IF(!NS_IsMainThread())) {
  193. xpc::Throw(aCx, NS_ERROR_UNCATCHABLE_EXCEPTION);
  194. return false;
  195. }
  196. PrincipalInfo info;
  197. if (!ReadPrincipalInfo(aReader, aTag, info)) {
  198. return false;
  199. }
  200. nsresult rv;
  201. nsCOMPtr<nsIPrincipal> prin = PrincipalInfoToPrincipal(info, &rv);
  202. if (NS_WARN_IF(NS_FAILED(rv))) {
  203. xpc::Throw(aCx, NS_ERROR_DOM_DATA_CLONE_ERR);
  204. return false;
  205. }
  206. *aOutPrincipals = get(prin.forget().take());
  207. return true;
  208. }
  209. static bool
  210. WriteSuffixAndSpec(JSStructuredCloneWriter* aWriter,
  211. const PrincipalOriginAttributes& aAttrs,
  212. const nsCString& aSpec)
  213. {
  214. nsAutoCString suffix;
  215. aAttrs.CreateSuffix(suffix);
  216. return JS_WriteUint32Pair(aWriter, suffix.Length(), aSpec.Length()) &&
  217. JS_WriteBytes(aWriter, suffix.get(), suffix.Length()) &&
  218. JS_WriteBytes(aWriter, aSpec.get(), aSpec.Length());
  219. }
  220. static bool
  221. WritePrincipalInfo(JSStructuredCloneWriter* aWriter, const PrincipalInfo& aInfo)
  222. {
  223. if (aInfo.type() == PrincipalInfo::TNullPrincipalInfo) {
  224. const NullPrincipalInfo& nullInfo = aInfo;
  225. return JS_WriteUint32Pair(aWriter, SCTAG_DOM_NULL_PRINCIPAL, 0) &&
  226. WriteSuffixAndSpec(aWriter, nullInfo.attrs(), EmptyCString());
  227. }
  228. if (aInfo.type() == PrincipalInfo::TSystemPrincipalInfo) {
  229. return JS_WriteUint32Pair(aWriter, SCTAG_DOM_SYSTEM_PRINCIPAL, 0);
  230. }
  231. if (aInfo.type() == PrincipalInfo::TExpandedPrincipalInfo) {
  232. const ExpandedPrincipalInfo& expanded = aInfo;
  233. if (!JS_WriteUint32Pair(aWriter, SCTAG_DOM_EXPANDED_PRINCIPAL, 0) ||
  234. !JS_WriteUint32Pair(aWriter, expanded.whitelist().Length(), 0)) {
  235. return false;
  236. }
  237. for (uint32_t i = 0; i < expanded.whitelist().Length(); i++) {
  238. if (!WritePrincipalInfo(aWriter, expanded.whitelist()[i])) {
  239. return false;
  240. }
  241. }
  242. return true;
  243. }
  244. MOZ_ASSERT(aInfo.type() == PrincipalInfo::TContentPrincipalInfo);
  245. const ContentPrincipalInfo& cInfo = aInfo;
  246. return JS_WriteUint32Pair(aWriter, SCTAG_DOM_CONTENT_PRINCIPAL, 0) &&
  247. WriteSuffixAndSpec(aWriter, cInfo.attrs(), cInfo.spec());
  248. }
  249. bool
  250. nsJSPrincipals::write(JSContext* aCx, JSStructuredCloneWriter* aWriter)
  251. {
  252. PrincipalInfo info;
  253. if (NS_WARN_IF(NS_FAILED(PrincipalToPrincipalInfo(this, &info)))) {
  254. xpc::Throw(aCx, NS_ERROR_DOM_DATA_CLONE_ERR);
  255. return false;
  256. }
  257. return WritePrincipalInfo(aWriter, info);
  258. }