1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#include "URLQueryStringStripper.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/StaticPrefs_privacy.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/Unused.h"
#include "nsEffectiveTLDService.h"
#include "nsISupportsImpl.h"
#include "nsIURI.h"
#include "nsIURIMutator.h"
#include "nsUnicharUtils.h"
#include "nsURLHelper.h"
namespace {
mozilla::StaticRefPtr<mozilla::URLQueryStringStripper> gQueryStringStripper;
} // namespace
namespace mozilla {
NS_IMPL_ISUPPORTS(URLQueryStringStripper, nsIURLQueryStrippingListObserver)
URLQueryStringStripper* URLQueryStringStripper::GetOrCreate() {
if (!gQueryStringStripper) {
gQueryStringStripper = new URLQueryStringStripper();
gQueryStringStripper->Init();
RunOnShutdown([&] {
gQueryStringStripper->Shutdown();
gQueryStringStripper = nullptr;
});
}
return gQueryStringStripper;
}
/* static */
bool URLQueryStringStripper::Strip(nsIURI* aURI, nsCOMPtr<nsIURI>& aOutput) {
if (!StaticPrefs::privacy_query_stripping_enabled()) {
return false;
}
RefPtr<URLQueryStringStripper> stripper = GetOrCreate();
if (stripper->CheckAllowList(aURI)) {
return false;
}
return stripper->StripQueryString(aURI, aOutput);
}
void URLQueryStringStripper::Init() {
mService = do_GetService("@mozilla.org/query-stripping-list-service;1");
NS_ENSURE_TRUE_VOID(mService);
mService->RegisterAndRunObserver(this);
}
void URLQueryStringStripper::Shutdown() {
mList.Clear();
mAllowList.Clear();
mService->UnregisterObserver(this);
mService = nullptr;
}
bool URLQueryStringStripper::StripQueryString(nsIURI* aURI,
nsCOMPtr<nsIURI>& aOutput) {
MOZ_ASSERT(aURI);
nsCOMPtr<nsIURI> uri(aURI);
nsAutoCString query;
nsresult rv = aURI->GetQuery(query);
NS_ENSURE_SUCCESS(rv, false);
// We don't need to do anything if there is no query string.
if (query.IsEmpty()) {
return false;
}
URLParams params;
bool hasStripped = false;
URLParams::Parse(query, [&](nsString&& name, nsString&& value) {
nsAutoString lowerCaseName;
ToLowerCase(name, lowerCaseName);
if (mList.Contains(lowerCaseName)) {
hasStripped = true;
return true;
}
params.Append(name, value);
return true;
});
// Return if there is no parameter has been stripped.
if (!hasStripped) {
return false;
}
nsAutoString newQuery;
params.Serialize(newQuery);
Unused << NS_MutateURI(uri)
.SetQuery(NS_ConvertUTF16toUTF8(newQuery))
.Finalize(aOutput);
return true;
}
bool URLQueryStringStripper::CheckAllowList(nsIURI* aURI) {
MOZ_ASSERT(aURI);
// Get the site(eTLD+1) from the URI.
nsAutoCString baseDomain;
nsresult rv =
nsEffectiveTLDService::GetInstance()->GetBaseDomain(aURI, 0, baseDomain);
NS_ENSURE_SUCCESS(rv, false);
return mAllowList.Contains(baseDomain);
}
void URLQueryStringStripper::PopulateStripList(const nsAString& aList) {
mList.Clear();
for (const nsAString& item : aList.Split(' ')) {
mList.Insert(item);
}
}
void URLQueryStringStripper::PopulateAllowList(const nsACString& aList) {
mAllowList.Clear();
for (const nsACString& item : aList.Split(',')) {
mAllowList.Insert(item);
}
}
NS_IMETHODIMP
URLQueryStringStripper::OnQueryStrippingListUpdate(
const nsAString& aStripList, const nsACString& aAllowList) {
PopulateStripList(aStripList);
PopulateAllowList(aAllowList);
return NS_OK;
}
} // namespace mozilla
|