问题描述
我的网站上需要安全的HTML。
I need safe html on my website.
我通过caja指南阅读,但我不确定我是否理解了conecpt。
I read though the caja guide and I am not sure if I understand the conecpt.
我认为它是这样的:
- 用户将恶意内容提交给我的数据库
- 我想呈现它。 Caja识别恶意代码并阻止它。
但是如何通过caja呈现它?他们没有在他们的页面上解释这一点,他们只展示如何替换代码。
But how do I render it though caja? They don't explain this on their page, they only show how to replace the code.
<script type="text/javascript">
document.getElementById('dynamicContent').innerHTML = 'Dynamic hello world';
</script>
假设我们的文件看起来像这样
Let's say our document would look like this
<body>
<div class="input">
<h3>User Input </h3>
<script> alert("I am really bad!"); </script>
</div>
<div class="input">
<h3>User Input </h3>
<p> I am safe HTML!</p>
</div>
</body>
我如何告诉caja阻止脚本标记?
How would I tell caja to block the script tag?
推荐答案
如果你想要清理完html(即根本没有脚本执行),你就不需要所有的Caja,只需要html-sanitizer。
If you want to have just sanitized html (ie. no script execution at all), you don't need all of Caja, just the html-sanitizer.
使用:
<script src="http://caja.appspot.com/html-css-sanitizer-minified.js"></script>
<script>
var sanitized = html_sanitize(untrustedCode,
/* optional */ function(url) { return url /* rewrite urls if needed */ },
/* optional */ function(id) { return id; /* rewrite ids, names and classes if needed */ })
</script>
如果您不想允许已清理的CSS样式,请使用。
If you don't want to allow sanitized css styles, use http://caja.appspot.com/html-sanitizer-minified.js instead.
这篇关于谷歌caja - 阻止恶意代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!