简体   繁体   English

链接 filter_input() 和 filter_var() 会导致问题吗?

[英]Does chaining filter_input() and filter_var() cause the problem?

I have this url for testing我有这个 url 用于测试

http://localhost:8000/2.php?site=https://google.com<script></script>

Then I am sanitizing "site":然后我正在清理“网站”:

$site = filter_input(INPUT_GET, 'site', FILTER_SANITIZE_URL);
var_dump($site); // string(35) "https://google.com"
echo "<br>";

Getting absolutely safe url.获取绝对安全的网址。 Then validating it:然后验证它:

$siteValidation = filter_var($site, FILTER_VALIDATE_URL);
var_dump($siteValidation); // bool(false)

And validation fails!验证失败! Why?为什么?

在此处输入图像描述

As you can read here , FILTER_SANITIZE_URL does not remove < or > from the string.如您在此处阅读的,FILTER_SANITIZE_URL 不会从字符串中删除 < 或 >。 That's why you see printed:这就是您看到打印的原因:

string(35) "https://google.com"

Clearly the string you see is not 35 chars long (inspect via developer tools in browser and you'll see the "script" part is still there).显然,您看到的字符串不是 35 个字符长(通过浏览器中的开发人员工具检查,您会看到“脚本”部分仍然存在)。

That's why next lines fail to validate.这就是为什么下一行无法验证的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM