Regexp.linear_time?
https://docs.ruby-lang.org/ja/latest/class/Regexp.html
ライブラリにはまだ記載なし (2022/12/31)
Ruby3.2.0より導入された、正規表現の実行時間が線形であるか返すメソッド
ReDoS対策の一種
確認コード(Github)
code:test.rb
puts Regexp.linear_time?("aa")
puts Regexp.linear_time?(/(a|a)*\1/)
puts Regexp.linear_time?(/^a()\1$/)
code:ruby
❯ ruby test.rb
true
false
false
linear_time?がfalseを返す場合であっても、必ずReDoSとなるわけではない
参考
https://bugs.ruby-lang.org/issues/19194
https://techlife.cookpad.com/entry/2022/12/12/162023
#Regexp