Catch malformed URI Components

Fix for error thrown when a value contains non-valid / malformed URI Component
Example: test=%c5%a1%e8ZM%80%82H
will throw "URI malformed"
This update will leave the value as-is when an error is thrown
This commit is contained in:
Jeroen Diderik 2021-11-24 11:13:27 +01:00 committed by Stephan Hoyer
parent 1eec62eb3e
commit fae15d3489
2 changed files with 14 additions and 2 deletions

View file

@ -1,5 +1,13 @@
"use strict"
function decodeURIComponentSave(str) {
try {
return decodeURIComponent(str)
} catch(err) {
return str
}
}
module.exports = function(string) {
if (string === "" || string == null) return {}
if (string.charAt(0) === "?") string = string.slice(1)
@ -7,8 +15,8 @@ module.exports = function(string) {
var entries = string.split("&"), counters = {}, data = {}
for (var i = 0; i < entries.length; i++) {
var entry = entries[i].split("=")
var key = decodeURIComponent(entry[0])
var value = entry.length === 2 ? decodeURIComponent(entry[1]) : ""
var key = decodeURIComponentSave(entry[0])
var value = entry.length === 2 ? decodeURIComponentSave(entry[1]) : ""
if (value === "true") value = true
else if (value === "false") value = false