Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove '\u0000' from input when sanitizing null input #89

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/rack/utf8_sanitizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Rack
class UTF8Sanitizer
StringIO = ::StringIO
NULL_BYTE_REGEX = /\x00/.freeze
NULL_BYTE_STRING_REGEX = Regexp.new('\\\u0000').freeze
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm quite confused about this PR. Isn't \\\u0000 matching normal text that also happens to be a Ruby string escape sequence? There shouldn't be any reason to remove it from the input unless you're doing something rather strange with the string.

What problem does this actually solve?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even though it's indeed normal text connection.escape(s) fails with PG when a string contains this.


class NullByteInString < StandardError; end

Expand Down Expand Up @@ -40,15 +41,15 @@ def call(env)
invalid: :replace,
undef: :replace)
if sanitize_null_bytes
input = input.gsub(NULL_BYTE_REGEX, "")
input = input.gsub(NULL_BYTE_REGEX, "").gsub(NULL_BYTE_STRING_REGEX, '')
end
input
end,
exception: lambda do |input, sanitize_null_bytes: false|
input.
force_encoding(Encoding::ASCII_8BIT).
encode!(Encoding::UTF_8)
if sanitize_null_bytes && NULL_BYTE_REGEX.match?(input)
if sanitize_null_bytes && (NULL_BYTE_REGEX.match?(input) || NULL_BYTE_STRING_REGEX.match?(input))
raise NullByteInString
end
input
Expand Down Expand Up @@ -262,7 +263,8 @@ def sanitize_string(input)
if input.is_a? String
input = input.dup.force_encoding(Encoding::UTF_8)

if input.valid_encoding? && !(@sanitize_null_bytes && input =~ NULL_BYTE_REGEX)
if input.valid_encoding? &&
!(@sanitize_null_bytes && (NULL_BYTE_REGEX.match?(input) || NULL_BYTE_STRING_REGEX.match?(input)))
input
else
@strategy.call(input, sanitize_null_bytes: @sanitize_null_bytes)
Expand Down
12 changes: 12 additions & 0 deletions test/test_utf8_sanitizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,18 @@ def read
end
end

it "optionally sanitizes null bytes plain string with the replace strategy" do
@app = Rack::UTF8Sanitizer.new(-> env { env }, sanitize_null_bytes: true)
input = "foo=bla\xED&quux=bar" + '\u0000'
@rack_input = StringIO.new input

sanitize_form_data do |sanitized_input|
sanitized_input.encoding.should == Encoding::UTF_8
sanitized_input.should.be.valid_encoding
sanitized_input.should == "foo=bla%EF%BF%BD&quux=bar"
end
end

it "optionally sanitizes encoded null bytes with the replace strategy" do
@app = Rack::UTF8Sanitizer.new(-> env { env }, sanitize_null_bytes: true)
input = "foo=bla%ED&quux=bar%00"
Expand Down