nsPrincipal.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "nsPrincipal.h"
  6. #include "mozIThirdPartyUtil.h"
  7. #include "nscore.h"
  8. #include "nsScriptSecurityManager.h"
  9. #include "nsString.h"
  10. #include "nsReadableUtils.h"
  11. #include "pratom.h"
  12. #include "nsIURI.h"
  13. #include "nsIURL.h"
  14. #include "nsIStandardURL.h"
  15. #include "nsIURIWithPrincipal.h"
  16. #include "nsJSPrincipals.h"
  17. #include "nsIEffectiveTLDService.h"
  18. #include "nsIClassInfoImpl.h"
  19. #include "nsIObjectInputStream.h"
  20. #include "nsIObjectOutputStream.h"
  21. #include "nsIProtocolHandler.h"
  22. #include "nsError.h"
  23. #include "nsIContentSecurityPolicy.h"
  24. #include "nsNetCID.h"
  25. #include "jswrapper.h"
  26. #include "mozilla/dom/nsCSPContext.h"
  27. #include "mozilla/dom/ScriptSettings.h"
  28. #include "mozilla/Preferences.h"
  29. #include "mozilla/HashFunctions.h"
  30. #include "nsIAppsService.h"
  31. using namespace mozilla;
  32. static bool gIsWhitelistingTestDomains = false;
  33. static bool gCodeBasePrincipalSupport = false;
  34. static bool URIIsImmutable(nsIURI* aURI)
  35. {
  36. nsCOMPtr<nsIMutable> mutableObj(do_QueryInterface(aURI));
  37. bool isMutable;
  38. return
  39. mutableObj &&
  40. NS_SUCCEEDED(mutableObj->GetMutable(&isMutable)) &&
  41. !isMutable;
  42. }
  43. NS_IMPL_CLASSINFO(nsPrincipal, nullptr, nsIClassInfo::MAIN_THREAD_ONLY,
  44. NS_PRINCIPAL_CID)
  45. NS_IMPL_QUERY_INTERFACE_CI(nsPrincipal,
  46. nsIPrincipal,
  47. nsISerializable)
  48. NS_IMPL_CI_INTERFACE_GETTER(nsPrincipal,
  49. nsIPrincipal,
  50. nsISerializable)
  51. // Called at startup:
  52. /* static */ void
  53. nsPrincipal::InitializeStatics()
  54. {
  55. Preferences::AddBoolVarCache(
  56. &gIsWhitelistingTestDomains,
  57. "layout.css.unprefixing-service.include-test-domains");
  58. Preferences::AddBoolVarCache(&gCodeBasePrincipalSupport,
  59. "signed.applets.codebase_principal_support",
  60. false);
  61. }
  62. nsPrincipal::nsPrincipal()
  63. : mCodebaseImmutable(false)
  64. , mDomainImmutable(false)
  65. , mInitialized(false)
  66. { }
  67. nsPrincipal::~nsPrincipal()
  68. {
  69. // let's clear the principal within the csp to avoid a tangling pointer
  70. if (mCSP) {
  71. static_cast<nsCSPContext*>(mCSP.get())->clearLoadingPrincipal();
  72. }
  73. }
  74. nsresult
  75. nsPrincipal::Init(nsIURI *aCodebase, const PrincipalOriginAttributes& aOriginAttributes)
  76. {
  77. NS_ENSURE_STATE(!mInitialized);
  78. NS_ENSURE_ARG(aCodebase);
  79. mInitialized = true;
  80. mCodebase = NS_TryToMakeImmutable(aCodebase);
  81. mCodebaseImmutable = URIIsImmutable(mCodebase);
  82. mOriginAttributes = aOriginAttributes;
  83. return NS_OK;
  84. }
  85. nsresult
  86. nsPrincipal::GetScriptLocation(nsACString &aStr)
  87. {
  88. return mCodebase->GetSpec(aStr);
  89. }
  90. /* static */ nsresult
  91. nsPrincipal::GetOriginForURI(nsIURI* aURI, nsACString& aOrigin)
  92. {
  93. if (!aURI) {
  94. return NS_ERROR_FAILURE;
  95. }
  96. nsCOMPtr<nsIURI> origin = NS_GetInnermostURI(aURI);
  97. if (!origin) {
  98. return NS_ERROR_FAILURE;
  99. }
  100. nsresult rv;
  101. // NB: This is only compiled for Thunderbird/Suite.
  102. #if IS_ORIGIN_IS_FULL_SPEC_DEFINED
  103. bool fullSpec = false;
  104. rv = NS_URIChainHasFlags(origin, nsIProtocolHandler::ORIGIN_IS_FULL_SPEC, &fullSpec);
  105. NS_ENSURE_SUCCESS(rv, rv);
  106. if (fullSpec) {
  107. return origin->GetAsciiSpec(aOrigin);
  108. }
  109. #endif
  110. nsAutoCString hostPort;
  111. // chrome: URLs don't have a meaningful origin, so make
  112. // sure we just get the full spec for them.
  113. // XXX this should be removed in favor of the solution in
  114. // bug 160042.
  115. bool isChrome;
  116. rv = origin->SchemeIs("chrome", &isChrome);
  117. if (NS_SUCCEEDED(rv) && !isChrome) {
  118. rv = origin->GetAsciiHostPort(hostPort);
  119. // Some implementations return an empty string, treat it as no support
  120. // for asciiHost by that implementation.
  121. if (hostPort.IsEmpty()) {
  122. rv = NS_ERROR_FAILURE;
  123. }
  124. }
  125. // We want the invariant that prinA.origin == prinB.origin i.f.f.
  126. // prinA.equals(prinB). However, this requires that we impose certain constraints
  127. // on the behavior and origin semantics of principals, and in particular, forbid
  128. // creating origin strings for principals whose equality constraints are not
  129. // expressible as strings (i.e. object equality). Moreover, we want to forbid URIs
  130. // containing the magic "^" we use as a separating character for origin
  131. // attributes.
  132. //
  133. // These constraints can generally be achieved by restricting .origin to
  134. // nsIStandardURL-based URIs, but there are a few other URI schemes that we need
  135. // to handle.
  136. bool isBehaved;
  137. if ((NS_SUCCEEDED(origin->SchemeIs("about", &isBehaved)) && isBehaved) ||
  138. (NS_SUCCEEDED(origin->SchemeIs("moz-safe-about", &isBehaved)) && isBehaved) ||
  139. (NS_SUCCEEDED(origin->SchemeIs("indexeddb", &isBehaved)) && isBehaved)) {
  140. rv = origin->GetAsciiSpec(aOrigin);
  141. NS_ENSURE_SUCCESS(rv, rv);
  142. // Remove query or ref part from about: origin
  143. int32_t pos = aOrigin.FindChar('?');
  144. int32_t hashPos = aOrigin.FindChar('#');
  145. if (hashPos != kNotFound && (pos == kNotFound || hashPos < pos)) {
  146. pos = hashPos;
  147. }
  148. if (pos != kNotFound) {
  149. aOrigin.Truncate(pos);
  150. }
  151. // These URIs could technically contain a '^', but they never should.
  152. if (NS_WARN_IF(aOrigin.FindChar('^', 0) != -1)) {
  153. aOrigin.Truncate();
  154. return NS_ERROR_FAILURE;
  155. }
  156. return NS_OK;
  157. }
  158. if (NS_SUCCEEDED(rv) && !isChrome) {
  159. rv = origin->GetScheme(aOrigin);
  160. NS_ENSURE_SUCCESS(rv, rv);
  161. aOrigin.AppendLiteral("://");
  162. aOrigin.Append(hostPort);
  163. }
  164. else {
  165. // If we reached this branch, we can only create an origin if we have a nsIStandardURL.
  166. // So, we query to a nsIStandardURL, and fail if we aren't an instance of an nsIStandardURL
  167. // nsIStandardURLs have the good property of escaping the '^' character in their specs,
  168. // which means that we can be sure that the caret character (which is reserved for delimiting
  169. // the end of the spec, and the beginning of the origin attributes) is not present in the
  170. // origin string
  171. nsCOMPtr<nsIStandardURL> standardURL = do_QueryInterface(origin);
  172. NS_ENSURE_TRUE(standardURL, NS_ERROR_FAILURE);
  173. rv = origin->GetAsciiSpec(aOrigin);
  174. NS_ENSURE_SUCCESS(rv, rv);
  175. // The origin, when taken from the spec, should not contain the ref part of
  176. // the URL.
  177. int32_t pos = aOrigin.FindChar('?');
  178. int32_t hashPos = aOrigin.FindChar('#');
  179. if (hashPos != kNotFound && (pos == kNotFound || hashPos < pos)) {
  180. pos = hashPos;
  181. }
  182. if (pos != kNotFound) {
  183. aOrigin.Truncate(pos);
  184. }
  185. }
  186. return NS_OK;
  187. }
  188. nsresult
  189. nsPrincipal::GetOriginInternal(nsACString& aOrigin)
  190. {
  191. return GetOriginForURI(mCodebase, aOrigin);
  192. }
  193. bool
  194. nsPrincipal::SubsumesInternal(nsIPrincipal* aOther,
  195. BasePrincipal::DocumentDomainConsideration aConsideration)
  196. {
  197. MOZ_ASSERT(aOther);
  198. // For nsPrincipal, Subsumes is equivalent to Equals.
  199. if (aOther == this) {
  200. return true;
  201. }
  202. // If either the subject or the object has changed its principal by
  203. // explicitly setting document.domain then the other must also have
  204. // done so in order to be considered the same origin. This prevents
  205. // DNS spoofing based on document.domain (154930)
  206. nsresult rv;
  207. if (aConsideration == ConsiderDocumentDomain) {
  208. // Get .domain on each principal.
  209. nsCOMPtr<nsIURI> thisDomain, otherDomain;
  210. GetDomain(getter_AddRefs(thisDomain));
  211. aOther->GetDomain(getter_AddRefs(otherDomain));
  212. // If either has .domain set, we have equality i.f.f. the domains match.
  213. // Otherwise, we fall through to the non-document-domain-considering case.
  214. if (thisDomain || otherDomain) {
  215. return nsScriptSecurityManager::SecurityCompareURIs(thisDomain, otherDomain);
  216. }
  217. }
  218. nsCOMPtr<nsIURI> otherURI;
  219. rv = aOther->GetURI(getter_AddRefs(otherURI));
  220. NS_ENSURE_SUCCESS(rv, false);
  221. // Compare codebases.
  222. return nsScriptSecurityManager::SecurityCompareURIs(mCodebase, otherURI);
  223. }
  224. NS_IMETHODIMP
  225. nsPrincipal::GetURI(nsIURI** aURI)
  226. {
  227. if (mCodebaseImmutable) {
  228. NS_ADDREF(*aURI = mCodebase);
  229. return NS_OK;
  230. }
  231. if (!mCodebase) {
  232. *aURI = nullptr;
  233. return NS_OK;
  234. }
  235. return NS_EnsureSafeToReturn(mCodebase, aURI);
  236. }
  237. bool
  238. nsPrincipal::MayLoadInternal(nsIURI* aURI)
  239. {
  240. // See if aURI is something like a Blob URI that is actually associated with
  241. // a principal.
  242. nsCOMPtr<nsIURIWithPrincipal> uriWithPrin = do_QueryInterface(aURI);
  243. nsCOMPtr<nsIPrincipal> uriPrin;
  244. if (uriWithPrin) {
  245. uriWithPrin->GetPrincipal(getter_AddRefs(uriPrin));
  246. }
  247. if (uriPrin) {
  248. return nsIPrincipal::Subsumes(uriPrin);
  249. }
  250. // If this principal is associated with an addon, check whether that addon
  251. // has been given permission to load from this domain.
  252. if (AddonAllowsLoad(aURI)) {
  253. return true;
  254. }
  255. if (nsScriptSecurityManager::SecurityCompareURIs(mCodebase, aURI)) {
  256. return true;
  257. }
  258. // If strict file origin policy is in effect, local files will always fail
  259. // SecurityCompareURIs unless they are identical. Explicitly check file origin
  260. // policy, in that case.
  261. if (nsScriptSecurityManager::GetStrictFileOriginPolicy() &&
  262. NS_URIIsLocalFile(aURI) &&
  263. NS_RelaxStrictFileOriginPolicy(aURI, mCodebase)) {
  264. return true;
  265. }
  266. return false;
  267. }
  268. void
  269. nsPrincipal::SetURI(nsIURI* aURI)
  270. {
  271. mCodebase = NS_TryToMakeImmutable(aURI);
  272. mCodebaseImmutable = URIIsImmutable(mCodebase);
  273. }
  274. NS_IMETHODIMP
  275. nsPrincipal::GetHashValue(uint32_t* aValue)
  276. {
  277. NS_PRECONDITION(mCodebase, "Need a codebase");
  278. *aValue = nsScriptSecurityManager::HashPrincipalByOrigin(this);
  279. return NS_OK;
  280. }
  281. NS_IMETHODIMP
  282. nsPrincipal::GetDomain(nsIURI** aDomain)
  283. {
  284. if (!mDomain) {
  285. *aDomain = nullptr;
  286. return NS_OK;
  287. }
  288. if (mDomainImmutable) {
  289. NS_ADDREF(*aDomain = mDomain);
  290. return NS_OK;
  291. }
  292. return NS_EnsureSafeToReturn(mDomain, aDomain);
  293. }
  294. NS_IMETHODIMP
  295. nsPrincipal::SetDomain(nsIURI* aDomain)
  296. {
  297. mDomain = NS_TryToMakeImmutable(aDomain);
  298. mDomainImmutable = URIIsImmutable(mDomain);
  299. // Recompute all wrappers between compartments using this principal and other
  300. // non-chrome compartments.
  301. AutoSafeJSContext cx;
  302. JSPrincipals *principals = nsJSPrincipals::get(static_cast<nsIPrincipal*>(this));
  303. bool success = js::RecomputeWrappers(cx, js::ContentCompartmentsOnly(),
  304. js::CompartmentsWithPrincipals(principals));
  305. NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
  306. success = js::RecomputeWrappers(cx, js::CompartmentsWithPrincipals(principals),
  307. js::ContentCompartmentsOnly());
  308. NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
  309. return NS_OK;
  310. }
  311. NS_IMETHODIMP
  312. nsPrincipal::GetBaseDomain(nsACString& aBaseDomain)
  313. {
  314. // For a file URI, we return the file path.
  315. if (NS_URIIsLocalFile(mCodebase)) {
  316. nsCOMPtr<nsIURL> url = do_QueryInterface(mCodebase);
  317. if (url) {
  318. return url->GetFilePath(aBaseDomain);
  319. }
  320. }
  321. bool hasNoRelativeFlag;
  322. nsresult rv = NS_URIChainHasFlags(mCodebase,
  323. nsIProtocolHandler::URI_NORELATIVE,
  324. &hasNoRelativeFlag);
  325. if (NS_WARN_IF(NS_FAILED(rv))) {
  326. return rv;
  327. }
  328. if (hasNoRelativeFlag) {
  329. return mCodebase->GetSpec(aBaseDomain);
  330. }
  331. // For everything else, we ask the TLD service via
  332. // the ThirdPartyUtil.
  333. nsCOMPtr<mozIThirdPartyUtil> thirdPartyUtil =
  334. do_GetService(THIRDPARTYUTIL_CONTRACTID);
  335. if (thirdPartyUtil) {
  336. return thirdPartyUtil->GetBaseDomain(mCodebase, aBaseDomain);
  337. }
  338. return NS_OK;
  339. }
  340. NS_IMETHODIMP
  341. nsPrincipal::Read(nsIObjectInputStream* aStream)
  342. {
  343. nsCOMPtr<nsISupports> supports;
  344. nsCOMPtr<nsIURI> codebase;
  345. nsresult rv = NS_ReadOptionalObject(aStream, true, getter_AddRefs(supports));
  346. if (NS_FAILED(rv)) {
  347. return rv;
  348. }
  349. codebase = do_QueryInterface(supports);
  350. nsCOMPtr<nsIURI> domain;
  351. rv = NS_ReadOptionalObject(aStream, true, getter_AddRefs(supports));
  352. if (NS_FAILED(rv)) {
  353. return rv;
  354. }
  355. domain = do_QueryInterface(supports);
  356. nsAutoCString suffix;
  357. rv = aStream->ReadCString(suffix);
  358. NS_ENSURE_SUCCESS(rv, rv);
  359. PrincipalOriginAttributes attrs;
  360. bool ok = attrs.PopulateFromSuffix(suffix);
  361. NS_ENSURE_TRUE(ok, NS_ERROR_FAILURE);
  362. rv = NS_ReadOptionalObject(aStream, true, getter_AddRefs(supports));
  363. NS_ENSURE_SUCCESS(rv, rv);
  364. rv = Init(codebase, attrs);
  365. NS_ENSURE_SUCCESS(rv, rv);
  366. mCSP = do_QueryInterface(supports, &rv);
  367. // make sure setRequestContext is called after Init(),
  368. // to make sure the principals URI been initalized.
  369. if (mCSP) {
  370. mCSP->SetRequestContext(nullptr, this);
  371. }
  372. SetDomain(domain);
  373. return NS_OK;
  374. }
  375. NS_IMETHODIMP
  376. nsPrincipal::Write(nsIObjectOutputStream* aStream)
  377. {
  378. NS_ENSURE_STATE(mCodebase);
  379. nsresult rv = NS_WriteOptionalCompoundObject(aStream, mCodebase, NS_GET_IID(nsIURI),
  380. true);
  381. if (NS_FAILED(rv)) {
  382. return rv;
  383. }
  384. rv = NS_WriteOptionalCompoundObject(aStream, mDomain, NS_GET_IID(nsIURI),
  385. true);
  386. if (NS_FAILED(rv)) {
  387. return rv;
  388. }
  389. nsAutoCString suffix;
  390. OriginAttributesRef().CreateSuffix(suffix);
  391. rv = aStream->WriteStringZ(suffix.get());
  392. NS_ENSURE_SUCCESS(rv, rv);
  393. rv = NS_WriteOptionalCompoundObject(aStream, mCSP,
  394. NS_GET_IID(nsIContentSecurityPolicy),
  395. true);
  396. if (NS_FAILED(rv)) {
  397. return rv;
  398. }
  399. // mCodebaseImmutable and mDomainImmutable will be recomputed based
  400. // on the deserialized URIs in Read().
  401. return NS_OK;
  402. }
  403. // Helper-function to indicate whether the CSS Unprefixing Service
  404. // whitelist should include dummy domains that are only intended for
  405. // use in testing. (Controlled by a pref.)
  406. static inline bool
  407. IsWhitelistingTestDomains()
  408. {
  409. return gIsWhitelistingTestDomains;
  410. }
  411. // Checks if the given URI's host is on our "full domain" whitelist
  412. // (i.e. if it's an exact match against a domain that needs unprefixing)
  413. static bool
  414. IsOnFullDomainWhitelist(nsIURI* aURI)
  415. {
  416. nsAutoCString hostStr;
  417. nsresult rv = aURI->GetHost(hostStr);
  418. NS_ENSURE_SUCCESS(rv, false);
  419. // NOTE: This static whitelist is expected to be short. If that changes,
  420. // we should consider a different representation; e.g. hash-set, prefix tree.
  421. static const nsLiteralCString sFullDomainsOnWhitelist[] = {
  422. // 0th entry only active when testing:
  423. NS_LITERAL_CSTRING("test1.example.org"),
  424. NS_LITERAL_CSTRING("map.baidu.com"),
  425. NS_LITERAL_CSTRING("3g.163.com"),
  426. NS_LITERAL_CSTRING("3glogo.gtimg.com"), // for 3g.163.com
  427. NS_LITERAL_CSTRING("info.3g.qq.com"), // for 3g.qq.com
  428. NS_LITERAL_CSTRING("3gimg.qq.com"), // for 3g.qq.com
  429. NS_LITERAL_CSTRING("img.m.baidu.com"), // for [shucheng|ks].baidu.com
  430. NS_LITERAL_CSTRING("m.mogujie.com"),
  431. NS_LITERAL_CSTRING("touch.qunar.com"),
  432. NS_LITERAL_CSTRING("mjs.sinaimg.cn"), // for sina.cn
  433. NS_LITERAL_CSTRING("static.qiyi.com"), // for m.iqiyi.com
  434. NS_LITERAL_CSTRING("cdn.kuaidi100.com"), // for m.kuaidi100.com
  435. NS_LITERAL_CSTRING("m.pc6.com"),
  436. NS_LITERAL_CSTRING("m.haosou.com"),
  437. NS_LITERAL_CSTRING("m.mi.com"),
  438. NS_LITERAL_CSTRING("wappass.baidu.com"),
  439. NS_LITERAL_CSTRING("m.video.baidu.com"),
  440. NS_LITERAL_CSTRING("m.video.baidu.com"),
  441. NS_LITERAL_CSTRING("imgcache.gtimg.cn"), // for m.v.qq.com
  442. NS_LITERAL_CSTRING("s.tabelog.jp"),
  443. NS_LITERAL_CSTRING("s.yimg.jp"), // for s.tabelog.jp
  444. NS_LITERAL_CSTRING("i.yimg.jp"), // for *.yahoo.co.jp
  445. NS_LITERAL_CSTRING("ai.yimg.jp"), // for *.yahoo.co.jp
  446. NS_LITERAL_CSTRING("m.finance.yahoo.co.jp"),
  447. NS_LITERAL_CSTRING("daily.c.yimg.jp"), // for sp.daily.co.jp
  448. NS_LITERAL_CSTRING("stat100.ameba.jp"), // for ameblo.jp
  449. NS_LITERAL_CSTRING("user.ameba.jp"), // for ameblo.jp
  450. NS_LITERAL_CSTRING("www.goo.ne.jp"),
  451. NS_LITERAL_CSTRING("x.gnst.jp"), // for mobile.gnavi.co.jp
  452. NS_LITERAL_CSTRING("c.x.gnst.jp"), // for mobile.gnavi.co.jp
  453. NS_LITERAL_CSTRING("www.smbc-card.com"),
  454. NS_LITERAL_CSTRING("static.card.jp.rakuten-static.com"), // for rakuten-card.co.jp
  455. NS_LITERAL_CSTRING("img.travel.rakuten.co.jp"), // for travel.rakuten.co.jp
  456. NS_LITERAL_CSTRING("img.mixi.net"), // for mixi.jp
  457. NS_LITERAL_CSTRING("girlschannel.net"),
  458. NS_LITERAL_CSTRING("www.fancl.co.jp"),
  459. NS_LITERAL_CSTRING("s.cosme.net"),
  460. NS_LITERAL_CSTRING("www.sapporobeer.jp"),
  461. NS_LITERAL_CSTRING("www.mapion.co.jp"),
  462. NS_LITERAL_CSTRING("touch.navitime.co.jp"),
  463. NS_LITERAL_CSTRING("sp.mbga.jp"),
  464. NS_LITERAL_CSTRING("ava-a.sp.mbga.jp"), // for sp.mbga.jp
  465. NS_LITERAL_CSTRING("www.ntv.co.jp"),
  466. NS_LITERAL_CSTRING("mobile.suntory.co.jp"), // for suntory.jp
  467. NS_LITERAL_CSTRING("www.aeonsquare.net"),
  468. NS_LITERAL_CSTRING("mw.nikkei.com"),
  469. NS_LITERAL_CSTRING("www.nhk.or.jp"),
  470. NS_LITERAL_CSTRING("www.tokyo-sports.co.jp"),
  471. NS_LITERAL_CSTRING("www.bellemaison.jp"),
  472. NS_LITERAL_CSTRING("www.kuronekoyamato.co.jp"),
  473. NS_LITERAL_CSTRING("formassist.jp"), // for orico.jp
  474. NS_LITERAL_CSTRING("sp.m.reuters.co.jp"),
  475. NS_LITERAL_CSTRING("www.atre.co.jp"),
  476. NS_LITERAL_CSTRING("www.jtb.co.jp"),
  477. NS_LITERAL_CSTRING("www.sharp.co.jp"),
  478. NS_LITERAL_CSTRING("www.biccamera.com"),
  479. NS_LITERAL_CSTRING("weathernews.jp"),
  480. NS_LITERAL_CSTRING("cache.ymail.jp"), // for www.yamada-denkiweb.com
  481. };
  482. static const size_t sNumFullDomainsOnWhitelist =
  483. MOZ_ARRAY_LENGTH(sFullDomainsOnWhitelist);
  484. // Skip 0th (dummy) entry in whitelist, unless a pref is enabled.
  485. const size_t firstWhitelistIdx = IsWhitelistingTestDomains() ? 0 : 1;
  486. for (size_t i = firstWhitelistIdx; i < sNumFullDomainsOnWhitelist; ++i) {
  487. if (hostStr == sFullDomainsOnWhitelist[i]) {
  488. return true;
  489. }
  490. }
  491. return false;
  492. }
  493. // Checks if the given URI's host is on our "base domain" whitelist
  494. // (i.e. if it's a subdomain of some host that we've whitelisted as needing
  495. // unprefixing for all its subdomains)
  496. static bool
  497. IsOnBaseDomainWhitelist(nsIURI* aURI)
  498. {
  499. static const nsLiteralCString sBaseDomainsOnWhitelist[] = {
  500. // 0th entry only active when testing:
  501. NS_LITERAL_CSTRING("test2.example.org"),
  502. NS_LITERAL_CSTRING("tbcdn.cn"), // for m.taobao.com
  503. NS_LITERAL_CSTRING("alicdn.com"), // for m.taobao.com
  504. NS_LITERAL_CSTRING("dpfile.com"), // for m.dianping.com
  505. NS_LITERAL_CSTRING("hao123img.com"), // for hao123.com
  506. NS_LITERAL_CSTRING("tabelog.k-img.com"), // for s.tabelog.com
  507. NS_LITERAL_CSTRING("tsite.jp"), // for *.tsite.jp
  508. };
  509. static const size_t sNumBaseDomainsOnWhitelist =
  510. MOZ_ARRAY_LENGTH(sBaseDomainsOnWhitelist);
  511. nsCOMPtr<nsIEffectiveTLDService> tldService =
  512. do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID);
  513. if (tldService) {
  514. // Skip 0th test-entry in whitelist, unless the testing pref is enabled.
  515. const size_t firstWhitelistIdx = IsWhitelistingTestDomains() ? 0 : 1;
  516. // Right now, the test base-domain "test2.example.org" is the only entry in
  517. // its whitelist with a nonzero "depth". So we'll only bother going beyond
  518. // 0 depth (to 1) if that entry is enabled. (No point in slowing down the
  519. // normal codepath, for the benefit of a disabled test domain.) If we add a
  520. // "real" base-domain with a depth of >= 1 to our whitelist, we can get rid
  521. // of this conditional & just make this a static variable.
  522. const uint32_t maxSubdomainDepth = IsWhitelistingTestDomains() ? 1 : 0;
  523. for (uint32_t subdomainDepth = 0;
  524. subdomainDepth <= maxSubdomainDepth; ++subdomainDepth) {
  525. // Get the base domain (to depth |subdomainDepth|) from passed-in URI:
  526. nsAutoCString baseDomainStr;
  527. nsresult rv = tldService->GetBaseDomain(aURI, subdomainDepth,
  528. baseDomainStr);
  529. if (NS_FAILED(rv)) {
  530. // aURI doesn't have |subdomainDepth| levels of subdomains. If we got
  531. // here without a match yet, then aURI is not on our whitelist.
  532. return false;
  533. }
  534. // Compare the base domain against each entry in our whitelist:
  535. for (size_t i = firstWhitelistIdx; i < sNumBaseDomainsOnWhitelist; ++i) {
  536. if (baseDomainStr == sBaseDomainsOnWhitelist[i]) {
  537. return true;
  538. }
  539. }
  540. }
  541. }
  542. return false;
  543. }
  544. // The actual (non-cached) implementation of IsOnCSSUnprefixingWhitelist():
  545. static bool
  546. IsOnCSSUnprefixingWhitelistImpl(nsIURI* aURI)
  547. {
  548. // Check scheme, so we can drop any non-HTTP/HTTPS URIs right away
  549. nsAutoCString schemeStr;
  550. nsresult rv = aURI->GetScheme(schemeStr);
  551. NS_ENSURE_SUCCESS(rv, false);
  552. // Only proceed if scheme is "http" or "https"
  553. if (!(StringBeginsWith(schemeStr, NS_LITERAL_CSTRING("http")) &&
  554. (schemeStr.Length() == 4 ||
  555. (schemeStr.Length() == 5 && schemeStr[4] == 's')))) {
  556. return false;
  557. }
  558. return (IsOnFullDomainWhitelist(aURI) ||
  559. IsOnBaseDomainWhitelist(aURI));
  560. }
  561. bool
  562. nsPrincipal::IsOnCSSUnprefixingWhitelist()
  563. {
  564. if (mIsOnCSSUnprefixingWhitelist.isNothing()) {
  565. // Value not cached -- perform our lazy whitelist-check.
  566. // (NOTE: If our URI is mutable, we just assume it's not on the whitelist,
  567. // since our caching strategy won't work. This isn't expected to be common.)
  568. mIsOnCSSUnprefixingWhitelist.emplace(
  569. mCodebaseImmutable &&
  570. IsOnCSSUnprefixingWhitelistImpl(mCodebase));
  571. }
  572. return *mIsOnCSSUnprefixingWhitelist;
  573. }
  574. /************************************************************************************************************************/
  575. NS_IMPL_CLASSINFO(nsExpandedPrincipal, nullptr, nsIClassInfo::MAIN_THREAD_ONLY,
  576. NS_EXPANDEDPRINCIPAL_CID)
  577. NS_IMPL_QUERY_INTERFACE_CI(nsExpandedPrincipal,
  578. nsIPrincipal,
  579. nsIExpandedPrincipal)
  580. NS_IMPL_CI_INTERFACE_GETTER(nsExpandedPrincipal,
  581. nsIPrincipal,
  582. nsIExpandedPrincipal)
  583. struct OriginComparator
  584. {
  585. bool LessThan(nsIPrincipal* a, nsIPrincipal* b) const
  586. {
  587. nsAutoCString originA;
  588. nsresult rv = a->GetOrigin(originA);
  589. NS_ENSURE_SUCCESS(rv, false);
  590. nsAutoCString originB;
  591. rv = b->GetOrigin(originB);
  592. NS_ENSURE_SUCCESS(rv, false);
  593. return originA < originB;
  594. }
  595. bool Equals(nsIPrincipal* a, nsIPrincipal* b) const
  596. {
  597. nsAutoCString originA;
  598. nsresult rv = a->GetOrigin(originA);
  599. NS_ENSURE_SUCCESS(rv, false);
  600. nsAutoCString originB;
  601. rv = b->GetOrigin(originB);
  602. NS_ENSURE_SUCCESS(rv, false);
  603. return a == b;
  604. }
  605. };
  606. nsExpandedPrincipal::nsExpandedPrincipal(nsTArray<nsCOMPtr<nsIPrincipal>> &aWhiteList,
  607. const PrincipalOriginAttributes& aAttrs)
  608. {
  609. // We force the principals to be sorted by origin so that nsExpandedPrincipal
  610. // origins can have a canonical form.
  611. OriginComparator c;
  612. for (size_t i = 0; i < aWhiteList.Length(); ++i) {
  613. mPrincipals.InsertElementSorted(aWhiteList[i], c);
  614. }
  615. mOriginAttributes = aAttrs;
  616. }
  617. nsExpandedPrincipal::~nsExpandedPrincipal()
  618. { }
  619. NS_IMETHODIMP
  620. nsExpandedPrincipal::GetDomain(nsIURI** aDomain)
  621. {
  622. *aDomain = nullptr;
  623. return NS_OK;
  624. }
  625. NS_IMETHODIMP
  626. nsExpandedPrincipal::SetDomain(nsIURI* aDomain)
  627. {
  628. return NS_OK;
  629. }
  630. nsresult
  631. nsExpandedPrincipal::GetOriginInternal(nsACString& aOrigin)
  632. {
  633. aOrigin.AssignLiteral("[Expanded Principal [");
  634. for (size_t i = 0; i < mPrincipals.Length(); ++i) {
  635. if (i != 0) {
  636. aOrigin.AppendLiteral(", ");
  637. }
  638. nsAutoCString subOrigin;
  639. nsresult rv = mPrincipals.ElementAt(i)->GetOrigin(subOrigin);
  640. NS_ENSURE_SUCCESS(rv, rv);
  641. aOrigin.Append(subOrigin);
  642. }
  643. aOrigin.Append("]]");
  644. return NS_OK;
  645. }
  646. bool
  647. nsExpandedPrincipal::SubsumesInternal(nsIPrincipal* aOther,
  648. BasePrincipal::DocumentDomainConsideration aConsideration)
  649. {
  650. // If aOther is an ExpandedPrincipal too, we break it down into its component
  651. // nsIPrincipals, and check subsumes on each one.
  652. nsCOMPtr<nsIExpandedPrincipal> expanded = do_QueryInterface(aOther);
  653. if (expanded) {
  654. nsTArray< nsCOMPtr<nsIPrincipal> >* otherList;
  655. expanded->GetWhiteList(&otherList);
  656. for (uint32_t i = 0; i < otherList->Length(); ++i){
  657. // Use SubsumesInternal rather than Subsumes here, since OriginAttribute
  658. // checks are only done between non-expanded sub-principals, and we don't
  659. // need to incur the extra virtual call overhead.
  660. if (!SubsumesInternal((*otherList)[i], aConsideration)) {
  661. return false;
  662. }
  663. }
  664. return true;
  665. }
  666. // We're dealing with a regular principal. One of our principals must subsume
  667. // it.
  668. for (uint32_t i = 0; i < mPrincipals.Length(); ++i) {
  669. if (Cast(mPrincipals[i])->Subsumes(aOther, aConsideration)) {
  670. return true;
  671. }
  672. }
  673. return false;
  674. }
  675. bool
  676. nsExpandedPrincipal::MayLoadInternal(nsIURI* uri)
  677. {
  678. for (uint32_t i = 0; i < mPrincipals.Length(); ++i){
  679. if (BasePrincipal::Cast(mPrincipals[i])->MayLoadInternal(uri)) {
  680. return true;
  681. }
  682. }
  683. return false;
  684. }
  685. NS_IMETHODIMP
  686. nsExpandedPrincipal::GetHashValue(uint32_t* result)
  687. {
  688. MOZ_CRASH("extended principal should never be used as key in a hash map");
  689. }
  690. NS_IMETHODIMP
  691. nsExpandedPrincipal::GetURI(nsIURI** aURI)
  692. {
  693. *aURI = nullptr;
  694. return NS_OK;
  695. }
  696. NS_IMETHODIMP
  697. nsExpandedPrincipal::GetWhiteList(nsTArray<nsCOMPtr<nsIPrincipal> >** aWhiteList)
  698. {
  699. *aWhiteList = &mPrincipals;
  700. return NS_OK;
  701. }
  702. NS_IMETHODIMP
  703. nsExpandedPrincipal::GetBaseDomain(nsACString& aBaseDomain)
  704. {
  705. return NS_ERROR_NOT_AVAILABLE;
  706. }
  707. bool
  708. nsExpandedPrincipal::AddonHasPermission(const nsAString& aPerm)
  709. {
  710. for (size_t i = 0; i < mPrincipals.Length(); ++i) {
  711. if (BasePrincipal::Cast(mPrincipals[i])->AddonHasPermission(aPerm)) {
  712. return true;
  713. }
  714. }
  715. return false;
  716. }
  717. bool
  718. nsExpandedPrincipal::IsOnCSSUnprefixingWhitelist()
  719. {
  720. // CSS Unprefixing Whitelist is a per-origin thing; doesn't really make sense
  721. // for an expanded principal. (And probably shouldn't be needed.)
  722. return false;
  723. }
  724. nsresult
  725. nsExpandedPrincipal::GetScriptLocation(nsACString& aStr)
  726. {
  727. aStr.Assign("[Expanded Principal [");
  728. for (size_t i = 0; i < mPrincipals.Length(); ++i) {
  729. if (i != 0) {
  730. aStr.AppendLiteral(", ");
  731. }
  732. nsAutoCString spec;
  733. nsresult rv =
  734. nsJSPrincipals::get(mPrincipals.ElementAt(i))->GetScriptLocation(spec);
  735. NS_ENSURE_SUCCESS(rv, rv);
  736. aStr.Append(spec);
  737. }
  738. aStr.Append("]]");
  739. return NS_OK;
  740. }
  741. //////////////////////////////////////////
  742. // Methods implementing nsISerializable //
  743. //////////////////////////////////////////
  744. NS_IMETHODIMP
  745. nsExpandedPrincipal::Read(nsIObjectInputStream* aStream)
  746. {
  747. return NS_ERROR_NOT_IMPLEMENTED;
  748. }
  749. NS_IMETHODIMP
  750. nsExpandedPrincipal::Write(nsIObjectOutputStream* aStream)
  751. {
  752. return NS_ERROR_NOT_IMPLEMENTED;
  753. }