确保用户不在代码中输入恶意html(Make sure user doesn't put in malicious html in code)

编程入门 行业动态 更新时间:2024-10-26 00:24:53
确保用户不在代码中输入恶意html(Make sure user doesn't put in malicious html in code)

我正在使用textarea从用户那里获取输入并将其显示在屏幕上。 我怎样才能确保他们输入类似的东西

<h1>YAY, I hacked in</h1>

我只是按原样显示它,它不会显示为<h1> 。 必须有一个功能。 帮帮我? :d

I'm using a textarea to get input from the user and display it on the screen. How can I make sure that if they put in something like

<h1>YAY, I hacked in</h1>

I only display it as it is, and it doesn't display as an <h1>. There must be a function for this. Help? :D

最满意答案

您需要在服务器端解决此问题。 如果您在表单提交时使用JavaScript进行过滤,则用户可以通过创建自己的页面,使用telnet,禁用JavaScript,使用Chrome / FF / IE控制台等来破坏过滤器。如果您在显示时过滤,则可以什么都没有减轻,你只是在页面上移动了突破点。

例如,在PHP中,如果您希望仅使用用户的格式转储原始字符,则可以使用:

print htmlentities($user_submitted_data, ENT_NOQUOTES, 'utf-8');

在.NET中:

someControl.innerHTML = Server.HtmlEncode(userSubmittedData);

如果您正在尝试清理内容客户端以进行即时/预览显示,那么这应该足够了:

out.innerHTML = user_data.replace(/</g, "&lt;").replace(/>/g, "&gt;");

You need to address this on the server side. If you filter with JavaScript at form submission time, the user can subvert your filter by creating their own page, using telnet, by disabling JavaScript, using the Chrome/FF/IE console, etc. And if you filter at display time, you haven't mitigated anything, you've only moved the breakin-point around on the page.

In PHP, for instance, if you wish to just dump the raw characters out with none of the user's formatting, you can use:

print htmlentities($user_submitted_data, ENT_NOQUOTES, 'utf-8');

In .NET:

someControl.innerHTML = Server.HtmlEncode(userSubmittedData);

If you're trying to sanitize the content client-side for immediate/preview display, this should be sufficient:

out.innerHTML = user_data.replace(/</g, "&lt;").replace(/>/g, "&gt;");

更多推荐

本文发布于:2023-07-14 20:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1107313.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:恶意   代码   用户   html   code

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!