清理用户输入作为 URL 的一部分

发布于 2024-12-04 15:59:05 字数 1829 浏览 1 评论 0原文

我从外部客户端脚本获取一个字符串,稍后必须将其附加为 URL 的一部分。现在我想知道清理此类数据的最佳方法是什么?

我得到的字符串将具有如下结构:
dynamicVal#staticVal:dynamicVal

然后该值将被添加到网址中:
http://the-page.com/dynamicVal#staticVal:dynamicVal

然后按如下方式使用 url:

$link = htmlspecialchars("http://external-page.com/dynamicVal#staticVal:dynamicVal", ENT_QUOTES);
$var = "<a href=\"javascript: window.open('$link')\">'Open URL'</a>";

问题是,htmlspecialchars 不会帮助阻止随机 javascript 代码的执行,例如,将此警报添加到值中:

dynamicVal#staticVal:dynamicVal'+alert(\"breakout\")+'

使用 rawurlencode 也不会有所帮助,因为它不是参数的值,而是 url 的真实部分。
那么,在连接到 url 时清理传递的字符串的最佳方法是什么?

提前致谢。

编辑: 仅在动态部分使用 rawurlencode 实际上也没有解决问题,javascript 仍然被执行。
测试片段:

$splitVal = "#staticVal:";
$tmpArr = explode($splitVal, "dynamicVal#staticVal:dynamicVal'+alert(\"breakout\")+'");
$link = htmlspecialchars(sprintf("http://external-page.com/"."%s$splitVal%s", rawurlencode($tmpArr[0]), rawurlencode($tmpArr[1])), ENT_QUOTES);
echo "<a href=\"javascript: window.open('$link')\">'Open URL'</a>";

编辑2: 将字符串作为 javascript 参数传递时使用 json_encode 也没有帮助。 改编后的测试片段:

$splitVal = "#staticVal:";
$tmpArr = explode($splitVal, "dynamicVal#staticVal:dynamicVal\"+alert('breakout')+\"");
$link = htmlspecialchars(sprintf("http://external-page.com/"."%s$splitVal%s", rawurlencode($tmpArr[0]), rawurlencode($tmpArr[1])), ENT_QUOTES);
echo  "<a href=\"javascript: window.open(".htmlspecialchars(json_encode($link), ENT_QUOTES).")\">'Open URL'</a>";

改编完成:
交换了恶意 JS 中的引号。
htmlspecialchars 移至 json_encode 周围,因为返回双引号字符串,否则会破坏 html。

I get a string, from an external clientside script, which must later be attached as part of an url. Now I am wondering what is the best way to santitize such data?

The string I get will have a structure like this:
dynamicVal#staticVal:dynamicVal

This value will then be added to an url:
http://the-page.com/dynamicVal#staticVal:dynamicVal

The url is then used as followed:

$link = htmlspecialchars("http://external-page.com/dynamicVal#staticVal:dynamicVal", ENT_QUOTES);
$var = "<a href=\"javascript: window.open('$link')\">'Open URL'</a>";

Problem is, htmlspecialchars wont help to prevent execution of random javascript code, e.g. by adding this alert to the value:

dynamicVal#staticVal:dynamicVal'+alert(\"breakout\")+'

Using rawurlencode wont help either, because it is not a value of a parameter but a real part of the url.
So what is the best way to sanitize the passed string when concatenating to the url?

Thanks in advance.

Edit:
Using rawurlencode only on the dynamic parts actually also didn't solve the issue, the javascript still got executed.
Test snippet:

$splitVal = "#staticVal:";
$tmpArr = explode($splitVal, "dynamicVal#staticVal:dynamicVal'+alert(\"breakout\")+'");
$link = htmlspecialchars(sprintf("http://external-page.com/"."%s$splitVal%s", rawurlencode($tmpArr[0]), rawurlencode($tmpArr[1])), ENT_QUOTES);
echo "<a href=\"javascript: window.open('$link')\">'Open URL'</a>";

Edit2:
Using json_encode when passing the string as javascript argument didn't help either.
Adapted test snippet:

$splitVal = "#staticVal:";
$tmpArr = explode($splitVal, "dynamicVal#staticVal:dynamicVal\"+alert('breakout')+\"");
$link = htmlspecialchars(sprintf("http://external-page.com/"."%s$splitVal%s", rawurlencode($tmpArr[0]), rawurlencode($tmpArr[1])), ENT_QUOTES);
echo  "<a href=\"javascript: window.open(".htmlspecialchars(json_encode($link), ENT_QUOTES).")\">'Open URL'</a>";

Adaptions done:
Switched the quotes in the malicous JS.
Moved htmlspecialchars around json_encode, because a double quoted string gets returned which would break the html otherwise.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

始终不够爱げ你 2024-12-11 15:59:05

为此,您应该使用 urlencode() 。不是在整个琴弦上,而是仅在动态部分上。

$link = sprintf('http://external-page.com/%s#staticVal:%s', urlencode('dynamicVal'), urlencode('dynamicVal'));
$var  = "<a href=\"javascript: window.open('$link')\">'Open URL'</a>";

编辑

好的 - 我看到你的问题了。我没有意识到您将代码插入到 JavaScript 函数调用中。您必须确保 JavaScript 解释器将您的链接视为 window.open() 的字符串参数:

$link = sprintf('http://external-page.com/%s#staticVal:%s', urlencode('dynamicVal'), urlencode('dynamicVal'));
$var  = "<a href=\"javascript: window.open(".json_encode($link).")\">'Open URL'</a>";

You should use urlencode() for this. Not on the whole string but on the dynamic parts only.

$link = sprintf('http://external-page.com/%s#staticVal:%s', urlencode('dynamicVal'), urlencode('dynamicVal'));
$var  = "<a href=\"javascript: window.open('$link')\">'Open URL'</a>";

EDIT:

OK - I see your problem. I didn't realize that you insert the code into a JavaScript function call. You'll have to ensure that the JavaScript interpreter treats your link as a string argument to window.open():

$link = sprintf('http://external-page.com/%s#staticVal:%s', urlencode('dynamicVal'), urlencode('dynamicVal'));
$var  = "<a href=\"javascript: window.open(".json_encode($link).")\">'Open URL'</a>";
開玄 2024-12-11 15:59:05

为了完整起见,我只需在使用 rawurlencode 之前将 addslashes 放在动态部分即可解决该问题。
需要两个函数调用来防止爆发。使用 addslashes 可以防止正常引号 (',") 并使用 rawurlencode 防止已编码的引号 (%29,%22) 造成伤害,

所以最终的解决方案如下所示:

$splitVal = "#staticVal:";
$tmpArr = explode($splitVal, "dynamicVal#staticVal:dynamicVal'+alert(\"breakout\")+'");
$link = htmlspecialchars(sprintf("http://external-page.com/"."%s$splitVal%s", rawurlencode(addslashes($tmpArr[0])), rawurlencode(addslashes($tmpArr[1]))), ENT_QUOTES);
echo "<a href=\"javascript: window.open('$link')\">'Open URL'</a>";

For completenes, I was able to solve that issue by simply putting addslashes on the dynamic part before using rawurlencode.
Both function calls are needed to prevent breaking out. Using addslashes prevents normal quotes (',") and rawurlencode prevents already encoded quotes (%29,%22) to cause harm.

So final solution looks like this:

$splitVal = "#staticVal:";
$tmpArr = explode($splitVal, "dynamicVal#staticVal:dynamicVal'+alert(\"breakout\")+'");
$link = htmlspecialchars(sprintf("http://external-page.com/"."%s$splitVal%s", rawurlencode(addslashes($tmpArr[0])), rawurlencode(addslashes($tmpArr[1]))), ENT_QUOTES);
echo "<a href=\"javascript: window.open('$link')\">'Open URL'</a>";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文