nsScriptSecurityManager.cpp 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771
  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. #include "nsScriptSecurityManager.h"
  6. #include "mozilla/ArrayUtils.h"
  7. #include "xpcpublic.h"
  8. #include "XPCWrapper.h"
  9. #include "nsIAppsService.h"
  10. #include "nsIInputStreamChannel.h"
  11. #include "nsILoadContext.h"
  12. #include "nsIServiceManager.h"
  13. #include "nsIScriptObjectPrincipal.h"
  14. #include "nsIScriptContext.h"
  15. #include "nsIURL.h"
  16. #include "nsINestedURI.h"
  17. #include "nspr.h"
  18. #include "nsJSPrincipals.h"
  19. #include "mozilla/BasePrincipal.h"
  20. #include "nsSystemPrincipal.h"
  21. #include "nsPrincipal.h"
  22. #include "nsNullPrincipal.h"
  23. #include "DomainPolicy.h"
  24. #include "nsXPIDLString.h"
  25. #include "nsCRT.h"
  26. #include "nsCRTGlue.h"
  27. #include "nsDocShell.h"
  28. #include "nsError.h"
  29. #include "nsDOMCID.h"
  30. #include "nsTextFormatter.h"
  31. #include "nsIStringBundle.h"
  32. #include "nsNetUtil.h"
  33. #include "nsIEffectiveTLDService.h"
  34. #include "nsIProperties.h"
  35. #include "nsDirectoryServiceDefs.h"
  36. #include "nsIFile.h"
  37. #include "nsIFileURL.h"
  38. #include "nsIZipReader.h"
  39. #include "nsIScriptGlobalObject.h"
  40. #include "nsPIDOMWindow.h"
  41. #include "nsIDocShell.h"
  42. #include "nsIPrompt.h"
  43. #include "nsIWindowWatcher.h"
  44. #include "nsIConsoleService.h"
  45. #include "nsIObserverService.h"
  46. #include "nsIContent.h"
  47. #include "nsDOMJSUtils.h"
  48. #include "nsAboutProtocolUtils.h"
  49. #include "nsIClassInfo.h"
  50. #include "nsIURIFixup.h"
  51. #include "nsCDefaultURIFixup.h"
  52. #include "nsIChromeRegistry.h"
  53. #include "nsIContentSecurityPolicy.h"
  54. #include "nsIAsyncVerifyRedirectCallback.h"
  55. #include "mozIApplication.h"
  56. #include "mozilla/Preferences.h"
  57. #include "mozilla/dom/BindingUtils.h"
  58. #include "mozilla/dom/ContentParent.h"
  59. #include <stdint.h>
  60. #include "mozilla/dom/ScriptSettings.h"
  61. #include "mozilla/ClearOnShutdown.h"
  62. #include "mozilla/StaticPtr.h"
  63. #include "nsContentUtils.h"
  64. #include "nsJSUtils.h"
  65. #include "nsILoadInfo.h"
  66. #include "nsXPCOMStrings.h"
  67. using namespace mozilla;
  68. using namespace mozilla::dom;
  69. using namespace mozilla::ipc;
  70. nsIIOService *nsScriptSecurityManager::sIOService = nullptr;
  71. nsIStringBundle *nsScriptSecurityManager::sStrBundle = nullptr;
  72. JSContext *nsScriptSecurityManager::sContext = nullptr;
  73. bool nsScriptSecurityManager::sStrictFileOriginPolicy = true;
  74. ///////////////////////////
  75. // Convenience Functions //
  76. ///////////////////////////
  77. class nsAutoInPrincipalDomainOriginSetter {
  78. public:
  79. nsAutoInPrincipalDomainOriginSetter() {
  80. ++sInPrincipalDomainOrigin;
  81. }
  82. ~nsAutoInPrincipalDomainOriginSetter() {
  83. --sInPrincipalDomainOrigin;
  84. }
  85. static uint32_t sInPrincipalDomainOrigin;
  86. };
  87. uint32_t nsAutoInPrincipalDomainOriginSetter::sInPrincipalDomainOrigin;
  88. static
  89. nsresult
  90. GetOriginFromURI(nsIURI* aURI, nsACString& aOrigin)
  91. {
  92. if (nsAutoInPrincipalDomainOriginSetter::sInPrincipalDomainOrigin > 1) {
  93. // Allow a single recursive call to GetPrincipalDomainOrigin, since that
  94. // might be happening on a different principal from the first call. But
  95. // after that, cut off the recursion; it just indicates that something
  96. // we're doing in this method causes us to reenter a security check here.
  97. return NS_ERROR_NOT_AVAILABLE;
  98. }
  99. nsAutoInPrincipalDomainOriginSetter autoSetter;
  100. nsCOMPtr<nsIURI> uri = NS_GetInnermostURI(aURI);
  101. NS_ENSURE_TRUE(uri, NS_ERROR_UNEXPECTED);
  102. nsAutoCString hostPort;
  103. nsresult rv = uri->GetHostPort(hostPort);
  104. if (NS_SUCCEEDED(rv)) {
  105. nsAutoCString scheme;
  106. rv = uri->GetScheme(scheme);
  107. NS_ENSURE_SUCCESS(rv, rv);
  108. aOrigin = scheme + NS_LITERAL_CSTRING("://") + hostPort;
  109. }
  110. else {
  111. // Some URIs (e.g., nsSimpleURI) don't support host. Just
  112. // get the full spec.
  113. rv = uri->GetSpec(aOrigin);
  114. NS_ENSURE_SUCCESS(rv, rv);
  115. }
  116. return NS_OK;
  117. }
  118. static
  119. nsresult
  120. GetPrincipalDomainOrigin(nsIPrincipal* aPrincipal,
  121. nsACString& aOrigin)
  122. {
  123. nsCOMPtr<nsIURI> uri;
  124. aPrincipal->GetDomain(getter_AddRefs(uri));
  125. if (!uri) {
  126. aPrincipal->GetURI(getter_AddRefs(uri));
  127. }
  128. NS_ENSURE_TRUE(uri, NS_ERROR_UNEXPECTED);
  129. return GetOriginFromURI(uri, aOrigin);
  130. }
  131. inline void SetPendingExceptionASCII(JSContext *cx, const char *aMsg)
  132. {
  133. JS_ReportErrorASCII(cx, "%s", aMsg);
  134. }
  135. inline void SetPendingException(JSContext *cx, const char16_t *aMsg)
  136. {
  137. NS_ConvertUTF16toUTF8 msg(aMsg);
  138. JS_ReportErrorUTF8(cx, "%s", msg.get());
  139. }
  140. // Helper class to get stuff from the ClassInfo and not waste extra time with
  141. // virtual method calls for things it has already gotten
  142. class ClassInfoData
  143. {
  144. public:
  145. ClassInfoData(nsIClassInfo *aClassInfo, const char *aName)
  146. : mClassInfo(aClassInfo),
  147. mName(const_cast<char *>(aName)),
  148. mDidGetFlags(false),
  149. mMustFreeName(false)
  150. {
  151. }
  152. ~ClassInfoData()
  153. {
  154. if (mMustFreeName)
  155. free(mName);
  156. }
  157. uint32_t GetFlags()
  158. {
  159. if (!mDidGetFlags) {
  160. if (mClassInfo) {
  161. nsresult rv = mClassInfo->GetFlags(&mFlags);
  162. if (NS_FAILED(rv)) {
  163. mFlags = 0;
  164. }
  165. } else {
  166. mFlags = 0;
  167. }
  168. mDidGetFlags = true;
  169. }
  170. return mFlags;
  171. }
  172. bool IsDOMClass()
  173. {
  174. return !!(GetFlags() & nsIClassInfo::DOM_OBJECT);
  175. }
  176. const char* GetName()
  177. {
  178. if (!mName) {
  179. if (mClassInfo) {
  180. mClassInfo->GetClassDescription(&mName);
  181. }
  182. if (mName) {
  183. mMustFreeName = true;
  184. } else {
  185. mName = const_cast<char *>("UnnamedClass");
  186. }
  187. }
  188. return mName;
  189. }
  190. private:
  191. nsIClassInfo *mClassInfo; // WEAK
  192. uint32_t mFlags;
  193. char *mName;
  194. bool mDidGetFlags;
  195. bool mMustFreeName;
  196. };
  197. /* static */
  198. bool
  199. nsScriptSecurityManager::SecurityCompareURIs(nsIURI* aSourceURI,
  200. nsIURI* aTargetURI)
  201. {
  202. return NS_SecurityCompareURIs(aSourceURI, aTargetURI, sStrictFileOriginPolicy);
  203. }
  204. // SecurityHashURI is consistent with SecurityCompareURIs because NS_SecurityHashURI
  205. // is consistent with NS_SecurityCompareURIs. See nsNetUtil.h.
  206. uint32_t
  207. nsScriptSecurityManager::SecurityHashURI(nsIURI* aURI)
  208. {
  209. return NS_SecurityHashURI(aURI);
  210. }
  211. uint16_t
  212. nsScriptSecurityManager::AppStatusForPrincipal(nsIPrincipal *aPrin)
  213. {
  214. uint32_t appId = aPrin->GetAppId();
  215. // After bug 1238160, the principal no longer knows how to answer "is this a
  216. // browser element", which is really what this code path wants. Currently,
  217. // desktop is the only platform where we intend to disable isolation on a
  218. // browser frame, so non-desktop should be able to assume that
  219. // inIsolatedMozBrowser is true for all mozbrowser frames. Additionally,
  220. // apps are no longer used on desktop, so appId is always NO_APP_ID. We use
  221. // a release assertion in nsFrameLoader::OwnerIsIsolatedMozBrowserFrame so
  222. // that platforms with apps can assume inIsolatedMozBrowser is true for all
  223. // mozbrowser frames.
  224. bool inIsolatedMozBrowser = aPrin->GetIsInIsolatedMozBrowserElement();
  225. NS_WARNING_ASSERTION(
  226. appId != nsIScriptSecurityManager::UNKNOWN_APP_ID,
  227. "Asking for app status on a principal with an unknown app id");
  228. // Installed apps have a valid app id (not NO_APP_ID or UNKNOWN_APP_ID)
  229. // and they are not inside a mozbrowser.
  230. if (appId == nsIScriptSecurityManager::NO_APP_ID ||
  231. appId == nsIScriptSecurityManager::UNKNOWN_APP_ID ||
  232. inIsolatedMozBrowser)
  233. {
  234. return nsIPrincipal::APP_STATUS_NOT_INSTALLED;
  235. }
  236. nsCOMPtr<nsIAppsService> appsService = do_GetService(APPS_SERVICE_CONTRACTID);
  237. NS_ENSURE_TRUE(appsService, nsIPrincipal::APP_STATUS_NOT_INSTALLED);
  238. nsCOMPtr<mozIApplication> app;
  239. appsService->GetAppByLocalId(appId, getter_AddRefs(app));
  240. NS_ENSURE_TRUE(app, nsIPrincipal::APP_STATUS_NOT_INSTALLED);
  241. uint16_t status = nsIPrincipal::APP_STATUS_INSTALLED;
  242. NS_ENSURE_SUCCESS(app->GetAppStatus(&status),
  243. nsIPrincipal::APP_STATUS_NOT_INSTALLED);
  244. nsString appOrigin;
  245. NS_ENSURE_SUCCESS(app->GetOrigin(appOrigin),
  246. nsIPrincipal::APP_STATUS_NOT_INSTALLED);
  247. nsCOMPtr<nsIURI> appURI;
  248. NS_ENSURE_SUCCESS(NS_NewURI(getter_AddRefs(appURI), appOrigin),
  249. nsIPrincipal::APP_STATUS_NOT_INSTALLED);
  250. // The app could contain a cross-origin iframe - make sure that the content
  251. // is actually same-origin with the app.
  252. MOZ_ASSERT(inIsolatedMozBrowser == false, "Checked this above");
  253. nsAutoCString suffix;
  254. PrincipalOriginAttributes attrs;
  255. NS_ENSURE_TRUE(attrs.PopulateFromOrigin(NS_ConvertUTF16toUTF8(appOrigin), suffix),
  256. nsIPrincipal::APP_STATUS_NOT_INSTALLED);
  257. attrs.mAppId = appId;
  258. attrs.mInIsolatedMozBrowser = false;
  259. nsCOMPtr<nsIPrincipal> appPrin = BasePrincipal::CreateCodebasePrincipal(appURI, attrs);
  260. NS_ENSURE_TRUE(appPrin, nsIPrincipal::APP_STATUS_NOT_INSTALLED);
  261. return aPrin->Equals(appPrin) ? status
  262. : nsIPrincipal::APP_STATUS_NOT_INSTALLED;
  263. }
  264. /*
  265. * GetChannelResultPrincipal will return the principal that the resource
  266. * returned by this channel will use. For example, if the resource is in
  267. * a sandbox, it will return the nullprincipal. If the resource is forced
  268. * to inherit principal, it will return the principal of its parent. If
  269. * the load doesn't require sandboxing or inheriting, it will return the same
  270. * principal as GetChannelURIPrincipal. Namely the principal of the URI
  271. * that is being loaded.
  272. */
  273. NS_IMETHODIMP
  274. nsScriptSecurityManager::GetChannelResultPrincipal(nsIChannel* aChannel,
  275. nsIPrincipal** aPrincipal)
  276. {
  277. return GetChannelResultPrincipal(aChannel, aPrincipal,
  278. /*aIgnoreSandboxing*/ false);
  279. }
  280. nsresult
  281. nsScriptSecurityManager::GetChannelResultPrincipalIfNotSandboxed(nsIChannel* aChannel,
  282. nsIPrincipal** aPrincipal)
  283. {
  284. return GetChannelResultPrincipal(aChannel, aPrincipal,
  285. /*aIgnoreSandboxing*/ true);
  286. }
  287. nsresult
  288. nsScriptSecurityManager::GetChannelResultPrincipal(nsIChannel* aChannel,
  289. nsIPrincipal** aPrincipal,
  290. bool aIgnoreSandboxing)
  291. {
  292. NS_PRECONDITION(aChannel, "Must have channel!");
  293. // Check whether we have an nsILoadInfo that says what we should do.
  294. nsCOMPtr<nsILoadInfo> loadInfo = aChannel->GetLoadInfo();
  295. if (loadInfo && loadInfo->GetForceInheritPrincipalOverruleOwner()) {
  296. nsCOMPtr<nsIPrincipal> principalToInherit = loadInfo->PrincipalToInherit();
  297. if (!principalToInherit) {
  298. principalToInherit = loadInfo->TriggeringPrincipal();
  299. }
  300. principalToInherit.forget(aPrincipal);
  301. return NS_OK;
  302. }
  303. nsCOMPtr<nsISupports> owner;
  304. aChannel->GetOwner(getter_AddRefs(owner));
  305. if (owner) {
  306. CallQueryInterface(owner, aPrincipal);
  307. if (*aPrincipal) {
  308. return NS_OK;
  309. }
  310. }
  311. if (loadInfo) {
  312. if (!aIgnoreSandboxing && loadInfo->GetLoadingSandboxed()) {
  313. RefPtr<nsNullPrincipal> prin;
  314. if (loadInfo->LoadingPrincipal()) {
  315. prin =
  316. nsNullPrincipal::CreateWithInheritedAttributes(loadInfo->LoadingPrincipal());
  317. } else {
  318. NeckoOriginAttributes nAttrs;
  319. loadInfo->GetOriginAttributes(&nAttrs);
  320. PrincipalOriginAttributes pAttrs;
  321. pAttrs.InheritFromNecko(nAttrs);
  322. prin = nsNullPrincipal::Create(pAttrs);
  323. }
  324. prin.forget(aPrincipal);
  325. // if the new NullPrincipal (above) loads an iframe[srcdoc], we
  326. // need to inherit an existing CSP to avoid bypasses (bug 1073952).
  327. // We continue inheriting for nested frames with e.g., data: URLs.
  328. if (loadInfo->GetExternalContentPolicyType() == nsIContentPolicy::TYPE_SUBDOCUMENT) {
  329. nsCOMPtr<nsIURI> uri;
  330. aChannel->GetURI(getter_AddRefs(uri));
  331. nsAutoCString URISpec;
  332. uri->GetSpec(URISpec);
  333. bool isData = (NS_SUCCEEDED(uri->SchemeIs("data", &isData)) && isData);
  334. if (URISpec.EqualsLiteral("about:srcdoc") || isData) {
  335. nsCOMPtr<nsIPrincipal> principalToInherit = loadInfo->PrincipalToInherit();
  336. if (!principalToInherit) {
  337. principalToInherit = loadInfo->TriggeringPrincipal();
  338. }
  339. nsCOMPtr<nsIContentSecurityPolicy> originalCSP;
  340. principalToInherit->GetCsp(getter_AddRefs(originalCSP));
  341. if (originalCSP) {
  342. // if the principalToInherit had a CSP,
  343. // add it to the newly created NullPrincipal
  344. // (unless it already has one)
  345. nsCOMPtr<nsIContentSecurityPolicy> nullPrincipalCSP;
  346. (*aPrincipal)->GetCsp(getter_AddRefs(nullPrincipalCSP));
  347. if (nullPrincipalCSP) {
  348. MOZ_ASSERT(nullPrincipalCSP == originalCSP,
  349. "There should be no other CSP here.");
  350. // CSPs are equal, no need to set it again.
  351. return NS_OK;
  352. } else {
  353. nsresult rv = (*aPrincipal)->SetCsp(originalCSP);
  354. NS_ENSURE_SUCCESS(rv, rv);
  355. }
  356. }
  357. }
  358. }
  359. return NS_OK;
  360. }
  361. bool forceInherit = loadInfo->GetForceInheritPrincipal();
  362. if (aIgnoreSandboxing && !forceInherit) {
  363. // Check if SEC_FORCE_INHERIT_PRINCIPAL was dropped because of
  364. // sandboxing:
  365. if (loadInfo->GetLoadingSandboxed() &&
  366. loadInfo->GetForceInheritPrincipalDropped()) {
  367. forceInherit = true;
  368. }
  369. }
  370. if (forceInherit) {
  371. nsCOMPtr<nsIPrincipal> principalToInherit = loadInfo->PrincipalToInherit();
  372. if (!principalToInherit) {
  373. principalToInherit = loadInfo->TriggeringPrincipal();
  374. }
  375. principalToInherit.forget(aPrincipal);
  376. return NS_OK;
  377. }
  378. nsSecurityFlags securityFlags = loadInfo->GetSecurityMode();
  379. // The data: inheritance flags should only apply to the initial load,
  380. // not to loads that it might have redirected to.
  381. if (loadInfo->RedirectChain().IsEmpty() &&
  382. (securityFlags == nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_DATA_INHERITS ||
  383. securityFlags == nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_INHERITS ||
  384. securityFlags == nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS)) {
  385. nsCOMPtr<nsIURI> uri;
  386. nsresult rv = NS_GetFinalChannelURI(aChannel, getter_AddRefs(uri));
  387. NS_ENSURE_SUCCESS(rv, rv);
  388. nsCOMPtr<nsIPrincipal> principalToInherit = loadInfo->PrincipalToInherit();
  389. if (!principalToInherit) {
  390. principalToInherit = loadInfo->TriggeringPrincipal();
  391. }
  392. bool inheritForAboutBlank = loadInfo->GetAboutBlankInherits();
  393. if (nsContentUtils::ChannelShouldInheritPrincipal(principalToInherit,
  394. uri,
  395. inheritForAboutBlank,
  396. false)) {
  397. principalToInherit.forget(aPrincipal);
  398. return NS_OK;
  399. }
  400. }
  401. }
  402. return GetChannelURIPrincipal(aChannel, aPrincipal);
  403. }
  404. nsresult
  405. nsScriptSecurityManager::MaybeSetAddonIdFromURI(PrincipalOriginAttributes& aAttrs, nsIURI* aURI)
  406. {
  407. nsAutoCString scheme;
  408. nsresult rv = aURI->GetScheme(scheme);
  409. NS_ENSURE_SUCCESS(rv, rv);
  410. if (scheme.EqualsLiteral("moz-extension") && GetAddonPolicyService()) {
  411. rv = GetAddonPolicyService()->ExtensionURIToAddonId(aURI, aAttrs.mAddonId);
  412. NS_ENSURE_SUCCESS(rv, rv);
  413. }
  414. return NS_OK;
  415. }
  416. /* The principal of the URI that this channel is loading. This is never
  417. * affected by things like sandboxed loads, or loads where we forcefully
  418. * inherit the principal. Think of this as the principal of the server
  419. * which this channel is loading from. Most callers should use
  420. * GetChannelResultPrincipal instead of GetChannelURIPrincipal. Only
  421. * call GetChannelURIPrincipal if you are sure that you want the
  422. * principal that matches the uri, even in cases when the load is
  423. * sandboxed or when the load could be a blob or data uri (i.e even when
  424. * you encounter loads that may or may not be sandboxed and loads
  425. * that may or may not inherit)."
  426. */
  427. NS_IMETHODIMP
  428. nsScriptSecurityManager::GetChannelURIPrincipal(nsIChannel* aChannel,
  429. nsIPrincipal** aPrincipal)
  430. {
  431. NS_PRECONDITION(aChannel, "Must have channel!");
  432. // Get the principal from the URI. Make sure this does the same thing
  433. // as nsDocument::Reset and XULDocument::StartDocumentLoad.
  434. nsCOMPtr<nsIURI> uri;
  435. nsresult rv = NS_GetFinalChannelURI(aChannel, getter_AddRefs(uri));
  436. NS_ENSURE_SUCCESS(rv, rv);
  437. nsCOMPtr<nsILoadInfo> loadInfo;
  438. aChannel->GetLoadInfo(getter_AddRefs(loadInfo));
  439. // Inherit the origin attributes from loadInfo.
  440. // If this is a top-level document load, the origin attributes of the
  441. // loadInfo will be set from nsDocShell::DoURILoad.
  442. // For subresource loading, the origin attributes of the loadInfo is from
  443. // its loadingPrincipal.
  444. PrincipalOriginAttributes attrs;
  445. // For addons loadInfo might be null.
  446. if (loadInfo) {
  447. attrs.InheritFromNecko(loadInfo->GetOriginAttributes());
  448. }
  449. rv = MaybeSetAddonIdFromURI(attrs, uri);
  450. NS_ENSURE_SUCCESS(rv, rv);
  451. nsCOMPtr<nsIPrincipal> prin = BasePrincipal::CreateCodebasePrincipal(uri, attrs);
  452. prin.forget(aPrincipal);
  453. return *aPrincipal ? NS_OK : NS_ERROR_FAILURE;
  454. }
  455. NS_IMETHODIMP
  456. nsScriptSecurityManager::IsSystemPrincipal(nsIPrincipal* aPrincipal,
  457. bool* aIsSystem)
  458. {
  459. *aIsSystem = (aPrincipal == mSystemPrincipal);
  460. return NS_OK;
  461. }
  462. /////////////////////////////
  463. // nsScriptSecurityManager //
  464. /////////////////////////////
  465. ////////////////////////////////////
  466. // Methods implementing ISupports //
  467. ////////////////////////////////////
  468. NS_IMPL_ISUPPORTS(nsScriptSecurityManager,
  469. nsIScriptSecurityManager,
  470. nsIObserver)
  471. ///////////////////////////////////////////////////
  472. // Methods implementing nsIScriptSecurityManager //
  473. ///////////////////////////////////////////////////
  474. ///////////////// Security Checks /////////////////
  475. bool
  476. nsScriptSecurityManager::ContentSecurityPolicyPermitsJSAction(JSContext *cx)
  477. {
  478. MOZ_ASSERT(cx == nsContentUtils::GetCurrentJSContext());
  479. nsCOMPtr<nsIPrincipal> subjectPrincipal = nsContentUtils::SubjectPrincipal();
  480. nsCOMPtr<nsIContentSecurityPolicy> csp;
  481. nsresult rv = subjectPrincipal->GetCsp(getter_AddRefs(csp));
  482. NS_ASSERTION(NS_SUCCEEDED(rv), "CSP: Failed to get CSP from principal.");
  483. // don't do anything unless there's a CSP
  484. if (!csp)
  485. return true;
  486. bool evalOK = true;
  487. bool reportViolation = false;
  488. rv = csp->GetAllowsEval(&reportViolation, &evalOK);
  489. if (NS_FAILED(rv))
  490. {
  491. NS_WARNING("CSP: failed to get allowsEval");
  492. return true; // fail open to not break sites.
  493. }
  494. if (reportViolation) {
  495. nsAutoString fileName;
  496. unsigned lineNum = 0;
  497. NS_NAMED_LITERAL_STRING(scriptSample, "call to eval() or related function blocked by CSP");
  498. JS::AutoFilename scriptFilename;
  499. if (JS::DescribeScriptedCaller(cx, &scriptFilename, &lineNum)) {
  500. if (const char *file = scriptFilename.get()) {
  501. CopyUTF8toUTF16(nsDependentCString(file), fileName);
  502. }
  503. } else {
  504. MOZ_ASSERT(!JS_IsExceptionPending(cx));
  505. }
  506. csp->LogViolationDetails(nsIContentSecurityPolicy::VIOLATION_TYPE_EVAL,
  507. fileName,
  508. scriptSample,
  509. lineNum,
  510. EmptyString(),
  511. EmptyString());
  512. }
  513. return evalOK;
  514. }
  515. // static
  516. bool
  517. nsScriptSecurityManager::JSPrincipalsSubsume(JSPrincipals *first,
  518. JSPrincipals *second)
  519. {
  520. return nsJSPrincipals::get(first)->Subsumes(nsJSPrincipals::get(second));
  521. }
  522. NS_IMETHODIMP
  523. nsScriptSecurityManager::CheckSameOriginURI(nsIURI* aSourceURI,
  524. nsIURI* aTargetURI,
  525. bool reportError)
  526. {
  527. if (!SecurityCompareURIs(aSourceURI, aTargetURI))
  528. {
  529. if (reportError) {
  530. ReportError(nullptr, NS_LITERAL_STRING("CheckSameOriginError"),
  531. aSourceURI, aTargetURI);
  532. }
  533. return NS_ERROR_DOM_BAD_URI;
  534. }
  535. return NS_OK;
  536. }
  537. /*static*/ uint32_t
  538. nsScriptSecurityManager::HashPrincipalByOrigin(nsIPrincipal* aPrincipal)
  539. {
  540. nsCOMPtr<nsIURI> uri;
  541. aPrincipal->GetDomain(getter_AddRefs(uri));
  542. if (!uri)
  543. aPrincipal->GetURI(getter_AddRefs(uri));
  544. return SecurityHashURI(uri);
  545. }
  546. NS_IMETHODIMP
  547. nsScriptSecurityManager::CheckLoadURIFromScript(JSContext *cx, nsIURI *aURI)
  548. {
  549. // Get principal of currently executing script.
  550. MOZ_ASSERT(cx == nsContentUtils::GetCurrentJSContext());
  551. nsIPrincipal* principal = nsContentUtils::SubjectPrincipal();
  552. nsresult rv = CheckLoadURIWithPrincipal(principal, aURI,
  553. nsIScriptSecurityManager::STANDARD);
  554. if (NS_SUCCEEDED(rv)) {
  555. // OK to load
  556. return NS_OK;
  557. }
  558. // See if we're attempting to load a file: URI. If so, let a
  559. // UniversalXPConnect capability trump the above check.
  560. bool isFile = false;
  561. bool isRes = false;
  562. if (NS_FAILED(aURI->SchemeIs("file", &isFile)) ||
  563. NS_FAILED(aURI->SchemeIs("resource", &isRes)))
  564. return NS_ERROR_FAILURE;
  565. if (isFile || isRes)
  566. {
  567. if (nsContentUtils::IsCallerChrome())
  568. return NS_OK;
  569. }
  570. // Report error.
  571. nsAutoCString spec;
  572. if (NS_FAILED(aURI->GetAsciiSpec(spec)))
  573. return NS_ERROR_FAILURE;
  574. nsAutoCString msg("Access to '");
  575. msg.Append(spec);
  576. msg.AppendLiteral("' from script denied");
  577. SetPendingExceptionASCII(cx, msg.get());
  578. return NS_ERROR_DOM_BAD_URI;
  579. }
  580. /**
  581. * Helper method to handle cases where a flag passed to
  582. * CheckLoadURIWithPrincipal means denying loading if the given URI has certain
  583. * nsIProtocolHandler flags set.
  584. * @return if success, access is allowed. Otherwise, deny access
  585. */
  586. static nsresult
  587. DenyAccessIfURIHasFlags(nsIURI* aURI, uint32_t aURIFlags)
  588. {
  589. NS_PRECONDITION(aURI, "Must have URI!");
  590. bool uriHasFlags;
  591. nsresult rv =
  592. NS_URIChainHasFlags(aURI, aURIFlags, &uriHasFlags);
  593. NS_ENSURE_SUCCESS(rv, rv);
  594. if (uriHasFlags) {
  595. return NS_ERROR_DOM_BAD_URI;
  596. }
  597. return NS_OK;
  598. }
  599. static bool
  600. EqualOrSubdomain(nsIURI* aProbeArg, nsIURI* aBase)
  601. {
  602. // Make a clone of the incoming URI, because we're going to mutate it.
  603. nsCOMPtr<nsIURI> probe;
  604. nsresult rv = aProbeArg->Clone(getter_AddRefs(probe));
  605. NS_ENSURE_SUCCESS(rv, false);
  606. nsCOMPtr<nsIEffectiveTLDService> tldService = do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID);
  607. NS_ENSURE_TRUE(tldService, false);
  608. while (true) {
  609. if (nsScriptSecurityManager::SecurityCompareURIs(probe, aBase)) {
  610. return true;
  611. }
  612. nsAutoCString host, newHost;
  613. rv = probe->GetHost(host);
  614. NS_ENSURE_SUCCESS(rv, false);
  615. rv = tldService->GetNextSubDomain(host, newHost);
  616. if (rv == NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS) {
  617. return false;
  618. }
  619. NS_ENSURE_SUCCESS(rv, false);
  620. rv = probe->SetHost(newHost);
  621. NS_ENSURE_SUCCESS(rv, false);
  622. }
  623. }
  624. NS_IMETHODIMP
  625. nsScriptSecurityManager::CheckLoadURIWithPrincipal(nsIPrincipal* aPrincipal,
  626. nsIURI *aTargetURI,
  627. uint32_t aFlags)
  628. {
  629. NS_PRECONDITION(aPrincipal, "CheckLoadURIWithPrincipal must have a principal");
  630. // If someone passes a flag that we don't understand, we should
  631. // fail, because they may need a security check that we don't
  632. // provide.
  633. NS_ENSURE_FALSE(aFlags & ~(nsIScriptSecurityManager::LOAD_IS_AUTOMATIC_DOCUMENT_REPLACEMENT |
  634. nsIScriptSecurityManager::ALLOW_CHROME |
  635. nsIScriptSecurityManager::DISALLOW_SCRIPT |
  636. nsIScriptSecurityManager::DISALLOW_INHERIT_PRINCIPAL |
  637. nsIScriptSecurityManager::DONT_REPORT_ERRORS),
  638. NS_ERROR_UNEXPECTED);
  639. NS_ENSURE_ARG_POINTER(aPrincipal);
  640. NS_ENSURE_ARG_POINTER(aTargetURI);
  641. // If DISALLOW_INHERIT_PRINCIPAL is set, we prevent loading of URIs which
  642. // would do such inheriting. That would be URIs that do not have their own
  643. // security context. We do this even for the system principal.
  644. if (aFlags & nsIScriptSecurityManager::DISALLOW_INHERIT_PRINCIPAL) {
  645. nsresult rv =
  646. DenyAccessIfURIHasFlags(aTargetURI,
  647. nsIProtocolHandler::URI_INHERITS_SECURITY_CONTEXT);
  648. NS_ENSURE_SUCCESS(rv, rv);
  649. }
  650. if (aPrincipal == mSystemPrincipal) {
  651. // Allow access
  652. return NS_OK;
  653. }
  654. nsCOMPtr<nsIURI> sourceURI;
  655. aPrincipal->GetURI(getter_AddRefs(sourceURI));
  656. if (!sourceURI) {
  657. nsCOMPtr<nsIExpandedPrincipal> expanded = do_QueryInterface(aPrincipal);
  658. if (expanded) {
  659. nsTArray< nsCOMPtr<nsIPrincipal> > *whiteList;
  660. expanded->GetWhiteList(&whiteList);
  661. for (uint32_t i = 0; i < whiteList->Length(); ++i) {
  662. nsresult rv = CheckLoadURIWithPrincipal((*whiteList)[i],
  663. aTargetURI,
  664. aFlags);
  665. if (NS_SUCCEEDED(rv)) {
  666. // Allow access if it succeeded with one of the white listed principals
  667. return NS_OK;
  668. }
  669. }
  670. // None of our whitelisted principals worked.
  671. return NS_ERROR_DOM_BAD_URI;
  672. }
  673. NS_ERROR("Non-system principals or expanded principal passed to CheckLoadURIWithPrincipal "
  674. "must have a URI!");
  675. return NS_ERROR_UNEXPECTED;
  676. }
  677. // Automatic loads are not allowed from certain protocols.
  678. if (aFlags & nsIScriptSecurityManager::LOAD_IS_AUTOMATIC_DOCUMENT_REPLACEMENT) {
  679. nsresult rv =
  680. DenyAccessIfURIHasFlags(sourceURI,
  681. nsIProtocolHandler::URI_FORBIDS_AUTOMATIC_DOCUMENT_REPLACEMENT);
  682. NS_ENSURE_SUCCESS(rv, rv);
  683. }
  684. // If either URI is a nested URI, get the base URI
  685. nsCOMPtr<nsIURI> sourceBaseURI = NS_GetInnermostURI(sourceURI);
  686. nsCOMPtr<nsIURI> targetBaseURI = NS_GetInnermostURI(aTargetURI);
  687. //-- get the target scheme
  688. nsAutoCString targetScheme;
  689. nsresult rv = targetBaseURI->GetScheme(targetScheme);
  690. if (NS_FAILED(rv)) return rv;
  691. //-- Some callers do not allow loading javascript:
  692. if ((aFlags & nsIScriptSecurityManager::DISALLOW_SCRIPT) &&
  693. targetScheme.EqualsLiteral("javascript"))
  694. {
  695. return NS_ERROR_DOM_BAD_URI;
  696. }
  697. // Check for uris that are only loadable by principals that subsume them
  698. bool hasFlags;
  699. rv = NS_URIChainHasFlags(targetBaseURI,
  700. nsIProtocolHandler::URI_LOADABLE_BY_SUBSUMERS,
  701. &hasFlags);
  702. NS_ENSURE_SUCCESS(rv, rv);
  703. if (hasFlags) {
  704. // check nothing else in the URI chain has flags that prevent
  705. // access:
  706. rv = CheckLoadURIFlags(sourceURI, aTargetURI, sourceBaseURI,
  707. targetBaseURI, aFlags);
  708. NS_ENSURE_SUCCESS(rv, rv);
  709. // Check the principal is allowed to load the target.
  710. return aPrincipal->CheckMayLoad(targetBaseURI, true, false);
  711. }
  712. //-- get the source scheme
  713. nsAutoCString sourceScheme;
  714. rv = sourceBaseURI->GetScheme(sourceScheme);
  715. if (NS_FAILED(rv)) return rv;
  716. // When comparing schemes, if the relevant pref is set, view-source URIs
  717. // are reachable from same-protocol (so e.g. file: can link to
  718. // view-source:file). This is required for reftests.
  719. static bool sViewSourceReachableFromInner = false;
  720. static bool sCachedViewSourcePref = false;
  721. if (!sCachedViewSourcePref) {
  722. sCachedViewSourcePref = true;
  723. mozilla::Preferences::AddBoolVarCache(&sViewSourceReachableFromInner,
  724. "security.view-source.reachable-from-inner-protocol");
  725. }
  726. bool targetIsViewSource = false;
  727. if (sourceScheme.LowerCaseEqualsLiteral(NS_NULLPRINCIPAL_SCHEME)) {
  728. // A null principal can target its own URI.
  729. if (sourceURI == aTargetURI) {
  730. return NS_OK;
  731. }
  732. }
  733. else if (sViewSourceReachableFromInner &&
  734. sourceScheme.EqualsIgnoreCase(targetScheme.get()) &&
  735. NS_SUCCEEDED(aTargetURI->SchemeIs("view-source", &targetIsViewSource)) &&
  736. targetIsViewSource)
  737. {
  738. // exception for foo: linking to view-source:foo for reftests...
  739. return NS_OK;
  740. }
  741. else if ((!sourceScheme.EqualsIgnoreCase("http") &&
  742. !sourceScheme.EqualsIgnoreCase("https")) &&
  743. targetScheme.EqualsIgnoreCase("moz-icon"))
  744. {
  745. // Exception for linking to moz-icon://.ext?size=...
  746. // Note that because targetScheme is the base (innermost) URI scheme,
  747. // this does NOT allow e.g. file -> moz-icon:file:///... links.
  748. // This is intentional.
  749. return NS_OK;
  750. }
  751. // If we get here, check all the schemes can link to each other, from the top down:
  752. nsCaseInsensitiveCStringComparator stringComparator;
  753. nsCOMPtr<nsIURI> currentURI = sourceURI;
  754. nsCOMPtr<nsIURI> currentOtherURI = aTargetURI;
  755. bool denySameSchemeLinks = false;
  756. rv = NS_URIChainHasFlags(aTargetURI, nsIProtocolHandler::URI_SCHEME_NOT_SELF_LINKABLE,
  757. &denySameSchemeLinks);
  758. if (NS_FAILED(rv)) return rv;
  759. while (currentURI && currentOtherURI) {
  760. nsAutoCString scheme, otherScheme;
  761. currentURI->GetScheme(scheme);
  762. currentOtherURI->GetScheme(otherScheme);
  763. bool schemesMatch = scheme.Equals(otherScheme, stringComparator);
  764. bool isSamePage = false;
  765. // about: URIs are special snowflakes.
  766. if (scheme.EqualsLiteral("about") && schemesMatch) {
  767. nsAutoCString moduleName, otherModuleName;
  768. // about: pages can always link to themselves:
  769. isSamePage =
  770. NS_SUCCEEDED(NS_GetAboutModuleName(currentURI, moduleName)) &&
  771. NS_SUCCEEDED(NS_GetAboutModuleName(currentOtherURI, otherModuleName)) &&
  772. moduleName.Equals(otherModuleName);
  773. if (!isSamePage) {
  774. // We will have allowed the load earlier if the source page has
  775. // system principal. So we know the source has a content
  776. // principal, and it's trying to link to something else.
  777. // Linkable about: pages are always reachable, even if we hit
  778. // the CheckLoadURIFlags call below.
  779. // We punch only 1 other hole: iff the source is unlinkable,
  780. // we let them link to other pages explicitly marked SAFE
  781. // for content. This avoids world-linkable about: pages linking
  782. // to non-world-linkable about: pages.
  783. nsCOMPtr<nsIAboutModule> module, otherModule;
  784. bool knowBothModules =
  785. NS_SUCCEEDED(NS_GetAboutModule(currentURI, getter_AddRefs(module))) &&
  786. NS_SUCCEEDED(NS_GetAboutModule(currentOtherURI, getter_AddRefs(otherModule)));
  787. uint32_t aboutModuleFlags = 0;
  788. uint32_t otherAboutModuleFlags = 0;
  789. knowBothModules = knowBothModules &&
  790. NS_SUCCEEDED(module->GetURIFlags(currentURI, &aboutModuleFlags)) &&
  791. NS_SUCCEEDED(otherModule->GetURIFlags(currentOtherURI, &otherAboutModuleFlags));
  792. if (knowBothModules) {
  793. isSamePage =
  794. !(aboutModuleFlags & nsIAboutModule::MAKE_LINKABLE) &&
  795. (otherAboutModuleFlags & nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT);
  796. if (isSamePage && otherAboutModuleFlags & nsIAboutModule::MAKE_LINKABLE) {
  797. //XXXgijs: this is a hack. The target will be nested
  798. // (with innerURI of moz-safe-about:whatever), and
  799. // the source isn't, so we won't pass if we finish
  800. // the loop. We *should* pass, though, so return here.
  801. // This hack can go away when bug 1228118 is fixed.
  802. return NS_OK;
  803. }
  804. }
  805. }
  806. } else {
  807. bool equalExceptRef = false;
  808. rv = currentURI->EqualsExceptRef(currentOtherURI, &equalExceptRef);
  809. isSamePage = NS_SUCCEEDED(rv) && equalExceptRef;
  810. }
  811. // If schemes are not equal, or they're equal but the target URI
  812. // is different from the source URI and doesn't always allow linking
  813. // from the same scheme, check if the URI flags of the current target
  814. // URI allow the current source URI to link to it.
  815. // The policy is specified by the protocol flags on both URIs.
  816. if (!schemesMatch || (denySameSchemeLinks && !isSamePage)) {
  817. return CheckLoadURIFlags(currentURI, currentOtherURI,
  818. sourceBaseURI, targetBaseURI, aFlags);
  819. }
  820. // Otherwise... check if we can nest another level:
  821. nsCOMPtr<nsINestedURI> nestedURI = do_QueryInterface(currentURI);
  822. nsCOMPtr<nsINestedURI> nestedOtherURI = do_QueryInterface(currentOtherURI);
  823. // If schemes match and neither URI is nested further, we're OK.
  824. if (!nestedURI && !nestedOtherURI) {
  825. return NS_OK;
  826. }
  827. // If one is nested and the other isn't, something is wrong.
  828. if (!nestedURI != !nestedOtherURI) {
  829. return NS_ERROR_DOM_BAD_URI;
  830. }
  831. // Otherwise, both should be nested and we'll go through the loop again.
  832. nestedURI->GetInnerURI(getter_AddRefs(currentURI));
  833. nestedOtherURI->GetInnerURI(getter_AddRefs(currentOtherURI));
  834. }
  835. // We should never get here. We should always return from inside the loop.
  836. return NS_ERROR_DOM_BAD_URI;
  837. }
  838. /**
  839. * Helper method to check whether the target URI and its innermost ("base") URI
  840. * has protocol flags that should stop it from being loaded by the source URI
  841. * (and/or the source URI's innermost ("base") URI), taking into account any
  842. * nsIScriptSecurityManager flags originally passed to
  843. * CheckLoadURIWithPrincipal and friends.
  844. *
  845. * @return if success, access is allowed. Otherwise, deny access
  846. */
  847. nsresult
  848. nsScriptSecurityManager::CheckLoadURIFlags(nsIURI *aSourceURI,
  849. nsIURI *aTargetURI,
  850. nsIURI *aSourceBaseURI,
  851. nsIURI *aTargetBaseURI,
  852. uint32_t aFlags)
  853. {
  854. // Note that the order of policy checks here is very important!
  855. // We start from most restrictive and work our way down.
  856. bool reportErrors = !(aFlags & nsIScriptSecurityManager::DONT_REPORT_ERRORS);
  857. NS_NAMED_LITERAL_STRING(errorTag, "CheckLoadURIError");
  858. nsAutoCString targetScheme;
  859. nsresult rv = aTargetBaseURI->GetScheme(targetScheme);
  860. if (NS_FAILED(rv)) return rv;
  861. // Check for system target URI
  862. rv = DenyAccessIfURIHasFlags(aTargetURI,
  863. nsIProtocolHandler::URI_DANGEROUS_TO_LOAD);
  864. if (NS_FAILED(rv)) {
  865. // Deny access, since the origin principal is not system
  866. if (reportErrors) {
  867. ReportError(nullptr, errorTag, aSourceURI, aTargetURI);
  868. }
  869. return rv;
  870. }
  871. // Check for chrome target URI
  872. bool hasFlags = false;
  873. rv = NS_URIChainHasFlags(aTargetBaseURI,
  874. nsIProtocolHandler::URI_IS_UI_RESOURCE,
  875. &hasFlags);
  876. NS_ENSURE_SUCCESS(rv, rv);
  877. if (hasFlags) {
  878. if (aFlags & nsIScriptSecurityManager::ALLOW_CHROME) {
  879. // For now, don't change behavior for resource:// and
  880. // just allow it. This is required for extensions injecting
  881. // extension-internal resource URLs in snippets in pages, e.g.
  882. // Adding custom controls in-page.
  883. if (!targetScheme.EqualsLiteral("chrome") &&
  884. !targetScheme.EqualsLiteral("moz-icon")) {
  885. return NS_OK;
  886. }
  887. // Allow a URI_IS_UI_RESOURCE source to link to a URI_IS_UI_RESOURCE
  888. // target if ALLOW_CHROME is set.
  889. //
  890. // ALLOW_CHROME is a flag that we pass on all loads _except_ docshell
  891. // loads (since docshell loads run the loaded content with its origin
  892. // principal). So we're effectively allowing resource://, chrome://,
  893. // and moz-icon:// source URIs to load resource://, chrome://, and
  894. // moz-icon:// files, so long as they're not loading it as a document.
  895. bool sourceIsUIResource;
  896. rv = NS_URIChainHasFlags(aSourceBaseURI,
  897. nsIProtocolHandler::URI_IS_UI_RESOURCE,
  898. &sourceIsUIResource);
  899. NS_ENSURE_SUCCESS(rv, rv);
  900. if (sourceIsUIResource) {
  901. return NS_OK;
  902. }
  903. // Allow the load only if the chrome package is whitelisted.
  904. nsCOMPtr<nsIXULChromeRegistry> reg(do_GetService(
  905. NS_CHROMEREGISTRY_CONTRACTID));
  906. if (reg) {
  907. bool accessAllowed = false;
  908. reg->AllowContentToAccess(aTargetBaseURI, &accessAllowed);
  909. if (accessAllowed) {
  910. return NS_OK;
  911. }
  912. }
  913. }
  914. // Special-case the hidden window: it's allowed to load
  915. // URI_IS_UI_RESOURCE no matter what. Bug 1145470 tracks removing this.
  916. nsAutoCString sourceSpec;
  917. if (NS_SUCCEEDED(aSourceBaseURI->GetSpec(sourceSpec)) &&
  918. sourceSpec.EqualsLiteral("resource://gre-resources/hiddenWindow.html")) {
  919. return NS_OK;
  920. }
  921. if (reportErrors) {
  922. ReportError(nullptr, errorTag, aSourceURI, aTargetURI);
  923. }
  924. return NS_ERROR_DOM_BAD_URI;
  925. }
  926. // Check for target URI pointing to a file
  927. rv = NS_URIChainHasFlags(aTargetURI,
  928. nsIProtocolHandler::URI_IS_LOCAL_FILE,
  929. &hasFlags);
  930. NS_ENSURE_SUCCESS(rv, rv);
  931. if (hasFlags) {
  932. // Allow domains that were whitelisted in the prefs. In 99.9% of cases,
  933. // this array is empty.
  934. for (nsIURI* uri : EnsureFileURIWhitelist()) {
  935. if (EqualOrSubdomain(aSourceURI, uri)) {
  936. return NS_OK;
  937. }
  938. }
  939. // Allow chrome://
  940. bool isChrome = false;
  941. if (NS_SUCCEEDED(aSourceBaseURI->SchemeIs("chrome", &isChrome)) && isChrome) {
  942. return NS_OK;
  943. }
  944. // Nothing else.
  945. if (reportErrors) {
  946. ReportError(nullptr, errorTag, aSourceURI, aTargetURI);
  947. }
  948. return NS_ERROR_DOM_BAD_URI;
  949. }
  950. // OK, everyone is allowed to load this, since unflagged handlers are
  951. // deprecated but treated as URI_LOADABLE_BY_ANYONE. But check whether we
  952. // need to warn. At some point we'll want to make this warning into an
  953. // error and treat unflagged handlers as URI_DANGEROUS_TO_LOAD.
  954. rv = NS_URIChainHasFlags(aTargetBaseURI,
  955. nsIProtocolHandler::URI_LOADABLE_BY_ANYONE,
  956. &hasFlags);
  957. NS_ENSURE_SUCCESS(rv, rv);
  958. // NB: we also get here if the base URI is URI_LOADABLE_BY_SUBSUMERS,
  959. // and none of the rest of the nested chain of URIs for aTargetURI
  960. // prohibits the load, so avoid warning in that case:
  961. bool hasSubsumersFlag = false;
  962. rv = NS_URIChainHasFlags(aTargetBaseURI,
  963. nsIProtocolHandler::URI_LOADABLE_BY_SUBSUMERS,
  964. &hasSubsumersFlag);
  965. NS_ENSURE_SUCCESS(rv, rv);
  966. if (!hasFlags && !hasSubsumersFlag) {
  967. nsXPIDLString message;
  968. NS_ConvertASCIItoUTF16 ucsTargetScheme(targetScheme);
  969. const char16_t* formatStrings[] = { ucsTargetScheme.get() };
  970. rv = sStrBundle->
  971. FormatStringFromName(u"ProtocolFlagError",
  972. formatStrings,
  973. ArrayLength(formatStrings),
  974. getter_Copies(message));
  975. if (NS_SUCCEEDED(rv)) {
  976. nsCOMPtr<nsIConsoleService> console(
  977. do_GetService("@mozilla.org/consoleservice;1"));
  978. NS_ENSURE_TRUE(console, NS_ERROR_FAILURE);
  979. console->LogStringMessage(message.get());
  980. }
  981. }
  982. return NS_OK;
  983. }
  984. nsresult
  985. nsScriptSecurityManager::ReportError(JSContext* cx, const nsAString& messageTag,
  986. nsIURI* aSource, nsIURI* aTarget)
  987. {
  988. nsresult rv;
  989. NS_ENSURE_TRUE(aSource && aTarget, NS_ERROR_NULL_POINTER);
  990. // Get the source URL spec
  991. nsAutoCString sourceSpec;
  992. rv = aSource->GetAsciiSpec(sourceSpec);
  993. NS_ENSURE_SUCCESS(rv, rv);
  994. // Get the target URL spec
  995. nsAutoCString targetSpec;
  996. rv = aTarget->GetAsciiSpec(targetSpec);
  997. NS_ENSURE_SUCCESS(rv, rv);
  998. // Localize the error message
  999. nsXPIDLString message;
  1000. NS_ConvertASCIItoUTF16 ucsSourceSpec(sourceSpec);
  1001. NS_ConvertASCIItoUTF16 ucsTargetSpec(targetSpec);
  1002. const char16_t *formatStrings[] = { ucsSourceSpec.get(), ucsTargetSpec.get() };
  1003. rv = sStrBundle->FormatStringFromName(PromiseFlatString(messageTag).get(),
  1004. formatStrings,
  1005. ArrayLength(formatStrings),
  1006. getter_Copies(message));
  1007. NS_ENSURE_SUCCESS(rv, rv);
  1008. // If a JS context was passed in, set a JS exception.
  1009. // Otherwise, print the error message directly to the JS console
  1010. // and to standard output
  1011. if (cx)
  1012. {
  1013. SetPendingException(cx, message.get());
  1014. }
  1015. else // Print directly to the console
  1016. {
  1017. nsCOMPtr<nsIConsoleService> console(
  1018. do_GetService("@mozilla.org/consoleservice;1"));
  1019. NS_ENSURE_TRUE(console, NS_ERROR_FAILURE);
  1020. console->LogStringMessage(message.get());
  1021. }
  1022. return NS_OK;
  1023. }
  1024. NS_IMETHODIMP
  1025. nsScriptSecurityManager::CheckLoadURIStrWithPrincipal(nsIPrincipal* aPrincipal,
  1026. const nsACString& aTargetURIStr,
  1027. uint32_t aFlags)
  1028. {
  1029. nsresult rv;
  1030. nsCOMPtr<nsIURI> target;
  1031. rv = NS_NewURI(getter_AddRefs(target), aTargetURIStr,
  1032. nullptr, nullptr, sIOService);
  1033. NS_ENSURE_SUCCESS(rv, rv);
  1034. rv = CheckLoadURIWithPrincipal(aPrincipal, target, aFlags);
  1035. if (rv == NS_ERROR_DOM_BAD_URI) {
  1036. // Don't warn because NS_ERROR_DOM_BAD_URI is one of the expected
  1037. // return values.
  1038. return rv;
  1039. }
  1040. NS_ENSURE_SUCCESS(rv, rv);
  1041. // Now start testing fixup -- since aTargetURIStr is a string, not
  1042. // an nsIURI, we may well end up fixing it up before loading.
  1043. // Note: This needs to stay in sync with the nsIURIFixup api.
  1044. nsCOMPtr<nsIURIFixup> fixup = do_GetService(NS_URIFIXUP_CONTRACTID);
  1045. if (!fixup) {
  1046. return rv;
  1047. }
  1048. uint32_t flags[] = {
  1049. nsIURIFixup::FIXUP_FLAG_NONE,
  1050. nsIURIFixup::FIXUP_FLAG_FIX_SCHEME_TYPOS,
  1051. nsIURIFixup::FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP,
  1052. nsIURIFixup::FIXUP_FLAGS_MAKE_ALTERNATE_URI,
  1053. nsIURIFixup::FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP |
  1054. nsIURIFixup::FIXUP_FLAGS_MAKE_ALTERNATE_URI
  1055. };
  1056. for (uint32_t i = 0; i < ArrayLength(flags); ++i) {
  1057. rv = fixup->CreateFixupURI(aTargetURIStr, flags[i], nullptr,
  1058. getter_AddRefs(target));
  1059. NS_ENSURE_SUCCESS(rv, rv);
  1060. rv = CheckLoadURIWithPrincipal(aPrincipal, target, aFlags);
  1061. if (rv == NS_ERROR_DOM_BAD_URI) {
  1062. // Don't warn because NS_ERROR_DOM_BAD_URI is one of the expected
  1063. // return values.
  1064. return rv;
  1065. }
  1066. NS_ENSURE_SUCCESS(rv, rv);
  1067. }
  1068. return rv;
  1069. }
  1070. ///////////////// Principals ///////////////////////
  1071. NS_IMETHODIMP
  1072. nsScriptSecurityManager::GetSystemPrincipal(nsIPrincipal **result)
  1073. {
  1074. NS_ADDREF(*result = mSystemPrincipal);
  1075. return NS_OK;
  1076. }
  1077. NS_IMETHODIMP
  1078. nsScriptSecurityManager::GetNoAppCodebasePrincipal(nsIURI* aURI,
  1079. nsIPrincipal** aPrincipal)
  1080. {
  1081. PrincipalOriginAttributes attrs(NO_APP_ID, false);
  1082. nsCOMPtr<nsIPrincipal> prin = BasePrincipal::CreateCodebasePrincipal(aURI, attrs);
  1083. prin.forget(aPrincipal);
  1084. return *aPrincipal ? NS_OK : NS_ERROR_FAILURE;
  1085. }
  1086. NS_IMETHODIMP
  1087. nsScriptSecurityManager::GetCodebasePrincipal(nsIURI* aURI,
  1088. nsIPrincipal** aPrincipal)
  1089. {
  1090. return GetNoAppCodebasePrincipal(aURI, aPrincipal);
  1091. }
  1092. NS_IMETHODIMP
  1093. nsScriptSecurityManager::CreateCodebasePrincipal(nsIURI* aURI, JS::Handle<JS::Value> aOriginAttributes,
  1094. JSContext* aCx, nsIPrincipal** aPrincipal)
  1095. {
  1096. PrincipalOriginAttributes attrs;
  1097. if (!aOriginAttributes.isObject() || !attrs.Init(aCx, aOriginAttributes)) {
  1098. return NS_ERROR_INVALID_ARG;
  1099. }
  1100. nsCOMPtr<nsIPrincipal> prin = BasePrincipal::CreateCodebasePrincipal(aURI, attrs);
  1101. prin.forget(aPrincipal);
  1102. return *aPrincipal ? NS_OK : NS_ERROR_FAILURE;
  1103. }
  1104. NS_IMETHODIMP
  1105. nsScriptSecurityManager::CreateCodebasePrincipalFromOrigin(const nsACString& aOrigin,
  1106. nsIPrincipal** aPrincipal)
  1107. {
  1108. if (StringBeginsWith(aOrigin, NS_LITERAL_CSTRING("["))) {
  1109. return NS_ERROR_INVALID_ARG;
  1110. }
  1111. if (StringBeginsWith(aOrigin, NS_LITERAL_CSTRING(NS_NULLPRINCIPAL_SCHEME ":"))) {
  1112. return NS_ERROR_INVALID_ARG;
  1113. }
  1114. nsCOMPtr<nsIPrincipal> prin = BasePrincipal::CreateCodebasePrincipal(aOrigin);
  1115. prin.forget(aPrincipal);
  1116. return *aPrincipal ? NS_OK : NS_ERROR_FAILURE;
  1117. }
  1118. NS_IMETHODIMP
  1119. nsScriptSecurityManager::CreateNullPrincipal(JS::Handle<JS::Value> aOriginAttributes,
  1120. JSContext* aCx, nsIPrincipal** aPrincipal)
  1121. {
  1122. PrincipalOriginAttributes attrs;
  1123. if (!aOriginAttributes.isObject() || !attrs.Init(aCx, aOriginAttributes)) {
  1124. return NS_ERROR_INVALID_ARG;
  1125. }
  1126. nsCOMPtr<nsIPrincipal> prin = nsNullPrincipal::Create(attrs);
  1127. prin.forget(aPrincipal);
  1128. return NS_OK;
  1129. }
  1130. NS_IMETHODIMP
  1131. nsScriptSecurityManager::GetAppCodebasePrincipal(nsIURI* aURI,
  1132. uint32_t aAppId,
  1133. bool aInIsolatedMozBrowser,
  1134. nsIPrincipal** aPrincipal)
  1135. {
  1136. NS_ENSURE_TRUE(aAppId != nsIScriptSecurityManager::UNKNOWN_APP_ID,
  1137. NS_ERROR_INVALID_ARG);
  1138. PrincipalOriginAttributes attrs(aAppId, aInIsolatedMozBrowser);
  1139. nsCOMPtr<nsIPrincipal> prin = BasePrincipal::CreateCodebasePrincipal(aURI, attrs);
  1140. prin.forget(aPrincipal);
  1141. return *aPrincipal ? NS_OK : NS_ERROR_FAILURE;
  1142. }
  1143. NS_IMETHODIMP
  1144. nsScriptSecurityManager::
  1145. GetLoadContextCodebasePrincipal(nsIURI* aURI,
  1146. nsILoadContext* aLoadContext,
  1147. nsIPrincipal** aPrincipal)
  1148. {
  1149. NS_ENSURE_STATE(aLoadContext);
  1150. DocShellOriginAttributes docShellAttrs;
  1151. bool result = aLoadContext->GetOriginAttributes(docShellAttrs);;
  1152. NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
  1153. PrincipalOriginAttributes attrs;
  1154. attrs.InheritFromDocShellToDoc(docShellAttrs, aURI);
  1155. nsresult rv = MaybeSetAddonIdFromURI(attrs, aURI);
  1156. NS_ENSURE_SUCCESS(rv, rv);
  1157. nsCOMPtr<nsIPrincipal> prin = BasePrincipal::CreateCodebasePrincipal(aURI, attrs);
  1158. prin.forget(aPrincipal);
  1159. return *aPrincipal ? NS_OK : NS_ERROR_FAILURE;
  1160. }
  1161. NS_IMETHODIMP
  1162. nsScriptSecurityManager::GetDocShellCodebasePrincipal(nsIURI* aURI,
  1163. nsIDocShell* aDocShell,
  1164. nsIPrincipal** aPrincipal)
  1165. {
  1166. PrincipalOriginAttributes attrs;
  1167. attrs.InheritFromDocShellToDoc(nsDocShell::Cast(aDocShell)->GetOriginAttributes(), aURI);
  1168. nsresult rv = MaybeSetAddonIdFromURI(attrs, aURI);
  1169. NS_ENSURE_SUCCESS(rv, rv);
  1170. nsCOMPtr<nsIPrincipal> prin = BasePrincipal::CreateCodebasePrincipal(aURI, attrs);
  1171. prin.forget(aPrincipal);
  1172. return *aPrincipal ? NS_OK : NS_ERROR_FAILURE;
  1173. }
  1174. // static
  1175. nsIPrincipal*
  1176. nsScriptSecurityManager::doGetObjectPrincipal(JSObject *aObj)
  1177. {
  1178. JSCompartment *compartment = js::GetObjectCompartment(aObj);
  1179. JSPrincipals *principals = JS_GetCompartmentPrincipals(compartment);
  1180. return nsJSPrincipals::get(principals);
  1181. }
  1182. NS_IMETHODIMP
  1183. nsScriptSecurityManager::CanCreateWrapper(JSContext *cx,
  1184. const nsIID &aIID,
  1185. nsISupports *aObj,
  1186. nsIClassInfo *aClassInfo)
  1187. {
  1188. // XXX Special case for nsIXPCException ?
  1189. ClassInfoData objClassInfo = ClassInfoData(aClassInfo, nullptr);
  1190. if (objClassInfo.IsDOMClass())
  1191. {
  1192. return NS_OK;
  1193. }
  1194. // We give remote-XUL whitelisted domains a free pass here. See bug 932906.
  1195. if (!xpc::AllowContentXBLScope(js::GetContextCompartment(cx)))
  1196. {
  1197. return NS_OK;
  1198. }
  1199. if (nsContentUtils::IsCallerChrome())
  1200. {
  1201. return NS_OK;
  1202. }
  1203. //-- Access denied, report an error
  1204. NS_ConvertUTF8toUTF16 strName("CreateWrapperDenied");
  1205. nsAutoCString origin;
  1206. nsIPrincipal* subjectPrincipal = nsContentUtils::SubjectPrincipal();
  1207. GetPrincipalDomainOrigin(subjectPrincipal, origin);
  1208. NS_ConvertUTF8toUTF16 originUnicode(origin);
  1209. NS_ConvertUTF8toUTF16 classInfoName(objClassInfo.GetName());
  1210. const char16_t* formatStrings[] = {
  1211. classInfoName.get(),
  1212. originUnicode.get()
  1213. };
  1214. uint32_t length = ArrayLength(formatStrings);
  1215. if (originUnicode.IsEmpty()) {
  1216. --length;
  1217. } else {
  1218. strName.AppendLiteral("ForOrigin");
  1219. }
  1220. nsXPIDLString errorMsg;
  1221. nsresult rv = sStrBundle->FormatStringFromName(strName.get(),
  1222. formatStrings,
  1223. length,
  1224. getter_Copies(errorMsg));
  1225. NS_ENSURE_SUCCESS(rv, rv);
  1226. SetPendingException(cx, errorMsg.get());
  1227. return NS_ERROR_DOM_XPCONNECT_ACCESS_DENIED;
  1228. }
  1229. NS_IMETHODIMP
  1230. nsScriptSecurityManager::CanCreateInstance(JSContext *cx,
  1231. const nsCID &aCID)
  1232. {
  1233. if (nsContentUtils::IsCallerChrome()) {
  1234. return NS_OK;
  1235. }
  1236. //-- Access denied, report an error
  1237. nsAutoCString errorMsg("Permission denied to create instance of class. CID=");
  1238. char cidStr[NSID_LENGTH];
  1239. aCID.ToProvidedString(cidStr);
  1240. errorMsg.Append(cidStr);
  1241. SetPendingExceptionASCII(cx, errorMsg.get());
  1242. return NS_ERROR_DOM_XPCONNECT_ACCESS_DENIED;
  1243. }
  1244. NS_IMETHODIMP
  1245. nsScriptSecurityManager::CanGetService(JSContext *cx,
  1246. const nsCID &aCID)
  1247. {
  1248. if (nsContentUtils::IsCallerChrome()) {
  1249. return NS_OK;
  1250. }
  1251. //-- Access denied, report an error
  1252. nsAutoCString errorMsg("Permission denied to get service. CID=");
  1253. char cidStr[NSID_LENGTH];
  1254. aCID.ToProvidedString(cidStr);
  1255. errorMsg.Append(cidStr);
  1256. SetPendingExceptionASCII(cx, errorMsg.get());
  1257. return NS_ERROR_DOM_XPCONNECT_ACCESS_DENIED;
  1258. }
  1259. /////////////////////////////////////
  1260. // Method implementing nsIObserver //
  1261. /////////////////////////////////////
  1262. const char sJSEnabledPrefName[] = "javascript.enabled";
  1263. const char sFileOriginPolicyPrefName[] =
  1264. "security.fileuri.strict_origin_policy";
  1265. static const char* kObservedPrefs[] = {
  1266. sJSEnabledPrefName,
  1267. sFileOriginPolicyPrefName,
  1268. "capability.policy.",
  1269. nullptr
  1270. };
  1271. NS_IMETHODIMP
  1272. nsScriptSecurityManager::Observe(nsISupports* aObject, const char* aTopic,
  1273. const char16_t* aMessage)
  1274. {
  1275. ScriptSecurityPrefChanged();
  1276. return NS_OK;
  1277. }
  1278. /////////////////////////////////////////////
  1279. // Constructor, Destructor, Initialization //
  1280. /////////////////////////////////////////////
  1281. nsScriptSecurityManager::nsScriptSecurityManager(void)
  1282. : mPrefInitialized(false)
  1283. , mIsJavaScriptEnabled(false)
  1284. {
  1285. static_assert(sizeof(intptr_t) == sizeof(void*),
  1286. "intptr_t and void* have different lengths on this platform. "
  1287. "This may cause a security failure with the SecurityLevel union.");
  1288. }
  1289. nsresult nsScriptSecurityManager::Init()
  1290. {
  1291. nsresult rv = CallGetService(NS_IOSERVICE_CONTRACTID, &sIOService);
  1292. NS_ENSURE_SUCCESS(rv, rv);
  1293. InitPrefs();
  1294. nsCOMPtr<nsIStringBundleService> bundleService =
  1295. mozilla::services::GetStringBundleService();
  1296. if (!bundleService)
  1297. return NS_ERROR_FAILURE;
  1298. rv = bundleService->CreateBundle("chrome://global/locale/security/caps.properties", &sStrBundle);
  1299. NS_ENSURE_SUCCESS(rv, rv);
  1300. // Create our system principal singleton
  1301. RefPtr<nsSystemPrincipal> system = new nsSystemPrincipal();
  1302. mSystemPrincipal = system;
  1303. //-- Register security check callback in the JS engine
  1304. // Currently this is used to control access to function.caller
  1305. sContext = danger::GetJSContext();
  1306. static const JSSecurityCallbacks securityCallbacks = {
  1307. ContentSecurityPolicyPermitsJSAction,
  1308. JSPrincipalsSubsume,
  1309. };
  1310. MOZ_ASSERT(!JS_GetSecurityCallbacks(sContext));
  1311. JS_SetSecurityCallbacks(sContext, &securityCallbacks);
  1312. JS_InitDestroyPrincipalsCallback(sContext, nsJSPrincipals::Destroy);
  1313. JS_SetTrustedPrincipals(sContext, system);
  1314. return NS_OK;
  1315. }
  1316. static StaticRefPtr<nsScriptSecurityManager> gScriptSecMan;
  1317. nsScriptSecurityManager::~nsScriptSecurityManager(void)
  1318. {
  1319. Preferences::RemoveObservers(this, kObservedPrefs);
  1320. if (mDomainPolicy) {
  1321. mDomainPolicy->Deactivate();
  1322. }
  1323. // ContentChild might hold a reference to the domain policy,
  1324. // and it might release it only after the security manager is
  1325. // gone. But we can still assert this for the main process.
  1326. MOZ_ASSERT_IF(XRE_IsParentProcess(),
  1327. !mDomainPolicy);
  1328. }
  1329. void
  1330. nsScriptSecurityManager::Shutdown()
  1331. {
  1332. if (sContext) {
  1333. JS_SetSecurityCallbacks(sContext, nullptr);
  1334. JS_SetTrustedPrincipals(sContext, nullptr);
  1335. sContext = nullptr;
  1336. }
  1337. NS_IF_RELEASE(sIOService);
  1338. NS_IF_RELEASE(sStrBundle);
  1339. }
  1340. nsScriptSecurityManager *
  1341. nsScriptSecurityManager::GetScriptSecurityManager()
  1342. {
  1343. return gScriptSecMan;
  1344. }
  1345. /* static */ void
  1346. nsScriptSecurityManager::InitStatics()
  1347. {
  1348. RefPtr<nsScriptSecurityManager> ssManager = new nsScriptSecurityManager();
  1349. nsresult rv = ssManager->Init();
  1350. if (NS_FAILED(rv)) {
  1351. MOZ_CRASH("ssManager->Init() failed");
  1352. }
  1353. ClearOnShutdown(&gScriptSecMan);
  1354. gScriptSecMan = ssManager;
  1355. }
  1356. // Currently this nsGenericFactory constructor is used only from FastLoad
  1357. // (XPCOM object deserialization) code, when "creating" the system principal
  1358. // singleton.
  1359. nsSystemPrincipal *
  1360. nsScriptSecurityManager::SystemPrincipalSingletonConstructor()
  1361. {
  1362. nsIPrincipal *sysprin = nullptr;
  1363. if (gScriptSecMan)
  1364. NS_ADDREF(sysprin = gScriptSecMan->mSystemPrincipal);
  1365. return static_cast<nsSystemPrincipal*>(sysprin);
  1366. }
  1367. struct IsWhitespace {
  1368. static bool Test(char aChar) { return NS_IsAsciiWhitespace(aChar); };
  1369. };
  1370. struct IsWhitespaceOrComma {
  1371. static bool Test(char aChar) { return aChar == ',' || NS_IsAsciiWhitespace(aChar); };
  1372. };
  1373. template <typename Predicate>
  1374. uint32_t SkipPast(const nsCString& str, uint32_t base)
  1375. {
  1376. while (base < str.Length() && Predicate::Test(str[base])) {
  1377. ++base;
  1378. }
  1379. return base;
  1380. }
  1381. template <typename Predicate>
  1382. uint32_t SkipUntil(const nsCString& str, uint32_t base)
  1383. {
  1384. while (base < str.Length() && !Predicate::Test(str[base])) {
  1385. ++base;
  1386. }
  1387. return base;
  1388. }
  1389. inline void
  1390. nsScriptSecurityManager::ScriptSecurityPrefChanged()
  1391. {
  1392. MOZ_ASSERT(mPrefInitialized);
  1393. mIsJavaScriptEnabled =
  1394. Preferences::GetBool(sJSEnabledPrefName, mIsJavaScriptEnabled);
  1395. sStrictFileOriginPolicy =
  1396. Preferences::GetBool(sFileOriginPolicyPrefName, false);
  1397. mFileURIWhitelist.reset();
  1398. }
  1399. void
  1400. nsScriptSecurityManager::AddSitesToFileURIWhitelist(const nsCString& aSiteList)
  1401. {
  1402. for (uint32_t base = SkipPast<IsWhitespace>(aSiteList, 0), bound = 0;
  1403. base < aSiteList.Length();
  1404. base = SkipPast<IsWhitespace>(aSiteList, bound))
  1405. {
  1406. // Grab the current site.
  1407. bound = SkipUntil<IsWhitespace>(aSiteList, base);
  1408. nsAutoCString site(Substring(aSiteList, base, bound - base));
  1409. // Check if the URI is schemeless. If so, add both http and https.
  1410. nsAutoCString unused;
  1411. if (NS_FAILED(sIOService->ExtractScheme(site, unused))) {
  1412. AddSitesToFileURIWhitelist(NS_LITERAL_CSTRING("http://") + site);
  1413. AddSitesToFileURIWhitelist(NS_LITERAL_CSTRING("https://") + site);
  1414. continue;
  1415. }
  1416. // Convert it to a URI and add it to our list.
  1417. nsCOMPtr<nsIURI> uri;
  1418. nsresult rv = NS_NewURI(getter_AddRefs(uri), site, nullptr, nullptr, sIOService);
  1419. if (NS_SUCCEEDED(rv)) {
  1420. mFileURIWhitelist.ref().AppendElement(uri);
  1421. } else {
  1422. nsCOMPtr<nsIConsoleService> console(do_GetService("@mozilla.org/consoleservice;1"));
  1423. if (console) {
  1424. nsAutoString msg = NS_LITERAL_STRING("Unable to to add site to file:// URI whitelist: ") +
  1425. NS_ConvertASCIItoUTF16(site);
  1426. console->LogStringMessage(msg.get());
  1427. }
  1428. }
  1429. }
  1430. }
  1431. nsresult
  1432. nsScriptSecurityManager::InitPrefs()
  1433. {
  1434. nsIPrefBranch* branch = Preferences::GetRootBranch();
  1435. NS_ENSURE_TRUE(branch, NS_ERROR_FAILURE);
  1436. mPrefInitialized = true;
  1437. // Set the initial value of the "javascript.enabled" prefs
  1438. ScriptSecurityPrefChanged();
  1439. // set observer callbacks in case the value of the prefs change
  1440. Preferences::AddStrongObservers(this, kObservedPrefs);
  1441. return NS_OK;
  1442. }
  1443. NS_IMETHODIMP
  1444. nsScriptSecurityManager::GetDomainPolicyActive(bool *aRv)
  1445. {
  1446. *aRv = !!mDomainPolicy;
  1447. return NS_OK;
  1448. }
  1449. NS_IMETHODIMP
  1450. nsScriptSecurityManager::ActivateDomainPolicy(nsIDomainPolicy** aRv)
  1451. {
  1452. if (!XRE_IsParentProcess()) {
  1453. return NS_ERROR_SERVICE_NOT_AVAILABLE;
  1454. }
  1455. return ActivateDomainPolicyInternal(aRv);
  1456. }
  1457. NS_IMETHODIMP
  1458. nsScriptSecurityManager::ActivateDomainPolicyInternal(nsIDomainPolicy** aRv)
  1459. {
  1460. // We only allow one domain policy at a time. The holder of the previous
  1461. // policy must explicitly deactivate it first.
  1462. if (mDomainPolicy) {
  1463. return NS_ERROR_SERVICE_NOT_AVAILABLE;
  1464. }
  1465. mDomainPolicy = new DomainPolicy();
  1466. nsCOMPtr<nsIDomainPolicy> ptr = mDomainPolicy;
  1467. ptr.forget(aRv);
  1468. return NS_OK;
  1469. }
  1470. // Intentionally non-scriptable. Script must have a reference to the
  1471. // nsIDomainPolicy to deactivate it.
  1472. void
  1473. nsScriptSecurityManager::DeactivateDomainPolicy()
  1474. {
  1475. mDomainPolicy = nullptr;
  1476. }
  1477. void
  1478. nsScriptSecurityManager::CloneDomainPolicy(DomainPolicyClone* aClone)
  1479. {
  1480. MOZ_ASSERT(aClone);
  1481. if (mDomainPolicy) {
  1482. mDomainPolicy->CloneDomainPolicy(aClone);
  1483. } else {
  1484. aClone->active() = false;
  1485. }
  1486. }
  1487. NS_IMETHODIMP
  1488. nsScriptSecurityManager::PolicyAllowsScript(nsIURI* aURI, bool *aRv)
  1489. {
  1490. nsresult rv;
  1491. // Compute our rule. If we don't have any domain policy set up that might
  1492. // provide exceptions to this rule, we're done.
  1493. *aRv = mIsJavaScriptEnabled;
  1494. if (!mDomainPolicy) {
  1495. return NS_OK;
  1496. }
  1497. // We have a domain policy. Grab the appropriate set of exceptions to the
  1498. // rule (either the blacklist or the whitelist, depending on whether script
  1499. // is enabled or disabled by default).
  1500. nsCOMPtr<nsIDomainSet> exceptions;
  1501. nsCOMPtr<nsIDomainSet> superExceptions;
  1502. if (*aRv) {
  1503. mDomainPolicy->GetBlacklist(getter_AddRefs(exceptions));
  1504. mDomainPolicy->GetSuperBlacklist(getter_AddRefs(superExceptions));
  1505. } else {
  1506. mDomainPolicy->GetWhitelist(getter_AddRefs(exceptions));
  1507. mDomainPolicy->GetSuperWhitelist(getter_AddRefs(superExceptions));
  1508. }
  1509. bool contains;
  1510. rv = exceptions->Contains(aURI, &contains);
  1511. NS_ENSURE_SUCCESS(rv, rv);
  1512. if (contains) {
  1513. *aRv = !*aRv;
  1514. return NS_OK;
  1515. }
  1516. rv = superExceptions->ContainsSuperDomain(aURI, &contains);
  1517. NS_ENSURE_SUCCESS(rv, rv);
  1518. if (contains) {
  1519. *aRv = !*aRv;
  1520. }
  1521. return NS_OK;
  1522. }
  1523. const nsTArray<nsCOMPtr<nsIURI>>&
  1524. nsScriptSecurityManager::EnsureFileURIWhitelist()
  1525. {
  1526. if (mFileURIWhitelist.isSome()) {
  1527. return mFileURIWhitelist.ref();
  1528. }
  1529. //
  1530. // Rebuild the set of principals for which we allow file:// URI loads. This
  1531. // implements a small subset of an old pref-based CAPS people that people
  1532. // have come to depend on. See bug 995943.
  1533. //
  1534. mFileURIWhitelist.emplace();
  1535. auto policies = mozilla::Preferences::GetCString("capability.policy.policynames");
  1536. for (uint32_t base = SkipPast<IsWhitespaceOrComma>(policies, 0), bound = 0;
  1537. base < policies.Length();
  1538. base = SkipPast<IsWhitespaceOrComma>(policies, bound))
  1539. {
  1540. // Grab the current policy name.
  1541. bound = SkipUntil<IsWhitespaceOrComma>(policies, base);
  1542. auto policyName = Substring(policies, base, bound - base);
  1543. // Figure out if this policy allows loading file:// URIs. If not, we can skip it.
  1544. nsCString checkLoadURIPrefName = NS_LITERAL_CSTRING("capability.policy.") +
  1545. policyName +
  1546. NS_LITERAL_CSTRING(".checkloaduri.enabled");
  1547. if (!Preferences::GetString(checkLoadURIPrefName.get()).LowerCaseEqualsLiteral("allaccess")) {
  1548. continue;
  1549. }
  1550. // Grab the list of domains associated with this policy.
  1551. nsCString domainPrefName = NS_LITERAL_CSTRING("capability.policy.") +
  1552. policyName +
  1553. NS_LITERAL_CSTRING(".sites");
  1554. auto siteList = Preferences::GetCString(domainPrefName.get());
  1555. AddSitesToFileURIWhitelist(siteList);
  1556. }
  1557. return mFileURIWhitelist.ref();
  1558. }