whistler 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Daniel Neighman
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,26 @@
1
+ Whistler
2
+ ==============
3
+
4
+ Whistler is a white listing markup filter based on the specifications of the rails plugin by Rick Olson, aka technoweenie.
5
+ http://techno-weenie.net/
6
+ Whistler relies on the Hpricot library http://code.whytheluckystiff.net/hpricot
7
+
8
+ This is very alpha at the moment. Please help make it great.
9
+
10
+ Whistler strips, and or sanitizes arbitrary XML/HTML style markup of any tags not explicitly
11
+ included in the white list. It doesn't try to play catch-up with possible exploites such as black-listing systems do.
12
+
13
+ Usage is very simple.
14
+
15
+ === Example
16
+ # Applies the normal white list defaults
17
+
18
+ Whistler.white_list( dodgy_markup )
19
+
20
+
21
+ # Adds custom tags to allow
22
+ Whistler.white_list(dodgy_markup, :add_tags => %w(news_tag my_tag other_tag))
23
+
24
+ If Whistler is unable to read tags as "tags" it will instead sanitize potential XSS attempts in the text.
25
+ Normal, non-malicious text should still appear correctly.
26
+
data/Rakefile ADDED
@@ -0,0 +1,44 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+
4
+ PLUGIN = "whistler"
5
+ NAME = "whistler"
6
+ VERSION = "0.0.1"
7
+ AUTHOR = "Daniel Neighman"
8
+ EMAIL = "has.sox@gmail.com"
9
+ HOMEPAGE = "http://github.com/hassox/whistler"
10
+ SUMMARY = "Whistler == White Lister"
11
+
12
+ spec = Gem::Specification.new do |s|
13
+ s.name = NAME
14
+ s.version = VERSION
15
+ s.platform = Gem::Platform::RUBY
16
+ s.has_rdoc = true
17
+ s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
18
+ s.summary = SUMMARY
19
+ s.description = s.summary
20
+ s.author = AUTHOR
21
+ s.email = EMAIL
22
+ s.homepage = HOMEPAGE
23
+ s.add_dependency('hpricot')
24
+ s.require_path = 'lib'
25
+ s.autorequire = PLUGIN
26
+ s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,specs}/**/*")
27
+ end
28
+
29
+ Rake::GemPackageTask.new(spec) do |pkg|
30
+ pkg.gem_spec = spec
31
+ end
32
+
33
+ task :install => [:package] do
34
+ sh %{sudo gem install pkg/#{NAME}-#{VERSION}}
35
+ end
36
+
37
+ namespace :jruby do
38
+
39
+ desc "Run :package and install the resulting .gem with jruby"
40
+ task :install => :package do
41
+ sh %{#{SUDO} jruby -S gem install pkg/#{NAME}-#{Merb::VERSION}.gem --no-rdoc --no-ri}
42
+ end
43
+
44
+ end
data/TODO ADDED
@@ -0,0 +1,5 @@
1
+ TODO:
2
+ Fix LICENSE with your name
3
+ Fix Rakefile with your name and contact info
4
+ Add your code to lib/merb_whitelist.rb
5
+ Add your Merb rake tasks to lib/merb_whitelist/merbtasks.rb
data/lib/whistler.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), "whistler", "white_list")
@@ -0,0 +1,6 @@
1
+ namespace :whistler do
2
+ desc "Do something for merb_whitelist"
3
+ task :default do
4
+ puts "merb_whitelist doesn't do anything"
5
+ end
6
+ end
@@ -0,0 +1,101 @@
1
+ require 'hpricot'
2
+ module Whistler
3
+
4
+ def self.protocol_attributes
5
+ @_protocol_attributes = %w(src href)
6
+ end
7
+
8
+ def self.protocol_separator
9
+ @_protocol_seperator = /:|(&#0*58)|(&#x70)|(%|%)3A/
10
+ end
11
+
12
+ # An array of default allowed tags.
13
+ def self.white_tags
14
+ @_white_tags ||= %w(strong em b i p code pre tt output samp kbd var sub sup dfn cite big small address hr br div span h1 h2 h3 h4 h5 h6 ul ol li dt dd abbr acronym a img blockquote del ins fieldset legend)
15
+ end
16
+
17
+ # An array of default allowed attributes
18
+ def self.white_attributes
19
+ @_white_attributes ||= %w(href src width height alt cite datetime title class name)
20
+ end
21
+
22
+ # An array of default allowed protocols
23
+ def self.white_protocols
24
+ @_white_protocols ||= %w(ed2k ftp http https irc mailto news gopher nntp telnet webcal xmpp callto feed)
25
+ end
26
+
27
+
28
+ # This is the work horse of the Whistler gem. It whitelists a string of Markup.
29
+ # *string* - The string to white list
30
+ # *opts* - A group of options to apply for this run
31
+ # === valid options
32
+ # * <tt>:tags</tt> - An array of allowed tags. This list is exlusive of all others and only tags included in this list will be allowed
33
+ # * <tt>:add_tags</tt> - An array of extra allowed tags. All normal tags are allowed, plus the ones specified in this array
34
+ # * <tt>:attributes</tt> - An array of allowed attributes. This list is exlusive of all others and only attributes included will be allowed.
35
+ #
36
+ # === Example
37
+ # {{{
38
+ # Whistler.white_list(my_markup_string, :add_tags => %w(object param) )
39
+ # }}}
40
+ # Allows object and param tags in addition to normal allowed tags.
41
+ def self.white_list(string, opts = {})
42
+ return nil if string.nil?
43
+ w_tags = get_white_tags(opts)
44
+ w_attrs = get_white_attributes(opts)
45
+
46
+ string = string.gsub("\000", "")
47
+
48
+ doc = Hpricot(string)
49
+ doc.traverse_element do |elem|
50
+ if elem.elem?
51
+ if w_tags.include?(elem.name)
52
+ (elem.attributes.keys - w_attrs).each{|a| elem.remove_attribute(a)}
53
+ (elem.attributes.keys & Whistler.protocol_attributes).each{|a| elem.remove_attribute(a) if contains_bad_protocols?(elem[a])}
54
+ elem.raw_attributes.each{|a,v| elem.raw_attributes[a] = clean_attribute(v)}
55
+ else
56
+ elem.parent.children.delete(elem)
57
+ end
58
+ elsif elem.text?
59
+ elem.parent.replace_child(elem, Hpricot::Text.new(escape_text(elem.to_s)))
60
+ end
61
+ end
62
+ doc.to_html
63
+ end
64
+
65
+ def white_list(string, opts = {} )
66
+ Whistler.white_list(string, opts)
67
+ end
68
+
69
+ private
70
+
71
+ def self.get_white_tags(opts)
72
+ return opts[:tags] if opts[:tags]
73
+
74
+ if opts[:add_tags]
75
+ wtags = Whistler.white_tags.dup
76
+ wtags << opts[:add_tags]
77
+ wtags = wtags.flatten
78
+ return wtags
79
+ end
80
+
81
+ return Whistler.white_tags
82
+ end
83
+
84
+ def self.get_white_attributes(opts)
85
+ return opts[:attributes] if opts[:attributes]
86
+ return Whistler.white_attributes
87
+ end
88
+
89
+ def self.contains_bad_protocols?(value)
90
+ value =~ Whistler.protocol_separator && !Whistler.white_protocols.include?(value.split(Whistler.protocol_separator).first)
91
+ end
92
+
93
+ def self.escape_text(string)
94
+ string.gsub(/</, "&lt;")
95
+ end
96
+
97
+ def self.clean_attribute(a)
98
+ a.gsub(/</, "&lt;").gsub(/>/, "&gt;")
99
+ end
100
+ end
101
+
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: whistler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Neighman
8
+ autorequire: whistler
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-03-22 00:00:00 +11:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hpricot
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ version:
24
+ description: Whistler == White Lister
25
+ email: has.sox@gmail.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README
32
+ - LICENSE
33
+ - TODO
34
+ files:
35
+ - LICENSE
36
+ - README
37
+ - Rakefile
38
+ - TODO
39
+ - lib/whistler
40
+ - lib/whistler/merbtasks.rb
41
+ - lib/whistler/white_list.rb
42
+ - lib/whistler.rb
43
+ has_rdoc: true
44
+ homepage: http://github.com/hassox/whistler
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.0.1
66
+ signing_key:
67
+ specification_version: 2
68
+ summary: Whistler == White Lister
69
+ test_files: []
70
+