1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* vim:set ts=4 sw=4 sts=4 et cindent: */
3 : /* ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is mozilla.org code.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Netscape Communications Corporation.
20 : * Portions created by the Initial Developer are Copyright (C) 1998
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Gagan Saksena (original author)
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 : #include "nsAboutRedirector.h"
41 : #include "nsNetUtil.h"
42 : #include "plstr.h"
43 : #include "nsIScriptSecurityManager.h"
44 : #include "nsAboutProtocolUtils.h"
45 :
46 132 : NS_IMPL_ISUPPORTS1(nsAboutRedirector, nsIAboutModule)
47 :
48 : struct RedirEntry {
49 : const char* id;
50 : const char* url;
51 : PRUint32 flags; // See nsIAboutModule. The URI_SAFE_FOR_UNTRUSTED_CONTENT
52 : // flag does double duty here -- if it's not set, we don't
53 : // drop chrome privileges.
54 : };
55 :
56 : /*
57 : Entries which do not have URI_SAFE_FOR_UNTRUSTED_CONTENT will run with chrome
58 : privileges. This is potentially dangerous. Please use
59 : URI_SAFE_FOR_UNTRUSTED_CONTENT in the third argument to each map item below
60 : unless your about: page really needs chrome privileges. Security review is
61 : required before adding new map entries without
62 : URI_SAFE_FOR_UNTRUSTED_CONTENT. Also note, however, that adding
63 : URI_SAFE_FOR_UNTRUSTED_CONTENT will allow random web sites to link to that
64 : URI. Perhaps we should separate the two concepts out...
65 : */
66 : static RedirEntry kRedirMap[] = {
67 : { "", "chrome://global/content/about.xhtml",
68 : nsIAboutModule::ALLOW_SCRIPT },
69 : { "about", "chrome://global/content/aboutAbout.xhtml", 0 },
70 : { "credits", "http://www.mozilla.org/credits/",
71 : nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT },
72 : { "mozilla", "chrome://global/content/mozilla.xhtml",
73 : nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT },
74 : { "plugins", "chrome://global/content/plugins.html", 0 },
75 : { "config", "chrome://global/content/config.xul", 0 },
76 : #ifdef MOZ_CRASHREPORTER
77 : { "crashes", "chrome://global/content/crashes.xhtml", 0 },
78 : #endif
79 : { "logo", "chrome://branding/content/about.png",
80 : nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT},
81 : { "buildconfig", "chrome://global/content/buildconfig.html",
82 : nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT },
83 : { "license", "chrome://global/content/license.html",
84 : nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT },
85 : { "neterror", "chrome://global/content/netError.xhtml",
86 : nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
87 : nsIAboutModule::ALLOW_SCRIPT |
88 : nsIAboutModule::HIDE_FROM_ABOUTABOUT },
89 : // aboutMemory.xhtml implements about:compartments
90 : { "compartments", "chrome://global/content/aboutMemory.xhtml",
91 : nsIAboutModule::ALLOW_SCRIPT },
92 : { "memory", "chrome://global/content/aboutMemory.xhtml",
93 : nsIAboutModule::ALLOW_SCRIPT },
94 : { "addons", "chrome://mozapps/content/extensions/extensions.xul",
95 : nsIAboutModule::ALLOW_SCRIPT },
96 : { "newaddon", "chrome://mozapps/content/extensions/newaddon.xul",
97 : nsIAboutModule::ALLOW_SCRIPT |
98 : nsIAboutModule::HIDE_FROM_ABOUTABOUT },
99 : { "support", "chrome://global/content/aboutSupport.xhtml",
100 : nsIAboutModule::ALLOW_SCRIPT }
101 : };
102 : static const int kRedirTotal = NS_ARRAY_LENGTH(kRedirMap);
103 :
104 : NS_IMETHODIMP
105 0 : nsAboutRedirector::NewChannel(nsIURI *aURI, nsIChannel **result)
106 : {
107 0 : NS_ENSURE_ARG_POINTER(aURI);
108 0 : NS_ASSERTION(result, "must not be null");
109 :
110 : nsresult rv;
111 :
112 0 : nsCAutoString path;
113 0 : rv = NS_GetAboutModuleName(aURI, path);
114 0 : if (NS_FAILED(rv))
115 0 : return rv;
116 :
117 0 : nsCOMPtr<nsIIOService> ioService = do_GetIOService(&rv);
118 0 : if (NS_FAILED(rv))
119 0 : return rv;
120 :
121 0 : for (int i=0; i<kRedirTotal; i++)
122 : {
123 0 : if (!strcmp(path.get(), kRedirMap[i].id))
124 : {
125 0 : nsCOMPtr<nsIChannel> tempChannel;
126 0 : rv = ioService->NewChannel(nsDependentCString(kRedirMap[i].url),
127 0 : nsnull, nsnull, getter_AddRefs(tempChannel));
128 0 : if (NS_FAILED(rv))
129 0 : return rv;
130 :
131 0 : tempChannel->SetOriginalURI(aURI);
132 :
133 : // Keep the page from getting unnecessary privileges unless it needs them
134 0 : if (kRedirMap[i].flags &
135 : nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT)
136 : {
137 : nsCOMPtr<nsIScriptSecurityManager> securityManager =
138 0 : do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
139 0 : if (NS_FAILED(rv))
140 0 : return rv;
141 :
142 0 : nsCOMPtr<nsIPrincipal> principal;
143 0 : rv = securityManager->GetCodebasePrincipal(aURI, getter_AddRefs(principal));
144 0 : if (NS_FAILED(rv))
145 0 : return rv;
146 :
147 0 : rv = tempChannel->SetOwner(principal);
148 0 : if (NS_FAILED(rv))
149 0 : return rv;
150 : }
151 :
152 0 : NS_ADDREF(*result = tempChannel);
153 0 : return rv;
154 : }
155 : }
156 :
157 0 : NS_ERROR("nsAboutRedirector called for unknown case");
158 0 : return NS_ERROR_ILLEGAL_VALUE;
159 : }
160 :
161 : NS_IMETHODIMP
162 13 : nsAboutRedirector::GetURIFlags(nsIURI *aURI, PRUint32 *result)
163 : {
164 13 : NS_ENSURE_ARG_POINTER(aURI);
165 :
166 26 : nsCAutoString name;
167 13 : nsresult rv = NS_GetAboutModuleName(aURI, name);
168 13 : NS_ENSURE_SUCCESS(rv, rv);
169 :
170 56 : for (int i=0; i < kRedirTotal; i++)
171 : {
172 56 : if (name.EqualsASCII(kRedirMap[i].id))
173 : {
174 13 : *result = kRedirMap[i].flags;
175 13 : return NS_OK;
176 : }
177 : }
178 :
179 0 : NS_ERROR("nsAboutRedirector called for unknown case");
180 0 : return NS_ERROR_ILLEGAL_VALUE;
181 : }
182 :
183 : nsresult
184 7 : nsAboutRedirector::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
185 : {
186 7 : nsAboutRedirector* about = new nsAboutRedirector();
187 7 : if (about == nsnull)
188 0 : return NS_ERROR_OUT_OF_MEMORY;
189 7 : NS_ADDREF(about);
190 7 : nsresult rv = about->QueryInterface(aIID, aResult);
191 7 : NS_RELEASE(about);
192 7 : return rv;
193 : }
|