-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTMLTagFilter.py
170 lines (137 loc) · 4.21 KB
/
HTMLTagFilter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
DENY_ALLOW = 0
ALLOW_DENY = 1
#str_regex_tag = r"""[^"'<>]*(?:"[^"]*"[^"'<>]*|'[^']*'[^"'<>]*)*(?:>|(?=<)|$(?!\n))"""
#str_regex_comment =r'<!(?:--[^-]*-(?:[^-]+-)*?-(?:[^>-]*(?:-[^>-]+)*?)??)*(?:>|$(?!\n)|--.*$)'
class HTMLTagFilter:
"""
allow / deny list:
a -> <a>
a:href -> <a href>
a:* -> a tag's any attribute
*:style -> any tag's style attribute
"""
def __init__(self, rule, allow_list, deny_list):
"""
create new object.
@param rule: filtering rule. DENY_ALLOW or ALLOW_DENY.
@type rule: int
@param allow_list: allowed tag/attribute's list.
@type allow_list: sequence
@param deny_list: denied tag/attribue's list.
@type deny_list: sequece
"""
self.rule = rule
self.allow_list = allow_list[:]
self.deny_list = deny_list[:]
allow_tuple = self._create_filtering_rule( allow_list )
deny_tuple = self._create_filtering_rule( deny_list )
self.allow_attributes = allow_tuple[0]
self.allow_elements = allow_tuple[1]
self.deny_attributes = deny_tuple[0]
self.deny_elements = deny_tuple[1]
def _create_filtering_rule( self, rule_list ):
attr_map = {}
elem_list = []
for item in rule_list:
item = item.strip()
if item.find(":") == -1 : # element rule
elem_list.append( item )
else: # attribute rule
match_obj = re.search( r"^(\w*|\*):(\w*|\*)$", item )
elem = match_obj.group(1)
attr = match_obj.group(2)
if elem == "":
elem = "*"
if attr == "":
attr = "*"
attr_list = attr_map.get( elem, [] )
attr_list.append( attr )
attr_map[elem] = attr_list
return (attr_map, elem_list)
def apply( self, str ):
"""
apply filter rule to string.
return string's filtered copy.
@param str: target string
@type str: string
"""
ret_str = ""
str_regex_split = r"""(<[^"'<>]*(?:"[^"]*"[^"'<>]*|'[^']*'[^"'<>]*)*(?:>|(?=<)|$(?!\n)))"""
str_regex_tag = r"""^<.*>$"""
regex_split = re.compile( str_regex_split )
regex_tag = re.compile( str_regex_tag )
splitted_list = regex_split.split( str )
for term in splitted_list:
if regex_tag.search( term ):
term = self.filter_tag( term )
else:
term = self.quote(term)
ret_str = ret_str + term
return ret_str
def filter_tag( self, str ):
# print str + ":"
match_obj = re.search( r"^<(/{0,1}\s*\w+)\s*(.*)>", str )
tag = ""
attr_list = []
if match_obj:
tag = match_obj.group(1)
attr = match_obj.group(2).strip()
if not attr == "":
attr_list = re.split( "\s+", attr )
# element filtering
tag = tag.replace( "/", "")
if self._check( tag, self.allow_elements, self.deny_elements ):
str = self.quote( str )
return str
# attribute filtering
new_list = []
for item in attr_list:
(attr, val) = item.split( "=", 1 )
allow_list = self.allow_attributes.get(tag, [] )
deny_list = self.deny_attributes.get(tag,[] )
if not self._check( attr, allow_list, deny_list ):
new_list.append(item)
if len(new_list) > 0:
str = "<" + tag + " " + " ".join( new_list ) + ">"
else:
str = "<" + tag + ">"
return str
def _check( self, str, allow_list, deny_list ):
is_allow = ( str in allow_list ) or ( "*" in allow_list )
is_deny = ( str in deny_list ) or ( "*" in deny_list )
if self.rule == ALLOW_DENY:
if (not is_allow) or (is_deny):
return 1
elif self.rule == DENY_ALLOW:
if (is_deny) and (not is_allow):
return 1
return 0
def quote( self, str ):
str_ret = str.replace( "&", "&" )
str_ret = str_ret.replace( "<", "<" )
str_ret = str_ret.replace( ">", ">" )
return str_ret
#testcode
#alist = ["a", "b", "br", "p", "a:href" ]
#dlist = ["*"]
#filter = HTMLTagFilter( DENY_ALLOW, alist, dlist)
#str = """hoge > hoge < hoge<a href="URL" style="<"><b>test</b><br>"""
# print "allow-elem:"
# for item in filter.allow_elements:
# print item
# print "deny-elem:"
# for item in filter.deny_elements:
# print item
# print "allow-attr:"
# for elem in filter.allow_attributes:
# for attr in filter.allow_attributes[elem]:
# print "%s : %s" % (elem, attr)
# print "deny-attr:"
# for elem in filter.deny_attributes:
# for attr in filter.deny_attributes[elem]:
# print "%s : %s" % (elem, attr)
#print "Input: %s" % str
#print filter.apply(str)