Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "nsAboutRedirector.h"
8 : #include "nsNetUtil.h"
9 : #include "nsAboutProtocolUtils.h"
10 : #include "mozilla/ArrayUtils.h"
11 : #include "nsIProtocolHandler.h"
12 :
13 0 : NS_IMPL_ISUPPORTS(nsAboutRedirector, nsIAboutModule)
14 :
15 : struct RedirEntry
16 : {
17 : const char* id;
18 : const char* url;
19 : uint32_t flags;
20 : };
21 :
22 : /*
23 : Entries which do not have URI_SAFE_FOR_UNTRUSTED_CONTENT will run with chrome
24 : privileges. This is potentially dangerous. Please use
25 : URI_SAFE_FOR_UNTRUSTED_CONTENT in the third argument to each map item below
26 : unless your about: page really needs chrome privileges. Security review is
27 : required before adding new map entries without
28 : URI_SAFE_FOR_UNTRUSTED_CONTENT. Also note, however, that adding
29 : URI_SAFE_FOR_UNTRUSTED_CONTENT will allow random web sites to link to that
30 : URI. Perhaps we should separate the two concepts out...
31 : */
32 : static const RedirEntry kRedirMap[] = {
33 : {
34 : "", "chrome://global/content/about.xhtml",
35 : nsIAboutModule::ALLOW_SCRIPT
36 : },
37 : { "about", "chrome://global/content/aboutAbout.xhtml", 0 },
38 : {
39 : "addons", "chrome://mozapps/content/extensions/extensions.xul",
40 : nsIAboutModule::ALLOW_SCRIPT
41 : },
42 : {
43 : "buildconfig", "chrome://global/content/buildconfig.html",
44 : nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT
45 : },
46 : {
47 : "checkerboard", "chrome://global/content/aboutCheckerboard.xhtml",
48 : nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
49 : nsIAboutModule::ALLOW_SCRIPT
50 : },
51 : { "config", "chrome://global/content/config.xul", 0 },
52 : #ifdef MOZ_CRASHREPORTER
53 : { "crashes", "chrome://global/content/crashes.xhtml", 0 },
54 : #endif
55 : {
56 : "credits", "https://www.mozilla.org/credits/",
57 : nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT
58 : },
59 : {
60 : "license", "chrome://global/content/license.html",
61 : nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
62 : nsIAboutModule::MAKE_LINKABLE
63 : },
64 : {
65 : "logo", "chrome://branding/content/about.png",
66 : nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
67 : // Linkable for testing reasons.
68 : nsIAboutModule::MAKE_LINKABLE
69 : },
70 : {
71 : "memory", "chrome://global/content/aboutMemory.xhtml",
72 : nsIAboutModule::ALLOW_SCRIPT
73 : },
74 : {
75 : "mozilla", "chrome://global/content/mozilla.xhtml",
76 : nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT
77 : },
78 : {
79 : "neterror", "chrome://global/content/netError.xhtml",
80 : nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
81 : nsIAboutModule::URI_CAN_LOAD_IN_CHILD |
82 : nsIAboutModule::ALLOW_SCRIPT |
83 : nsIAboutModule::HIDE_FROM_ABOUTABOUT
84 : },
85 : {
86 : "networking", "chrome://global/content/aboutNetworking.xhtml",
87 : nsIAboutModule::ALLOW_SCRIPT
88 : },
89 : {
90 : "newaddon", "chrome://mozapps/content/extensions/newaddon.xul",
91 : nsIAboutModule::ALLOW_SCRIPT |
92 : nsIAboutModule::HIDE_FROM_ABOUTABOUT
93 : },
94 : {
95 : "performance", "chrome://global/content/aboutPerformance.xhtml",
96 : nsIAboutModule::ALLOW_SCRIPT
97 : },
98 : {
99 : "plugins", "chrome://global/content/plugins.html",
100 : nsIAboutModule::URI_MUST_LOAD_IN_CHILD
101 : },
102 : {
103 : "serviceworkers", "chrome://global/content/aboutServiceWorkers.xhtml",
104 : nsIAboutModule::URI_CAN_LOAD_IN_CHILD |
105 : nsIAboutModule::URI_MUST_LOAD_IN_CHILD |
106 : nsIAboutModule::ALLOW_SCRIPT
107 : },
108 : #ifndef ANDROID
109 : {
110 : "profiles", "chrome://global/content/aboutProfiles.xhtml",
111 : nsIAboutModule::ALLOW_SCRIPT
112 : },
113 : #endif
114 : // about:srcdoc is unresolvable by specification. It is included here
115 : // because the security manager would disallow srcdoc iframes otherwise.
116 : {
117 : "srcdoc", "about:blank",
118 : nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
119 : nsIAboutModule::HIDE_FROM_ABOUTABOUT |
120 : // Needs to be linkable so content can touch its own srcdoc frames
121 : nsIAboutModule::MAKE_LINKABLE |
122 : nsIAboutModule::URI_CAN_LOAD_IN_CHILD
123 : },
124 : {
125 : "support", "chrome://global/content/aboutSupport.xhtml",
126 : nsIAboutModule::ALLOW_SCRIPT
127 : },
128 : {
129 : "telemetry", "chrome://global/content/aboutTelemetry.xhtml",
130 : nsIAboutModule::ALLOW_SCRIPT
131 : },
132 : {
133 : "url-classifier", "chrome://global/content/aboutUrlClassifier.xhtml",
134 : nsIAboutModule::ALLOW_SCRIPT
135 : },
136 : {
137 : "webrtc", "chrome://global/content/aboutwebrtc/aboutWebrtc.html",
138 : nsIAboutModule::ALLOW_SCRIPT
139 : },
140 : {
141 : "printpreview", "about:blank",
142 : nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
143 : nsIAboutModule::HIDE_FROM_ABOUTABOUT |
144 : nsIAboutModule::URI_CAN_LOAD_IN_CHILD
145 : }
146 : };
147 : static const int kRedirTotal = mozilla::ArrayLength(kRedirMap);
148 :
149 : NS_IMETHODIMP
150 0 : nsAboutRedirector::NewChannel(nsIURI* aURI,
151 : nsILoadInfo* aLoadInfo,
152 : nsIChannel** aResult)
153 : {
154 0 : NS_ENSURE_ARG_POINTER(aURI);
155 0 : NS_ENSURE_ARG_POINTER(aLoadInfo);
156 0 : NS_ASSERTION(aResult, "must not be null");
157 :
158 0 : nsAutoCString path;
159 0 : nsresult rv = NS_GetAboutModuleName(aURI, path);
160 0 : NS_ENSURE_SUCCESS(rv, rv);
161 :
162 0 : nsCOMPtr<nsIIOService> ioService = do_GetIOService(&rv);
163 0 : NS_ENSURE_SUCCESS(rv, rv);
164 :
165 0 : for (int i = 0; i < kRedirTotal; i++) {
166 0 : if (!strcmp(path.get(), kRedirMap[i].id)) {
167 0 : nsCOMPtr<nsIChannel> tempChannel;
168 0 : nsCOMPtr<nsIURI> tempURI;
169 0 : rv = NS_NewURI(getter_AddRefs(tempURI), kRedirMap[i].url);
170 0 : NS_ENSURE_SUCCESS(rv, rv);
171 :
172 0 : rv = NS_NewChannelInternal(getter_AddRefs(tempChannel),
173 : tempURI,
174 : aLoadInfo);
175 0 : NS_ENSURE_SUCCESS(rv, rv);
176 :
177 : // If tempURI links to an external URI (i.e. something other than
178 : // chrome:// or resource://) then set result principal URI on the
179 : // load info which forces the channel principal to reflect the displayed
180 : // URL rather then being the systemPrincipal.
181 0 : bool isUIResource = false;
182 0 : rv = NS_URIChainHasFlags(tempURI, nsIProtocolHandler::URI_IS_UI_RESOURCE,
183 : &isUIResource);
184 0 : NS_ENSURE_SUCCESS(rv, rv);
185 :
186 0 : bool isAboutBlank = NS_IsAboutBlank(tempURI);
187 :
188 0 : if (!isUIResource && !isAboutBlank) {
189 0 : aLoadInfo->SetResultPrincipalURI(tempURI);
190 : }
191 :
192 0 : tempChannel->SetOriginalURI(aURI);
193 :
194 0 : tempChannel.forget(aResult);
195 0 : return rv;
196 : }
197 : }
198 :
199 0 : NS_ERROR("nsAboutRedirector called for unknown case");
200 0 : return NS_ERROR_ILLEGAL_VALUE;
201 : }
202 :
203 : NS_IMETHODIMP
204 0 : nsAboutRedirector::GetURIFlags(nsIURI* aURI, uint32_t* aResult)
205 : {
206 0 : NS_ENSURE_ARG_POINTER(aURI);
207 :
208 0 : nsAutoCString name;
209 0 : nsresult rv = NS_GetAboutModuleName(aURI, name);
210 0 : NS_ENSURE_SUCCESS(rv, rv);
211 :
212 0 : for (int i = 0; i < kRedirTotal; i++) {
213 0 : if (name.EqualsASCII(kRedirMap[i].id)) {
214 0 : *aResult = kRedirMap[i].flags;
215 0 : return NS_OK;
216 : }
217 : }
218 :
219 0 : NS_ERROR("nsAboutRedirector called for unknown case");
220 0 : return NS_ERROR_ILLEGAL_VALUE;
221 : }
222 :
223 : nsresult
224 0 : nsAboutRedirector::Create(nsISupports* aOuter, REFNSIID aIID, void** aResult)
225 : {
226 0 : RefPtr<nsAboutRedirector> about = new nsAboutRedirector();
227 0 : return about->QueryInterface(aIID, aResult);
228 : }
|