java 富文本 过滤xss_集成富文本编辑器XSS预防过滤措施

本文介绍了一个使用HTMLParser实现的XSSHtml类,用于过滤和防止XSS攻击。允许特定的HTML标签,并对属性进行安全处理,如链接转义、样式过滤等,确保富文本内容的安全展示。
摘要由CSDN通过智能技术生成

import re

import copy

from html.parser import HTMLParser

class XSSHtml(HTMLParser):

allow_tags = [‘a‘, ‘img‘, ‘br‘, ‘strong‘, ‘b‘, ‘code‘, ‘pre‘,

‘p‘, ‘div‘, ‘em‘, ‘span‘, ‘h1‘, ‘h2‘, ‘h3‘, ‘h4‘,

‘h5‘, ‘h6‘, ‘blockquote‘, ‘ul‘, ‘ol‘, ‘tr‘, ‘th‘, ‘td‘,

‘hr‘, ‘li‘, ‘u‘, ‘embed‘, ‘s‘, ‘table‘, ‘thead‘, ‘tbody‘,

‘caption‘, ‘small‘, ‘q‘, ‘sup‘, ‘sub‘, ‘font‘]

common_attrs = ["style", "class", "name"]

nonend_tags = ["img", "hr", "br", "embed"]

tags_own_attrs = {

"img": ["src", "width", "height", "alt", "align"],

"a": ["href", "target", "rel", "title"],

"embed": ["src", "width", "height", "type", "allowfullscreen", "loop", "play", "wmode", "menu"],

"table": ["border", "cellpadding", "cellspacing"],

"font": ["color"]

}

def __init__(self, allows=[]):

HTMLParser.__init__(self)

self.allow_tags = allows if allows else self.allow_tags

self.result = [

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值