Skip to content

Commit

Permalink
Use Regexp#match? over String#=~ when testing for null bytes
Browse files Browse the repository at this point in the history
https://github.com/fastruby/fast-ruby#regexp-vs-regexpmatch-vs-regexpmatch-vs-stringmatch-vs-string-vs-stringmatch-code-

This change updates the null byte checking in the included `exception`
strategy to scan for null bytes with `Regexp#match?`. It appears this
will be 2.5x faster when parsing the data, which might be helpful given
the overhead of running this on every request.

Co-authored-by: David Runger <david.runger@commonlit.org>
  • Loading branch information
geoffharcourt and davidrunger-cl committed Feb 21, 2024
1 parent 0249300 commit ba11a31
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rack/utf8_sanitizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def call(env)
input.
force_encoding(Encoding::ASCII_8BIT).
encode!(Encoding::UTF_8)
if sanitize_null_bytes && input =~ NULL_BYTE_REGEX
if sanitize_null_bytes && NULL_BYTE_REGEX.match?(input)
raise NullByteInString
end
input
Expand Down

0 comments on commit ba11a31

Please sign in to comment.