将 HTML 存储在数据库中,同时避免持久的 xss/sql 注入
我正在 asp.net 中构建一个页面,该页面将使用 tiny mce 在页面上提供富文本编辑器。 Tiny mce 将富文本输出为 html,我想将其保存到数据库中。然后稍后,我想从数据库中提取 HTML 并将其显示在页面中。
我担心允许恶意 html、js 标签进入我的数据库,这些标签稍后会被输出。
有人可以指导我在流程中的哪个点应该进行 html 编码/解码等以防止持久的 xss 攻击和/或 sql 注入攻击吗?
I'm building a page in asp.net that will use tiny mce to provide a rich text editor on the page. Tiny mce outputs the rich text as html which I would like to save to a database. Then at a later date, I want to pull the HTML from the database and display it in a page.
I'm concerned about allowing malicious html, js tags into my database that would later be output.
Can someone walk me through at what point in my process I should html encode/decode etc. to prevent a persistent xss attack and or sql injection attack?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我们使用 Microsoft Web Protection Library 来清除传入的任何潜在危险的 HTML。我的意思是“在途中” - 当页面发布到服务器时,我们使用 MS WPL 清理 HTML 并获取结果并将其放入数据库中。甚至不要让任何不良数据进入您的数据库,这样您就会更安全。就编码而言,您不想弄乱 HTML 编码/解码 - 只需获取tinyMCE 控件中的所有内容,擦洗并保存即可。然后在你的显示页面上,只需将它写出来,就像它存在于数据库中一样,写入文字控件或类似的东西,你应该就可以了。
我相信
Microsoft.Security.Application.Sanitizer.GetSafeHtmlFragment(input)
会完全满足您的需求。We use the Microsoft Web Protection Library to scrape out any potentially dangerous HTML on the way in. What I mean by "on the way in" - when the page is posted to the server, we scrub the HTML using MS WPL and take the results of that and throw that into the database. Don't even let any bad data get to your database, and you'll be safer for it. As far as encoding, you won't want to mess with HTML encoding/decoding - just take whatever is in your tinyMCE control, scrub it, and save it. Then on your display page, just write it out like it exists in your database into a literal control or something like that, and you should be good.
I believe
Microsoft.Security.Application.Sanitizer.GetSafeHtmlFragment(input)
will do exactly what you want here.这些管理员是否正在使用 RTE?如果是这样,我就不会担心了。
如果没有,那么我不建议使用所见即所得的工具,例如TinyMCE。您必须实际查找恶意输入,并且很可能您会错过一些。由于 RTE 输出纯 HTML(我假设您想要),因此您不能只转换 HTML 实体。这会消除使用 TinyMCE 的全部意义。
当将数据插入数据库时,停止 SQL 注入是在后端完成的。您将需要使用参数化查询或转义输入(不知道在 ASP.NET 中如何,我是 PHP 人员。)
Are these admins that are using the RTE? If so, I wouldn't worry about it.
If not, then I don't recommend using a WYSIWYIG such as TinyMCE. You'll have to actually look for malicious input, and chances are, you will miss some. Since the RTE outputs plain HTML, which I assume you want, you can't just convert HTML entities. That would kind of eliminate the whole point of using TinyMCE.
Stopping SQL injection is done in the backend when inserting the data into the database. You will want to use a parametrized query or escape the input (not sure how in ASP.NET, I'm a PHP guy.)
难道您不能使用使用 BBCode 的富文本编辑器并在服务器上转义所有需要转义的内容,然后将 BBCode 转换为 HTML 标记吗?
您还可以不在客户端上生成 BBCode,而是在服务器上将 HTML 标记转换为 BBCode,转义剩余的 HTML,并将结果从 BBCode 转换回 HTML。
Couldn't you use a rich text editor that uses BBCode and on the server, escape everything that needs to be escaped and convert BBCode to HTML markup afterwards?
You could also, instead of producing BBCode on the client, convert the HTML markup to BBCode on the server, escape the remaining HTML and convert the result from BBCode back to HTML.
有两种方法,您可能会使用第一种方法
1) 您将列出允许的标签并转义/剥离其余标签。 TinyMCE 可能有一些功能禁止用户使用某些标签..(vut 这只是客户端,您应该在服务器上验证它)
2)您将以不同的方式对允许的标签进行编码([b]bold[/b]),而不是您可以将所有内容保存到数据库,并在渲染时转义所有内容,然后解释您的特殊标签
第三种方法:如果用户是管理员(应该知道他在做什么的人),那么您可以留下所有内容而不转义...他是负责的一个是为了自己的错误......
There are two approaches, you will probably use the first one
1) you will make a list of permitted tags and escape/strip rest of them. TinyMCE has probably some feature to disallow user to use some tags..(vut this is only client side, you should validate it on server)
2) you will encode permitted tags differently ([b]bold[/b]), than you could save everything to DB and while rendering escape everything and than interpret your special tags
Third approach: if the user is admin (the one who should know whats is he doing), than you can leave everyhing without escaping...he is the responsible one for his own mistakes....