Regexp.timeout
https://docs.ruby-lang.org/ja/latest/class/Regexp.html
ライブラリ
日本語のリファレンスマニュアルにはまだ記載なし (2023/2/18)
https://rubyapi.org/3.2/o/regexp#method-c-timeout-3D
正規表現のマッチにタイムアウトを設定する
Ruby3.2.0より導入
デフォルト値はnil
ReDoS対策の一種
確認コード(Github)
code:test.rb
Regexp.timeout = 1
/(\d{2}+)+$/.match("1" * 100 + "a")
code:ruby
❯ ruby test.rb
test.rb:2:in `match': regexp match timeout (Regexp::TimeoutError)
from test.rb:2:in `<main>'
参考
https://bugs.ruby-lang.org/issues/17837
https://techlife.cookpad.com/entry/2022/12/26/121950#正規表現マッチングにタイムアウトが指定できるようになった
https://bugs.ruby-lang.org/issues/20284
timeoutがうまく効かないパターンが有るようだが限定的
#Regexp