我正在尝试消毒,但不起作用

问题描述 投票:0回答:1

我试图在将 html 字符串发送到数据库之前对其进行清理,我正在使用 sanitize-html npm 包,但它不起作用

 if (noteContent) {
      const resultContent = sanitize(noteContent);
      console.log(resultContent);
    } else {
      setErrorMessages((prevState) => {
        return ["note content cannot be empty"];
      });
    }

当我进入时
它返回:


我不明白我做错了什么?

reactjs database next.js xss sanitize
1个回答
0
投票

根据 Github 上提供的文档,您正在寻找的函数应该称为

sanitizeHtml()
而不是
sanitize()

但是,如果您仔细阅读,默认情况下它确实允许此处列出的相当多的 html 标签和属性:https://github.com/apostropecms/sanitize-html?tab=readme-ov-file#default-options

为了更加严格,请遵循其文档中提供的示例。

// Allow only a super restricted set of tags and attributes
const clean = sanitizeHtml(dirty, {
  allowedTags: [ 'b', 'i', 'em', 'strong', 'a' ],
  allowedAttributes: {
    'a': [ 'href' ]
  },
  allowedIframeHostnames: ['www.youtube.com']
});
© www.soinside.com 2019 - 2024. All rights reserved.