Skip to content

Commit bf90e95

Browse files
ericproulxdblock
andauthored
Call valid_encoding? before scrub (#2646)
Co-authored-by: Daniel (dB.) Doubrovkine <dblock@dblock.org>
1 parent a3b9c9e commit bf90e95

File tree

5 files changed

+15
-4
lines changed

5 files changed

+15
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* [#2639](https://github.com/ruby-grape/grape/pull/2639): Refactor mime_types_for - [@ericproulx](https://github.com/ericproulx).
1111
* [#2638](https://github.com/ruby-grape/grape/pull/2638): Remove unnecessary path string duplication - [@ericproulx](https://github.com/ericproulx).
1212
* [#2643](https://github.com/ruby-grape/grape/pull/2638): Remove `try` method in codebase - [@ericproulx](https://github.com/ericproulx).
13+
* [#2646](https://github.com/ruby-grape/grape/pull/2646): Call `valid_encoding?` before scrub - [@ericproulx](https://github.com/ericproulx).
1314
* [#2644](https://github.com/ruby-grape/grape/pull/2644): Clean useless/not valuable dependencies - [@ericproulx](https://github.com/ericproulx).
1415
* Your contribution here.
1516

lib/grape/middleware/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def merge_default_options(options)
111111
end
112112

113113
def try_scrub(obj)
114-
(obj.respond_to?(:scrub) && obj.scrub) || obj
114+
obj.respond_to?(:valid_encoding?) && !obj.valid_encoding? ? obj.scrub : obj
115115
end
116116
end
117117
end

lib/grape/validations/validators/allow_blank_validator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def validate_param!(attr_name, params)
88
return if (options_key?(:value) ? @option[:value] : @option) || !params.is_a?(Hash)
99

1010
value = params[attr_name]
11-
value = value.scrub if value.respond_to?(:scrub)
11+
value = value.scrub if value.respond_to?(:valid_encoding?) && !value.valid_encoding?
1212

1313
return if value == false || value.present?
1414

lib/grape/validations/validators/regexp_validator.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,20 @@ module Validators
66
class RegexpValidator < Base
77
def validate_param!(attr_name, params)
88
return unless params.respond_to?(:key) && params.key?(attr_name)
9-
return if Array.wrap(params[attr_name]).all? { |param| param.nil? || param.to_s.scrub.match?((options_key?(:value) ? @option[:value] : @option)) }
9+
10+
value = options_key?(:value) ? @option[:value] : @option
11+
return if Array.wrap(params[attr_name]).all? { |param| param.nil? || scrub(param.to_s).match?(value) }
1012

1113
raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message(:regexp))
1214
end
15+
16+
private
17+
18+
def scrub(param)
19+
return param if param.valid_encoding?
20+
21+
param.scrub
22+
end
1323
end
1424
end
1525
end

lib/grape/validations/validators/values_validator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def validate_param!(attr_name, params)
1616

1717
return if val.nil? && !required_for_root_scope?
1818

19-
val = val.scrub if val.respond_to?(:scrub)
19+
val = val.scrub if val.respond_to?(:valid_encoding?) && !val.valid_encoding?
2020

2121
# don't forget that +false.blank?+ is true
2222
return if val != false && val.blank? && @allow_blank

0 commit comments

Comments
 (0)