1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/docshell/base/nsAboutRedirector.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,149 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* vim:set ts=4 sw=4 sts=4 et cindent: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "nsAboutRedirector.h" 1.11 +#include "nsNetUtil.h" 1.12 +#include "nsAboutProtocolUtils.h" 1.13 +#include "mozilla/ArrayUtils.h" 1.14 + 1.15 +NS_IMPL_ISUPPORTS(nsAboutRedirector, nsIAboutModule) 1.16 + 1.17 +struct RedirEntry { 1.18 + const char* id; 1.19 + const char* url; 1.20 + uint32_t flags; 1.21 +}; 1.22 + 1.23 +/* 1.24 + Entries which do not have URI_SAFE_FOR_UNTRUSTED_CONTENT will run with chrome 1.25 + privileges. This is potentially dangerous. Please use 1.26 + URI_SAFE_FOR_UNTRUSTED_CONTENT in the third argument to each map item below 1.27 + unless your about: page really needs chrome privileges. Security review is 1.28 + required before adding new map entries without 1.29 + URI_SAFE_FOR_UNTRUSTED_CONTENT. Also note, however, that adding 1.30 + URI_SAFE_FOR_UNTRUSTED_CONTENT will allow random web sites to link to that 1.31 + URI. Perhaps we should separate the two concepts out... 1.32 + */ 1.33 +static RedirEntry kRedirMap[] = { 1.34 + { "", "chrome://global/content/about.xhtml", 1.35 + nsIAboutModule::ALLOW_SCRIPT }, 1.36 + { "about", "chrome://global/content/aboutAbout.xhtml", 0 }, 1.37 + { "credits", "http://www.mozilla.org/credits/", 1.38 + nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT }, 1.39 + { "mozilla", "chrome://global/content/mozilla.xhtml", 1.40 + nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT }, 1.41 + { "plugins", "chrome://global/content/plugins.html", 0 }, 1.42 + { "config", "chrome://global/content/config.xul", 0 }, 1.43 +#ifdef MOZ_CRASHREPORTER 1.44 + { "crashes", "chrome://global/content/crashes.xhtml", 0 }, 1.45 +#endif 1.46 + { "logo", "chrome://branding/content/about.png", 1.47 + nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT}, 1.48 + { "buildconfig", "chrome://global/content/buildconfig.html", 1.49 + nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT }, 1.50 + { "license", "chrome://global/content/license.html", 1.51 + nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT }, 1.52 + { "neterror", "chrome://global/content/netError.xhtml", 1.53 + nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT | 1.54 + nsIAboutModule::ALLOW_SCRIPT | 1.55 + nsIAboutModule::HIDE_FROM_ABOUTABOUT }, 1.56 + { "compartments", "chrome://global/content/aboutCompartments.xhtml", 1.57 + nsIAboutModule::ALLOW_SCRIPT | 1.58 + nsIAboutModule::HIDE_FROM_ABOUTABOUT }, 1.59 + { "memory", "chrome://global/content/aboutMemory.xhtml", 1.60 + nsIAboutModule::ALLOW_SCRIPT }, 1.61 + { "addons", "chrome://mozapps/content/extensions/extensions.xul", 1.62 + nsIAboutModule::ALLOW_SCRIPT }, 1.63 + { "newaddon", "chrome://mozapps/content/extensions/newaddon.xul", 1.64 + nsIAboutModule::ALLOW_SCRIPT | 1.65 + nsIAboutModule::HIDE_FROM_ABOUTABOUT }, 1.66 + { "support", "chrome://global/content/aboutSupport.xhtml", 1.67 + nsIAboutModule::ALLOW_SCRIPT }, 1.68 + { "telemetry", "chrome://global/content/aboutTelemetry.xhtml", 1.69 + nsIAboutModule::ALLOW_SCRIPT }, 1.70 + { "networking", "chrome://global/content/aboutNetworking.xhtml", 1.71 + nsIAboutModule::ALLOW_SCRIPT }, 1.72 + { "webrtc", "chrome://global/content/aboutWebrtc.xhtml", 1.73 + nsIAboutModule::ALLOW_SCRIPT }, 1.74 + // about:srcdoc is unresolvable by specification. It is included here 1.75 + // because the security manager would disallow srcdoc iframes otherwise. 1.76 + { "srcdoc", "about:blank", 1.77 + nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT | 1.78 + nsIAboutModule::HIDE_FROM_ABOUTABOUT } 1.79 +}; 1.80 +static const int kRedirTotal = mozilla::ArrayLength(kRedirMap); 1.81 + 1.82 +NS_IMETHODIMP 1.83 +nsAboutRedirector::NewChannel(nsIURI *aURI, nsIChannel **result) 1.84 +{ 1.85 + NS_ENSURE_ARG_POINTER(aURI); 1.86 + NS_ASSERTION(result, "must not be null"); 1.87 + 1.88 + nsresult rv; 1.89 + 1.90 + nsAutoCString path; 1.91 + rv = NS_GetAboutModuleName(aURI, path); 1.92 + if (NS_FAILED(rv)) 1.93 + return rv; 1.94 + 1.95 + nsCOMPtr<nsIIOService> ioService = do_GetIOService(&rv); 1.96 + if (NS_FAILED(rv)) 1.97 + return rv; 1.98 + 1.99 + for (int i=0; i<kRedirTotal; i++) 1.100 + { 1.101 + if (!strcmp(path.get(), kRedirMap[i].id)) 1.102 + { 1.103 + nsCOMPtr<nsIChannel> tempChannel; 1.104 + rv = ioService->NewChannel(nsDependentCString(kRedirMap[i].url), 1.105 + nullptr, nullptr, getter_AddRefs(tempChannel)); 1.106 + if (NS_FAILED(rv)) 1.107 + return rv; 1.108 + 1.109 + tempChannel->SetOriginalURI(aURI); 1.110 + 1.111 + NS_ADDREF(*result = tempChannel); 1.112 + return rv; 1.113 + } 1.114 + } 1.115 + 1.116 + NS_ERROR("nsAboutRedirector called for unknown case"); 1.117 + return NS_ERROR_ILLEGAL_VALUE; 1.118 +} 1.119 + 1.120 +NS_IMETHODIMP 1.121 +nsAboutRedirector::GetURIFlags(nsIURI *aURI, uint32_t *result) 1.122 +{ 1.123 + NS_ENSURE_ARG_POINTER(aURI); 1.124 + 1.125 + nsAutoCString name; 1.126 + nsresult rv = NS_GetAboutModuleName(aURI, name); 1.127 + NS_ENSURE_SUCCESS(rv, rv); 1.128 + 1.129 + for (int i=0; i < kRedirTotal; i++) 1.130 + { 1.131 + if (name.EqualsASCII(kRedirMap[i].id)) 1.132 + { 1.133 + *result = kRedirMap[i].flags; 1.134 + return NS_OK; 1.135 + } 1.136 + } 1.137 + 1.138 + NS_ERROR("nsAboutRedirector called for unknown case"); 1.139 + return NS_ERROR_ILLEGAL_VALUE; 1.140 +} 1.141 + 1.142 +nsresult 1.143 +nsAboutRedirector::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult) 1.144 +{ 1.145 + nsAboutRedirector* about = new nsAboutRedirector(); 1.146 + if (about == nullptr) 1.147 + return NS_ERROR_OUT_OF_MEMORY; 1.148 + NS_ADDREF(about); 1.149 + nsresult rv = about->QueryInterface(aIID, aResult); 1.150 + NS_RELEASE(about); 1.151 + return rv; 1.152 +}