nsAboutProtocolHandler.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /* -*- Mode: C++; tab-width: 2; 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 "base/basictypes.h"
  6. #include "mozilla/ArrayUtils.h"
  7. #include "nsAboutProtocolHandler.h"
  8. #include "nsIURI.h"
  9. #include "nsIAboutModule.h"
  10. #include "nsString.h"
  11. #include "nsNetCID.h"
  12. #include "nsAboutProtocolUtils.h"
  13. #include "nsError.h"
  14. #include "nsNetUtil.h"
  15. #include "nsIObjectInputStream.h"
  16. #include "nsIObjectOutputStream.h"
  17. #include "nsAutoPtr.h"
  18. #include "nsIWritablePropertyBag2.h"
  19. #include "nsIChannel.h"
  20. #include "nsIScriptError.h"
  21. #include "nsContentUtils.h"
  22. namespace mozilla {
  23. namespace net {
  24. static NS_DEFINE_CID(kSimpleURICID, NS_SIMPLEURI_CID);
  25. static NS_DEFINE_CID(kNestedAboutURICID, NS_NESTEDABOUTURI_CID);
  26. static bool IsSafeForUntrustedContent(nsIAboutModule *aModule, nsIURI *aURI) {
  27. uint32_t flags;
  28. nsresult rv = aModule->GetURIFlags(aURI, &flags);
  29. NS_ENSURE_SUCCESS(rv, false);
  30. return (flags & nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT) != 0;
  31. }
  32. static bool IsSafeToLinkForUntrustedContent(nsIAboutModule *aModule, nsIURI *aURI) {
  33. uint32_t flags;
  34. nsresult rv = aModule->GetURIFlags(aURI, &flags);
  35. NS_ENSURE_SUCCESS(rv, false);
  36. return (flags & nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT) && (flags & nsIAboutModule::MAKE_LINKABLE);
  37. }
  38. ////////////////////////////////////////////////////////////////////////////////
  39. NS_IMPL_ISUPPORTS(nsAboutProtocolHandler, nsIProtocolHandler,
  40. nsIProtocolHandlerWithDynamicFlags, nsISupportsWeakReference)
  41. ////////////////////////////////////////////////////////////////////////////////
  42. // nsIProtocolHandler methods:
  43. NS_IMETHODIMP
  44. nsAboutProtocolHandler::GetScheme(nsACString &result)
  45. {
  46. result.AssignLiteral("about");
  47. return NS_OK;
  48. }
  49. NS_IMETHODIMP
  50. nsAboutProtocolHandler::GetDefaultPort(int32_t *result)
  51. {
  52. *result = -1; // no port for about: URLs
  53. return NS_OK;
  54. }
  55. NS_IMETHODIMP
  56. nsAboutProtocolHandler::GetProtocolFlags(uint32_t *result)
  57. {
  58. *result = URI_NORELATIVE | URI_NOAUTH | URI_DANGEROUS_TO_LOAD | URI_SCHEME_NOT_SELF_LINKABLE;
  59. return NS_OK;
  60. }
  61. NS_IMETHODIMP
  62. nsAboutProtocolHandler::GetFlagsForURI(nsIURI* aURI, uint32_t* aFlags)
  63. {
  64. // First use the default (which is "unsafe for content"):
  65. GetProtocolFlags(aFlags);
  66. // Now try to see if this URI overrides the default:
  67. nsCOMPtr<nsIAboutModule> aboutMod;
  68. nsresult rv = NS_GetAboutModule(aURI, getter_AddRefs(aboutMod));
  69. if (NS_FAILED(rv)) {
  70. // Swallow this and just tell the consumer the default:
  71. return NS_OK;
  72. }
  73. uint32_t aboutModuleFlags = 0;
  74. rv = aboutMod->GetURIFlags(aURI, &aboutModuleFlags);
  75. // This should never happen, so pass back the error:
  76. NS_ENSURE_SUCCESS(rv, rv);
  77. // Secure (https) pages can load safe about pages without becoming
  78. // mixed content.
  79. if (aboutModuleFlags & nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT) {
  80. *aFlags |= URI_SAFE_TO_LOAD_IN_SECURE_CONTEXT;
  81. // about: pages can only be loaded by unprivileged principals
  82. // if they are marked as LINKABLE
  83. if (aboutModuleFlags & nsIAboutModule::MAKE_LINKABLE) {
  84. // Replace URI_DANGEROUS_TO_LOAD with URI_LOADABLE_BY_ANYONE.
  85. *aFlags &= ~URI_DANGEROUS_TO_LOAD;
  86. *aFlags |= URI_LOADABLE_BY_ANYONE;
  87. }
  88. }
  89. return NS_OK;
  90. }
  91. NS_IMETHODIMP
  92. nsAboutProtocolHandler::NewURI(const nsACString &aSpec,
  93. const char *aCharset, // ignore charset info
  94. nsIURI *aBaseURI,
  95. nsIURI **result)
  96. {
  97. *result = nullptr;
  98. nsresult rv;
  99. // Use a simple URI to parse out some stuff first
  100. nsCOMPtr<nsIURI> url = do_CreateInstance(kSimpleURICID, &rv);
  101. if (NS_FAILED(rv)) return rv;
  102. rv = url->SetSpec(aSpec);
  103. if (NS_FAILED(rv)) {
  104. return rv;
  105. }
  106. // Unfortunately, people create random about: URIs that don't correspond to
  107. // about: modules... Since those URIs will never open a channel, might as
  108. // well consider them unsafe for better perf, and just in case.
  109. bool isSafe = false;
  110. nsCOMPtr<nsIAboutModule> aboutMod;
  111. rv = NS_GetAboutModule(url, getter_AddRefs(aboutMod));
  112. if (NS_SUCCEEDED(rv)) {
  113. isSafe = IsSafeToLinkForUntrustedContent(aboutMod, url);
  114. }
  115. if (isSafe) {
  116. // We need to indicate that this baby is safe. Use an inner URI that
  117. // no one but the security manager will see. Make sure to preserve our
  118. // path, in case someone decides to hardcode checks for particular
  119. // about: URIs somewhere.
  120. nsAutoCString spec;
  121. rv = url->GetPath(spec);
  122. NS_ENSURE_SUCCESS(rv, rv);
  123. spec.Insert("moz-safe-about:", 0);
  124. nsCOMPtr<nsIURI> inner;
  125. rv = NS_NewURI(getter_AddRefs(inner), spec);
  126. NS_ENSURE_SUCCESS(rv, rv);
  127. nsSimpleNestedURI* outer = new nsNestedAboutURI(inner, aBaseURI);
  128. NS_ENSURE_TRUE(outer, NS_ERROR_OUT_OF_MEMORY);
  129. // Take a ref to it in the COMPtr we plan to return
  130. url = outer;
  131. rv = outer->SetSpec(aSpec);
  132. NS_ENSURE_SUCCESS(rv, rv);
  133. }
  134. // We don't want to allow mutation, since it would allow safe and
  135. // unsafe URIs to change into each other...
  136. NS_TryToSetImmutable(url);
  137. url.swap(*result);
  138. return NS_OK;
  139. }
  140. NS_IMETHODIMP
  141. nsAboutProtocolHandler::NewChannel2(nsIURI* uri,
  142. nsILoadInfo* aLoadInfo,
  143. nsIChannel** result)
  144. {
  145. NS_ENSURE_ARG_POINTER(uri);
  146. // about:what you ask?
  147. nsCOMPtr<nsIAboutModule> aboutMod;
  148. nsresult rv = NS_GetAboutModule(uri, getter_AddRefs(aboutMod));
  149. nsAutoCString path;
  150. nsresult rv2 = NS_GetAboutModuleName(uri, path);
  151. if (NS_SUCCEEDED(rv2) && path.EqualsLiteral("srcdoc")) {
  152. // about:srcdoc is meant to be unresolvable, yet is included in the
  153. // about lookup tables so that it can pass security checks when used in
  154. // a srcdoc iframe. To ensure that it stays unresolvable, we pretend
  155. // that it doesn't exist.
  156. rv = NS_ERROR_FACTORY_NOT_REGISTERED;
  157. }
  158. if (NS_SUCCEEDED(rv)) {
  159. // The standard return case:
  160. rv = aboutMod->NewChannel(uri, aLoadInfo, result);
  161. if (NS_SUCCEEDED(rv)) {
  162. // Not all implementations of nsIAboutModule::NewChannel()
  163. // set the LoadInfo on the newly created channel yet, as
  164. // an interim solution we set the LoadInfo here if not
  165. // available on the channel. Bug 1087720
  166. nsCOMPtr<nsILoadInfo> loadInfo = (*result)->GetLoadInfo();
  167. if (aLoadInfo != loadInfo) {
  168. if (loadInfo) {
  169. NS_ASSERTION(false,
  170. "nsIAboutModule->newChannel(aURI, aLoadInfo) needs to set LoadInfo");
  171. const char16_t* params[] = {
  172. u"nsIAboutModule->newChannel(aURI)",
  173. u"nsIAboutModule->newChannel(aURI, aLoadInfo)"
  174. };
  175. nsContentUtils::ReportToConsole(
  176. nsIScriptError::warningFlag,
  177. NS_LITERAL_CSTRING("Security by Default"),
  178. nullptr, // aDocument
  179. nsContentUtils::eNECKO_PROPERTIES,
  180. "APIDeprecationWarning",
  181. params, mozilla::ArrayLength(params));
  182. }
  183. (*result)->SetLoadInfo(aLoadInfo);
  184. }
  185. // If this URI is safe for untrusted content, enforce that its
  186. // principal be based on the channel's originalURI by setting the
  187. // owner to null.
  188. // Note: this relies on aboutMod's newChannel implementation
  189. // having set the proper originalURI, which probably isn't ideal.
  190. if (IsSafeForUntrustedContent(aboutMod, uri)) {
  191. (*result)->SetOwner(nullptr);
  192. }
  193. RefPtr<nsNestedAboutURI> aboutURI;
  194. nsresult rv2 = uri->QueryInterface(kNestedAboutURICID,
  195. getter_AddRefs(aboutURI));
  196. if (NS_SUCCEEDED(rv2) && aboutURI->GetBaseURI()) {
  197. nsCOMPtr<nsIWritablePropertyBag2> writableBag =
  198. do_QueryInterface(*result);
  199. if (writableBag) {
  200. writableBag->
  201. SetPropertyAsInterface(NS_LITERAL_STRING("baseURI"),
  202. aboutURI->GetBaseURI());
  203. }
  204. }
  205. }
  206. return rv;
  207. }
  208. // mumble...
  209. if (rv == NS_ERROR_FACTORY_NOT_REGISTERED) {
  210. // This looks like an about: we don't know about. Convert
  211. // this to an invalid URI error.
  212. rv = NS_ERROR_MALFORMED_URI;
  213. }
  214. return rv;
  215. }
  216. NS_IMETHODIMP
  217. nsAboutProtocolHandler::NewChannel(nsIURI* uri, nsIChannel* *result)
  218. {
  219. return NewChannel2(uri, nullptr, result);
  220. }
  221. NS_IMETHODIMP
  222. nsAboutProtocolHandler::AllowPort(int32_t port, const char *scheme, bool *_retval)
  223. {
  224. // don't override anything.
  225. *_retval = false;
  226. return NS_OK;
  227. }
  228. ////////////////////////////////////////////////////////////////////////////////
  229. // Safe about protocol handler impl
  230. NS_IMPL_ISUPPORTS(nsSafeAboutProtocolHandler, nsIProtocolHandler, nsISupportsWeakReference)
  231. // nsIProtocolHandler methods:
  232. NS_IMETHODIMP
  233. nsSafeAboutProtocolHandler::GetScheme(nsACString &result)
  234. {
  235. result.AssignLiteral("moz-safe-about");
  236. return NS_OK;
  237. }
  238. NS_IMETHODIMP
  239. nsSafeAboutProtocolHandler::GetDefaultPort(int32_t *result)
  240. {
  241. *result = -1; // no port for moz-safe-about: URLs
  242. return NS_OK;
  243. }
  244. NS_IMETHODIMP
  245. nsSafeAboutProtocolHandler::GetProtocolFlags(uint32_t *result)
  246. {
  247. *result = URI_NORELATIVE | URI_NOAUTH | URI_LOADABLE_BY_ANYONE | URI_SAFE_TO_LOAD_IN_SECURE_CONTEXT;
  248. return NS_OK;
  249. }
  250. NS_IMETHODIMP
  251. nsSafeAboutProtocolHandler::NewURI(const nsACString &aSpec,
  252. const char *aCharset, // ignore charset info
  253. nsIURI *aBaseURI,
  254. nsIURI **result)
  255. {
  256. nsresult rv;
  257. nsCOMPtr<nsIURI> url = do_CreateInstance(kSimpleURICID, &rv);
  258. if (NS_FAILED(rv)) return rv;
  259. rv = url->SetSpec(aSpec);
  260. if (NS_FAILED(rv)) {
  261. return rv;
  262. }
  263. NS_TryToSetImmutable(url);
  264. *result = nullptr;
  265. url.swap(*result);
  266. return rv;
  267. }
  268. NS_IMETHODIMP
  269. nsSafeAboutProtocolHandler::NewChannel2(nsIURI* uri,
  270. nsILoadInfo* aLoadInfo,
  271. nsIChannel** result)
  272. {
  273. *result = nullptr;
  274. return NS_ERROR_NOT_AVAILABLE;
  275. }
  276. NS_IMETHODIMP
  277. nsSafeAboutProtocolHandler::NewChannel(nsIURI* uri, nsIChannel* *result)
  278. {
  279. *result = nullptr;
  280. return NS_ERROR_NOT_AVAILABLE;
  281. }
  282. NS_IMETHODIMP
  283. nsSafeAboutProtocolHandler::AllowPort(int32_t port, const char *scheme, bool *_retval)
  284. {
  285. // don't override anything.
  286. *_retval = false;
  287. return NS_OK;
  288. }
  289. ////////////////////////////////////////////////////////////
  290. // nsNestedAboutURI implementation
  291. NS_INTERFACE_MAP_BEGIN(nsNestedAboutURI)
  292. if (aIID.Equals(kNestedAboutURICID))
  293. foundInterface = static_cast<nsIURI*>(this);
  294. else
  295. NS_INTERFACE_MAP_END_INHERITING(nsSimpleNestedURI)
  296. // nsISerializable
  297. NS_IMETHODIMP
  298. nsNestedAboutURI::Read(nsIObjectInputStream* aStream)
  299. {
  300. nsresult rv = nsSimpleNestedURI::Read(aStream);
  301. if (NS_FAILED(rv)) return rv;
  302. bool haveBase;
  303. rv = aStream->ReadBoolean(&haveBase);
  304. if (NS_FAILED(rv)) return rv;
  305. if (haveBase) {
  306. nsCOMPtr<nsISupports> supports;
  307. rv = aStream->ReadObject(true, getter_AddRefs(supports));
  308. if (NS_FAILED(rv)) return rv;
  309. mBaseURI = do_QueryInterface(supports, &rv);
  310. if (NS_FAILED(rv)) return rv;
  311. }
  312. return NS_OK;
  313. }
  314. NS_IMETHODIMP
  315. nsNestedAboutURI::Write(nsIObjectOutputStream* aStream)
  316. {
  317. nsresult rv = nsSimpleNestedURI::Write(aStream);
  318. if (NS_FAILED(rv)) return rv;
  319. rv = aStream->WriteBoolean(mBaseURI != nullptr);
  320. if (NS_FAILED(rv)) return rv;
  321. if (mBaseURI) {
  322. // A previous iteration of this code wrote out mBaseURI as nsISupports
  323. // and then read it in as nsIURI, which is non-kosher when mBaseURI
  324. // implements more than just a single line of interfaces and the
  325. // canonical nsISupports* isn't the one a static_cast<> of mBaseURI
  326. // would produce. For backwards compatibility with existing
  327. // serializations we continue to write mBaseURI as nsISupports but
  328. // switch to reading it as nsISupports, with a post-read QI to get to
  329. // nsIURI.
  330. rv = aStream->WriteCompoundObject(mBaseURI, NS_GET_IID(nsISupports),
  331. true);
  332. if (NS_FAILED(rv)) return rv;
  333. }
  334. return NS_OK;
  335. }
  336. // nsSimpleURI
  337. /* virtual */ nsSimpleURI*
  338. nsNestedAboutURI::StartClone(nsSimpleURI::RefHandlingEnum aRefHandlingMode,
  339. const nsACString& aNewRef)
  340. {
  341. // Sadly, we can't make use of nsSimpleNestedURI::StartClone here.
  342. // However, this function is expected to exactly match that function,
  343. // aside from the "new ns***URI()" call.
  344. NS_ENSURE_TRUE(mInnerURI, nullptr);
  345. nsCOMPtr<nsIURI> innerClone;
  346. nsresult rv;
  347. if (aRefHandlingMode == eHonorRef) {
  348. rv = mInnerURI->Clone(getter_AddRefs(innerClone));
  349. } else if (aRefHandlingMode == eReplaceRef) {
  350. rv = mInnerURI->CloneWithNewRef(aNewRef, getter_AddRefs(innerClone));
  351. } else {
  352. rv = mInnerURI->CloneIgnoringRef(getter_AddRefs(innerClone));
  353. }
  354. if (NS_FAILED(rv)) {
  355. return nullptr;
  356. }
  357. nsNestedAboutURI* url = new nsNestedAboutURI(innerClone, mBaseURI);
  358. SetRefOnClone(url, aRefHandlingMode, aNewRef);
  359. url->SetMutable(false);
  360. return url;
  361. }
  362. // nsIClassInfo
  363. NS_IMETHODIMP
  364. nsNestedAboutURI::GetClassIDNoAlloc(nsCID *aClassIDNoAlloc)
  365. {
  366. *aClassIDNoAlloc = kNestedAboutURICID;
  367. return NS_OK;
  368. }
  369. } // namespace net
  370. } // namespace mozilla