input_sanitizer 0.1.9 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/gempush.yml +28 -0
- data/.gitignore +2 -1
- data/.travis.yml +4 -8
- data/CHANGELOG +96 -0
- data/LICENSE +201 -22
- data/README.md +22 -3
- data/input_sanitizer.gemspec +10 -4
- data/lib/input_sanitizer.rb +5 -2
- data/lib/input_sanitizer/errors.rb +142 -0
- data/lib/input_sanitizer/extended_converters.rb +5 -52
- data/lib/input_sanitizer/extended_converters/comma_joined_integers_converter.rb +15 -0
- data/lib/input_sanitizer/extended_converters/comma_joined_strings_converter.rb +15 -0
- data/lib/input_sanitizer/extended_converters/positive_integer_converter.rb +12 -0
- data/lib/input_sanitizer/extended_converters/specific_values_converter.rb +19 -0
- data/lib/input_sanitizer/restricted_hash.rb +49 -8
- data/lib/input_sanitizer/v1.rb +22 -0
- data/lib/input_sanitizer/v1/clean_field.rb +38 -0
- data/lib/input_sanitizer/{default_converters.rb → v1/default_converters.rb} +30 -13
- data/lib/input_sanitizer/v1/sanitizer.rb +166 -0
- data/lib/input_sanitizer/v2.rb +13 -0
- data/lib/input_sanitizer/v2/clean_field.rb +36 -0
- data/lib/input_sanitizer/v2/clean_payload_collection_field.rb +41 -0
- data/lib/input_sanitizer/v2/clean_query_collection_field.rb +40 -0
- data/lib/input_sanitizer/v2/error_collection.rb +49 -0
- data/lib/input_sanitizer/v2/nested_sanitizer_factory.rb +19 -0
- data/lib/input_sanitizer/v2/payload_sanitizer.rb +130 -0
- data/lib/input_sanitizer/v2/payload_transform.rb +42 -0
- data/lib/input_sanitizer/v2/query_sanitizer.rb +33 -0
- data/lib/input_sanitizer/v2/types.rb +213 -0
- data/lib/input_sanitizer/version.rb +1 -1
- data/spec/extended_converters/comma_joined_integers_converter_spec.rb +18 -0
- data/spec/extended_converters/comma_joined_strings_converter_spec.rb +18 -0
- data/spec/extended_converters/positive_integer_converter_spec.rb +18 -0
- data/spec/extended_converters/specific_values_converter_spec.rb +27 -0
- data/spec/restricted_hash_spec.rb +37 -7
- data/spec/sanitizer_spec.rb +129 -26
- data/spec/spec_helper.rb +17 -2
- data/spec/v1/default_converters_spec.rb +141 -0
- data/spec/v2/converters_spec.rb +174 -0
- data/spec/v2/payload_sanitizer_spec.rb +460 -0
- data/spec/v2/payload_transform_spec.rb +98 -0
- data/spec/v2/query_sanitizer_spec.rb +300 -0
- data/v2.md +52 -0
- metadata +105 -40
- data/lib/input_sanitizer/sanitizer.rb +0 -152
- data/spec/default_converters_spec.rb +0 -101
- data/spec/extended_converters_spec.rb +0 -62
metadata
CHANGED
@@ -1,106 +1,171 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: input_sanitizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.4.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
|
-
-
|
9
|
-
- Tomasz Werbicki
|
10
|
-
- Michal Bugno
|
7
|
+
- Zendesk
|
11
8
|
autorequire:
|
12
9
|
bindir: bin
|
13
10
|
cert_chain: []
|
14
|
-
date:
|
11
|
+
date: 2020-08-20 00:00:00.000000000 Z
|
15
12
|
dependencies:
|
16
13
|
- !ruby/object:Gem::Dependency
|
17
|
-
name:
|
14
|
+
name: method_struct
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.2.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.2.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
18
29
|
requirement: !ruby/object:Gem::Requirement
|
19
|
-
none: false
|
20
30
|
requirements:
|
21
|
-
- -
|
31
|
+
- - ">="
|
22
32
|
- !ruby/object:Gem::Version
|
23
|
-
version:
|
33
|
+
version: 3.0.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.0.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.10.1
|
24
48
|
type: :development
|
25
49
|
prerelease: false
|
26
50
|
version_requirements: !ruby/object:Gem::Requirement
|
27
|
-
none: false
|
28
51
|
requirements:
|
29
|
-
- -
|
52
|
+
- - "~>"
|
30
53
|
- !ruby/object:Gem::Version
|
31
|
-
version:
|
54
|
+
version: 0.10.1
|
32
55
|
- !ruby/object:Gem::Dependency
|
33
56
|
name: simplecov
|
34
57
|
requirement: !ruby/object:Gem::Requirement
|
35
|
-
none: false
|
36
58
|
requirements:
|
37
|
-
- -
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.9.2
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.9.2
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
38
74
|
- !ruby/object:Gem::Version
|
39
|
-
version:
|
75
|
+
version: 3.2.0
|
40
76
|
type: :development
|
41
77
|
prerelease: false
|
42
78
|
version_requirements: !ruby/object:Gem::Requirement
|
43
|
-
none: false
|
44
79
|
requirements:
|
45
|
-
- -
|
80
|
+
- - "~>"
|
46
81
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
82
|
+
version: 3.2.0
|
48
83
|
description: Gem to sanitize hash of incoming data
|
49
84
|
email:
|
50
|
-
-
|
51
|
-
- tomasz@futuresimple.com
|
52
|
-
- michal@futuresimple.com
|
85
|
+
- opensource@zendesk.com
|
53
86
|
executables: []
|
54
87
|
extensions: []
|
55
88
|
extra_rdoc_files: []
|
56
89
|
files:
|
57
|
-
- .
|
58
|
-
- .
|
59
|
-
- .
|
90
|
+
- ".github/workflows/gempush.yml"
|
91
|
+
- ".gitignore"
|
92
|
+
- ".rspec"
|
93
|
+
- ".travis.yml"
|
94
|
+
- CHANGELOG
|
60
95
|
- Gemfile
|
61
96
|
- LICENSE
|
62
97
|
- README.md
|
63
98
|
- Rakefile
|
64
99
|
- input_sanitizer.gemspec
|
65
100
|
- lib/input_sanitizer.rb
|
66
|
-
- lib/input_sanitizer/
|
101
|
+
- lib/input_sanitizer/errors.rb
|
67
102
|
- lib/input_sanitizer/extended_converters.rb
|
103
|
+
- lib/input_sanitizer/extended_converters/comma_joined_integers_converter.rb
|
104
|
+
- lib/input_sanitizer/extended_converters/comma_joined_strings_converter.rb
|
105
|
+
- lib/input_sanitizer/extended_converters/positive_integer_converter.rb
|
106
|
+
- lib/input_sanitizer/extended_converters/specific_values_converter.rb
|
68
107
|
- lib/input_sanitizer/restricted_hash.rb
|
69
|
-
- lib/input_sanitizer/
|
108
|
+
- lib/input_sanitizer/v1.rb
|
109
|
+
- lib/input_sanitizer/v1/clean_field.rb
|
110
|
+
- lib/input_sanitizer/v1/default_converters.rb
|
111
|
+
- lib/input_sanitizer/v1/sanitizer.rb
|
112
|
+
- lib/input_sanitizer/v2.rb
|
113
|
+
- lib/input_sanitizer/v2/clean_field.rb
|
114
|
+
- lib/input_sanitizer/v2/clean_payload_collection_field.rb
|
115
|
+
- lib/input_sanitizer/v2/clean_query_collection_field.rb
|
116
|
+
- lib/input_sanitizer/v2/error_collection.rb
|
117
|
+
- lib/input_sanitizer/v2/nested_sanitizer_factory.rb
|
118
|
+
- lib/input_sanitizer/v2/payload_sanitizer.rb
|
119
|
+
- lib/input_sanitizer/v2/payload_transform.rb
|
120
|
+
- lib/input_sanitizer/v2/query_sanitizer.rb
|
121
|
+
- lib/input_sanitizer/v2/types.rb
|
70
122
|
- lib/input_sanitizer/version.rb
|
71
|
-
- spec/
|
72
|
-
- spec/
|
123
|
+
- spec/extended_converters/comma_joined_integers_converter_spec.rb
|
124
|
+
- spec/extended_converters/comma_joined_strings_converter_spec.rb
|
125
|
+
- spec/extended_converters/positive_integer_converter_spec.rb
|
126
|
+
- spec/extended_converters/specific_values_converter_spec.rb
|
73
127
|
- spec/restricted_hash_spec.rb
|
74
128
|
- spec/sanitizer_spec.rb
|
75
129
|
- spec/spec_helper.rb
|
130
|
+
- spec/v1/default_converters_spec.rb
|
131
|
+
- spec/v2/converters_spec.rb
|
132
|
+
- spec/v2/payload_sanitizer_spec.rb
|
133
|
+
- spec/v2/payload_transform_spec.rb
|
134
|
+
- spec/v2/query_sanitizer_spec.rb
|
135
|
+
- v2.md
|
76
136
|
homepage: ''
|
77
|
-
licenses:
|
137
|
+
licenses:
|
138
|
+
- Apache-2.0
|
139
|
+
metadata: {}
|
78
140
|
post_install_message:
|
79
141
|
rdoc_options: []
|
80
142
|
require_paths:
|
81
143
|
- lib
|
82
144
|
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
-
none: false
|
84
145
|
requirements:
|
85
|
-
- -
|
146
|
+
- - ">="
|
86
147
|
- !ruby/object:Gem::Version
|
87
148
|
version: '0'
|
88
149
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
-
none: false
|
90
150
|
requirements:
|
91
|
-
- -
|
151
|
+
- - ">="
|
92
152
|
- !ruby/object:Gem::Version
|
93
153
|
version: '0'
|
94
154
|
requirements: []
|
95
|
-
|
96
|
-
rubygems_version: 1.8.23
|
155
|
+
rubygems_version: 3.0.3
|
97
156
|
signing_key:
|
98
|
-
specification_version:
|
157
|
+
specification_version: 4
|
99
158
|
summary: Gem to sanitize hash of incoming data
|
100
159
|
test_files:
|
101
|
-
- spec/
|
102
|
-
- spec/
|
160
|
+
- spec/extended_converters/comma_joined_integers_converter_spec.rb
|
161
|
+
- spec/extended_converters/comma_joined_strings_converter_spec.rb
|
162
|
+
- spec/extended_converters/positive_integer_converter_spec.rb
|
163
|
+
- spec/extended_converters/specific_values_converter_spec.rb
|
103
164
|
- spec/restricted_hash_spec.rb
|
104
165
|
- spec/sanitizer_spec.rb
|
105
166
|
- spec/spec_helper.rb
|
106
|
-
|
167
|
+
- spec/v1/default_converters_spec.rb
|
168
|
+
- spec/v2/converters_spec.rb
|
169
|
+
- spec/v2/payload_sanitizer_spec.rb
|
170
|
+
- spec/v2/payload_transform_spec.rb
|
171
|
+
- spec/v2/query_sanitizer_spec.rb
|
@@ -1,152 +0,0 @@
|
|
1
|
-
require 'input_sanitizer/restricted_hash'
|
2
|
-
require 'input_sanitizer/default_converters'
|
3
|
-
|
4
|
-
class InputSanitizer::Sanitizer
|
5
|
-
def initialize(data)
|
6
|
-
@data = symbolize_keys(data)
|
7
|
-
@performed = false
|
8
|
-
@errors = []
|
9
|
-
@cleaned = InputSanitizer::RestrictedHash.new(self.class.fields.keys)
|
10
|
-
end
|
11
|
-
|
12
|
-
def self.clean(data)
|
13
|
-
new(data).cleaned
|
14
|
-
end
|
15
|
-
|
16
|
-
def [](field)
|
17
|
-
cleaned[field]
|
18
|
-
end
|
19
|
-
|
20
|
-
def cleaned
|
21
|
-
return @cleaned if @performed
|
22
|
-
self.class.fields.each do |field, hash|
|
23
|
-
type = hash[:type]
|
24
|
-
required = hash[:options][:required]
|
25
|
-
clean_field(field, type, required)
|
26
|
-
end
|
27
|
-
@performed = true
|
28
|
-
@cleaned.freeze
|
29
|
-
end
|
30
|
-
|
31
|
-
def valid?
|
32
|
-
cleaned
|
33
|
-
@errors.empty?
|
34
|
-
end
|
35
|
-
|
36
|
-
def errors
|
37
|
-
cleaned
|
38
|
-
@errors
|
39
|
-
end
|
40
|
-
|
41
|
-
def self.converters
|
42
|
-
{
|
43
|
-
:integer => InputSanitizer::IntegerConverter.new,
|
44
|
-
:string => InputSanitizer::StringConverter.new,
|
45
|
-
:date => InputSanitizer::DateConverter.new,
|
46
|
-
:time => InputSanitizer::TimeConverter.new,
|
47
|
-
:boolean => InputSanitizer::BooleanConverter.new,
|
48
|
-
:integer_or_blank => InputSanitizer::IntegerConverter.new.extend(InputSanitizer::AllowNil),
|
49
|
-
:string_or_blank => InputSanitizer::StringConverter.new.extend(InputSanitizer::AllowNil),
|
50
|
-
:date_or_blank => InputSanitizer::DateConverter.new.extend(InputSanitizer::AllowNil),
|
51
|
-
:time_or_blank => InputSanitizer::TimeConverter.new.extend(InputSanitizer::AllowNil),
|
52
|
-
:boolean_or_blank => InputSanitizer::BooleanConverter.new.extend(InputSanitizer::AllowNil),
|
53
|
-
}
|
54
|
-
end
|
55
|
-
|
56
|
-
def self.inherited(subclass)
|
57
|
-
subclass.fields = self.fields.dup
|
58
|
-
end
|
59
|
-
|
60
|
-
def self.string(*keys)
|
61
|
-
set_keys_to_type(keys, :string)
|
62
|
-
end
|
63
|
-
|
64
|
-
def self.integer(*keys)
|
65
|
-
set_keys_to_type(keys, :integer)
|
66
|
-
end
|
67
|
-
|
68
|
-
def self.boolean(*keys)
|
69
|
-
set_keys_to_type(keys, :boolean)
|
70
|
-
end
|
71
|
-
|
72
|
-
def self.date(*keys)
|
73
|
-
set_keys_to_type(keys, :date)
|
74
|
-
end
|
75
|
-
|
76
|
-
def self.time(*keys)
|
77
|
-
set_keys_to_type(keys, :time)
|
78
|
-
end
|
79
|
-
|
80
|
-
def self.custom(*keys)
|
81
|
-
options = keys.pop
|
82
|
-
converter = options.delete(:converter)
|
83
|
-
keys.push(options)
|
84
|
-
raise "You did not define a converter for a custom type" if converter == nil
|
85
|
-
self.set_keys_to_type(keys, converter)
|
86
|
-
end
|
87
|
-
|
88
|
-
protected
|
89
|
-
def self.fields
|
90
|
-
@fields ||= {}
|
91
|
-
end
|
92
|
-
|
93
|
-
def self.fields=(new_fields)
|
94
|
-
@fields = new_fields
|
95
|
-
end
|
96
|
-
|
97
|
-
private
|
98
|
-
def self.extract_options!(array)
|
99
|
-
array.last.is_a?(Hash) ? array.pop : {}
|
100
|
-
end
|
101
|
-
|
102
|
-
def self.extract_options(array)
|
103
|
-
array.last.is_a?(Hash) ? array.last : {}
|
104
|
-
end
|
105
|
-
|
106
|
-
def clean_field(field, type, required)
|
107
|
-
if @data.has_key?(field)
|
108
|
-
begin
|
109
|
-
@cleaned[field] = convert(field, type)
|
110
|
-
rescue InputSanitizer::ConversionError => ex
|
111
|
-
add_error(field, :invalid_value, @data[field], ex.message)
|
112
|
-
end
|
113
|
-
elsif required
|
114
|
-
add_missing(field)
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
def add_error(field, error_type, value, description = nil)
|
119
|
-
@errors << {
|
120
|
-
:field => field,
|
121
|
-
:type => error_type,
|
122
|
-
:value => value,
|
123
|
-
:description => description
|
124
|
-
}
|
125
|
-
end
|
126
|
-
|
127
|
-
def add_missing(field)
|
128
|
-
add_error(field, :missing, nil, nil)
|
129
|
-
end
|
130
|
-
|
131
|
-
def convert(field, type)
|
132
|
-
converter(type).call(@data[field])
|
133
|
-
end
|
134
|
-
|
135
|
-
def converter(type)
|
136
|
-
type.respond_to?(:call) ? type : self.class.converters[type]
|
137
|
-
end
|
138
|
-
|
139
|
-
def symbolize_keys(data)
|
140
|
-
data.inject({}) do |memo, kv|
|
141
|
-
memo[kv.first.to_sym] = kv.last
|
142
|
-
memo
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
def self.set_keys_to_type(keys, type)
|
147
|
-
opts = extract_options!(keys)
|
148
|
-
keys.each do |key|
|
149
|
-
fields[key] = { :type => type, :options => opts }
|
150
|
-
end
|
151
|
-
end
|
152
|
-
end
|
@@ -1,101 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe InputSanitizer::IntegerConverter do
|
4
|
-
let(:converter) { InputSanitizer::IntegerConverter.new }
|
5
|
-
|
6
|
-
it "casts string to integer" do
|
7
|
-
converter.call("42").should == 42
|
8
|
-
end
|
9
|
-
|
10
|
-
it "casts integer to integer" do
|
11
|
-
converter.call(42).should == 42
|
12
|
-
end
|
13
|
-
|
14
|
-
it "raises error if cannot cast" do
|
15
|
-
lambda { converter.call("f") }.should raise_error(InputSanitizer::ConversionError)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
describe InputSanitizer::DateConverter do
|
20
|
-
let(:converter) { InputSanitizer::DateConverter.new }
|
21
|
-
|
22
|
-
it "casts dates in iso format" do
|
23
|
-
converter.call("2012-05-15").should == Date.new(2012, 5, 15)
|
24
|
-
end
|
25
|
-
|
26
|
-
it "raises error if cannot cast" do
|
27
|
-
lambda { converter.call("2012-02-30") }.should raise_error(InputSanitizer::ConversionError)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
describe InputSanitizer::BooleanConverter do
|
32
|
-
let(:converter) { InputSanitizer::BooleanConverter.new }
|
33
|
-
|
34
|
-
it "casts 'true' to true" do
|
35
|
-
converter.call('true').should be_true
|
36
|
-
end
|
37
|
-
|
38
|
-
it "casts true to true" do
|
39
|
-
converter.call(true).should be_true
|
40
|
-
end
|
41
|
-
|
42
|
-
it "casts '1' to true" do
|
43
|
-
converter.call('1').should be_true
|
44
|
-
end
|
45
|
-
|
46
|
-
it "casts 'yes' to true" do
|
47
|
-
converter.call('yes').should be_true
|
48
|
-
end
|
49
|
-
|
50
|
-
it "casts 'false' to false" do
|
51
|
-
converter.call('false').should be_false
|
52
|
-
end
|
53
|
-
|
54
|
-
it "casts false to false" do
|
55
|
-
converter.call(false).should be_false
|
56
|
-
end
|
57
|
-
|
58
|
-
it "casts '0' to false" do
|
59
|
-
converter.call('0').should be_false
|
60
|
-
end
|
61
|
-
|
62
|
-
it "casts 'no' to false" do
|
63
|
-
converter.call('no').should be_false
|
64
|
-
end
|
65
|
-
|
66
|
-
it "raises error if cannot cast" do
|
67
|
-
lambda { converter.call("notboolean") }.should raise_error(InputSanitizer::ConversionError)
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
|
72
|
-
describe InputSanitizer::TimeConverter do
|
73
|
-
let(:converter) { InputSanitizer::TimeConverter.new }
|
74
|
-
|
75
|
-
it "raises if timezone part given" do
|
76
|
-
lambda { converter.call("2012-05-15 13:42:54 +01:00") }.should raise_error(InputSanitizer::ConversionError)
|
77
|
-
end
|
78
|
-
|
79
|
-
it "casts date time in iso format" do
|
80
|
-
t = Time.utc(2012, 5, 15, 13, 42, 54)
|
81
|
-
converter.call("2012-05-15 13:42:54").should == t
|
82
|
-
converter.call("2012-05-15T13:42:54").should == t
|
83
|
-
converter.call("20120515134254").should == t
|
84
|
-
|
85
|
-
end
|
86
|
-
|
87
|
-
it "does not require time part" do
|
88
|
-
converter.call("2012-05-15 13:42").should == Time.utc(2012, 5, 15, 13, 42)
|
89
|
-
converter.call("2012-05-15 13").should == Time.utc(2012, 5, 15, 13)
|
90
|
-
converter.call("2012-05-15").should == Time.utc(2012, 5, 15)
|
91
|
-
|
92
|
-
end
|
93
|
-
|
94
|
-
it "raises error if can format is wrong" do
|
95
|
-
lambda { converter.call("2/10/2031 13:44:22") }.should raise_error(InputSanitizer::ConversionError)
|
96
|
-
end
|
97
|
-
|
98
|
-
it "raises error if date is wrong" do
|
99
|
-
lambda { converter.call("2012-02-32") }.should raise_error(InputSanitizer::ConversionError)
|
100
|
-
end
|
101
|
-
end
|