/
owasp_esapi_validator_spec.rb
224 lines (207 loc) · 9.09 KB
/
owasp_esapi_validator_spec.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
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
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
module Owasp
module Esapi
describe Validator do
let(:validator) { Owasp::Esapi::Validator}
let(:allow_null) { false }
before do
# Global HTTP Validation Rules
# Values with Base64 encoded data (e.g. encrypted state) will need at least [a-zA-Z0-9\/+=]
{
"AccountName"=>"^[a-zA-Z0-9]{3,20}$",
"SystemCommand"=>"^[a-zA-Z\\-\\/]{1,64}$",
"RoleName"=>"^[a-z]{1,20}$",
"HTTPScheme"=>"^(http|https)$",
"HTTPServerName"=>"^[a-zA-Z0-9_.\\-]*$",
"HTTPParameterName"=>"^[a-zA-Z0-9_]{1,32}$",
"HTTPParameterValue"=>"^[a-zA-Z0-9.\\-\\/+=_ ]*$",
"HTTPCookieName"=>"^[a-zA-Z0-9\\-_]{1,32}$",
"HTTPCookieValue"=>"^[a-zA-Z0-9\\-\\/+=_ ]*$",
"HTTPHeaderName"=>"^[a-zA-Z0-9\\-_]{1,32}$",
"HTTPHeaderValue"=>"^[a-zA-Z0-9()\\-=\\*\\.\\?;,+\\/:&_ ]*$",
"HTTPContextPath"=>"^[a-zA-Z0-9.\\-\\/_]*$",
"HTTPServletPath"=>"^[a-zA-Z0-9.\\-\\/_]*$",
"HTTPPath"=>"^[a-zA-Z0-9.\\-_]*$",
"HTTPQueryString"=>"^[a-zA-Z0-9()\\-=\\*\\.\\?;,+\\/:&_ %]*$",
"HTTPURI"=>"^[a-zA-Z0-9()\\-=\\*\\.\\?;,+\\/:&_ ]*$",
"HTTPURL"=>"^.*$",
"HTTPJSESSIONID"=>"^[A-Z0-9]{10,30}$",
"FileName"=>/[a-zA-Z0-9!@#$\%^&{}\[\]()_+\-=,.~\'` ]{1,255}$/,
"DirectoryName"=>'^[a-zA-Z0-9:/\\\\!@#$%^&{}\[\]()_+\-=,.~\'` ]{1,255}$',
"SafeString"=>%w{^[.\\p{Alnum}\\p{Space}]{0,1024}$},
"Email"=>"^[A-Za-z0-9._%\-]+@[A-Za-z0-9.\-]+\\.[a-zA-Z]{2,4}$",
"IPAddress"=>/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/,
"URL"=>/^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\:\'\/\\\\+=&%\$#_]*)?$/,
"CreditCard"=>/^(\d{4}[- ]?){3}\d{4}$/,
"SSN"=>/^(?!000)([0-6]\d{2}|7([0-6]\d|7[012]))([ -]?)(?!00)\d\d\3(?!0000)\d{4}$/,
"USZipCode" => "^\\d{5}(\\-\\d{4})?$",
"ItalianZipCode" => "^\\d{5}$",
}.each_pair do |name,expression|
Owasp::Esapi.security_config.add_pattern(name,expression)
end
end
describe "-FileContent Tests-" do
it "should allow this mock data with input size less than 1024 bytes" do
io = StringIO.new("this is some junk")
validator.valid_file_contents?("test",io,100,false).should be_true
end
it "should fail this mock data with input size greater than 10 bytes" do
io = StringIO.new("this is some junk")
validator.valid_file_contents?("test",io,10,false).should be_false
end
end
describe "-Number Tests-" do
# floats
{
"1.0" => [0,20,true],
"3.222" => [0,20,true],
"junk" => [0,20,false],
"-5.1" => [0,10,false],
"-10.334" => [-20,20,true],
"1" => [0,10,true]
}.each_pair do |val,rule|
min = rule[0]
max = rule[1]
correct = rule[2]
it "should check float value #{val} as #{correct} within #{min} and #{max}" do
validator.valid_float?("test",val,min,max,false).should == correct
end
end
#integers
{
"1" => [0,20,true],
"3" => [0,20,true],
"junk" => [0,20,false],
"-5" => [0,10,false],
"-10.334" => [-20,20,true],
"1" => [0,10,true]
}.each_pair do |val,rule|
min = rule[0]
max = rule[1]
correct = rule[2]
it "should check float value #{val} as #{correct} within #{min} and #{max}" do
validator.valid_float?("test",val,min,max,false).should == correct
end
end
end
describe "-FileName Tests-" do
it "should check filename.zip as valid with zip as allowed extension" do
validator.valid_file_name?("test","filename.zip",%w[zip .zip doc .doc],false).should be_true
end
it "should check filename.junk as invalid with default allowed extension" do
validator.valid_file_name?("test","filename.junk",nil,false).should be_false
end
# test some random files
{
"test ;name.zip" => false,
"test.zip" => true,
"test.doc" => true,
"test.jsp" => false,
"test.junk" => false,
"../../etc.gz" => false
}.each_pair do |file,value|
it "should validate #{file} with default extensions should return #{value}" do
validator.valid_file_name?("test",file,Esapi.security_config.allowed_extenions,false).should == value
end
end
end
describe "-HTML Tests-" do
{
"Test. <script>alert(document.cookie)</script>" => "Test.",
"<b>Jeff</b>" => "Jeff",
"<a href=\"http://www.aspectsecurity.com\">Aspect Security</a>" => "<a href=\"http://www.aspectsecurity.com\">Aspect Security</a>",
"Test. <<div on<script></script>load=alert()" => "Test. load=alert()",
"Test. <div style={xss:expression(xss)}>b</div>" => "<p>Test. </p><div>b</div>",
"Test. <s%00cript>alert(document.cookie)</script>" => "Test. alert(document.cookie)",
"Test. <s\tcript>alert(document.cookie)</script>" => "Test. alert(document.cookie)",
"Test. <s\tcript>alert(document.cookie)</script>" => "Test. alert(document.cookie)",
}.each_pair do |html,result|
it "should convert #{html} to #{result}" do
validator.valid_html("test",html,100,false).should == result
end
end
end
describe "-Directory Path Tests-" do
{
"c:\\ridiculus" => ["C:\\",false,Owasp::Esapi::Codec::OsCodec::WINDOWS_HOST],
"c:\\jeff" => ["C:\\",false,Owasp::Esapi::Codec::OsCodec::WINDOWS_HOST],
"c:\\temp\\..\\etc" => ["C:\\",false,Owasp::Esapi::Codec::OsCodec::WINDOWS_HOST],
"C:\\" => ["C:\\",true,Owasp::Esapi::Codec::OsCodec::WINDOWS_HOST],
"C:\\windows" => ["C:\\",true,Owasp::Esapi::Codec::OsCodec::WINDOWS_HOST],
"C:\\windows\\System32" => ["C:\\",true,Owasp::Esapi::Codec::OsCodec::WINDOWS_HOST],
"/tmp" => ["/",true,Owasp::Esapi::Codec::OsCodec::UNIX_HOST],
"/bin/sh" => ["/",true,Owasp::Esapi::Codec::OsCodec::UNIX_HOST],
"/etc" => ["/",true,Owasp::Esapi::Codec::OsCodec::UNIX_HOST],
"/etc/ridiculous" => ["/",false,Owasp::Esapi::Codec::OsCodec::UNIX_HOST],
"/tmp/../etc" => ["/",false,Owasp::Esapi::Codec::OsCodec::UNIX_HOST],
"/bin/sh" => ["/",false,Owasp::Esapi::Codec::OsCodec::UNIX_HOST],
}.each_pair do |path,parent_and_os|
os = Owasp::Esapi::Codec::OsCodec.new.os
it "should check #{path}, and #{parent_and_os[1] ? 'pass' : 'fail'} on #{parent_and_os.last} executing test on #{os}" do
x = validator.valid_directory?("test", path,parent_and_os.first,false)
if os == parent_and_os.last
x.should == parent_and_os[1]
else
x.should be_false
end
end
end
end
describe "-DateTests-" do
it "should return true for September 11, 2001 with the default format" do
validator.valid_date?("test", "September 11, 2001",nil,true).should be_true
end
it "should return false for September 11, 2001 with the '%d-%m-%Y' as the format" do
validator.valid_date?("test", "September 11, 2001",'%d-%m-%Y',true).should be_false
end
it "should return true for nil with the '%d-%m-%Y' as the format" do
validator.valid_date?("test", nil,'%d-%m-%Y',true).should be_true
end
it "should return false for nil with the '%d-%m-%Y' as the format with allow_nil set to false" do
validator.valid_date?("test", nil,'%d-%m-%Y',false).should be_false
end
end
describe "-StringTests-" do
it "should return true for a nil" do
validator.valid_string?("test",nil,"Email",100,true,true).should be_true
end
# test cases that should pass
{
"jeff.williams@aspectsecurity.com" => "Email",
"123.168.100.234" => "IPAddress",
"192.168.1.234" => "IPAddress",
"http://www.aspectsecurity.com"=> "URL",
"078-05-1120"=> "SSN",
"078 05 1120"=> "SSN",
"078051120"=> "SSN",
"c:\\ridiculous" => "DirectoryName",
"c:\\temp\\..\\etc" => "DirectoryName",
"/bin/sh" => 'DirectoryName',
"1234987600000008" => "CreditCard",
}.each_pair do |input,rule|
it "should test #{rule} against #{input} as valid" do
validator.valid_string?("test",input,rule,100,true,true).should be_true
end
end
# test cases that should fail
{
"jeff.williams@@aspectsecurity.com" => "Email",
"jeff.williams@aspectsecurity" => "Email",
"..168.1.234"=> "IPAddress",
"10.x.1.234"=> "IPAddress",
"http:///www.aspectsecurity.com" => "URL",
"http://www.aspect security.com"=> "URL",
"987-65-4320"=> "SSN",
"000-00-0000"=> "SSN",
"(555) 555-5555"=> "SSN",
"test"=> "SSN",
"4417 1234 5678 911Z" => "CreditCard",
"C:test" => "DirectoryPath",
}.each_pair do |input,rule|
it "should test #{rule} against #{input} as invalid" do
validator.valid_string?("test",input,rule,100,false).should be_false
end
end
end
end
end
end