javascript - Google caja - 阻止恶意代码

标签 javascript html security google-caja

我的网站需要安全的 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-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 样式,请使用 http://caja.appspot.com/html-sanitizer-minified.js相反。

关于javascript - Google caja - 阻止恶意代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12253686/

相关文章:

javascript - 将 Prop 传递给给定组件 react 的 Prop

javascript - 如何正确分页数据表中的结果(ajax,服务器端)?

javascript - 使用 jquery scrollify 更改固定 div 的 Div 内容

javascript - jQuery Mobile - 移至下一页/上一页 - 单击后执行操作两次

java - Spring MVC Web 应用程序检测暴力攻击的最佳方法?

asp.net - Azure asp.net 站点(无数据库)的简单登录/安全性?

security - 引用 header 中的 CDN 和个人身份信息

javascript - 克隆 <tr> 并在 JavaScript/jQuery 中修改克隆的内容

javascript - 在 React 元素之间传输数据

html - 选择类在 chrome 中不起作用