Skip to content

Commit 65983f3

Browse files
committed
avoid cache attack in Ruby scanner
(eg. using Unicode-delimited Fancy Strings)
1 parent c3c70e0 commit 65983f3

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

lib/coderay/scanners/ruby/string_state.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ class StringState < Struct.new :type, :interpreted, :delim, :heredoc,
1414
{ }
1515
] ].each { |k,v| k.freeze; v.freeze } # debug, if I try to change it with <<
1616

17-
# FIXME: cache attack
1817
STRING_PATTERN = Hash.new do |h, k|
1918
delim, interpreted = *k
2019
delim_pattern = Regexp.escape(delim)
@@ -29,12 +28,13 @@ class StringState < Struct.new :type, :interpreted, :delim, :heredoc,
2928
# '| [|?*+(){}\[\].^$]'
3029
# end
3130

32-
h[k] =
33-
if interpreted && delim != '#'
34-
/ (?= [#{delim_pattern}] | \# [{$@] ) /mx
35-
else
36-
/ (?= [#{delim_pattern}] ) /mx
37-
end
31+
if interpreted && delim != '#'
32+
/ (?= [#{delim_pattern}] | \# [{$@] ) /mx
33+
else
34+
/ (?= [#{delim_pattern}] ) /mx
35+
end.tap do |pattern|
36+
h[k] = pattern if (delim.respond_to?(:ord) ? delim.ord : delim[0]) < 256
37+
end
3838
end
3939

4040
def initialize kind, interpreted, delim, heredoc = false

0 commit comments

Comments
 (0)