File tree 2 files changed +17
-3
lines changed
2 files changed +17
-3
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ module Rack
8
8
class UTF8Sanitizer
9
9
StringIO = ::StringIO
10
10
NULL_BYTE_REGEX = /\x00 / . freeze
11
+ NULL_BYTE_STRING_REGEX = Regexp . new ( '\\\u0000' ) . freeze
11
12
12
13
class NullByteInString < StandardError ; end
13
14
@@ -40,15 +41,15 @@ def call(env)
40
41
invalid : :replace ,
41
42
undef : :replace )
42
43
if sanitize_null_bytes
43
- input = input . gsub ( NULL_BYTE_REGEX , "" )
44
+ input = input . gsub ( NULL_BYTE_REGEX , "" ) . gsub ( NULL_BYTE_STRING_REGEX , '' )
44
45
end
45
46
input
46
47
end ,
47
48
exception : lambda do |input , sanitize_null_bytes : false |
48
49
input .
49
50
force_encoding ( Encoding ::ASCII_8BIT ) .
50
51
encode! ( Encoding ::UTF_8 )
51
- if sanitize_null_bytes && NULL_BYTE_REGEX . match? ( input )
52
+ if sanitize_null_bytes && ( NULL_BYTE_REGEX . match? ( input ) || NULL_BYTE_STRING_REGEX . match? ( input ) )
52
53
raise NullByteInString
53
54
end
54
55
input
@@ -262,7 +263,8 @@ def sanitize_string(input)
262
263
if input . is_a? String
263
264
input = input . dup . force_encoding ( Encoding ::UTF_8 )
264
265
265
- if input . valid_encoding? && !( @sanitize_null_bytes && input =~ NULL_BYTE_REGEX )
266
+ if input . valid_encoding? &&
267
+ !( @sanitize_null_bytes && ( NULL_BYTE_REGEX . match? ( input ) || NULL_BYTE_STRING_REGEX . match? ( input ) ) )
266
268
input
267
269
else
268
270
@strategy . call ( input , sanitize_null_bytes : @sanitize_null_bytes )
Original file line number Diff line number Diff line change @@ -395,6 +395,18 @@ def read
395
395
end
396
396
end
397
397
398
+ it "optionally sanitizes null bytes plain string with the replace strategy" do
399
+ @app = Rack ::UTF8Sanitizer . new ( -> env { env } , sanitize_null_bytes : true )
400
+ input = "foo=bla\xED &quux=bar" + '\u0000'
401
+ @rack_input = StringIO . new input
402
+
403
+ sanitize_form_data do |sanitized_input |
404
+ sanitized_input . encoding . should == Encoding ::UTF_8
405
+ sanitized_input . should . be . valid_encoding
406
+ sanitized_input . should == "foo=bla%EF%BF%BD&quux=bar"
407
+ end
408
+ end
409
+
398
410
it "optionally sanitizes encoded null bytes with the replace strategy" do
399
411
@app = Rack ::UTF8Sanitizer . new ( -> env { env } , sanitize_null_bytes : true )
400
412
input = "foo=bla%ED&quux=bar%00"
You can’t perform that action at this time.
0 commit comments