Joi 消毒扩展

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

我正在使用我自己的代码使用 sanitize-html 包来清理 html。它不起作用。

const BaseJoi = require('joi');
const sanitizeHtml = require('sanitize-html');

const extension = (joi) => ({
  type: 'string',
  base: joi.string(),
  messages: {
    'string.escapeHTML': '{{#label}} must not include HTML!',
  },
  rules: {
    escapeHTML: {
      validate(value, helpers) {
        const clean = sanitizeHtml(value, {
          allowedTags: [],
          allowedAttributes: {},
        });
        if (clean !== value)
          return helpers.error('string.escapeHTML', { value });
        return clean;
      },
    },
  },
});

const Joi = BaseJoi.extend(extension);

我在我的架构中使用了 escapeHTML 函数,但它不起作用。我希望它显示一个错误页面,上面写着“不允许使用 Html”,但我没有看到任何错误。我需要 joi 模式并且它有效。但是扩展代码(escapeHtml)不起作用。

问题出在最新版本的 sanitize-html 包上。我使用的是旧版本,它应该显示错误。

javascript xss joi sanitize
© www.soinside.com 2019 - 2024. All rights reserved.