Attachment #624217: CheckMayLoad changes to allow sandboxed workers to load blob and data URIs v3 for bug #341604

View | Details | Raw Unified | Return to bug 341604
Collapse All | Expand All

(-)a/caps/idl/nsIPrincipal.idl (-3 / +13 lines)
Line     Link Here 
 Lines 47-63   struct JSPrincipals; Link Here 
47
%}
47
%}
48
48
49
interface nsIURI;
49
interface nsIURI;
50
interface nsIContentSecurityPolicy;
50
interface nsIContentSecurityPolicy;
51
51
52
[ptr] native JSContext(JSContext);
52
[ptr] native JSContext(JSContext);
53
[ptr] native JSPrincipals(JSPrincipals);
53
[ptr] native JSPrincipals(JSPrincipals);
54
54
55
[scriptable, uuid(42ba6a38-d619-49ab-8248-3d247e959d5e)]
55
[scriptable, uuid(825ffce8-962d-11e1-aef3-8f2b6188709b)]
56
interface nsIPrincipal : nsISerializable
56
interface nsIPrincipal : nsISerializable
57
{
57
{
58
    /**
58
    /**
59
     * Values of capabilities for each principal. Order is
59
     * Values of capabilities for each principal. Order is
60
     * significant: if an operation is performed on a set
60
     * significant: if an operation is performed on a set
61
     * of capabilities, the minimum is computed.
61
     * of capabilities, the minimum is computed.
62
     */
62
     */
63
    const short ENABLE_DENIED                = 1;
63
    const short ENABLE_DENIED                = 1;
 Lines 199-231   interface nsIPrincipal : nsISerializable Link Here 
199
     */
199
     */
200
    boolean subsumesIgnoringDomain(in nsIPrincipal other);
200
    boolean subsumesIgnoringDomain(in nsIPrincipal other);
201
201
202
    /**
202
    /**
203
     * Checks whether this principal is allowed to load the network resource
203
     * Checks whether this principal is allowed to load the network resource
204
     * located at the given URI under the same-origin policy. This means that
204
     * located at the given URI under the same-origin policy. This means that
205
     * codebase principals are only allowed to load resources from the same
205
     * codebase principals are only allowed to load resources from the same
206
     * domain, the system principal is allowed to load anything, and null
206
     * domain, the system principal is allowed to load anything, and null
207
     * principals are not allowed to load anything.
207
     * principals are not allowed to load anything. This is changed slightly
208
     * by the optional flag allowIfInheritsPrincipal (which defaults to false)
209
     * which allows the load of a data: URI (which inherits the principal of
210
     * its loader) or a URI with the same principal as its loader (eg. a 
211
     * Blob URI).
212
     * In these cases, with allowIfInheritsPrincipal set to true, the URI can
213
     * be loaded by a null principal.
208
     *
214
     *
209
     * If the load is allowed this function does nothing. If the load is not
215
     * If the load is allowed this function does nothing. If the load is not
210
     * allowed the function throws NS_ERROR_DOM_BAD_URI.
216
     * allowed the function throws NS_ERROR_DOM_BAD_URI.
211
     *
217
     *
212
     * NOTE: Other policies might override this, such as the Access-Control
218
     * NOTE: Other policies might override this, such as the Access-Control
213
     *       specification.
219
     *       specification.
214
     * NOTE: The 'domain' attribute has no effect on the behaviour of this
220
     * NOTE: The 'domain' attribute has no effect on the behaviour of this
215
     *       function.
221
     *       function.
216
     *
222
     *
217
     *
223
     *
218
     * @param uri    The URI about to be loaded.
224
     * @param uri    The URI about to be loaded.
219
     * @param report If true, will report a warning to the console service
225
     * @param report If true, will report a warning to the console service
220
     *               if the load is not allowed.
226
     *               if the load is not allowed.
227
     * @param allowIfInheritsPrincipal   If true, the load is allowed if the
228
     *                                   loadee inherits the principal of the
229
     *                                   loader.
221
     * @throws NS_ERROR_DOM_BAD_URI if the load is not allowed.
230
     * @throws NS_ERROR_DOM_BAD_URI if the load is not allowed.
222
     */
231
     */
223
    void checkMayLoad(in nsIURI uri, in boolean report);
232
    void checkMayLoad(in nsIURI uri, in boolean report,
233
                      in boolean allowIfInheritsPrincipal);
224
234
225
    /**
235
    /**
226
     * The subject name for the certificate.  This actually identifies the
236
     * The subject name for the certificate.  This actually identifies the
227
     * subject of the certificate.  This may well not be a string that would
237
     * subject of the certificate.  This may well not be a string that would
228
     * mean much to a typical user on its own (e.g. it may have a number of
238
     * mean much to a typical user on its own (e.g. it may have a number of
229
     * different names all concatenated together with some information on what
239
     * different names all concatenated together with some information on what
230
     * they mean in between).
240
     * they mean in between).
231
     *
241
     *
(-)a/caps/include/nsNullPrincipal.h (+1 lines)
Line     Link Here 
 Lines 43-58    Link Here 
43
 */
43
 */
44
44
45
#ifndef nsNullPrincipal_h__
45
#ifndef nsNullPrincipal_h__
46
#define nsNullPrincipal_h__
46
#define nsNullPrincipal_h__
47
47
48
#include "nsIPrincipal.h"
48
#include "nsIPrincipal.h"
49
#include "nsJSPrincipals.h"
49
#include "nsJSPrincipals.h"
50
#include "nsCOMPtr.h"
50
#include "nsCOMPtr.h"
51
#include "nsPrincipal.h"
51
52
52
class nsIURI;
53
class nsIURI;
53
54
54
#define NS_NULLPRINCIPAL_CLASSNAME "nullprincipal"
55
#define NS_NULLPRINCIPAL_CLASSNAME "nullprincipal"
55
#define NS_NULLPRINCIPAL_CID \
56
#define NS_NULLPRINCIPAL_CID \
56
{ 0xdd156d62, 0xd26f, 0x4441, \
57
{ 0xdd156d62, 0xd26f, 0x4441, \
57
 { 0x9c, 0xdb, 0xe8, 0xf0, 0x91, 0x07, 0xc2, 0x73 } }
58
 { 0x9c, 0xdb, 0xe8, 0xf0, 0x91, 0x07, 0xc2, 0x73 } }
58
#define NS_NULLPRINCIPAL_CONTRACTID "@mozilla.org/nullprincipal;1"
59
#define NS_NULLPRINCIPAL_CONTRACTID "@mozilla.org/nullprincipal;1"
(-)a/caps/include/nsPrincipal.h (-1 / +19 lines)
Line     Link Here 
 Lines 41-57    Link Here 
41
#define nsPrincipal_h__
41
#define nsPrincipal_h__
42
42
43
#include "nsAutoPtr.h"
43
#include "nsAutoPtr.h"
44
#include "nsCOMPtr.h"
44
#include "nsCOMPtr.h"
45
#include "nsVoidArray.h"
45
#include "nsVoidArray.h"
46
#include "nsHashtable.h"
46
#include "nsHashtable.h"
47
#include "nsJSPrincipals.h"
47
#include "nsJSPrincipals.h"
48
#include "nsTArray.h"
48
#include "nsTArray.h"
49
#include "nsAutoPtr.h"
49
#include "nsIProtocolHandler.h"
50
#include "nsNetUtil.h"
51
#include "nsScriptSecurityManager.h"
50
52
51
class nsIObjectInputStream;
53
class nsIObjectInputStream;
52
class nsIObjectOutputStream;
54
class nsIObjectOutputStream;
53
55
54
class nsPrincipal : public nsJSPrincipals
56
class nsPrincipal : public nsJSPrincipals
55
{
57
{
56
public:
58
public:
57
  nsPrincipal();
59
  nsPrincipal();
 Lines 97-112   public: Link Here 
97
  void SetURI(nsIURI *aURI);
99
  void SetURI(nsIURI *aURI);
98
  nsresult SetCapability(const char *capability, void **annotation, 
100
  nsresult SetCapability(const char *capability, void **annotation, 
99
                         AnnotationValue value);
101
                         AnnotationValue value);
100
102
101
  static const char sInvalid[];
103
  static const char sInvalid[];
102
104
103
  virtual void GetScriptLocation(nsACString &aStr) MOZ_OVERRIDE;
105
  virtual void GetScriptLocation(nsACString &aStr) MOZ_OVERRIDE;
104
106
107
  static bool IsPrincipalInherited(nsIURI* aURI) {
108
    // return true if the loadee URI has
109
    // the URI_INHERITS_SECURITY_CONTEXT flag set.
110
    bool doesInheritSecurityContext;
111
    nsresult rv =
112
    NS_URIChainHasFlags(aURI,
113
                        nsIProtocolHandler::URI_INHERITS_SECURITY_CONTEXT,
114
                        &doesInheritSecurityContext);
115
116
    if (NS_SUCCEEDED(rv) && doesInheritSecurityContext) {
117
      return true;
118
    }
119
120
    return false;
121
  }
122
105
#ifdef DEBUG
123
#ifdef DEBUG
106
  virtual void dumpImpl() MOZ_OVERRIDE;
124
  virtual void dumpImpl() MOZ_OVERRIDE;
107
#endif 
125
#endif 
108
126
109
protected:
127
protected:
110
  // Formerly an IDL method. Now just a protected helper.
128
  // Formerly an IDL method. Now just a protected helper.
111
  nsresult SetCanEnableCapability(const char *capability, PRInt16 canEnable);
129
  nsresult SetCanEnableCapability(const char *capability, PRInt16 canEnable);
112
130
(-)a/caps/src/nsNullPrincipal.cpp (-1 / +19 lines)
Line     Link Here 
 Lines 319-336   nsNullPrincipal::Subsumes(nsIPrincipal * Link Here 
319
319
320
NS_IMETHODIMP
320
NS_IMETHODIMP
321
nsNullPrincipal::SubsumesIgnoringDomain(nsIPrincipal *aOther, bool *aResult)
321
nsNullPrincipal::SubsumesIgnoringDomain(nsIPrincipal *aOther, bool *aResult)
322
{
322
{
323
  return Subsumes(aOther, aResult);
323
  return Subsumes(aOther, aResult);
324
}
324
}
325
325
326
NS_IMETHODIMP
326
NS_IMETHODIMP
327
nsNullPrincipal::CheckMayLoad(nsIURI* aURI, bool aReport)
327
nsNullPrincipal::CheckMayLoad(nsIURI* aURI, bool aReport, bool aAllowIfInheritsPrincipal)
328
{
328
{
329
  if (aAllowIfInheritsPrincipal) {
330
    if (nsPrincipal::IsPrincipalInherited(aURI)) {
331
      return NS_OK;
332
    }
333
334
    // Also allow the load if the principal of the URI being checked is exactly
335
    // us ie this.
336
    nsCOMPtr<nsIURIWithPrincipal> uriPrinc = do_QueryInterface(aURI);
337
    if (uriPrinc) {
338
      nsCOMPtr<nsIPrincipal> principal;
339
      uriPrinc->GetPrincipal(getter_AddRefs(principal));
340
341
      if (principal && principal == this) {
342
        return NS_OK;
343
      }
344
    }
345
  }
346
329
  if (aReport) {
347
  if (aReport) {
330
    nsScriptSecurityManager::ReportError(
348
    nsScriptSecurityManager::ReportError(
331
      nsnull, NS_LITERAL_STRING("CheckSameOriginError"), mURI, aURI);
349
      nsnull, NS_LITERAL_STRING("CheckSameOriginError"), mURI, aURI);
332
  }
350
  }
333
351
334
  return NS_ERROR_DOM_BAD_URI;
352
  return NS_ERROR_DOM_BAD_URI;
335
}
353
}
336
354
(-)a/caps/src/nsPrincipal.cpp (-1 / +9 lines)
Line     Link Here 
 Lines 387-404   URIIsLocalFile(nsIURI *aURI) Link Here 
387
387
388
  return util && NS_SUCCEEDED(util->ProtocolHasFlags(aURI,
388
  return util && NS_SUCCEEDED(util->ProtocolHasFlags(aURI,
389
                                nsIProtocolHandler::URI_IS_LOCAL_FILE,
389
                                nsIProtocolHandler::URI_IS_LOCAL_FILE,
390
                                &isFile)) &&
390
                                &isFile)) &&
391
         isFile;
391
         isFile;
392
}
392
}
393
393
394
NS_IMETHODIMP
394
NS_IMETHODIMP
395
nsPrincipal::CheckMayLoad(nsIURI* aURI, bool aReport)
395
nsPrincipal::CheckMayLoad(nsIURI* aURI, bool aReport, bool aAllowIfInheritsPrincipal)
396
{
396
{
397
   if (aAllowIfInheritsPrincipal) {
398
    // If the caller specified to allow loads of URIs that inherit
399
    // our principal, allow the load if this URI inherits its principal
400
    if (nsPrincipal::IsPrincipalInherited(aURI)) {
401
      return NS_OK;
402
    }
403
  }
404
397
  if (!nsScriptSecurityManager::SecurityCompareURIs(mCodebase, aURI)) {
405
  if (!nsScriptSecurityManager::SecurityCompareURIs(mCodebase, aURI)) {
398
    if (nsScriptSecurityManager::GetStrictFileOriginPolicy() &&
406
    if (nsScriptSecurityManager::GetStrictFileOriginPolicy() &&
399
        URIIsLocalFile(aURI)) {
407
        URIIsLocalFile(aURI)) {
400
      nsCOMPtr<nsIFileURL> fileURL(do_QueryInterface(aURI));
408
      nsCOMPtr<nsIFileURL> fileURL(do_QueryInterface(aURI));
401
409
402
      if (!URIIsLocalFile(mCodebase)) {
410
      if (!URIIsLocalFile(mCodebase)) {
403
        // If the codebase is not also a file: uri then forget it
411
        // If the codebase is not also a file: uri then forget it
404
        // (don't want resource: principals in a file: doc)
412
        // (don't want resource: principals in a file: doc)
(-)a/caps/src/nsScriptSecurityManager.cpp (-1 / +1 lines)
Line     Link Here 
 Lines 1481-1497   nsScriptSecurityManager::CheckLoadURIWit Link Here 
1481
    // Check for uris that are only loadable by principals that subsume them
1481
    // Check for uris that are only loadable by principals that subsume them
1482
    bool hasFlags;
1482
    bool hasFlags;
1483
    rv = NS_URIChainHasFlags(targetBaseURI,
1483
    rv = NS_URIChainHasFlags(targetBaseURI,
1484
                             nsIProtocolHandler::URI_LOADABLE_BY_SUBSUMERS,
1484
                             nsIProtocolHandler::URI_LOADABLE_BY_SUBSUMERS,
1485
                             &hasFlags);
1485
                             &hasFlags);
1486
    NS_ENSURE_SUCCESS(rv, rv);
1486
    NS_ENSURE_SUCCESS(rv, rv);
1487
1487
1488
    if (hasFlags) {
1488
    if (hasFlags) {
1489
        return aPrincipal->CheckMayLoad(targetBaseURI, true);
1489
        return aPrincipal->CheckMayLoad(targetBaseURI, true, false);
1490
    }
1490
    }
1491
1491
1492
    //-- get the source scheme
1492
    //-- get the source scheme
1493
    nsCAutoString sourceScheme;
1493
    nsCAutoString sourceScheme;
1494
    rv = sourceBaseURI->GetScheme(sourceScheme);
1494
    rv = sourceBaseURI->GetScheme(sourceScheme);
1495
    if (NS_FAILED(rv)) return rv;
1495
    if (NS_FAILED(rv)) return rv;
1496
1496
1497
    if (sourceScheme.LowerCaseEqualsLiteral(NS_NULLPRINCIPAL_SCHEME)) {
1497
    if (sourceScheme.LowerCaseEqualsLiteral(NS_NULLPRINCIPAL_SCHEME)) {
(-)a/caps/src/nsSystemPrincipal.cpp (-1 / +1 lines)
Line     Link Here 
 Lines 141-157   nsSystemPrincipal::Subsumes(nsIPrincipal Link Here 
141
NS_IMETHODIMP
141
NS_IMETHODIMP
142
nsSystemPrincipal::SubsumesIgnoringDomain(nsIPrincipal *other, bool *result)
142
nsSystemPrincipal::SubsumesIgnoringDomain(nsIPrincipal *other, bool *result)
143
{
143
{
144
    *result = true;
144
    *result = true;
145
    return NS_OK;
145
    return NS_OK;
146
}
146
}
147
147
148
NS_IMETHODIMP
148
NS_IMETHODIMP
149
nsSystemPrincipal::CheckMayLoad(nsIURI* uri, bool aReport)
149
nsSystemPrincipal::CheckMayLoad(nsIURI* uri, bool aReport, bool aAllowIfInheritsPrincipal)
150
{
150
{
151
    return NS_OK;
151
    return NS_OK;
152
}
152
}
153
153
154
NS_IMETHODIMP
154
NS_IMETHODIMP
155
nsSystemPrincipal::GetHashValue(PRUint32 *result)
155
nsSystemPrincipal::GetHashValue(PRUint32 *result)
156
{
156
{
157
    *result = NS_PTR_TO_INT32(this);
157
    *result = NS_PTR_TO_INT32(this);
(-)a/content/base/public/nsContentUtils.h (-1 / +1 lines)
Line     Link Here 
 Lines 1709-1725   public: Link Here 
1709
   *
1709
   *
1710
   * @param aScriptContext the script context to get the document for; can be null
1710
   * @param aScriptContext the script context to get the document for; can be null
1711
   *
1711
   *
1712
   * @return the document associated with the script context
1712
   * @return the document associated with the script context
1713
   */
1713
   */
1714
  static already_AddRefed<nsIDocument>
1714
  static already_AddRefed<nsIDocument>
1715
  GetDocumentFromScriptContext(nsIScriptContext *aScriptContext);
1715
  GetDocumentFromScriptContext(nsIScriptContext *aScriptContext);
1716
1716
1717
  static bool CheckMayLoad(nsIPrincipal* aPrincipal, nsIChannel* aChannel);
1717
  static bool CheckMayLoad(nsIPrincipal* aPrincipal, nsIChannel* aChannel, bool aAllowIfInheritsPrincipal);
1718
1718
1719
  /**
1719
  /**
1720
   * The method checks whether the caller can access native anonymous content.
1720
   * The method checks whether the caller can access native anonymous content.
1721
   * If there is no JS in the stack or privileged JS is running, this
1721
   * If there is no JS in the stack or privileged JS is running, this
1722
   * method returns true, otherwise false.
1722
   * method returns true, otherwise false.
1723
   */
1723
   */
1724
  static bool CanAccessNativeAnon();
1724
  static bool CanAccessNativeAnon();
1725
1725
(-)a/content/base/src/nsContentSink.cpp (-1 / +1 lines)
Line     Link Here 
 Lines 1042-1058   nsContentSink::ProcessOfflineManifest(co Link Here 
1042
    nsContentUtils::NewURIWithDocumentCharset(getter_AddRefs(manifestURI),
1042
    nsContentUtils::NewURIWithDocumentCharset(getter_AddRefs(manifestURI),
1043
                                              aManifestSpec, mDocument,
1043
                                              aManifestSpec, mDocument,
1044
                                              mDocumentURI);
1044
                                              mDocumentURI);
1045
    if (!manifestURI) {
1045
    if (!manifestURI) {
1046
      return;
1046
      return;
1047
    }
1047
    }
1048
1048
1049
    // Documents must list a manifest from the same origin
1049
    // Documents must list a manifest from the same origin
1050
    rv = mDocument->NodePrincipal()->CheckMayLoad(manifestURI, true);
1050
    rv = mDocument->NodePrincipal()->CheckMayLoad(manifestURI, true, false);
1051
    if (NS_FAILED(rv)) {
1051
    if (NS_FAILED(rv)) {
1052
      action = CACHE_SELECTION_RESELECT_WITHOUT_MANIFEST;
1052
      action = CACHE_SELECTION_RESELECT_WITHOUT_MANIFEST;
1053
    }
1053
    }
1054
    else {
1054
    else {
1055
      // Only continue if the document has permission to use offline APIs.
1055
      // Only continue if the document has permission to use offline APIs.
1056
      if (!nsContentUtils::OfflineAppAllowed(mDocument->NodePrincipal())) {
1056
      if (!nsContentUtils::OfflineAppAllowed(mDocument->NodePrincipal())) {
1057
        return;
1057
        return;
1058
      }
1058
      }
(-)a/content/base/src/nsContentUtils.cpp (-5 / +5 lines)
Line     Link Here 
 Lines 4497-4513   nsContentUtils::CheckSecurityBeforeLoad( Link Here 
4497
4497
4498
  // Same Origin
4498
  // Same Origin
4499
  if ((aAllowData && SchemeIs(aURIToLoad, "data")) ||
4499
  if ((aAllowData && SchemeIs(aURIToLoad, "data")) ||
4500
      ((aCheckLoadFlags & nsIScriptSecurityManager::ALLOW_CHROME) &&
4500
      ((aCheckLoadFlags & nsIScriptSecurityManager::ALLOW_CHROME) &&
4501
       SchemeIs(aURIToLoad, "chrome"))) {
4501
       SchemeIs(aURIToLoad, "chrome"))) {
4502
    return NS_OK;
4502
    return NS_OK;
4503
  }
4503
  }
4504
4504
4505
  return aLoadingPrincipal->CheckMayLoad(aURIToLoad, true);
4505
  return aLoadingPrincipal->CheckMayLoad(aURIToLoad, true, false);
4506
}
4506
}
4507
4507
4508
bool
4508
bool
4509
nsContentUtils::IsSystemPrincipal(nsIPrincipal* aPrincipal)
4509
nsContentUtils::IsSystemPrincipal(nsIPrincipal* aPrincipal)
4510
{
4510
{
4511
  bool isSystem;
4511
  bool isSystem;
4512
  nsresult rv = sSecurityManager->IsSystemPrincipal(aPrincipal, &isSystem);
4512
  nsresult rv = sSecurityManager->IsSystemPrincipal(aPrincipal, &isSystem);
4513
  return NS_SUCCEEDED(rv) && isSystem;
4513
  return NS_SUCCEEDED(rv) && isSystem;
 Lines 5615-5633   nsContentUtils::CheckSameOrigin(nsIChann Link Here 
5615
5615
5616
  nsCOMPtr<nsIURI> newURI;
5616
  nsCOMPtr<nsIURI> newURI;
5617
  aNewChannel->GetURI(getter_AddRefs(newURI));
5617
  aNewChannel->GetURI(getter_AddRefs(newURI));
5618
  nsCOMPtr<nsIURI> newOriginalURI;
5618
  nsCOMPtr<nsIURI> newOriginalURI;
5619
  aNewChannel->GetOriginalURI(getter_AddRefs(newOriginalURI));
5619
  aNewChannel->GetOriginalURI(getter_AddRefs(newOriginalURI));
5620
5620
5621
  NS_ENSURE_STATE(oldPrincipal && newURI && newOriginalURI);
5621
  NS_ENSURE_STATE(oldPrincipal && newURI && newOriginalURI);
5622
5622
5623
  nsresult rv = oldPrincipal->CheckMayLoad(newURI, false);
5623
  nsresult rv = oldPrincipal->CheckMayLoad(newURI, false, false);
5624
  if (NS_SUCCEEDED(rv) && newOriginalURI != newURI) {
5624
  if (NS_SUCCEEDED(rv) && newOriginalURI != newURI) {
5625
    rv = oldPrincipal->CheckMayLoad(newOriginalURI, false);
5625
    rv = oldPrincipal->CheckMayLoad(newOriginalURI, false, false);
5626
  }
5626
  }
5627
5627
5628
  return rv;
5628
  return rv;
5629
}
5629
}
5630
5630
5631
NS_IMPL_ISUPPORTS2(nsSameOriginChecker,
5631
NS_IMPL_ISUPPORTS2(nsSameOriginChecker,
5632
                   nsIChannelEventSink,
5632
                   nsIChannelEventSink,
5633
                   nsIInterfaceRequestor)
5633
                   nsIInterfaceRequestor)
 Lines 5788-5810   nsContentUtils::GetDocumentFromScriptCon Link Here 
5788
      CallQueryInterface(domdoc, &doc);
5788
      CallQueryInterface(domdoc, &doc);
5789
    }
5789
    }
5790
  }
5790
  }
5791
  return doc;
5791
  return doc;
5792
}
5792
}
5793
5793
5794
/* static */
5794
/* static */
5795
bool
5795
bool
5796
nsContentUtils::CheckMayLoad(nsIPrincipal* aPrincipal, nsIChannel* aChannel)
5796
nsContentUtils::CheckMayLoad(nsIPrincipal* aPrincipal, nsIChannel* aChannel, bool aAllowIfInheritsPrincipal)
5797
{
5797
{
5798
  nsCOMPtr<nsIURI> channelURI;
5798
  nsCOMPtr<nsIURI> channelURI;
5799
  nsresult rv = NS_GetFinalChannelURI(aChannel, getter_AddRefs(channelURI));
5799
  nsresult rv = NS_GetFinalChannelURI(aChannel, getter_AddRefs(channelURI));
5800
  NS_ENSURE_SUCCESS(rv, false);
5800
  NS_ENSURE_SUCCESS(rv, false);
5801
5801
5802
  return NS_SUCCEEDED(aPrincipal->CheckMayLoad(channelURI, false));
5802
  return NS_SUCCEEDED(aPrincipal->CheckMayLoad(channelURI, false, aAllowIfInheritsPrincipal));
5803
}
5803
}
5804
5804
5805
nsContentTypeParser::nsContentTypeParser(const nsAString& aString)
5805
nsContentTypeParser::nsContentTypeParser(const nsAString& aString)
5806
  : mString(aString), mService(nsnull)
5806
  : mString(aString), mService(nsnull)
5807
{
5807
{
5808
  CallGetService("@mozilla.org/network/mime-hdrparam;1", &mService);
5808
  CallGetService("@mozilla.org/network/mime-hdrparam;1", &mService);
5809
}
5809
}
5810
5810
(-)a/content/base/src/nsCrossSiteListenerProxy.cpp (-2 / +2 lines)
Line     Link Here 
 Lines 776-795   nsCORSListenerProxy::UpdateChannel(nsICh Link Here 
776
  if (originalURI != uri) {
776
  if (originalURI != uri) {
777
    rv = nsContentUtils::GetSecurityManager()->
777
    rv = nsContentUtils::GetSecurityManager()->
778
      CheckLoadURIWithPrincipal(mRequestingPrincipal, originalURI,
778
      CheckLoadURIWithPrincipal(mRequestingPrincipal, originalURI,
779
                                nsIScriptSecurityManager::STANDARD);
779
                                nsIScriptSecurityManager::STANDARD);
780
    NS_ENSURE_SUCCESS(rv, rv);
780
    NS_ENSURE_SUCCESS(rv, rv);
781
  }
781
  }
782
782
783
  if (!mHasBeenCrossSite &&
783
  if (!mHasBeenCrossSite &&
784
      NS_SUCCEEDED(mRequestingPrincipal->CheckMayLoad(uri, false)) &&
784
      NS_SUCCEEDED(mRequestingPrincipal->CheckMayLoad(uri, false, false)) &&
785
      (originalURI == uri ||
785
      (originalURI == uri ||
786
       NS_SUCCEEDED(mRequestingPrincipal->CheckMayLoad(originalURI,
786
       NS_SUCCEEDED(mRequestingPrincipal->CheckMayLoad(originalURI,
787
                                                       false)))) {
787
                                                       false, false)))) {
788
    return NS_OK;
788
    return NS_OK;
789
  }
789
  }
790
790
791
  // It's a cross site load
791
  // It's a cross site load
792
  mHasBeenCrossSite = true;
792
  mHasBeenCrossSite = true;
793
793
794
  nsCString userpass;
794
  nsCString userpass;
795
  uri->GetUserPass(userpass);
795
  uri->GetUserPass(userpass);
(-)a/content/base/src/nsDocument.cpp (-10 / +4 lines)
Line     Link Here 
 Lines 1100-1127   nsExternalResourceMap::PendingLoad::Star Link Here 
1100
1100
1101
  nsIPrincipal* requestingPrincipal = aRequestingNode->NodePrincipal();
1101
  nsIPrincipal* requestingPrincipal = aRequestingNode->NodePrincipal();
1102
1102
1103
  nsresult rv = nsContentUtils::GetSecurityManager()->
1103
  nsresult rv = nsContentUtils::GetSecurityManager()->
1104
    CheckLoadURIWithPrincipal(requestingPrincipal, aURI,
1104
    CheckLoadURIWithPrincipal(requestingPrincipal, aURI,
1105
                              nsIScriptSecurityManager::STANDARD);
1105
                              nsIScriptSecurityManager::STANDARD);
1106
  NS_ENSURE_SUCCESS(rv, rv);
1106
  NS_ENSURE_SUCCESS(rv, rv);
1107
  
1107
  
1108
  // Allow data URIs (let them skip the CheckMayLoad call), since we want
1108
  // Allow data URIs and other URI's that inherit their principal by passing
1109
  // true as the 3rd argument of CheckMayLoad, since we want
1109
  // to allow external resources from data URIs regardless of the difference
1110
  // to allow external resources from data URIs regardless of the difference
1110
  // in URI scheme.
1111
  // in URI scheme.
1111
  bool doesInheritSecurityContext;
1112
  rv = requestingPrincipal->CheckMayLoad(aURI, true, true);
1112
  rv =
1113
  NS_ENSURE_SUCCESS(rv, rv);
1113
    NS_URIChainHasFlags(aURI,
1114
                        nsIProtocolHandler::URI_INHERITS_SECURITY_CONTEXT,
1115
                        &doesInheritSecurityContext);
1116
  if (NS_FAILED(rv) || !doesInheritSecurityContext) {
1117
    rv = requestingPrincipal->CheckMayLoad(aURI, true);
1118
    NS_ENSURE_SUCCESS(rv, rv);
1119
  }
1120
1114
1121
  PRInt16 shouldLoad = nsIContentPolicy::ACCEPT;
1115
  PRInt16 shouldLoad = nsIContentPolicy::ACCEPT;
1122
  rv = NS_CheckContentLoadPolicy(nsIContentPolicy::TYPE_OTHER,
1116
  rv = NS_CheckContentLoadPolicy(nsIContentPolicy::TYPE_OTHER,
1123
                                 aURI,
1117
                                 aURI,
1124
                                 requestingPrincipal,
1118
                                 requestingPrincipal,
1125
                                 aRequestingNode,
1119
                                 aRequestingNode,
1126
                                 EmptyCString(), //mime guess
1120
                                 EmptyCString(), //mime guess
1127
                                 nsnull,         //extra
1121
                                 nsnull,         //extra
(-)a/content/base/src/nsXMLHttpRequest.cpp (-12 / +3 lines)
Line     Link Here 
 Lines 1747-1774   nsXMLHttpRequest::IsSystemXHR() Link Here 
1747
nsresult
1747
nsresult
1748
nsXMLHttpRequest::CheckChannelForCrossSiteRequest(nsIChannel* aChannel)
1748
nsXMLHttpRequest::CheckChannelForCrossSiteRequest(nsIChannel* aChannel)
1749
{
1749
{
1750
  // First check if cross-site requests are enabled...
1750
  // First check if cross-site requests are enabled...
1751
  if (IsSystemXHR()) {
1751
  if (IsSystemXHR()) {
1752
    return NS_OK;
1752
    return NS_OK;
1753
  }
1753
  }
1754
1754
1755
  // ...or if this is a same-origin request.
1755
  // If this is a same-origin request or the channel's URI inherits
1756
  if (nsContentUtils::CheckMayLoad(mPrincipal, aChannel)) {
1756
  // its principal, it's allowed.
1757
    return NS_OK;
1757
  if (nsContentUtils::CheckMayLoad(mPrincipal, aChannel, true)) {
1758
  }
1759
1760
  // exempt data URIs from the same origin check.
1761
  nsCOMPtr<nsIURI> channelURI;
1762
  bool dataScheme = false;
1763
  if (NS_SUCCEEDED(NS_GetFinalChannelURI(aChannel,
1764
                                         getter_AddRefs(channelURI))) &&
1765
      NS_SUCCEEDED(channelURI->SchemeIs("data", &dataScheme)) &&
1766
      dataScheme) {
1767
    return NS_OK;
1758
    return NS_OK;
1768
  }
1759
  }
1769
1760
1770
  // This is a cross-site request
1761
  // This is a cross-site request
1771
  mState |= XML_HTTP_REQUEST_USE_XSITE_AC;
1762
  mState |= XML_HTTP_REQUEST_USE_XSITE_AC;
1772
1763
1773
  // Check if we need to do a preflight request.
1764
  // Check if we need to do a preflight request.
1774
  nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(aChannel);
1765
  nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(aChannel);
(-)a/content/xbl/src/nsXBLService.cpp (-1 / +1 lines)
Line     Link Here 
 Lines 977-993   nsXBLService::LoadBindingDocumentInfo(ns Link Here 
977
    NS_ENSURE_SUCCESS(rv, NS_ERROR_XBL_BLOCKED);
977
    NS_ENSURE_SUCCESS(rv, NS_ERROR_XBL_BLOCKED);
978
978
979
    if (!IsSystemOrChromeURLPrincipal(aOriginPrincipal)) {
979
    if (!IsSystemOrChromeURLPrincipal(aOriginPrincipal)) {
980
      // Also make sure that we're same-origin with the bound document
980
      // Also make sure that we're same-origin with the bound document
981
      // except if the stylesheet has the system principal.
981
      // except if the stylesheet has the system principal.
982
      if (!(gAllowDataURIs && SchemeIs(aBindingURI, "data")) &&
982
      if (!(gAllowDataURIs && SchemeIs(aBindingURI, "data")) &&
983
          !SchemeIs(aBindingURI, "chrome")) {
983
          !SchemeIs(aBindingURI, "chrome")) {
984
        rv = aBoundDocument->NodePrincipal()->CheckMayLoad(aBindingURI,
984
        rv = aBoundDocument->NodePrincipal()->CheckMayLoad(aBindingURI,
985
                                                           true);
985
                                                           true, false);
986
        NS_ENSURE_SUCCESS(rv, NS_ERROR_XBL_BLOCKED);
986
        NS_ENSURE_SUCCESS(rv, NS_ERROR_XBL_BLOCKED);
987
      }
987
      }
988
988
989
      // Finally check if this document is allowed to use XBL at all.
989
      // Finally check if this document is allowed to use XBL at all.
990
      NS_ENSURE_TRUE(aBoundDocument->AllowXULXBL(),
990
      NS_ENSURE_TRUE(aBoundDocument->AllowXULXBL(),
991
                     NS_ERROR_XBL_BLOCKED);
991
                     NS_ERROR_XBL_BLOCKED);
992
    }
992
    }
993
  }
993
  }
(-)a/content/xml/document/src/nsXMLDocument.cpp (-1 / +1 lines)
Line     Link Here 
 Lines 362-378   nsXMLDocument::Load(const nsAString& aUr Link Here 
362
  // that we don't end up in a case where code with elevated privileges is
362
  // that we don't end up in a case where code with elevated privileges is
363
  // calling us and changing the principal of this document.
363
  // calling us and changing the principal of this document.
364
364
365
  // Enforce same-origin even for chrome loaders to avoid someone accidentally
365
  // Enforce same-origin even for chrome loaders to avoid someone accidentally
366
  // using a document that content has a reference to and turn that into a
366
  // using a document that content has a reference to and turn that into a
367
  // chrome document.
367
  // chrome document.
368
  nsCOMPtr<nsIPrincipal> principal = NodePrincipal();
368
  nsCOMPtr<nsIPrincipal> principal = NodePrincipal();
369
  if (!nsContentUtils::IsSystemPrincipal(principal)) {
369
  if (!nsContentUtils::IsSystemPrincipal(principal)) {
370
    rv = principal->CheckMayLoad(uri, false);
370
    rv = principal->CheckMayLoad(uri, false, false);
371
    NS_ENSURE_SUCCESS(rv, rv);
371
    NS_ENSURE_SUCCESS(rv, rv);
372
372
373
    PRInt16 shouldLoad = nsIContentPolicy::ACCEPT;
373
    PRInt16 shouldLoad = nsIContentPolicy::ACCEPT;
374
    rv = NS_CheckContentLoadPolicy(nsIContentPolicy::TYPE_XMLHTTPREQUEST,
374
    rv = NS_CheckContentLoadPolicy(nsIContentPolicy::TYPE_XMLHTTPREQUEST,
375
                                   uri,
375
                                   uri,
376
                                   principal,
376
                                   principal,
377
                                   callingDoc ? callingDoc.get() :
377
                                   callingDoc ? callingDoc.get() :
378
                                     static_cast<nsIDocument*>(this),
378
                                     static_cast<nsIDocument*>(this),
(-)a/content/xul/document/src/nsXULDocument.cpp (-1 / +1 lines)
Line     Link Here 
 Lines 2700-2716   nsXULDocument::LoadOverlayInternal(nsIUR Link Here 
2700
2700
2701
    // Chrome documents are allowed to load overlays from anywhere.
2701
    // Chrome documents are allowed to load overlays from anywhere.
2702
    // In all other cases, the overlay is only allowed to load if
2702
    // In all other cases, the overlay is only allowed to load if
2703
    // the master document and prototype document have the same origin.
2703
    // the master document and prototype document have the same origin.
2704
2704
2705
    bool documentIsChrome = IsChromeURI(mDocumentURI);
2705
    bool documentIsChrome = IsChromeURI(mDocumentURI);
2706
    if (!documentIsChrome) {
2706
    if (!documentIsChrome) {
2707
        // Make sure we're allowed to load this overlay.
2707
        // Make sure we're allowed to load this overlay.
2708
        rv = NodePrincipal()->CheckMayLoad(aURI, true);
2708
        rv = NodePrincipal()->CheckMayLoad(aURI, true, false);
2709
        if (NS_FAILED(rv)) {
2709
        if (NS_FAILED(rv)) {
2710
            *aFailureFromContent = true;
2710
            *aFailureFromContent = true;
2711
            return rv;
2711
            return rv;
2712
        }
2712
        }
2713
    }
2713
    }
2714
2714
2715
    // Look in the prototype cache for the prototype document with
2715
    // Look in the prototype cache for the prototype document with
2716
    // the specified overlay URI. Only use the cache if the containing
2716
    // the specified overlay URI. Only use the cache if the containing
(-)a/content/xul/templates/src/nsXULTemplateBuilder.cpp (-1 / +1 lines)
Line     Link Here 
 Lines 1372-1388   nsXULTemplateBuilder::LoadDataSourceUrls Link Here 
1372
1372
1373
        nsCOMPtr<nsIURI> uri;
1373
        nsCOMPtr<nsIURI> uri;
1374
        rv = NS_NewURI(getter_AddRefs(uri), uriStr);
1374
        rv = NS_NewURI(getter_AddRefs(uri), uriStr);
1375
        if (NS_FAILED(rv) || !uri)
1375
        if (NS_FAILED(rv) || !uri)
1376
            continue; // Necko will barf if our URI is weird
1376
            continue; // Necko will barf if our URI is weird
1377
1377
1378
        // don't add the uri to the list if the document is not allowed to
1378
        // don't add the uri to the list if the document is not allowed to
1379
        // load it
1379
        // load it
1380
        if (!isTrusted && NS_FAILED(docPrincipal->CheckMayLoad(uri, true)))
1380
        if (!isTrusted && NS_FAILED(docPrincipal->CheckMayLoad(uri, true, false)))
1381
          continue;
1381
          continue;
1382
1382
1383
        uriList->AppendElement(uri, false);
1383
        uriList->AppendElement(uri, false);
1384
    }
1384
    }
1385
1385
1386
    nsCOMPtr<nsIDOMNode> rootNode = do_QueryInterface(mRoot);
1386
    nsCOMPtr<nsIDOMNode> rootNode = do_QueryInterface(mRoot);
1387
    rv = mQueryProcessor->GetDatasource(uriList,
1387
    rv = mQueryProcessor->GetDatasource(uriList,
1388
                                        rootNode,
1388
                                        rootNode,
(-)a/docshell/base/nsDocShell.cpp (-1 / +1 lines)
Line     Link Here 
 Lines 9889-9905   nsDocShell::AddState(nsIVariant *aData, Link Here 
9889
9889
9890
            if (!docScriptObj) {
9890
            if (!docScriptObj) {
9891
                return NS_ERROR_DOM_SECURITY_ERR;
9891
                return NS_ERROR_DOM_SECURITY_ERR;
9892
            }
9892
            }
9893
9893
9894
            nsCOMPtr<nsIPrincipal> principal = docScriptObj->GetPrincipal();
9894
            nsCOMPtr<nsIPrincipal> principal = docScriptObj->GetPrincipal();
9895
9895
9896
            if (!principal ||
9896
            if (!principal ||
9897
                NS_FAILED(principal->CheckMayLoad(newURI, true))) {
9897
                NS_FAILED(principal->CheckMayLoad(newURI, true, false))) {
9898
9898
9899
                return NS_ERROR_DOM_SECURITY_ERR;
9899
                return NS_ERROR_DOM_SECURITY_ERR;
9900
            }
9900
            }
9901
        }
9901
        }
9902
9902
9903
        if (mCurrentURI) {
9903
        if (mCurrentURI) {
9904
            mCurrentURI->Equals(newURI, &equalURIs);
9904
            mCurrentURI->Equals(newURI, &equalURIs);
9905
        }
9905
        }
(-)a/dom/workers/ScriptLoader.cpp (-8 / +9 lines)
Line     Link Here 
 Lines 329-349   public: Link Here 
329
      // If this script loader is being used to make a new worker then we need
329
      // If this script loader is being used to make a new worker then we need
330
      // to do a same-origin check. Otherwise we need to clear the load with the
330
      // to do a same-origin check. Otherwise we need to clear the load with the
331
      // security manager.
331
      // security manager.
332
      if (mIsWorkerScript) {
332
      if (mIsWorkerScript) {
333
        nsCString scheme;
333
        nsCString scheme;
334
        rv = uri->GetScheme(scheme);
334
        rv = uri->GetScheme(scheme);
335
        NS_ENSURE_SUCCESS(rv, rv);
335
        NS_ENSURE_SUCCESS(rv, rv);
336
336
337
        // We exempt data URLs from the same origin check.
337
        // We pass true as the 3rd argument to checkMayLoad here.
338
        if (!scheme.EqualsLiteral("data")) {
338
        // This allows workers in sandboxed documents to load data URLs
339
          rv = principal->CheckMayLoad(uri, false);
339
        // (and other URLs that inherit their principal from their
340
          NS_ENSURE_SUCCESS(rv, rv);
340
        // creator.)
341
        }
341
        rv = principal->CheckMayLoad(uri, false, true);
342
        NS_ENSURE_SUCCESS(rv, rv);
342
      }
343
      }
343
      else {
344
      else {
344
        rv = secMan->CheckLoadURIWithPrincipal(principal, uri, 0);
345
        rv = secMan->CheckLoadURIWithPrincipal(principal, uri, 0);
345
        NS_ENSURE_SUCCESS(rv, rv);
346
        NS_ENSURE_SUCCESS(rv, rv);
346
      }
347
      }
347
348
348
      // We need to know which index we're on in OnStreamComplete so we know
349
      // We need to know which index we're on in OnStreamComplete so we know
349
      // where to put the result.
350
      // where to put the result.
 Lines 516-534   public: Link Here 
516
          return NS_ERROR_DOM_BAD_URI;
517
          return NS_ERROR_DOM_BAD_URI;
517
        }
518
        }
518
      }
519
      }
519
      else  {
520
      else  {
520
        nsCString scheme;
521
        nsCString scheme;
521
        rv = finalURI->GetScheme(scheme);
522
        rv = finalURI->GetScheme(scheme);
522
        NS_ENSURE_SUCCESS(rv, rv);
523
        NS_ENSURE_SUCCESS(rv, rv);
523
524
524
        // We exempt data urls again.
525
        // We exempt data urls and other URI's that inherit their
525
        if (!scheme.EqualsLiteral("data") &&
526
        // principal again.
526
            NS_FAILED(loadPrincipal->CheckMayLoad(finalURI, false))) {
527
        if (NS_FAILED(loadPrincipal->CheckMayLoad(finalURI, false, true))) {
527
          return NS_ERROR_DOM_BAD_URI;
528
          return NS_ERROR_DOM_BAD_URI;
528
        }
529
        }
529
      }
530
      }
530
531
531
      mWorkerPrivate->SetPrincipal(channelPrincipal);
532
      mWorkerPrivate->SetPrincipal(channelPrincipal);
532
    }
533
    }
533
534
534
    return NS_OK;
535
    return NS_OK;
(-)a/layout/style/Loader.cpp (-1 / +1 lines)
Line     Link Here 
 Lines 1568-1584   Loader::LoadSheet(SheetLoadData* aLoadDa Link Here 
1568
  if (aLoadData->mLoaderPrincipal) {
1568
  if (aLoadData->mLoaderPrincipal) {
1569
    bool inherit;
1569
    bool inherit;
1570
    rv = NS_URIChainHasFlags(aLoadData->mURI,
1570
    rv = NS_URIChainHasFlags(aLoadData->mURI,
1571
                             nsIProtocolHandler::URI_INHERITS_SECURITY_CONTEXT,
1571
                             nsIProtocolHandler::URI_INHERITS_SECURITY_CONTEXT,
1572
                             &inherit);
1572
                             &inherit);
1573
    if ((NS_SUCCEEDED(rv) && inherit) ||
1573
    if ((NS_SUCCEEDED(rv) && inherit) ||
1574
        (nsContentUtils::URIIsLocalFile(aLoadData->mURI) &&
1574
        (nsContentUtils::URIIsLocalFile(aLoadData->mURI) &&
1575
         NS_SUCCEEDED(aLoadData->mLoaderPrincipal->
1575
         NS_SUCCEEDED(aLoadData->mLoaderPrincipal->
1576
                      CheckMayLoad(aLoadData->mURI, false)))) {
1576
                      CheckMayLoad(aLoadData->mURI, false, false)))) {
1577
      channel->SetOwner(aLoadData->mLoaderPrincipal);
1577
      channel->SetOwner(aLoadData->mLoaderPrincipal);
1578
    }
1578
    }
1579
  }
1579
  }
1580
1580
1581
  // We don't have to hold on to the stream loader.  The ownership
1581
  // We don't have to hold on to the stream loader.  The ownership
1582
  // model is: Necko owns the stream loader, which owns the load data,
1582
  // model is: Necko owns the stream loader, which owns the load data,
1583
  // which owns us
1583
  // which owns us
1584
  nsCOMPtr<nsIUnicharStreamLoader> streamLoader;
1584
  nsCOMPtr<nsIUnicharStreamLoader> streamLoader;
(-)a/rdf/base/src/nsRDFXMLDataSource.cpp (-2 / +2 lines)
Line     Link Here 
 Lines 915-933   RDFXMLDataSourceImpl::AsyncOnChannelRedi Link Here 
915
915
916
    nsCOMPtr<nsIURI> newURI;
916
    nsCOMPtr<nsIURI> newURI;
917
    aNewChannel->GetURI(getter_AddRefs(newURI));
917
    aNewChannel->GetURI(getter_AddRefs(newURI));
918
    nsCOMPtr<nsIURI> newOriginalURI;
918
    nsCOMPtr<nsIURI> newOriginalURI;
919
    aNewChannel->GetOriginalURI(getter_AddRefs(newOriginalURI));
919
    aNewChannel->GetOriginalURI(getter_AddRefs(newOriginalURI));
920
920
921
    NS_ENSURE_STATE(oldPrincipal && newURI && newOriginalURI);
921
    NS_ENSURE_STATE(oldPrincipal && newURI && newOriginalURI);
922
922
923
    rv = oldPrincipal->CheckMayLoad(newURI, false);
923
    rv = oldPrincipal->CheckMayLoad(newURI, false, false);
924
    if (NS_SUCCEEDED(rv) && newOriginalURI != newURI) {
924
    if (NS_SUCCEEDED(rv) && newOriginalURI != newURI) {
925
        rv = oldPrincipal->CheckMayLoad(newOriginalURI, false);
925
        rv = oldPrincipal->CheckMayLoad(newOriginalURI, false, false);
926
    }
926
    }
927
927
928
    if (NS_FAILED(rv))
928
    if (NS_FAILED(rv))
929
        return rv;
929
        return rv;
930
930
931
    cb->OnRedirectVerifyCallback(NS_OK);
931
    cb->OnRedirectVerifyCallback(NS_OK);
932
    return NS_OK;
932
    return NS_OK;
933
}
933
}

Return to bug 341604