/
xss_terminate_test.rb
76 lines (54 loc) · 2.74 KB
/
xss_terminate_test.rb
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
require File.join(File.dirname(__FILE__), 'setup_test')
class XssTerminateTest < Test::Unit::TestCase
def test_strip_tags_on_discovered_fields
c = Comment.create!(:title => "<script>alert('xss in title')</script>",
:body => "<script>alert('xss in body')</script>")
assert_equal "alert('xss in title')", c.title
assert_equal "alert('xss in body')", c.body
end
def test_rails_sanitization_on_specified_fields
e = Entry.create!(:title => "<script>alert('xss in title')</script>",
:body => "<script>alert('xss in body')</script>",
:extended => "<script>alert('xss in extended')</script>",
:person_id => 1)
assert_equal [:body, :extended], e.xss_terminate_options[:sanitize]
assert_equal "alert('xss in title')", e.title
assert_equal "", e.body
assert_equal "", e.extended
end
def test_rails_sanitization_with_options
e = Entry.create!(:title => 'Title',
:body => '<script>alert("xss in body")</script><strong>Bold</strong><i>Italic</i><p>Paragraph</p>',
:extended => '<script>alert("xss in extended")</script>',
:person_id => 1)
assert_equal '<strong>Bold</strong><i>Italic</i>Paragraph', e.body
assert_equal '', e.extended
end
def test_excepting_specified_fields
p = Person.create!(:name => "<strong>Mallory</strong>")
assert_equal [:name], p.xss_terminate_options[:except]
assert_equal "<strong>Mallory</strong>", p.name
end
def test_html5lib_sanitization_on_specified_fields
r = Review.create!(:title => "<script>alert('xss in title')</script>",
:body => "<script>alert('xss in body')</script>",
:extended => "<script>alert('xss in extended')</script>",
:person_id => 1)
assert_equal [:body, :extended], r.xss_terminate_options[:html5lib_sanitize]
assert_equal "alert('xss in title')", r.title
assert_equal "<script>alert('xss in body')</script>", r.body
assert_equal "<script>alert('xss in extended')</script>", r.extended
end
# issue reported by linojon
def test_nil_attributes_should_be_allowed_with_html5
review = Review.create!(:title => nil, :body => nil)
assert_nil review.title
assert_nil review.body
end
# issue reported by Garrett Dimon and jmcnevin
def test_active_record_session_store_does_not_cause_nil_exception
assert_nil CGI::Session::ActiveRecordStore::Session.xss_terminate_options
session = CGI::Session::ActiveRecordStore::Session.new(:session_id => 'foo', :data => 'blah')
assert session.save
end
end