AboutRedirector.cpp
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is mozilla.org code.
16  *
17  * The Initial Developer of the Original Code is
18  * Netscape Communications Corporation.
19  * Portions created by the Initial Developer are Copyright (C) 1998
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  * Gagan Saksena (original author)
24  * Ryan Flint <rflint@mozilla.com>
25  *
26  * Alternatively, the contents of this file may be used under the terms of
27  * either the GNU General Public License Version 2 or later (the "GPL"), or
28  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29  * in which case the provisions of the GPL or the LGPL are applicable instead
30  * of those above. If you wish to allow use of your version of this file only
31  * under the terms of either the GPL or the LGPL, and not to allow others to
32  * use your version of this file under the terms of the MPL, indicate your
33  * decision by deleting the provisions above and replace them with the notice
34  * and other provisions required by the GPL or the LGPL. If you do not delete
35  * the provisions above, a recipient may use your version of this file under
36  * the terms of any one of the MPL, the GPL or the LGPL.
37  *
38  * ***** END LICENSE BLOCK ***** */
39 
40 // See also: docshell/base/nsAboutRedirector.cpp
41 
42 #include "AboutRedirector.h"
43 #include "nsNetUtil.h"
44 #include "nsIScriptSecurityManager.h"
45 
46 namespace mozilla {
47 namespace browser {
48 
49 NS_IMPL_ISUPPORTS1(AboutRedirector, nsIAboutModule)
50 
51 struct RedirEntry {
52  const char* id;
53  const char* url;
54  PRUint32 flags; // See nsIAboutModule. The URI_SAFE_FOR_UNTRUSTED_CONTENT
55  // flag does double duty here -- if it's not set, we don't
56  // drop chrome privileges.
57 };
58 
59 /*
60  Entries which do not have URI_SAFE_FOR_UNTRUSTED_CONTENT will run with chrome
61  privileges. This is potentially dangerous. Please use
62  URI_SAFE_FOR_UNTRUSTED_CONTENT in the third argument to each map item below
63  unless your about: page really needs chrome privileges. Security review is
64  required before adding new map entries without
65  URI_SAFE_FOR_UNTRUSTED_CONTENT. Also note, however, that adding
66  URI_SAFE_FOR_UNTRUSTED_CONTENT will allow random web sites to link to that
67  URI. Perhaps we should separate the two concepts out...
68  */
69 static RedirEntry kRedirMap[] = {
70 #ifdef MOZ_SAFE_BROWSING
71  { "blocked", "chrome://browser/content/safebrowsing/blockedSite.xhtml",
72  nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
73  nsIAboutModule::ALLOW_SCRIPT },
74 #endif
75  { "certerror", "chrome://browser/content/certerror/aboutCertError.xhtml",
76  nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
77  nsIAboutModule::ALLOW_SCRIPT },
78  { "feeds", "chrome://browser/content/feeds/subscribe.xhtml",
79  nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
80  nsIAboutModule::ALLOW_SCRIPT },
81  { "privatebrowsing", "chrome://browser/content/aboutPrivateBrowsing.xhtml",
82  nsIAboutModule::ALLOW_SCRIPT },
83  { "rights",
84 #ifdef OFFICIAL_BUILD
85  "chrome://global/content/aboutRights.xhtml",
86 #else
87  "chrome://global/content/aboutRights-unbranded.xhtml",
88 #endif
89  nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
90  nsIAboutModule::ALLOW_SCRIPT },
91  { "robots", "chrome://browser/content/aboutRobots.xhtml",
92  nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
93  nsIAboutModule::ALLOW_SCRIPT },
94  { "sessionrestore", "chrome://browser/content/aboutSessionRestore.xhtml",
95  nsIAboutModule::ALLOW_SCRIPT },
96  { "support", "chrome://browser/content/aboutSupport.xhtml",
97  nsIAboutModule::ALLOW_SCRIPT },
98 };
99 static const int kRedirTotal = NS_ARRAY_LENGTH(kRedirMap);
100 
101 static nsCAutoString
102 GetAboutModuleName(nsIURI *aURI)
103 {
104  nsCAutoString path;
105  aURI->GetPath(path);
106 
107  PRInt32 f = path.FindChar('#');
108  if (f >= 0)
109  path.SetLength(f);
110 
111  f = path.FindChar('?');
112  if (f >= 0)
113  path.SetLength(f);
114 
115  ToLowerCase(path);
116  return path;
117 }
118 
119 NS_IMETHODIMP
120 AboutRedirector::NewChannel(nsIURI *aURI, nsIChannel **result)
121 {
122  NS_ENSURE_ARG_POINTER(aURI);
123  NS_ASSERTION(result, "must not be null");
124 
125  nsCAutoString path = GetAboutModuleName(aURI);
126 
127  nsresult rv;
128  nsCOMPtr<nsIIOService> ioService = do_GetIOService(&rv);
129  NS_ENSURE_SUCCESS(rv, rv);
130 
131  for (int i = 0; i < kRedirTotal; i++) {
132  if (!strcmp(path.get(), kRedirMap[i].id)) {
133  nsCOMPtr<nsIChannel> tempChannel;
134  rv = ioService->NewChannel(nsDependentCString(kRedirMap[i].url),
135  nsnull, nsnull, getter_AddRefs(tempChannel));
136  NS_ENSURE_SUCCESS(rv, rv);
137 
138  tempChannel->SetOriginalURI(aURI);
139 
140  // Keep the page from getting unnecessary privileges unless it needs them
141  if (kRedirMap[i].flags & nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT) {
142  nsCOMPtr<nsIScriptSecurityManager> securityManager =
143  do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
144  NS_ENSURE_SUCCESS(rv, rv);
145 
146  nsCOMPtr<nsIPrincipal> principal;
147  rv = securityManager->GetCodebasePrincipal(aURI, getter_AddRefs(principal));
148  NS_ENSURE_SUCCESS(rv, rv);
149 
150  rv = tempChannel->SetOwner(principal);
151  NS_ENSURE_SUCCESS(rv, rv);
152  }
153 
154  NS_ADDREF(*result = tempChannel);
155  return rv;
156  }
157  }
158 
159  return NS_ERROR_ILLEGAL_VALUE;
160 }
161 
162 NS_IMETHODIMP
163 AboutRedirector::GetURIFlags(nsIURI *aURI, PRUint32 *result)
164 {
165  NS_ENSURE_ARG_POINTER(aURI);
166 
167  nsCAutoString name = GetAboutModuleName(aURI);
168 
169  for (int i = 0; i < kRedirTotal; i++) {
170  if (name.Equals(kRedirMap[i].id)) {
171  *result = kRedirMap[i].flags;
172  return NS_OK;
173  }
174  }
175 
176  return NS_ERROR_ILLEGAL_VALUE;
177 }
178 
179 NS_METHOD
180 AboutRedirector::Create(nsISupports *aOuter, REFNSIID aIID, void **result)
181 {
182  AboutRedirector* about = new AboutRedirector();
183  if (about == nsnull)
184  return NS_ERROR_OUT_OF_MEMORY;
185  NS_ADDREF(about);
186  nsresult rv = about->QueryInterface(aIID, result);
187  NS_RELEASE(about);
188  return rv;
189 }
190 
191 } // namespace browser
192 } // namespace mozilla
return NS_OK
NS_DECL_ISUPPORTS NS_DECL_NSIABOUTMODULE AboutRedirector()
NS_IMPL_ISUPPORTS1(sbDeviceCapabilitiesUtils, sbIDeviceCapabilitiesUtils) sbDeviceCapabilitiesUtils
static RedirEntry kRedirMap[]
sbDeviceFirmwareAutoCheckForUpdate prototype flags
static const int kRedirTotal
static nsCAutoString GetAboutModuleName(nsIURI *aURI)
var ioService
const char * url
const nsIChannel
function url(spec)
const char * id
PRUint32 flags
var browser
Definition: openLocation.js:42
_getSelectedPageStyle s i
static NS_METHOD Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)