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 NS_IMPL_ISUPPORTS(nsAboutRedirector, nsIAboutModule)
14 
15 struct RedirEntry {
16   const char* id;
17   const char* url;
18   uint32_t flags;
19 };
20 
21 /*
22   Entries which do not have URI_SAFE_FOR_UNTRUSTED_CONTENT will run with chrome
23   privileges. This is potentially dangerous. Please use
24   URI_SAFE_FOR_UNTRUSTED_CONTENT in the third argument to each map item below
25   unless your about: page really needs chrome privileges. Security review is
26   required before adding new map entries without
27   URI_SAFE_FOR_UNTRUSTED_CONTENT.  Also note, however, that adding
28   URI_SAFE_FOR_UNTRUSTED_CONTENT will allow random web sites to link to that
29   URI.  Perhaps we should separate the two concepts out...
30  */
31 static const RedirEntry kRedirMap[] = {
32     {"about", "chrome://global/content/aboutAbout.xhtml", 0},
33     {"addons", "chrome://mozapps/content/extensions/extensions.xul",
34      nsIAboutModule::ALLOW_SCRIPT},
35     {"buildconfig", "chrome://global/content/buildconfig.html",
36      nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT},
37     {"checkerboard", "chrome://global/content/aboutCheckerboard.xhtml",
38      nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
39          nsIAboutModule::ALLOW_SCRIPT},
40     {"config", "chrome://global/content/config.xul", 0},
41 #ifdef MOZ_CRASHREPORTER
42     {"crashes", "chrome://global/content/crashes.xhtml", 0},
43 #endif
44     {"credits", "https://www.mozilla.org/credits/",
45      nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT},
46     {"license", "chrome://global/content/license.html",
47      nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
48          nsIAboutModule::MAKE_LINKABLE},
49     {"logo", "chrome://branding/content/about.png",
50      nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
51          // Linkable for testing reasons.
52          nsIAboutModule::MAKE_LINKABLE},
53     {"memory", "chrome://global/content/aboutMemory.xhtml",
54      nsIAboutModule::ALLOW_SCRIPT},
55     {"mozilla", "chrome://global/content/mozilla.xhtml",
56      nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT},
57     {"neterror", "chrome://global/content/netError.xhtml",
58      nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
59          nsIAboutModule::URI_CAN_LOAD_IN_CHILD | nsIAboutModule::ALLOW_SCRIPT |
60          nsIAboutModule::HIDE_FROM_ABOUTABOUT},
61     {"networking", "chrome://global/content/aboutNetworking.xhtml",
62      nsIAboutModule::ALLOW_SCRIPT},
63     {"newaddon", "chrome://mozapps/content/extensions/newaddon.xul",
64      nsIAboutModule::ALLOW_SCRIPT | nsIAboutModule::HIDE_FROM_ABOUTABOUT},
65     {"performance", "chrome://global/content/aboutPerformance.xhtml",
66      nsIAboutModule::ALLOW_SCRIPT},
67     {"plugins", "chrome://global/content/plugins.html",
68      nsIAboutModule::URI_MUST_LOAD_IN_CHILD},
69     {"serviceworkers", "chrome://global/content/aboutServiceWorkers.xhtml",
70      nsIAboutModule::URI_CAN_LOAD_IN_CHILD |
71          nsIAboutModule::URI_MUST_LOAD_IN_CHILD | nsIAboutModule::ALLOW_SCRIPT},
72 #ifndef ANDROID
73     {"profiles", "chrome://global/content/aboutProfiles.xhtml",
74      nsIAboutModule::ALLOW_SCRIPT},
75 #endif
76     // about:srcdoc is unresolvable by specification.  It is included here
77     // because the security manager would disallow srcdoc iframes otherwise.
78     {"srcdoc", "about:blank",
79      nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
80          nsIAboutModule::HIDE_FROM_ABOUTABOUT |
81          // Needs to be linkable so content can touch its own srcdoc frames
82          nsIAboutModule::MAKE_LINKABLE | nsIAboutModule::URI_CAN_LOAD_IN_CHILD},
83     {"support", "chrome://global/content/aboutSupport.xhtml",
84      nsIAboutModule::ALLOW_SCRIPT},
85     {"telemetry", "chrome://global/content/aboutTelemetry.xhtml",
86      nsIAboutModule::ALLOW_SCRIPT},
87     {"url-classifier", "chrome://global/content/aboutUrlClassifier.xhtml",
88      nsIAboutModule::ALLOW_SCRIPT},
89     {"webrtc", "chrome://global/content/aboutwebrtc/aboutWebrtc.html",
90      nsIAboutModule::ALLOW_SCRIPT},
91     {"printpreview", "about:blank",
92      nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT |
93          nsIAboutModule::HIDE_FROM_ABOUTABOUT |
94          nsIAboutModule::URI_CAN_LOAD_IN_CHILD}};
95 static const int kRedirTotal = mozilla::ArrayLength(kRedirMap);
96 
97 NS_IMETHODIMP
NewChannel(nsIURI * aURI,nsILoadInfo * aLoadInfo,nsIChannel ** aResult)98 nsAboutRedirector::NewChannel(nsIURI* aURI, nsILoadInfo* aLoadInfo,
99                               nsIChannel** aResult) {
100   NS_ENSURE_ARG_POINTER(aURI);
101   NS_ENSURE_ARG_POINTER(aLoadInfo);
102   NS_ASSERTION(aResult, "must not be null");
103 
104   nsAutoCString path;
105   nsresult rv = NS_GetAboutModuleName(aURI, path);
106   NS_ENSURE_SUCCESS(rv, rv);
107 
108   nsCOMPtr<nsIIOService> ioService = do_GetIOService(&rv);
109   NS_ENSURE_SUCCESS(rv, rv);
110 
111   for (int i = 0; i < kRedirTotal; i++) {
112     if (!strcmp(path.get(), kRedirMap[i].id)) {
113       nsCOMPtr<nsIChannel> tempChannel;
114       nsCOMPtr<nsIURI> tempURI;
115       rv = NS_NewURI(getter_AddRefs(tempURI), kRedirMap[i].url);
116       NS_ENSURE_SUCCESS(rv, rv);
117 
118       rv = NS_NewChannelInternal(getter_AddRefs(tempChannel), tempURI,
119                                  aLoadInfo);
120       NS_ENSURE_SUCCESS(rv, rv);
121 
122       // If tempURI links to an external URI (i.e. something other than
123       // chrome:// or resource://) then set result principal URI on the
124       // load info which forces the channel principal to reflect the displayed
125       // URL rather then being the systemPrincipal.
126       bool isUIResource = false;
127       rv = NS_URIChainHasFlags(tempURI, nsIProtocolHandler::URI_IS_UI_RESOURCE,
128                                &isUIResource);
129       NS_ENSURE_SUCCESS(rv, rv);
130 
131       bool isAboutBlank = NS_IsAboutBlank(tempURI);
132 
133       if (!isUIResource && !isAboutBlank) {
134         aLoadInfo->SetResultPrincipalURI(tempURI);
135       }
136 
137       tempChannel->SetOriginalURI(aURI);
138 
139       tempChannel.forget(aResult);
140       return rv;
141     }
142   }
143 
144   NS_ERROR("nsAboutRedirector called for unknown case");
145   return NS_ERROR_ILLEGAL_VALUE;
146 }
147 
148 NS_IMETHODIMP
GetURIFlags(nsIURI * aURI,uint32_t * aResult)149 nsAboutRedirector::GetURIFlags(nsIURI* aURI, uint32_t* aResult) {
150   NS_ENSURE_ARG_POINTER(aURI);
151 
152   nsAutoCString name;
153   nsresult rv = NS_GetAboutModuleName(aURI, name);
154   NS_ENSURE_SUCCESS(rv, rv);
155 
156   for (int i = 0; i < kRedirTotal; i++) {
157     if (name.EqualsASCII(kRedirMap[i].id)) {
158       *aResult = kRedirMap[i].flags;
159       return NS_OK;
160     }
161   }
162 
163   NS_ERROR("nsAboutRedirector called for unknown case");
164   return NS_ERROR_ILLEGAL_VALUE;
165 }
166 
Create(nsISupports * aOuter,REFNSIID aIID,void ** aResult)167 nsresult nsAboutRedirector::Create(nsISupports* aOuter, REFNSIID aIID,
168                                    void** aResult) {
169   RefPtr<nsAboutRedirector> about = new nsAboutRedirector();
170   return about->QueryInterface(aIID, aResult);
171 }
172