使用 ajax 查询请求验证?
我网站上的用户有一个可以编写自己的 html 的页面。我希望将其用于排序列表、样式等,但有些人会尝试插入脚本,这是我不允许的。
更新用户描述的机制是通过ajax 实现的。我从 javascript 向文件 ajax.ashx
发送请求,该文件调用 ajaxMethods.cs
中的函数。在该函数中,我使用用户的新描述更新 sql 服务器。
在将描述提交到服务器之前,如何验证函数中的输入?我想删除所有与脚本相关的内容,但保留正常的 html 标签,例如
。有没有任何工具可以处理所有这些?
Users on my site have a page they can write their own html to. I want this to be used for things like ordered lists, styling, and so on, but some people will try to insert script, which I can't allow.
The mechanism for updating a user's description is through ajax. From javascript, I send a request to a file ajax.ashx
, which calls a function in ajaxMethods.cs
. In the function I update the sql server with the user's new description.
How can I validate the input in the function, before the description is submited to the server? I want to take out anything to do with scripting, but leave the normal html tags like <p>
. Are there any tools that will handle all of this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不允许用户改用自定义格式/语言(例如 Markdown),然后解析这个服务器端到 HTML 吗?这样您就知道在实际请求中找到的任何脚本/html 代码都是无效的,可以被删除(或编码)。这还为您提供了仅允许预先确定的标签列表的优势。它基本上会为您提供与 StackOverflow 相同的功能。
Why not allow users to use a custom format / language instead (for example Markdown) and then parse this server side to HTML? This way you know that any script / html code you find within the actual request is invalid and can be stripped (or encoded). This also gives you the advantage of only allow a predetermined list of tags. It would basically give you the same functionality as StackOverflow has.
我建议您
HttpServerUtility.HtmlEncode
您的输出。尝试解析脚本标签而不是 HTML 并不是一个好方法。请参阅下面的参考:XSS(跨站脚本)备忘单
I recommend you
HttpServerUtility.HtmlEncode
your output. Trying to parse out the script tags and not the HTML is not a good approach. See reference below:XSS (Cross Site Scripting) Cheat Sheet