nsAboutRedirector.cpp 10.3 KB
Newer Older
1
2
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
4
5
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * 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 "nsBaseChannel.h"
11
#include "mozilla/ArrayUtils.h"
12
#include "nsIProtocolHandler.h"
13
#include "nsXULAppAPI.h"
14
#include "mozilla/Preferences.h"
15

16
#define ABOUT_CONFIG_ENABLED_PREF "general.aboutConfig.enable"
17

18
NS_IMPL_ISUPPORTS(nsAboutRedirector, nsIAboutModule)
19

20
struct RedirEntry {
21
22
23
  const char* id;
  const char* url;
  uint32_t flags;
24
25
};

26
27
28
class CrashChannel final : public nsBaseChannel {
 public:
  explicit CrashChannel(nsIURI* aURI) { SetURI(aURI); }
29

30
31
  nsresult OpenContentStream(bool async, nsIInputStream** stream,
                             nsIChannel** channel) override {
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
    nsAutoCString spec;
    mURI->GetSpec(spec);

    if (spec.EqualsASCII("about:crashparent") && XRE_IsParentProcess()) {
      MOZ_CRASH("Crash via about:crashparent");
    }

    if (spec.EqualsASCII("about:crashcontent") && XRE_IsContentProcess()) {
      MOZ_CRASH("Crash via about:crashcontent");
    }

    NS_WARNING("Unhandled about:crash* URI or wrong process");
    return NS_ERROR_NOT_IMPLEMENTED;
  }

47
 protected:
48
49
50
  virtual ~CrashChannel() = default;
};

51
/*
52
53
54
55
56
  Entries which do not have URI_SAFE_FOR_UNTRUSTED_CONTENT will run with chrome
  privileges. This is potentially dangerous. Please use
  URI_SAFE_FOR_UNTRUSTED_CONTENT in the third argument to each map item below
  unless your about: page really needs chrome privileges. Security review is
  required before adding new map entries without
57
58
59
60
  URI_SAFE_FOR_UNTRUSTED_CONTENT.

  URI_SAFE_FOR_UNTRUSTED_CONTENT is not enough to let web pages load that page,
  for that you need MAKE_LINKABLE.
61
62
63

  NOTE: changes to this redir map need to be accompanied with changes to
    docshell/build/components.conf
64
 */
65
static const RedirEntry kRedirMap[] = {
66
    {"about", "chrome://global/content/aboutAbout.html", 0},
67
    {"addons", "chrome://mozapps/content/extensions/aboutaddons.html",
68
69
70
     nsIAboutModule::ALLOW_SCRIPT},
    {"buildconfig", "chrome://global/content/buildconfig.html",
     nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT},
71
    {"checkerboard", "chrome://global/content/aboutCheckerboard.html",
72
73
     nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
         nsIAboutModule::ALLOW_SCRIPT},
74
75
76
77
#ifndef MOZ_WIDGET_ANDROID
    {"config", "chrome://global/content/aboutconfig/aboutconfig.html", 0},
#else
    {"config", "chrome://geckoview/content/config.xhtml", 0},
78
#endif
79
#ifdef MOZ_CRASHREPORTER
80
    {"crashes", "chrome://global/content/crashes.html", 0},
81
#endif
82
    {"credits", "https://www.mozilla.org/credits/",
83
84
     nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
         nsIAboutModule::URI_MUST_LOAD_IN_CHILD},
85
86
87
88
    {"httpsonlyerror", "chrome://global/content/httpsonlyerror/errorpage.html",
     nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
         nsIAboutModule::URI_CAN_LOAD_IN_CHILD | nsIAboutModule::ALLOW_SCRIPT |
         nsIAboutModule::HIDE_FROM_ABOUTABOUT},
89
    {"license", "chrome://global/content/license.html",
90
     nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT},
91
92
93
94
    {"logo", "chrome://branding/content/about.png",
     nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
         // Linkable for testing reasons.
         nsIAboutModule::MAKE_LINKABLE},
95
    {"memory", "chrome://global/content/aboutMemory.xhtml",
96
     nsIAboutModule::ALLOW_SCRIPT},
97
98
99
100
    {"certificate", "chrome://global/content/certviewer/certviewer.html",
     nsIAboutModule::ALLOW_SCRIPT |
         nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
         nsIAboutModule::URI_MUST_LOAD_IN_CHILD |
101
         nsIAboutModule::URI_CAN_LOAD_IN_PRIVILEGEDABOUT_PROCESS},
102
    {"mozilla", "chrome://global/content/mozilla.html",
103
104
105
106
107
     nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT},
    {"neterror", "chrome://global/content/netError.xhtml",
     nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
         nsIAboutModule::URI_CAN_LOAD_IN_CHILD | nsIAboutModule::ALLOW_SCRIPT |
         nsIAboutModule::HIDE_FROM_ABOUTABOUT},
108
    {"networking", "chrome://global/content/aboutNetworking.html",
109
     nsIAboutModule::ALLOW_SCRIPT},
110
    {"performance", "chrome://global/content/aboutPerformance.html",
111
112
113
     nsIAboutModule::ALLOW_SCRIPT},
    {"plugins", "chrome://global/content/plugins.html",
     nsIAboutModule::URI_MUST_LOAD_IN_CHILD},
114
115
    {"processes", "chrome://global/content/aboutProcesses.html",
     nsIAboutModule::ALLOW_SCRIPT},
116
117
118
119
120
121
122
123
124
    // about:serviceworkers always wants to load in the parent process because
    // when dom.serviceWorkers.parent_intercept is set to true (the new default)
    // then the only place nsIServiceWorkerManager has any data is in the
    // parent process.
    //
    // There is overlap without about:debugging, but about:debugging is not
    // available on mobile at this time, and it's useful to be able to know if
    // a ServiceWorker is registered directly from the mobile browser without
    // having to connect the device to a desktop machine and all that entails.
125
    {"serviceworkers", "chrome://global/content/aboutServiceWorkers.xhtml",
126
     nsIAboutModule::ALLOW_SCRIPT},
127
#ifndef ANDROID
128
129
    {"profiles", "chrome://global/content/aboutProfiles.xhtml",
     nsIAboutModule::ALLOW_SCRIPT},
130
#endif
131
132
133
134
135
136
137
138
139
    // about:srcdoc is unresolvable by specification.  It is included here
    // because the security manager would disallow srcdoc iframes otherwise.
    {"srcdoc", "about:blank",
     nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
         nsIAboutModule::HIDE_FROM_ABOUTABOUT |
         // Needs to be linkable so content can touch its own srcdoc frames
         nsIAboutModule::MAKE_LINKABLE | nsIAboutModule::URI_CAN_LOAD_IN_CHILD},
    {"support", "chrome://global/content/aboutSupport.xhtml",
     nsIAboutModule::ALLOW_SCRIPT},
140
#ifndef MOZ_GLEAN_ANDROID
141
142
143
    {"glean", "chrome://global/content/aboutGlean.html",
     nsIAboutModule::HIDE_FROM_ABOUTABOUT | nsIAboutModule::ALLOW_SCRIPT},
#endif
144
145
146
147
148
149
150
151
152
153
154
155
156
157
    {"telemetry", "chrome://global/content/aboutTelemetry.xhtml",
     nsIAboutModule::ALLOW_SCRIPT},
    {"url-classifier", "chrome://global/content/aboutUrlClassifier.xhtml",
     nsIAboutModule::ALLOW_SCRIPT},
    {"webrtc", "chrome://global/content/aboutwebrtc/aboutWebrtc.html",
     nsIAboutModule::ALLOW_SCRIPT},
    {"printpreview", "about:blank",
     nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
         nsIAboutModule::HIDE_FROM_ABOUTABOUT |
         nsIAboutModule::URI_CAN_LOAD_IN_CHILD},
    {"crashparent", "about:blank", nsIAboutModule::HIDE_FROM_ABOUTABOUT},
    {"crashcontent", "about:blank",
     nsIAboutModule::HIDE_FROM_ABOUTABOUT |
         nsIAboutModule::URI_CAN_LOAD_IN_CHILD |
158
159
160
161
162
         nsIAboutModule::URI_MUST_LOAD_IN_CHILD},
    {"tor", "chrome://torbutton/content/aboutTor/aboutTor.xhtml",
     nsIAboutModule::URI_MUST_LOAD_IN_CHILD |
         nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
         nsIAboutModule::ALLOW_SCRIPT}};
163
static const int kRedirTotal = mozilla::ArrayLength(kRedirMap);
164
165

NS_IMETHODIMP
166
167
nsAboutRedirector::NewChannel(nsIURI* aURI, nsILoadInfo* aLoadInfo,
                              nsIChannel** aResult) {
168
  NS_ENSURE_ARG_POINTER(aURI);
169
  NS_ENSURE_ARG_POINTER(aLoadInfo);
170
171
172
173
174
175
176
177
178
  NS_ASSERTION(aResult, "must not be null");

  nsAutoCString path;
  nsresult rv = NS_GetAboutModuleName(aURI, path);
  NS_ENSURE_SUCCESS(rv, rv);

  nsCOMPtr<nsIIOService> ioService = do_GetIOService(&rv);
  NS_ENSURE_SUCCESS(rv, rv);

179
  if (path.EqualsASCII("crashparent") || path.EqualsASCII("crashcontent")) {
180
181
182
183
184
185
    bool isExternal;
    aLoadInfo->GetLoadTriggeredFromExternal(&isExternal);
    if (isExternal) {
      return NS_ERROR_NOT_AVAILABLE;
    }

186
    nsCOMPtr<nsIChannel> channel = new CrashChannel(aURI);
187
    channel->SetLoadInfo(aLoadInfo);
188
189
    channel.forget(aResult);
    return NS_OK;
190
191
  }

192
193
  if (path.EqualsASCII("config") &&
      !mozilla::Preferences::GetBool(ABOUT_CONFIG_ENABLED_PREF, true)) {
194
195
196
    return NS_ERROR_NOT_AVAILABLE;
  }

197
198
199
200
201
202
  for (int i = 0; i < kRedirTotal; i++) {
    if (!strcmp(path.get(), kRedirMap[i].id)) {
      nsCOMPtr<nsIChannel> tempChannel;
      nsCOMPtr<nsIURI> tempURI;
      rv = NS_NewURI(getter_AddRefs(tempURI), kRedirMap[i].url);
      NS_ENSURE_SUCCESS(rv, rv);
203

204
      rv = NS_NewChannelInternal(getter_AddRefs(tempChannel), tempURI,
205
206
207
                                 aLoadInfo);
      NS_ENSURE_SUCCESS(rv, rv);

208
      // If tempURI links to an external URI (i.e. something other than
209
210
      // chrome:// or resource://) then set result principal URI on the
      // load info which forces the channel principal to reflect the displayed
211
      // URL rather then being the systemPrincipal.
212
213
214
215
216
      bool isUIResource = false;
      rv = NS_URIChainHasFlags(tempURI, nsIProtocolHandler::URI_IS_UI_RESOURCE,
                               &isUIResource);
      NS_ENSURE_SUCCESS(rv, rv);

217
218
      bool isAboutBlank = NS_IsAboutBlank(tempURI);

219
220
221
      if (!isUIResource && !isAboutBlank) {
        aLoadInfo->SetResultPrincipalURI(tempURI);
      }
222
223
224

      tempChannel->SetOriginalURI(aURI);

225
      tempChannel.forget(aResult);
226
      return rv;
227
    }
228
  }
229

230
231
  NS_ERROR("nsAboutRedirector called for unknown case");
  return NS_ERROR_ILLEGAL_VALUE;
232
233
}

234
NS_IMETHODIMP
235
nsAboutRedirector::GetURIFlags(nsIURI* aURI, uint32_t* aResult) {
236
237
238
239
240
241
242
243
244
245
  NS_ENSURE_ARG_POINTER(aURI);

  nsAutoCString name;
  nsresult rv = NS_GetAboutModuleName(aURI, name);
  NS_ENSURE_SUCCESS(rv, rv);

  for (int i = 0; i < kRedirTotal; i++) {
    if (name.EqualsASCII(kRedirMap[i].id)) {
      *aResult = kRedirMap[i].flags;
      return NS_OK;
246
    }
247
  }
248

249
250
  NS_ERROR("nsAboutRedirector called for unknown case");
  return NS_ERROR_ILLEGAL_VALUE;
251
252
}

253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
NS_IMETHODIMP
nsAboutRedirector::GetChromeURI(nsIURI* aURI, nsIURI** chromeURI) {
  NS_ENSURE_ARG_POINTER(aURI);

  nsAutoCString name;
  nsresult rv = NS_GetAboutModuleName(aURI, name);
  NS_ENSURE_SUCCESS(rv, rv);

  for (const auto& redir : kRedirMap) {
    if (name.EqualsASCII(redir.id)) {
      return NS_NewURI(chromeURI, redir.url);
    }
  }

  NS_ERROR("nsAboutRedirector called for unknown case");
  return NS_ERROR_ILLEGAL_VALUE;
}

271
272
nsresult nsAboutRedirector::Create(nsISupports* aOuter, REFNSIID aIID,
                                   void** aResult) {
273
  RefPtr<nsAboutRedirector> about = new nsAboutRedirector();
274
  return about->QueryInterface(aIID, aResult);
275
}