~ubuntu-branches/ubuntu/trusty/xulrunner/trusty

« back to all changes in this revision

Viewing changes to content/base/src/nsScriptLoader.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Devid Antonio Filoni
  • Date: 2008-08-25 13:04:18 UTC
  • mfrom: (1.1.12 upstream)
  • Revision ID: james.westby@ubuntu.com-20080825130418-ck1i2ms384tzb9m0
Tags: 1.8.1.16+nobinonly-0ubuntu1
* New upstream release (taken from upstream CVS), LP: #254618.
* Fix MFSA 2008-35, MFSA 2008-34, MFSA 2008-33, MFSA 2008-32, MFSA 2008-31,
  MFSA 2008-30, MFSA 2008-29, MFSA 2008-28, MFSA 2008-27, MFSA 2008-25,
  MFSA 2008-24, MFSA 2008-23, MFSA 2008-22, MFSA 2008-21, MFSA 2008-26 also
  known as CVE-2008-2933, CVE-2008-2785, CVE-2008-2811, CVE-2008-2810,
  CVE-2008-2809, CVE-2008-2808, CVE-2008-2807, CVE-2008-2806, CVE-2008-2805,
  CVE-2008-2803, CVE-2008-2802, CVE-2008-2801, CVE-2008-2800, CVE-2008-2798.
* Drop 89_bz419350_attachment_306066 patch, merged upstream.
* Bump Standards-Version to 3.8.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
#include "nsIScriptSecurityManager.h"
53
53
#include "nsIPrincipal.h"
54
54
#include "nsContentPolicyUtils.h"
55
 
#include "nsIDOMWindow.h"
 
55
#include "nsPIDOMWindow.h"
56
56
#include "nsIHttpChannel.h"
57
57
#include "nsIScriptElement.h"
58
58
#include "nsIDOMHTMLScriptElement.h"
67
67
static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);
68
68
 
69
69
//////////////////////////////////////////////////////////////
70
 
//
71
 
//////////////////////////////////////////////////////////////
72
 
 
73
 
static already_AddRefed<nsIPrincipal>
74
 
IntersectPrincipalCerts(nsIPrincipal *aOld, nsIPrincipal *aNew)
75
 
{
76
 
  NS_PRECONDITION(aOld, "Null old principal!");
77
 
  NS_PRECONDITION(aNew, "Null new principal!");
78
 
 
79
 
  nsIPrincipal *principal = aOld;
80
 
 
81
 
  PRBool hasCert;
82
 
  aOld->GetHasCertificate(&hasCert);
83
 
  if (hasCert) {
84
 
    PRBool equal;
85
 
    aOld->Equals(aNew, &equal);
86
 
    if (!equal) {
87
 
      nsCOMPtr<nsIURI> uri, domain;
88
 
      aOld->GetURI(getter_AddRefs(uri));
89
 
      aOld->GetDomain(getter_AddRefs(domain));
90
 
 
91
 
      nsContentUtils::GetSecurityManager()->GetCodebasePrincipal(uri, &principal);
92
 
      if (principal && domain) {
93
 
        principal->SetDomain(domain);
94
 
      }
95
 
 
96
 
      return principal;
97
 
    }
98
 
  }
99
 
 
100
 
  NS_ADDREF(principal);
101
 
 
102
 
  return principal;
103
 
}
104
 
 
105
 
//////////////////////////////////////////////////////////////
106
70
// Per-request data structure
107
71
//////////////////////////////////////////////////////////////
108
72
 
763
727
  }
764
728
 
765
729
  nsIScriptGlobalObject *globalObject = mDocument->GetScriptGlobalObject();
766
 
  NS_ENSURE_TRUE(globalObject, NS_ERROR_FAILURE);
 
730
 
 
731
  nsCOMPtr<nsPIDOMWindow> pwin(do_QueryInterface(globalObject));
 
732
  NS_ENSURE_TRUE(pwin && pwin->IsInnerWindow(), NS_ERROR_FAILURE);
767
733
 
768
734
  // Make sure context is a strong reference since we access it after
769
735
  // we've executed a script, which may cause all other references to
1054
1020
      return NS_OK;
1055
1021
    }
1056
1022
 
1057
 
    //-- Merge the principal of the script file with that of the document
1058
 
    if (channel) {
1059
 
      nsCOMPtr<nsISupports> owner;
1060
 
      channel->GetOwner(getter_AddRefs(owner));
1061
 
      nsCOMPtr<nsIPrincipal> principal = do_QueryInterface(owner);
1062
 
 
1063
 
      if (principal) {
1064
 
        nsIPrincipal *docPrincipal = mDocument->GetPrincipal();
1065
 
        if (docPrincipal) {
1066
 
          nsCOMPtr<nsIPrincipal> newPrincipal =
1067
 
              IntersectPrincipalCerts(docPrincipal, principal);
1068
 
 
1069
 
          mDocument->SetPrincipal(newPrincipal);
1070
 
        } else {
1071
 
          mPendingRequests.RemoveObject(request);
1072
 
          FireScriptAvailable(rv, request, EmptyString());
1073
 
          ProcessPendingReqests();
1074
 
          return NS_OK;
1075
 
        }
1076
 
      }
 
1023
    if (!ShouldExecuteScript(mDocument, channel)) {
 
1024
      return NS_ERROR_NOT_AVAILABLE;
1077
1025
    }
1078
1026
  }
1079
1027
 
1095
1043
  return NS_OK;
1096
1044
}
1097
1045
 
 
1046
/**
 
1047
 * Get the "final" URI for a channel.  This is either the same as GetURI or
 
1048
 * GetOriginalURI, depending on whether this channel has
 
1049
 * nsIChanel::LOAD_REPLACE set.  For channels without that flag set, the final
 
1050
 * URI is the original URI, while for ones with the flag the final URI is the
 
1051
 * channel URI.
 
1052
 */
 
1053
static nsresult
 
1054
NS_GetFinalChannelURI(nsIChannel* channel, nsIURI** uri)
 
1055
{
 
1056
    *uri = nsnull;
 
1057
    nsLoadFlags loadFlags = 0;
 
1058
    nsresult rv = channel->GetLoadFlags(&loadFlags);
 
1059
    NS_ENSURE_SUCCESS(rv, rv);
 
1060
    
 
1061
    if (loadFlags & nsIChannel::LOAD_REPLACE) {
 
1062
        return channel->GetURI(uri);
 
1063
    }
 
1064
    
 
1065
    return channel->GetOriginalURI(uri);
 
1066
}
 
1067
 
 
1068
static nsresult
 
1069
GetChannelPrincipal(nsIChannel* aChannel, nsIPrincipal** aPrincipal)
 
1070
{
 
1071
  NS_PRECONDITION(aChannel, "Must have channel!");
 
1072
  nsCOMPtr<nsISupports> owner;
 
1073
  aChannel->GetOwner(getter_AddRefs(owner));
 
1074
  if (owner) {
 
1075
    CallQueryInterface(owner, aPrincipal);
 
1076
    if (*aPrincipal) {
 
1077
      return NS_OK;
 
1078
    }
 
1079
  }
 
1080
 
 
1081
  // OK, get the principal from the URI.  Make sure this does the same thing
 
1082
  // as nsDocument::Reset and nsXULDocument::StartDocumentLoad.
 
1083
  nsCOMPtr<nsIURI> uri;
 
1084
  nsresult rv = NS_GetFinalChannelURI(aChannel, getter_AddRefs(uri));
 
1085
  NS_ENSURE_SUCCESS(rv, rv);
 
1086
 
 
1087
  return nsContentUtils::GetSecurityManager()->
 
1088
    GetCodebasePrincipal(uri, aPrincipal);
 
1089
}
 
1090
 
 
1091
/* static */
 
1092
PRBool
 
1093
nsScriptLoader::ShouldExecuteScript(nsIDocument* aDocument,
 
1094
                                    nsIChannel* aChannel)
 
1095
{
 
1096
  if (!aChannel) {
 
1097
    return PR_FALSE;
 
1098
  }
 
1099
 
 
1100
  PRBool hasCert;
 
1101
  nsIPrincipal *docPrincipal = aDocument->GetPrincipal();
 
1102
  docPrincipal->GetHasCertificate(&hasCert);
 
1103
  if (!hasCert) {
 
1104
    return PR_TRUE;
 
1105
  }
 
1106
 
 
1107
  nsCOMPtr<nsIPrincipal> channelPrincipal;
 
1108
  nsresult rv = GetChannelPrincipal(aChannel,
 
1109
                                    getter_AddRefs(channelPrincipal));
 
1110
  NS_ENSURE_SUCCESS(rv, PR_FALSE);
 
1111
 
 
1112
  NS_ASSERTION(channelPrincipal, "Gotta have a principal here!");
 
1113
 
 
1114
  // If the document principal is a cert principal and is not the same
 
1115
  // as the channel principal, then we don't execute the script.
 
1116
  PRBool equal;
 
1117
  rv = docPrincipal->Equals(channelPrincipal, &equal);
 
1118
  return NS_SUCCEEDED(rv) && equal;
 
1119
}
 
1120
 
1098
1121
NS_IMETHODIMP
1099
1122
nsScriptLoader::GetEnabled(PRBool *aEnabled)
1100
1123
{