Change the parseQueryString function to return a Map rather than an Object (issue 13829)

Even though the code as-is *should* be safe, given that we're using an Object with a `null` prototype, it cannot hurt to change this to a Map to prevent any issues (since we're parsing unknown and potentially unsafe data).

Overall I also think that these changes improve the `parseQueryString` call-sites, since we now have a proper way of checking for the existence of a particular key (and don't have to use `in` which stringifies the keys in the Object).

This patch also changes the default, when no `value` exists, from `null` to an empty string since the use of `decodeURIComponent` currently can modify the value in a somewhat surprising way (at least to me).
Note how `decodeURIComponent(null) === "null"` which is unlikely to be what you actually want, whereas `decodeURIComponent("") === ""` which seems much more helpful.
This commit is contained in:
Jonas Jenwald 2021-07-31 22:15:30 +02:00
parent 222c9e7e84
commit 4ab4efd42f
4 changed files with 47 additions and 42 deletions

View file

@ -180,16 +180,18 @@ function watchScroll(viewAreaElement, callback) {
}
/**
* Helper function to parse query string (e.g. ?param1=value&parm2=...).
* Helper function to parse query string (e.g. ?param1=value&param2=...).
* @param {string}
* @returns {Map}
*/
function parseQueryString(query) {
const parts = query.split("&");
const params = Object.create(null);
const params = new Map();
for (let i = 0, ii = parts.length; i < ii; ++i) {
const param = parts[i].split("=");
const key = param[0].toLowerCase();
const value = param.length > 1 ? param[1] : null;
params[decodeURIComponent(key)] = decodeURIComponent(value);
const param = parts[i].split("="),
key = param[0].toLowerCase(),
value = param.length > 1 ? param[1] : "";
params.set(decodeURIComponent(key), decodeURIComponent(value));
}
return params;
}