Class: AntiSamy::SaxFilter
- Inherits:
-
Nokogiri::XML::SAX::Document
- Object
- Nokogiri::XML::SAX::Document
- AntiSamy::SaxFilter
- Defined in:
- lib/antisamy/html/sax_filter.rb
Instance Method Summary collapse
-
#cdata_block(text) ⇒ Object
Add cdata a cdata block.
-
#characters(text) ⇒ Object
Add character data to the current tag.
-
#comment(text) ⇒ Object
Add a comment block.
- #convert_array(x) ⇒ Object
-
#end_element(name) ⇒ Object
End an elements, will raise an error on a loose tag.
- #end_element_namespace(name, prefix, uri) ⇒ Object
- #error(text) ⇒ Object
- #fetch_attribute(array, key) ⇒ Object
-
#initialize(policy, handler, param_tag) ⇒ SaxFilter
constructor
A new instance of SaxFilter.
-
#start_document ⇒ Object
Always create a HTML document unless the DECL was set beforehand.
-
#start_element(name, attributes = []) ⇒ Object
Start an element,.
- #start_element_namespace(name, attrs = [], prefix = nil, uri = nil, ns = nil) ⇒ Object
- #warning(text) ⇒ Object
Constructor Details
#initialize(policy, handler, param_tag) ⇒ SaxFilter
Returns a new instance of SaxFilter.
36 37 38 39 40 41 42 43 44 |
# File 'lib/antisamy/html/sax_filter.rb', line 36 def initialize(policy,handler,param_tag) @policy = policy @handler = handler @stack = Stack.new @css_content = nil @css_attributes = nil @css_scanner = CssScanner.new(policy) @param_tag = param_tag end |
Instance Method Details
#cdata_block(text) ⇒ Object
Add cdata a cdata block
292 293 294 295 296 297 298 299 300 |
# File 'lib/antisamy/html/sax_filter.rb', line 292 def cdata_block(text) if @stack.peek?(:css) @css_content << text elsif !@stack.peek?(:remove) @handler.characters(text) else @handler.cdata(@handler.encode_text(text)) unless @stack.peek == :remove end end |
#characters(text) ⇒ Object
Add character data to the current tag
245 246 247 248 249 250 251 252 253 254 |
# File 'lib/antisamy/html/sax_filter.rb', line 245 def characters(text) unless text =~ /\S/ # skip whitespace return unless @policy.directive(Policy::PRESERVE_SPACE) end if @stack.peek?(:css) @css_content << text elsif !@stack.peek?(:remove) @handler.characters(text) end end |
#comment(text) ⇒ Object
Add a comment block
57 58 59 60 61 62 63 64 65 |
# File 'lib/antisamy/html/sax_filter.rb', line 57 def comment(text) return if text.nil? if @policy.directive(Policy::PRESERVE_COMMENTS) =~ /true/i # Strip out conditional directives text.gsub!(%r{<!?!\[(?:end)?if*\]}ixm,"") text.gsub!(%r{\[(?:if).*\]>},"") @handler.comment(text) end end |
#convert_array(x) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/antisamy/html/sax_filter.rb', line 67 def convert_array(x) if x and x.first.is_a?(Array) return x end i = 0 h = [] while i < x.size m = [] m[0] = x[i] m[1] = x[i+1] h << m i += 2 end h end |
#end_element(name) ⇒ Object
End an elements, will raise an error on a loose tag
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/antisamy/html/sax_filter.rb', line 257 def end_element(name) if @stack.peek?(:remove) @stack.pop elsif @stack.peek?(:filter) @stack.pop elsif @stack.peek?(:css) @stack.pop # Do css stuff here begin results = @css_scanner.scan_sheet(@css_content,@policy.max_input) @handler.errors << results. @handler.errors.flatten! unless results.clean_html.nil? or results.clean_html.empty? @handler.start_element(name,@css_attributes) @handler.characters results.clean_html @handler.end_element(name) else @handler.start_element(name,@css_attributes) @handler.characters "/* */" @handler.end_element(name) end rescue Exception => e puts e @handler.errors << ScanMessage.new(ScanMessage::ERROR_CSS_TAG_MALFORMED,name,@handler.encode_text(@css_content)) ensure @css_content = nil @css_attributes = nil end else @stack.pop @handler.end_element(name) end end |
#end_element_namespace(name, prefix, uri) ⇒ Object
240 241 242 |
# File 'lib/antisamy/html/sax_filter.rb', line 240 def end_element_namespace(name,prefix,uri) end_element(name) end |
#error(text) ⇒ Object
46 47 |
# File 'lib/antisamy/html/sax_filter.rb', line 46 def error(text) end |
#fetch_attribute(array, key) ⇒ Object
83 84 85 86 87 88 89 90 |
# File 'lib/antisamy/html/sax_filter.rb', line 83 def fetch_attribute(array,key) array.each do |pair| if pair.first.eql?(key) return pair.last end end nil end |
#start_document ⇒ Object
Always create a HTML document unless the DECL was set beforehand
53 54 |
# File 'lib/antisamy/html/sax_filter.rb', line 53 def start_document end |
#start_element(name, attributes = []) ⇒ Object
Start an element,
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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/antisamy/html/sax_filter.rb', line 93 def start_element(name, attributes = []) attributes = convert_array(attributes) o_attributes = attributes.dup tag = @policy.tag(name) masquerade = false = nil = nil # Handle validate param tag as an embed tag if tag.nil? && @policy.directive(Policy::VALIDATE_P_AS_E) && name.eql?("param") = @param_tag if @policy.tag("embed") = @policy.tag("embed") end if and .action == Policy::ACTION_VALIDATE tag = masquerade = true = fetch_attribute(attributes,"name") = fetch_attribute(attributes,"value") attributes = [ [,] ] end end valid_attributes = [] if @stack.peek?(:css) or @stack.peek?(:remove) # We are in remove mode to remove this tag as well as any child style elements if css mode @stack.push(:remove) elsif (tag.nil? && @policy.directive(Policy::ON_UNKNOWN_TAG).eql?("encode")) or (!tag.nil? && tag.action.eql?(Policy::ACTION_ENCODE)) or @policy.encode?(name.downcase) tmp = "<#{name}>" @handler.characters(tmp) @stack.push(:filter) elsif tag.nil? # We ignore missing HTML and BODY tags since we are fragment parsing, but the # Nokogiri HTML::SAX parser injects HTML/BODY if they are missing unless name.eql?("html") or name.eql?("body") @handler.errors << ScanMessage.new(ScanMessage::ERROR_TAG_NOT_IN_POLICY,name) end # Nokogiri work around for a style tag being auto inserted inot head if name.eql?("head") @stack.push(:remove) else @stack.push(:filter) end elsif tag.action.eql?(Policy::ACTION_FILTER) @handler.errors << ScanMessage.new(ScanMessage::ERROR_TAG_FILTERED,name) @stack.push(:filter) elsif tag.action.eql?(Policy::ACTION_VALIDATE) # Handle validation remove_tag = false filter_tag = false is_style = name.include?("style") if is_style @stack.push(:css) @css_content = '' @css_attributes = [] else # Validate attributes attributes.each do |pair| a_name = pair.first a_value = pair.last attrib = tag.attribute(a_name.downcase) if attrib.nil? attrib = @policy.global(a_name.downcase) end # check if the attribute is a style if a_name.eql?("style") # Handle Style tags begin results = @css_scanner.scan_inline(a_value,name,@policy.max_input) unless result.clean_html.empty? valid_attributes << [a_name,results.clean_html] end @handler.errors << results. @handler.errors.flatten! rescue Exception => e @handler.errors << ScanMessage.new(ScanMessage::ERROR_CSS_ATTRIBUTE_MALFORMED,name,@handler.encode_text(a_value)) end elsif !attrib.nil? # Attribute is not nil lets check it valid = false attrib.values.each do |av| if av.eql?(a_value) valid_attributes << [a_name,a_value] valid = true break end end unless valid attrib.expressions.each do |ae| mc = ae.match(a_value) if mc and mc.to_s == a_value valid_attributes << [a_name,a_value] valid = true break end end end # we check the matches if !valid && attrib.action.eql?(Attribute::ACTION_REMOVE_TAG) @handler.errors << ScanMessage.new(ScanMessage::ERROR_ATTRIBUTE_INVALID_REMOVED,tag.name,@handler.encode_text(a_name),@handler.encode_text(a_value)) remove_tag = true elsif !valid && attrib.action.eql?(Attribute::ACTION_FILTER_TAG) @handler.errors << ScanMessage.new(ScanMessage::ERROR_ATTRIBUTE_CAUSE_FILTER,tag.name,@handler.encode_text(a_name),@handler.encode_text(a_value)) filter_tag = true elsif !valid @handler.errors << ScanMessage.new(ScanMessage::ERROR_ATTRIBUTE_INVALID,tag.name,@handler.encode_text(a_name),@handler.encode_text(a_value)) end else # attribute was null @handler.errors << ScanMessage.new(ScanMessage::ERROR_ATTRIBUTE_NOT_IN_POLICY,tag.name,a_name,@handler.encode_text(a_value)) if masquerade filter_tag = true end end end # end attirubte loop end if remove_tag @stack.push(:remove) elsif filter_tag @stack.push(:filter) else if name.eql?("a") and @policy.directive(Policy::ANCHROS_NOFOLLOW) valid_attributes << ["rel","nofollow"] end if masquerade valid_attributes = [] valid_attributes << ["name",] valid_attributes << ["value",] end @stack.push(:keep) unless @stack.peek?(:css) end # End validation action elsif tag.action.eql?(Policy::ACTION_TRUNCATE) @stack.push(:truncate) else @handler.errors << ScanMessage.new(ScanMessage::ERROR_TAG_DISALLOWED,name) @stack.push(:remove) end # We now know wether to keep or truncat this tag if @stack.peek?(:truncate) @handler.start_element(name,[]) elsif @stack.peek?(:keep) @handler.start_element(name,valid_attributes) end end |
#start_element_namespace(name, attrs = [], prefix = nil, uri = nil, ns = nil) ⇒ Object
236 237 238 |
# File 'lib/antisamy/html/sax_filter.rb', line 236 def start_element_namespace(name,attrs=[],prefix = nil, uri = nil, ns = nil) start_element(name,attrs) end |
#warning(text) ⇒ Object
49 50 |
# File 'lib/antisamy/html/sax_filter.rb', line 49 def warning(text) end |