PDA

View Full Version : Ext.urlDecode does not handle commuted parameters consistently



levl289
25 Mar 2011, 9:52 AM
There is an issue with Ext.urlDecode wherein serialized parameter order affects the resulting data returned:



var order1 = "foo=&foo=1";
var order2 = "foo=1&foo=";

console.log(Ext.urlDecode(order1));
// "1"
console.log(Ext.urlDecode(order2));
// [ "1", "" ]


The problem lies in the ternary operator within Ext.urlDecode:



urlDecode : function(string, overwrite){
if(Ext.isEmpty(string)){
return {};
}
var obj = {},
pairs = string.split('&'),
d = decodeURIComponent,
name,
value;
Ext.each(pairs, function(pair) {
pair = pair.split('=');
name = d(pair[0]);
value = d(pair[1]);

// The following ternary is performing two checks - one of them incorrectly:
// if obj has a "name" property with a false value, it will be overwritten.
obj[name] = overwrite || !obj[name] ? value :
[].concat(obj[name]).concat(value);
});
return obj;
}


The solution is to use the hasOwnProperty method:



urlDecode : function(string, overwrite){
if(Ext.isEmpty(string)){
return {};
}
var obj = {},
pairs = string.split('&'),
d = decodeURIComponent,
name,
value;
Ext.each(pairs, function(pair) {
pair = pair.split('=');
name = d(pair[0]);
value = d(pair[1]);
obj[name] = overwrite || !obj.hasOwnProperty(name) ? value :
[].concat(obj[name]).concat(value);
});
return obj;
}


This bug can lead to unexpected serialization and correlation errors in the case that there are several rows of input elements. An working example of this is available here:

http://jsfiddle.net/levl289/Df53g/