PHP警告:strtolower()期望参数1是字符串,数组给定



我的网站上有一个querystring变量白名单检查器。它根据允许/允许的querystring变量(键和值)检查给定的url。第一部分根据允许的键检查querystring键—如果键不允许(不在白名单中),它将拒绝querystring。第二部分检查"值"。查询字符串键/值的一部分,用于查看该值是否包含黑名单中的坏单词-如果该值在黑名单中,它将拒绝查询字符串。

这似乎工作得很好,但我从服务器日志中注意到,将querystring值转换为小写的行会导致PHP警告:

PHP警告:strtolower()期望参数1是字符串,数组鉴于

这是代码:

$rejectqstr = "N";
//loop through the querystring, check each querystring KEY against the three whitelist arrays. Any key found which is not in the list will set the reject variable to Y
foreach ($_GET as $key => $value){
if(in_array($key, $cmsnumparams) || in_array($key, $cmsstrparams) || in_array($key, $cmsboolparams)){
//do nothing if it found
} else {
$rejectqstr = "Y";
}
//loop through the blacklist values and check each querystring value against the list
$value = strtolower($value);
foreach($cmsblklstparams as $blklist){
if(strpos($value, $blklist) !== false){
$rejectqstr = "Y";
}
}
}

在服务器日志中作为警告记录的$value = strtolower($value);行。

我看不出这有什么问题。我的黑名单数组(cmsblklstparams)都是小写的,所以在查看它是否在黑名单数组中之前,我将querystring值转换为小写。

这个警告并没有一直列在服务器日志中,所以我猜这可能是由于用户试图"注入"。在查询字符串的东西(改变它从一个字符串到一个数组)?

是否有更好的方法来做到这一点,或者我应该检查如果$value是一个数组(如果是,拒绝querystring),然后将其转换为小写?

我试着用正常的查询字符串测试代码,它似乎工作得很好,所以我不确定它被添加到查询字符串中导致服务器日志中的此警告。

许多谢谢。

我猜这可能是由于用户试图"注射"。在查询字符串的东西(改变它从一个字符串到一个数组)?

我同意,不管是有意还是无意。以以下HTML表单为例:
<form action="my-request.php" method="GET">
<label for="checkbox-setting1">
Setting 1
<input type="checkbox" id="checkbox-setting1" name="setting[]" value="setting1" />
</label>
<label for="checkbox-setting2">
Setting 2
<input type="checkbox" id="checkbox-setting2" name="setting[]" value="setting2" />
</label>
<label for="checkbox-setting3">
Setting 3
<input type="checkbox" id="checkbox-setting3" name="setting[]" value="setting3" />
</label>
<button type="submit">Search</button>
</form>

假设提交此表单时每个复选框都被选中,它将使用URL.../my-request.php?setting[]=setting1&setting[]=setting2&setting[]=setting3

发出请求这是一个有效的GET请求,PHP将把setting查询字符串参数解释为一个数组。当前代码的设置方式是,它总是假设传入的查询字符串参数将是一个string。

听起来你想做一个类型检查来检查当前迭代的查询字符串参数是否是一个数组。如果是,并且您不允许使用数组,则执行拒绝。如果是,并且你允许数组,那么设置一个内部循环:

// do not allow query string parameter values
if (is_array($value))
{
$rejectqstr = "Y";
}
// or allow them
foreach($cmsblklstparams as $blklist)
{
if (is_array($value))
{
foreach($value as $innerValue)
{
if(strpos($innerValue, $blklist) !== false)
{
$rejectqstr = "Y";
}
}
}
else if(strpos($value, $blklist) !== false)
{
$rejectqstr = "Y";
}
}

最新更新