谷歌caja-阻止恶意代码

Google caja - Block malicious code

本文关键字:恶意 代码 caja- 谷歌      更新时间:2023-09-26

我的网站上需要安全的html。

我阅读了caja指南,我不确定我是否理解该指南。

https://developers.google.com/caja/docs/gettingstarted/

我想是这样的:

  • 用户向我的数据库提交恶意内容
  • 我想渲染它。Caja识别恶意代码并阻止它

但是我该如何渲染caja呢?他们没有在自己的页面上解释这一点,他们只展示了如何替换代码。

<script type="text/javascript">
      document.getElementById('dynamicContent').innerHTML = 'Dynamic hello world';
</script>

假设我们的文档看起来像这个

<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阻止脚本标记?

如果你只想净化html(即根本不执行脚本),你不需要所有的Caja,只需要html净化程序。

使用:

<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样式,请使用http://caja.appspot.com/html-sanitizer-minified.js相反

在我看来,AntiSamy是一种更好的方法。

https://www.owasp.org/index.php/Category:OWASP_AntiSamy_Project#What_is_it.3F

这真的很简单