input_sanitizer 0.2.2 → 0.3.33

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +2 -0
  3. data/.travis.yml +2 -0
  4. data/CHANGELOG +92 -0
  5. data/LICENSE +201 -22
  6. data/README.md +7 -0
  7. data/input_sanitizer.gemspec +15 -5
  8. data/lib/input_sanitizer/errors.rb +142 -0
  9. data/lib/input_sanitizer/extended_converters/comma_joined_integers_converter.rb +15 -0
  10. data/lib/input_sanitizer/extended_converters/comma_joined_strings_converter.rb +15 -0
  11. data/lib/input_sanitizer/extended_converters/positive_integer_converter.rb +12 -0
  12. data/lib/input_sanitizer/extended_converters/specific_values_converter.rb +19 -0
  13. data/lib/input_sanitizer/extended_converters.rb +5 -55
  14. data/lib/input_sanitizer/restricted_hash.rb +49 -8
  15. data/lib/input_sanitizer/v1/clean_field.rb +38 -0
  16. data/lib/input_sanitizer/{default_converters.rb → v1/default_converters.rb} +8 -11
  17. data/lib/input_sanitizer/v1/sanitizer.rb +163 -0
  18. data/lib/input_sanitizer/v1.rb +22 -0
  19. data/lib/input_sanitizer/v2/clean_field.rb +36 -0
  20. data/lib/input_sanitizer/v2/clean_payload_collection_field.rb +41 -0
  21. data/lib/input_sanitizer/v2/clean_query_collection_field.rb +40 -0
  22. data/lib/input_sanitizer/v2/error_collection.rb +49 -0
  23. data/lib/input_sanitizer/v2/nested_sanitizer_factory.rb +19 -0
  24. data/lib/input_sanitizer/v2/payload_sanitizer.rb +130 -0
  25. data/lib/input_sanitizer/v2/payload_transform.rb +42 -0
  26. data/lib/input_sanitizer/v2/query_sanitizer.rb +33 -0
  27. data/lib/input_sanitizer/v2/types.rb +213 -0
  28. data/lib/input_sanitizer/v2.rb +13 -0
  29. data/lib/input_sanitizer/version.rb +1 -1
  30. data/lib/input_sanitizer.rb +5 -2
  31. data/spec/extended_converters/comma_joined_integers_converter_spec.rb +18 -0
  32. data/spec/extended_converters/comma_joined_strings_converter_spec.rb +18 -0
  33. data/spec/extended_converters/positive_integer_converter_spec.rb +18 -0
  34. data/spec/extended_converters/specific_values_converter_spec.rb +27 -0
  35. data/spec/restricted_hash_spec.rb +37 -7
  36. data/spec/sanitizer_spec.rb +32 -22
  37. data/spec/spec_helper.rb +3 -1
  38. data/spec/{default_converters_spec.rb → v1/default_converters_spec.rb} +27 -9
  39. data/spec/v2/converters_spec.rb +174 -0
  40. data/spec/v2/payload_sanitizer_spec.rb +460 -0
  41. data/spec/v2/payload_transform_spec.rb +98 -0
  42. data/spec/v2/query_sanitizer_spec.rb +300 -0
  43. data/v2.md +52 -0
  44. metadata +86 -30
  45. data/Gemfile.lock +0 -44
  46. data/lib/input_sanitizer/sanitizer.rb +0 -179
  47. data/spec/extended_converters_spec.rb +0 -78
@@ -0,0 +1,460 @@
1
+ require 'spec_helper'
2
+
3
+ class AddressSanitizer < InputSanitizer::V2::PayloadSanitizer
4
+ string :city
5
+ string :zip
6
+ end
7
+
8
+ class TagSanitizer < InputSanitizer::V2::PayloadSanitizer
9
+ integer :id
10
+ string :name
11
+ nested :addresses, :sanitizer => AddressSanitizer, :collection => true
12
+ end
13
+
14
+ class TestedPayloadSanitizer < InputSanitizer::V2::PayloadSanitizer
15
+ integer :array, :collection => true
16
+ integer :array_nil, :collection => true, :allow_nil => true
17
+ string :status, :allow => ['current', 'past']
18
+ string :status_with_empty, :allow => ['', 'current', 'past']
19
+ string :regexp_string, :regexp => /^#?([a-f0-9]{6}|[a-f0-9]{3})$/
20
+ nested :address, :sanitizer => AddressSanitizer
21
+ nested :nullable_address, :sanitizer => AddressSanitizer, :allow_nil => true
22
+ nested :tags, :sanitizer => TagSanitizer, :collection => true
23
+
24
+ float :float_attribute, :minimum => 1, :maximum => 100
25
+ integer :integer_attribute, :minimum => 1, :maximum => 100
26
+ string :string_attribute
27
+ boolean :bool_attribute
28
+ datetime :datetime_attribute
29
+ date :date_attribute, :minimum => Date.new(-2015, 01, 01), :maximum => Date.new(2015, 12, 31)
30
+
31
+ url :website
32
+ string :limited_collection, :collection => { :minimum => 1, :maximum => 2 }, :minimum => 2, :maximum => 12
33
+ string :allow_collection, :collection => true, :allow => ['yes', 'no']
34
+ end
35
+
36
+ class BlankValuesPayloadSanitizer < InputSanitizer::V2::PayloadSanitizer
37
+ string :required_string, :required => true
38
+ datetime :non_nil_datetime, :allow_nil => false
39
+ url :non_blank_url, :allow_blank => false
40
+ end
41
+
42
+ class CustomConverterWithProvidedValue < InputSanitizer::V2::PayloadSanitizer
43
+ integer :from
44
+ custom :to, :provide => :from, :converter => lambda { |value, options|
45
+ InputSanitizer::V2::Types::IntegerCheck.new.call(value)
46
+ raise InputSanitizer::ValueError.new(value, options[:provided][:from], nil) if options[:provided][:from] > value
47
+ value
48
+ }
49
+ end
50
+
51
+ describe InputSanitizer::V2::PayloadSanitizer do
52
+ let(:sanitizer) { TestedPayloadSanitizer.new(@params) }
53
+ let(:cleaned) { sanitizer.cleaned }
54
+
55
+ describe "collections" do
56
+ it "is invalid if collection is not an array" do
57
+ @params = { :array => {} }
58
+ sanitizer.should_not be_valid
59
+ end
60
+
61
+ it "is valid if collection is an array" do
62
+ @params = { :array => [] }
63
+ sanitizer.should be_valid
64
+ end
65
+
66
+ it "is valid if collection is an nil and allow_nil is passed" do
67
+ @params = { :array_nil => nil }
68
+ sanitizer.should be_valid
69
+ end
70
+
71
+ it "is invalid if there are too few elements" do
72
+ @params = { :limited_collection => [] }
73
+ sanitizer.should_not be_valid
74
+ end
75
+
76
+ it "is invalid if there are too many elements" do
77
+ @params = { :limited_collection => ['bear', 'bear', 'bear'] }
78
+ sanitizer.should_not be_valid
79
+ end
80
+
81
+ it "is valid when there are just enough elements" do
82
+ @params = { :limited_collection => ['goldilocks'] }
83
+ sanitizer.should be_valid
84
+ end
85
+
86
+ it "is invalid when any of the elements are too long" do
87
+ @params = { :limited_collection => ['more_than_the_limit'] }
88
+ sanitizer.should_not be_valid
89
+ end
90
+
91
+ it "is invalid when any of the elements are too short" do
92
+ @params = { :limited_collection => ['a'] }
93
+ sanitizer.should_not be_valid
94
+ end
95
+
96
+ it "is invalid when given disallowed value in a collection" do
97
+ @params = { :allow_collection => ['yes', 'no', 'whoa'] }
98
+ sanitizer.should_not be_valid
99
+ end
100
+
101
+ it "is valid when given allowed values in a collection" do
102
+ @params = { :allow_collection => ['yes', 'no'] }
103
+ sanitizer.should be_valid
104
+ end
105
+ end
106
+
107
+ describe "allow option" do
108
+ it "is valid when given an allowed string" do
109
+ @params = { :status => 'past' }
110
+ sanitizer.should be_valid
111
+ end
112
+
113
+ it "is invalid when given an empty string" do
114
+ @params = { :status => '' }
115
+ sanitizer.should_not be_valid
116
+ sanitizer.errors[0].field.should eq('/status')
117
+ end
118
+
119
+ it "is valid when given an allowed empty string" do
120
+ @params = { :status_with_empty => '' }
121
+ sanitizer.should be_valid
122
+ end
123
+
124
+ it "is invalid when given a disallowed string" do
125
+ @params = { :status => 'current bad string' }
126
+ sanitizer.should_not be_valid
127
+ sanitizer.errors[0].field.should eq('/status')
128
+ end
129
+ end
130
+
131
+ describe "minimum and maximum options" do
132
+ it "is invalid if integer is lower than the minimum" do
133
+ @params = { :integer_attribute => 0 }
134
+ sanitizer.should_not be_valid
135
+ end
136
+
137
+ it "is invalid if integer is greater than the maximum" do
138
+ @params = { :integer_attribute => 101 }
139
+ sanitizer.should_not be_valid
140
+ end
141
+
142
+ it "is valid when integer is within given range" do
143
+ @params = { :limited_collection => ['goldilocks'] }
144
+ sanitizer.should be_valid
145
+ end
146
+
147
+ it "is invalid if float is lower than the minimum" do
148
+ @params = { :float_attribute => 0.0 }
149
+ sanitizer.should_not be_valid
150
+ end
151
+
152
+ it "is invalid if float is greater than the maximum" do
153
+ @params = { :float_attribute => 101.0 }
154
+ sanitizer.should_not be_valid
155
+ end
156
+ end
157
+
158
+ describe "strict param checking" do
159
+ it "is invalid when given extra params" do
160
+ @params = { :extra => 'test', :extra2 => 1 }
161
+ sanitizer.should_not be_valid
162
+ sanitizer.errors.count.should eq(2)
163
+ end
164
+
165
+ it "is invalid when given extra params in a nested sanitizer" do
166
+ @params = { :address => { :extra => 0 }, :tags => [ { :extra2 => 1 } ] }
167
+ sanitizer.should_not be_valid
168
+ sanitizer.errors.map(&:field).should contain_exactly('/address/extra', '/tags/0/extra2')
169
+ end
170
+ end
171
+
172
+ describe "converters with provided values" do
173
+ let(:sanitizer) { CustomConverterWithProvidedValue.new(@params) }
174
+
175
+ it "is valid when converter passes check with provided value" do
176
+ @params = {from: 1, to: 3}
177
+ sanitizer.should be_valid
178
+ end
179
+
180
+ it "is invalid when converter does not pass check with provided value" do
181
+ @params = {from: 3, to: 1}
182
+ sanitizer.should_not be_valid
183
+ end
184
+ end
185
+
186
+ describe "strict type checking" do
187
+ it "is invalid when given string instead of integer" do
188
+ @params = { :integer_attribute => '1' }
189
+ sanitizer.should_not be_valid
190
+ sanitizer.errors[0].field.should eq('/integer_attribute')
191
+ end
192
+
193
+ it "is valid when given an integer" do
194
+ @params = { :integer_attribute => 50 }
195
+ sanitizer.should be_valid
196
+ sanitizer[:integer_attribute].should eq(50)
197
+ end
198
+
199
+ it "is invalid when given a float" do
200
+ @params = { :integer_attribute => 50.99 }
201
+ sanitizer.should_not be_valid
202
+ end
203
+
204
+ it "is valid when given nil for an integer" do
205
+ @params = { :integer_attribute => nil }
206
+ sanitizer.should be_valid
207
+ sanitizer[:integer_attribute].should be_nil
208
+ end
209
+
210
+ it "is invalid when given string instead of float" do
211
+ @params = { :float_attribute => '1' }
212
+ sanitizer.should_not be_valid
213
+ sanitizer.errors[0].field.should eq('/float_attribute')
214
+ end
215
+
216
+ it "is valid when given a float" do
217
+ @params = { :float_attribute => 50.0 }
218
+ sanitizer.should be_valid
219
+ sanitizer[:float_attribute].should eq(50.0)
220
+ end
221
+
222
+ it "is valid when given nil for a float" do
223
+ @params = { :float_attribute => nil }
224
+ sanitizer.should be_valid
225
+ sanitizer[:float_attribute].should be_nil
226
+ end
227
+
228
+ it "is valid when given string is matching regexp" do
229
+ @params = { :regexp_string => "#8bd635" }
230
+ sanitizer.should be_valid
231
+ sanitizer[:regexp_string].should eq('#8bd635')
232
+ end
233
+
234
+ it "is invalid when given string is not matching regexp" do
235
+ @params = { :regexp_string => "not a hex value" }
236
+ sanitizer.should_not be_valid
237
+ sanitizer.errors[0].field.should eq('/regexp_string')
238
+ end
239
+
240
+ it "is invalid when given integer instead of string" do
241
+ @params = { :string_attribute => 0 }
242
+ sanitizer.should_not be_valid
243
+ sanitizer.errors[0].field.should eq('/string_attribute')
244
+ end
245
+
246
+ it "is invalid when given float instead of string" do
247
+ @params = { :string_attribute => 3.1415 }
248
+ sanitizer.should_not be_valid
249
+ sanitizer.errors[0].field.should eq('/string_attribute')
250
+ end
251
+
252
+ it "is valid when given a string" do
253
+ @params = { :string_attribute => '#@!#%#$@#ad' }
254
+ sanitizer.should be_valid
255
+ sanitizer[:string_attribute].should eq('#@!#%#$@#ad')
256
+ end
257
+
258
+ it "is invalid when given 'yes' as a bool" do
259
+ @params = { :bool_attribute => 'yes' }
260
+ sanitizer.should_not be_valid
261
+ sanitizer.errors[0].field.should eq('/bool_attribute')
262
+ end
263
+
264
+ it "is valid when given true as a bool" do
265
+ @params = { :bool_attribute => true }
266
+ sanitizer.should be_valid
267
+ end
268
+
269
+ it "is valid when given false as a bool" do
270
+ @params = { :bool_attribute => false }
271
+ sanitizer.should be_valid
272
+ end
273
+
274
+ it "is invalid when given an incorrect datetime" do
275
+ @params = { :datetime_attribute => "2014-08-2716:32:56Z" }
276
+ sanitizer.should_not be_valid
277
+ sanitizer.errors[0].field.should eq('/datetime_attribute')
278
+ end
279
+
280
+ it "is valid when given a correct datetime" do
281
+ @params = { :datetime_attribute => "2014-08-27T16:32:56Z" }
282
+ sanitizer.should be_valid
283
+ end
284
+
285
+ it "is valid when given a 'forever' timestamp" do
286
+ @params = { :datetime_attribute => "9999-12-31T00:00:00Z" }
287
+ sanitizer.should be_valid
288
+ end
289
+
290
+ it "is invalid when given an incorrect date" do
291
+ @params = { :date_attribute => "invalid" }
292
+ sanitizer.should_not be_valid
293
+ sanitizer.errors[0].field.should eq('/date_attribute')
294
+ end
295
+
296
+ it "is valid when given a correct date" do
297
+ @params = { :date_attribute => "2015-08-27" }
298
+ sanitizer.should be_valid
299
+ end
300
+
301
+ it "is valid when given a correct negative date" do
302
+ @params = { :date_attribute => "-2014-08-27" }
303
+ sanitizer.should be_valid
304
+ end
305
+
306
+ it "is valid when given a correct URL" do
307
+ @params = { :website => "https://google.com" }
308
+ sanitizer.should be_valid
309
+ sanitizer[:website].should eq("https://google.com")
310
+ end
311
+
312
+ it "is invalid when given an invalid URL" do
313
+ @params = { :website => "ht:/google.com" }
314
+ sanitizer.should_not be_valid
315
+ end
316
+
317
+ it "is invalid when given an invalid URL that contains a valid URL" do
318
+ @params = { :website => "watwat http://google.com wat" }
319
+ sanitizer.should_not be_valid
320
+ end
321
+
322
+ describe "blank and required values" do
323
+ let(:sanitizer) { BlankValuesPayloadSanitizer.new(@params) }
324
+ let(:defaults) { { :required_string => 'zz' } }
325
+
326
+ it "is invalid if required string is missing" do
327
+ @params = {}
328
+ sanitizer.should_not be_valid
329
+ sanitizer.errors[0].should be_an_instance_of(InputSanitizer::ValueMissingError)
330
+ sanitizer.errors[0].field.should eq('/required_string')
331
+ end
332
+
333
+ it "is invalid if required string is nil" do
334
+ @params = { :required_string => nil }
335
+ sanitizer.should_not be_valid
336
+ sanitizer.errors[0].should be_an_instance_of(InputSanitizer::BlankValueError)
337
+ sanitizer.errors[0].field.should eq('/required_string')
338
+ end
339
+
340
+ it "is invalid if required string is blank" do
341
+ @params = { :required_string => ' ' }
342
+ sanitizer.should_not be_valid
343
+ sanitizer.errors[0].should be_an_instance_of(InputSanitizer::BlankValueError)
344
+ sanitizer.errors[0].field.should eq('/required_string')
345
+ end
346
+
347
+ it "is invalid if non-nil datetime is null" do
348
+ @params = defaults.merge({ :non_nil_datetime => nil })
349
+ sanitizer.should_not be_valid
350
+ sanitizer.errors[0].should be_an_instance_of(InputSanitizer::BlankValueError)
351
+ sanitizer.errors[0].field.should eq('/non_nil_datetime')
352
+ end
353
+
354
+ it "is valid if non-nil datetime is blank" do
355
+ @params = defaults.merge({ :non_nil_datetime => '' })
356
+ sanitizer.should be_valid
357
+ end
358
+
359
+ it "is invalid if non-blank url is nil" do
360
+ @params = defaults.merge({ :non_blank_url => nil })
361
+ sanitizer.should_not be_valid
362
+ sanitizer.errors[0].should be_an_instance_of(InputSanitizer::BlankValueError)
363
+ sanitizer.errors[0].field.should eq('/non_blank_url')
364
+ end
365
+
366
+ it "is invalid if non-blank url is blank" do
367
+ @params = defaults.merge({ :non_blank_url => '' })
368
+ sanitizer.should_not be_valid
369
+ sanitizer.errors[0].should be_an_instance_of(InputSanitizer::BlankValueError)
370
+ sanitizer.errors[0].field.should eq('/non_blank_url')
371
+ end
372
+ end
373
+
374
+ describe "nested checking" do
375
+ describe "simple array" do
376
+ it "returns JSON pointer for invalid fields" do
377
+ @params = { :array => [1, 'z', '3', 4] }
378
+ sanitizer.errors.length.should eq(2)
379
+ sanitizer.errors.map(&:field).should contain_exactly('/array/1', '/array/2')
380
+ end
381
+ end
382
+
383
+ describe "nested object" do
384
+ it "returns an error when given a nil for a nested value" do
385
+ @params = { :address => nil }
386
+ sanitizer.should_not be_valid
387
+ end
388
+
389
+ it "returns an error when given a string for a nested value" do
390
+ @params = { :address => 'nope' }
391
+ sanitizer.should_not be_valid
392
+ end
393
+
394
+ it "returns an error when given an array for a nested value" do
395
+ @params = { :address => ['a'] }
396
+ sanitizer.should_not be_valid
397
+ end
398
+
399
+ it "returns JSON pointer for invalid fields" do
400
+ @params = { :address => { :city => 0, :zip => 1 } }
401
+ sanitizer.errors.length.should eq(2)
402
+ sanitizer.errors.map(&:field).should contain_exactly('/address/city', '/address/zip')
403
+ end
404
+
405
+ it "allows nil with `allow_nil` flag" do
406
+ @params = { :nullable_address => nil }
407
+ sanitizer.should be_valid
408
+ sanitizer.cleaned.fetch(:nullable_address).should eq(nil)
409
+ end
410
+ end
411
+
412
+ describe "array of nested objects" do
413
+ it "returns an error when given a nil for a collection" do
414
+ @params = { :tags => nil }
415
+ sanitizer.should_not be_valid
416
+ end
417
+
418
+ it "returns an error when given a string for a collection" do
419
+ @params = { :tags => 'nope' }
420
+ sanitizer.should_not be_valid
421
+ end
422
+
423
+ it "returns an error when given a hash for a collection" do
424
+ @params = { :tags => { :a => 1 } }
425
+ sanitizer.should_not be_valid
426
+ end
427
+
428
+ it "returns JSON pointer for invalid fields" do
429
+ @params = { :tags => [ { :id => 'n', :name => 1 }, { :id => 10, :name => 2 } ] }
430
+ sanitizer.errors.length.should eq(3)
431
+ sanitizer.errors.map(&:field).should contain_exactly(
432
+ '/tags/0/id',
433
+ '/tags/0/name',
434
+ '/tags/1/name'
435
+ )
436
+ end
437
+ end
438
+
439
+ describe "array of nested objects that have array of nested objects" do
440
+ it "returns JSON pointer for invalid fields" do
441
+ @params = { :tags => [
442
+ { :id => 'n', :addresses => [ { :city => 0 }, { :city => 1 } ] },
443
+ { :name => 2, :addresses => [ { :city => 3 } ] },
444
+ ] }
445
+ sanitizer.errors.length.should eq(5)
446
+ sanitizer.errors.map(&:field).should contain_exactly(
447
+ '/tags/0/id',
448
+ '/tags/0/addresses/0/city',
449
+ '/tags/0/addresses/1/city',
450
+ '/tags/1/name',
451
+ '/tags/1/addresses/0/city'
452
+ )
453
+
454
+ ec = sanitizer.error_collection
455
+ ec.length.should eq(5)
456
+ end
457
+ end
458
+ end
459
+ end
460
+ end