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
This commit is contained in:
abhishekmadan30 2024-03-26 14:42:07 +00:00
parent f81c16c09c
commit 6053848bf6
9 changed files with 40 additions and 33 deletions

View File

@ -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")) {

View File

@ -422,7 +422,7 @@ already_AddRefed<FormData> BodyUtil::ConsumeFormData(
if (isValidUrlEncodedMimeType) {
RefPtr<FormData> fd = new FormData(aParent);
DebugOnly<bool> 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());

View File

@ -7658,7 +7658,7 @@ Result<bool, nsresult> 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;

View File

@ -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")) {

View File

@ -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;
});

View File

@ -257,19 +257,20 @@ class URLParams final {
* true otherwise
*/
template <typename ParamHandler>
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;

View File

@ -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();

View File

@ -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);

View File

@ -1135,7 +1135,7 @@ GetQueryParamFunction::OnFunctionCall(mozIStorageValueArray* aArguments,
RefPtr<nsVariant> result = new nsVariant();
if (!queryString.IsEmpty() && !paramName.IsEmpty()) {
URLParams::Parse(
queryString,
queryString, true,
[&paramName, &result](const nsAString& aName, const nsAString& aValue) {
NS_ConvertUTF16toUTF8 name(aName);
if (!paramName.Equals(name)) {