RequestPolicy/requestpolicy — Issue #239: Fails to load policy from prefs with leading space, results in data loss
Author: jsamuel
Created: 2011-12-22 06:03:54+00:00
State: open
Labels: extension

The _prefStringToObj() function splits to a string on [space] and exits if the first element in the split array is an empty string.
{{{
_prefStringToObj : function(prefString) {
var prefObj = {};
var prefArray = prefString.split(" ");
if (prefArray[0] != "") {
for (var i in prefArray) {
prefObj[prefArray[i]] = true;
}
}
return prefObj;
},
}}}
As implemented, any pref with a leading space will not be loaded.
{{{
var prefString = ' example.com example.net example.org';
// => prefObj = {};
// vs prefObj['example.com'] = true;
// prefObj['example.net'] = true;
// prefObj['example.org'] = true;
}}}
Further, any double space in any pref will lead to the empty string being added as a property of the object.
{{{
var prefString = 'example.com example.net example.org';
// prefObj['example.com'] = true;
// prefObj[''] = true; // this was not intended!
// prefObj['example.net'] = true;
// prefObj['example.org'] = true;
}}}
Additionally, this fails to accommodate other whitespace characters, eg:
{{{
var prefString = "example.com\texample.net\texample.org";
// => prefObj["example.com\texample.net\texample.org"] = true;
// vs prefObj['example.com'] = true;
// prefObj['example.net'] = true;
// prefObj['example.org'] = true;
}}}
Instead, just remove leading and trailing whitespace, split on the remaining whitespace, and add each element as properties of the object.
{{{
_prefStringToObj : function(prefString) {
var prefObj = {};
var prefArray = prefString.replace(/^\s+/, '').replace(/\s+$/, '').split(/\s+/);
for (var i=prefArray.length;i--;) prefObj[prefArray[i]] = true;
return prefObj;
},
}}}
This is a serious defect as-is. When blacklist support is added, this will be a grave defect.