From 6053848bf6b7c764d1809e4d4d96c4e5b89fcaf3 Mon Sep 17 00:00:00 2001 From: abhishekmadan30 <148475204+abhishekmadan30@users.noreply.github.com> Date: Tue, 26 Mar 2024 14:42:07 +0000 Subject: [PATCH] Bug 1884941 - Add an option to URLParams::Parse() for if it should decode the parameters or not. r=anti-tracking-reviewers,necko-reviewers,valentin,timhuang. Differential Revision: https://phabricator.services.mozilla.com/D204914 --- caps/OriginAttributes.cpp | 2 +- dom/base/BodyUtil.cpp | 2 +- dom/quota/ActorsParent.cpp | 2 +- dom/quota/StorageOriginAttributes.cpp | 2 +- netwerk/base/nsURLHelper.cpp | 17 +++++++----- netwerk/base/nsURLHelper.h | 15 ++++++----- storage/mozStorageConnection.cpp | 27 ++++++++++--------- .../antitracking/URLQueryStringStripper.cpp | 4 +-- toolkit/components/places/SQLFunctions.cpp | 2 +- 9 files changed, 40 insertions(+), 33 deletions(-) diff --git a/caps/OriginAttributes.cpp b/caps/OriginAttributes.cpp index 8540d2a12a47..211d6b00e7f5 100644 --- a/caps/OriginAttributes.cpp +++ b/caps/OriginAttributes.cpp @@ -324,7 +324,7 @@ bool OriginAttributes::PopulateFromSuffix(const nsACString& aStr) { MOZ_RELEASE_ASSERT(mPartitionKey.IsEmpty()); return URLParams::Parse( - Substring(aStr, 1, aStr.Length() - 1), + Substring(aStr, 1, aStr.Length() - 1), true, [this](const nsAString& aName, const nsAString& aValue) { if (aName.EqualsLiteral("inBrowser")) { if (!aValue.EqualsLiteral("1")) { diff --git a/dom/base/BodyUtil.cpp b/dom/base/BodyUtil.cpp index 1f35c895c7b5..e8de3d18ec00 100644 --- a/dom/base/BodyUtil.cpp +++ b/dom/base/BodyUtil.cpp @@ -422,7 +422,7 @@ already_AddRefed BodyUtil::ConsumeFormData( if (isValidUrlEncodedMimeType) { RefPtr fd = new FormData(aParent); DebugOnly status = URLParams::Parse( - aStr, [&fd](const nsAString& aName, const nsAString& aValue) { + aStr, true, [&fd](const nsAString& aName, const nsAString& aValue) { ErrorResult rv; fd->Append(aName, aValue, rv); MOZ_ASSERT(!rv.Failed()); diff --git a/dom/quota/ActorsParent.cpp b/dom/quota/ActorsParent.cpp index 4c671e703002..e7cf88bc915a 100644 --- a/dom/quota/ActorsParent.cpp +++ b/dom/quota/ActorsParent.cpp @@ -7658,7 +7658,7 @@ Result UpgradeStorageFrom1_0To2_0Helper::MaybeRemoveAppsData( MOZ_ASSERT(originalSuffix[0] == '^'); if (!URLParams::Parse( - Substring(originalSuffix, 1, originalSuffix.Length() - 1), + Substring(originalSuffix, 1, originalSuffix.Length() - 1), true, [](const nsAString& aName, const nsAString& aValue) { if (aName.EqualsLiteral("appId")) { return false; diff --git a/dom/quota/StorageOriginAttributes.cpp b/dom/quota/StorageOriginAttributes.cpp index c07b4d04bbab..bcdf47bce805 100644 --- a/dom/quota/StorageOriginAttributes.cpp +++ b/dom/quota/StorageOriginAttributes.cpp @@ -68,7 +68,7 @@ bool StorageOriginAttributes::PopulateFromSuffix(const nsACString& aStr) { } bool ok = - URLParams::Parse(Substring(aStr, 1, aStr.Length() - 1), + URLParams::Parse(Substring(aStr, 1, aStr.Length() - 1), true, [this](const nsAString& aName, const nsAString& aValue) { if (aName.EqualsLiteral("inBrowser")) { if (!aValue.EqualsLiteral("1")) { diff --git a/netwerk/base/nsURLHelper.cpp b/netwerk/base/nsURLHelper.cpp index 3850c6865a74..ca82b0cd0083 100644 --- a/netwerk/base/nsURLHelper.cpp +++ b/netwerk/base/nsURLHelper.cpp @@ -1236,8 +1236,8 @@ void URLParams::DecodeString(const nsACString& aInput, nsAString& aOutput) { /* static */ bool URLParams::ParseNextInternal(const char*& aStart, const char* const aEnd, - nsAString* aOutDecodedName, - nsAString* aOutDecodedValue) { + bool aShouldDecode, nsAString* aOutputName, + nsAString* aOutputValue) { nsDependentCSubstring string; const char* const iter = std::find(aStart, aEnd, '&'); @@ -1267,9 +1267,14 @@ bool URLParams::ParseNextInternal(const char*& aStart, const char* const aEnd, name.Rebind(string, 0); } - DecodeString(name, *aOutDecodedName); - DecodeString(value, *aOutDecodedValue); + if (aShouldDecode) { + DecodeString(name, *aOutputName); + DecodeString(value, *aOutputValue); + return true; + } + ConvertString(name, *aOutputName); + ConvertString(value, *aOutputValue); return true; } @@ -1278,7 +1283,7 @@ bool URLParams::Extract(const nsACString& aInput, const nsAString& aName, nsAString& aValue) { aValue.SetIsVoid(true); return !URLParams::Parse( - aInput, [&aName, &aValue](const nsAString& name, nsString&& value) { + aInput, true, [&aName, &aValue](const nsAString& name, nsString&& value) { if (aName == name) { aValue = std::move(value); return false; @@ -1291,7 +1296,7 @@ void URLParams::ParseInput(const nsACString& aInput) { // Remove all the existing data before parsing a new input. DeleteAll(); - URLParams::Parse(aInput, [this](nsString&& name, nsString&& value) { + URLParams::Parse(aInput, true, [this](nsString&& name, nsString&& value) { mParams.AppendElement(Param{std::move(name), std::move(value)}); return true; }); diff --git a/netwerk/base/nsURLHelper.h b/netwerk/base/nsURLHelper.h index f5ccc8bac663..36844e6e4b10 100644 --- a/netwerk/base/nsURLHelper.h +++ b/netwerk/base/nsURLHelper.h @@ -257,19 +257,20 @@ class URLParams final { * true otherwise */ template - static bool Parse(const nsACString& aInput, ParamHandler aParamHandler) { + static bool Parse(const nsACString& aInput, bool aShouldDecode, + ParamHandler aParamHandler) { const char* start = aInput.BeginReading(); const char* const end = aInput.EndReading(); while (start != end) { - nsAutoString decodedName; - nsAutoString decodedValue; + nsAutoString name; + nsAutoString value; - if (!ParseNextInternal(start, end, &decodedName, &decodedValue)) { + if (!ParseNextInternal(start, end, aShouldDecode, &name, &value)) { continue; } - if (!aParamHandler(std::move(decodedName), std::move(decodedValue))) { + if (!aParamHandler(std::move(name), std::move(value))) { return false; } } @@ -357,8 +358,8 @@ class URLParams final { static void DecodeString(const nsACString& aInput, nsAString& aOutput); static void ConvertString(const nsACString& aInput, nsAString& aOutput); static bool ParseNextInternal(const char*& aStart, const char* aEnd, - nsAString* aOutDecodedName, - nsAString* aOutDecodedValue); + bool aShouldDecode, nsAString* aOutputName, + nsAString* aOutputValue); struct Param { nsString mKey; diff --git a/storage/mozStorageConnection.cpp b/storage/mozStorageConnection.cpp index dc3df7c95229..5dd2c9223e0c 100644 --- a/storage/mozStorageConnection.cpp +++ b/storage/mozStorageConnection.cpp @@ -1155,19 +1155,20 @@ nsresult Connection::initialize(nsIFileURL* aFileURL) { bool hasKey = false; bool hasDirectoryLockId = false; - MOZ_ALWAYS_TRUE(URLParams::Parse( - query, [&hasKey, &hasDirectoryLockId](const nsAString& aName, - const nsAString& aValue) { - if (aName.EqualsLiteral("key")) { - hasKey = true; - return true; - } - if (aName.EqualsLiteral("directoryLockId")) { - hasDirectoryLockId = true; - return true; - } - return true; - })); + MOZ_ALWAYS_TRUE( + URLParams::Parse(query, true, + [&hasKey, &hasDirectoryLockId](const nsAString& aName, + const nsAString& aValue) { + if (aName.EqualsLiteral("key")) { + hasKey = true; + return true; + } + if (aName.EqualsLiteral("directoryLockId")) { + hasDirectoryLockId = true; + return true; + } + return true; + })); bool exclusive = StaticPrefs::storage_sqlite_exclusiveLock_enabled(); diff --git a/toolkit/components/antitracking/URLQueryStringStripper.cpp b/toolkit/components/antitracking/URLQueryStringStripper.cpp index 3b4673828007..2e154b910381 100644 --- a/toolkit/components/antitracking/URLQueryStringStripper.cpp +++ b/toolkit/components/antitracking/URLQueryStringStripper.cpp @@ -98,7 +98,7 @@ URLQueryStringStripper::StripForCopyOrShare(nsIURI* aURI, URLParams params; - URLParams::Parse(query, [&](nsString&& name, nsString&& value) { + URLParams::Parse(query, true, [&](nsString&& name, nsString&& value) { nsAutoString lowerCaseName; ToLowerCase(name, lowerCaseName); // Look through the global rules. @@ -308,7 +308,7 @@ nsresult URLQueryStringStripper::StripQueryString(nsIURI* aURI, URLParams params; - URLParams::Parse(query, [&](nsString&& name, nsString&& value) { + URLParams::Parse(query, false, [&](nsString&& name, nsString&& value) { nsAutoString lowerCaseName; ToLowerCase(name, lowerCaseName); diff --git a/toolkit/components/places/SQLFunctions.cpp b/toolkit/components/places/SQLFunctions.cpp index 6803693728ed..85c5cc8d1716 100644 --- a/toolkit/components/places/SQLFunctions.cpp +++ b/toolkit/components/places/SQLFunctions.cpp @@ -1135,7 +1135,7 @@ GetQueryParamFunction::OnFunctionCall(mozIStorageValueArray* aArguments, RefPtr result = new nsVariant(); if (!queryString.IsEmpty() && !paramName.IsEmpty()) { URLParams::Parse( - queryString, + queryString, true, [¶mName, &result](const nsAString& aName, const nsAString& aValue) { NS_ConvertUTF16toUTF8 name(aName); if (!paramName.Equals(name)) {