michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // See also: docshell/base/nsAboutRedirector.cpp michael@0: michael@0: #include "AboutRedirector.h" michael@0: #include "nsNetUtil.h" michael@0: #include "nsIScriptSecurityManager.h" michael@0: #include "mozilla/ArrayUtils.h" michael@0: michael@0: namespace mozilla { michael@0: namespace browser { michael@0: michael@0: NS_IMPL_ISUPPORTS(AboutRedirector, nsIAboutModule) michael@0: michael@0: struct RedirEntry { michael@0: const char* id; michael@0: const char* url; michael@0: uint32_t flags; michael@0: }; michael@0: michael@0: /* michael@0: Entries which do not have URI_SAFE_FOR_UNTRUSTED_CONTENT will run with chrome michael@0: privileges. This is potentially dangerous. Please use michael@0: URI_SAFE_FOR_UNTRUSTED_CONTENT in the third argument to each map item below michael@0: unless your about: page really needs chrome privileges. Security review is michael@0: required before adding new map entries without michael@0: URI_SAFE_FOR_UNTRUSTED_CONTENT. Also note, however, that adding michael@0: URI_SAFE_FOR_UNTRUSTED_CONTENT will allow random web sites to link to that michael@0: URI. Perhaps we should separate the two concepts out... michael@0: */ michael@0: static RedirEntry kRedirMap[] = { michael@0: #ifdef MOZ_SAFE_BROWSING michael@0: { "blocked", "chrome://browser/content/blockedSite.xhtml", michael@0: nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT | michael@0: nsIAboutModule::ALLOW_SCRIPT | michael@0: nsIAboutModule::HIDE_FROM_ABOUTABOUT }, michael@0: #endif michael@0: { "certerror", "chrome://browser/content/certerror/aboutCertError.xhtml", michael@0: nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT | michael@0: nsIAboutModule::ALLOW_SCRIPT | michael@0: nsIAboutModule::HIDE_FROM_ABOUTABOUT }, michael@0: { "socialerror", "chrome://browser/content/aboutSocialError.xhtml", michael@0: nsIAboutModule::ALLOW_SCRIPT | michael@0: nsIAboutModule::HIDE_FROM_ABOUTABOUT }, michael@0: { "tabcrashed", "chrome://browser/content/aboutTabCrashed.xhtml", michael@0: nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT | michael@0: nsIAboutModule::ALLOW_SCRIPT | michael@0: nsIAboutModule::HIDE_FROM_ABOUTABOUT }, michael@0: { "feeds", "chrome://browser/content/feeds/subscribe.xhtml", michael@0: nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT | michael@0: nsIAboutModule::ALLOW_SCRIPT | michael@0: nsIAboutModule::HIDE_FROM_ABOUTABOUT }, michael@0: { "privatebrowsing", "chrome://browser/content/aboutPrivateBrowsing.xhtml", michael@0: nsIAboutModule::ALLOW_SCRIPT }, michael@0: { "rights", michael@0: #ifdef MOZ_OFFICIAL_BRANDING michael@0: "chrome://global/content/aboutRights.xhtml", michael@0: #else michael@0: "chrome://global/content/aboutRights-unbranded.xhtml", michael@0: #endif michael@0: nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT | michael@0: nsIAboutModule::ALLOW_SCRIPT }, michael@0: { "robots", "chrome://browser/content/aboutRobots.xhtml", michael@0: nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT | michael@0: nsIAboutModule::ALLOW_SCRIPT }, michael@0: { "sessionrestore", "chrome://browser/content/aboutSessionRestore.xhtml", michael@0: nsIAboutModule::ALLOW_SCRIPT }, michael@0: { "welcomeback", "chrome://browser/content/aboutWelcomeBack.xhtml", michael@0: nsIAboutModule::ALLOW_SCRIPT }, michael@0: #ifdef MOZ_SERVICES_SYNC michael@0: { "sync-progress", "chrome://browser/content/sync/progress.xhtml", michael@0: nsIAboutModule::ALLOW_SCRIPT }, michael@0: { "sync-tabs", "chrome://browser/content/sync/aboutSyncTabs.xul", michael@0: nsIAboutModule::ALLOW_SCRIPT }, michael@0: #endif michael@0: { "home", "chrome://browser/content/abouthome/aboutHome.xhtml", michael@0: nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT | michael@0: nsIAboutModule::ALLOW_SCRIPT }, michael@0: { "newtab", "chrome://browser/content/newtab/newTab.xul", michael@0: nsIAboutModule::ALLOW_SCRIPT }, michael@0: { "permissions", "chrome://browser/content/preferences/aboutPermissions.xul", michael@0: nsIAboutModule::ALLOW_SCRIPT }, michael@0: { "preferences", "chrome://browser/content/preferences/in-content/preferences.xul", michael@0: nsIAboutModule::ALLOW_SCRIPT }, michael@0: { "downloads", "chrome://browser/content/downloads/contentAreaDownloadsView.xul", michael@0: nsIAboutModule::ALLOW_SCRIPT }, michael@0: #ifdef MOZ_SERVICES_HEALTHREPORT michael@0: { "healthreport", "chrome://browser/content/abouthealthreport/abouthealth.xhtml", michael@0: nsIAboutModule::ALLOW_SCRIPT }, michael@0: #endif michael@0: { "accounts", "chrome://browser/content/aboutaccounts/aboutaccounts.xhtml", michael@0: nsIAboutModule::ALLOW_SCRIPT }, michael@0: { "app-manager", "chrome://browser/content/devtools/app-manager/index.xul", michael@0: nsIAboutModule::ALLOW_SCRIPT }, michael@0: { "customizing", "chrome://browser/content/customizableui/aboutCustomizing.xul", michael@0: nsIAboutModule::ALLOW_SCRIPT }, michael@0: }; michael@0: static const int kRedirTotal = ArrayLength(kRedirMap); michael@0: michael@0: static nsAutoCString michael@0: GetAboutModuleName(nsIURI *aURI) michael@0: { michael@0: nsAutoCString path; michael@0: aURI->GetPath(path); michael@0: michael@0: int32_t f = path.FindChar('#'); michael@0: if (f >= 0) michael@0: path.SetLength(f); michael@0: michael@0: f = path.FindChar('?'); michael@0: if (f >= 0) michael@0: path.SetLength(f); michael@0: michael@0: ToLowerCase(path); michael@0: return path; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: AboutRedirector::NewChannel(nsIURI *aURI, nsIChannel **result) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aURI); michael@0: NS_ASSERTION(result, "must not be null"); michael@0: michael@0: nsAutoCString path = GetAboutModuleName(aURI); michael@0: michael@0: nsresult rv; michael@0: nsCOMPtr ioService = do_GetIOService(&rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: for (int i = 0; i < kRedirTotal; i++) { michael@0: if (!strcmp(path.get(), kRedirMap[i].id)) { michael@0: nsCOMPtr tempChannel; michael@0: rv = ioService->NewChannel(nsDependentCString(kRedirMap[i].url), michael@0: nullptr, nullptr, getter_AddRefs(tempChannel)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: tempChannel->SetOriginalURI(aURI); michael@0: michael@0: NS_ADDREF(*result = tempChannel); michael@0: return rv; michael@0: } michael@0: } michael@0: michael@0: return NS_ERROR_ILLEGAL_VALUE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: AboutRedirector::GetURIFlags(nsIURI *aURI, uint32_t *result) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aURI); michael@0: michael@0: nsAutoCString name = GetAboutModuleName(aURI); michael@0: michael@0: for (int i = 0; i < kRedirTotal; i++) { michael@0: if (name.Equals(kRedirMap[i].id)) { michael@0: *result = kRedirMap[i].flags; michael@0: return NS_OK; michael@0: } michael@0: } michael@0: michael@0: return NS_ERROR_ILLEGAL_VALUE; michael@0: } michael@0: michael@0: nsresult michael@0: AboutRedirector::Create(nsISupports *aOuter, REFNSIID aIID, void **result) michael@0: { michael@0: AboutRedirector* about = new AboutRedirector(); michael@0: if (about == nullptr) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: NS_ADDREF(about); michael@0: nsresult rv = about->QueryInterface(aIID, result); michael@0: NS_RELEASE(about); michael@0: return rv; michael@0: } michael@0: michael@0: } // namespace browser michael@0: } // namespace mozilla