

Ruby introduces Regexp.timeout
source link: https://blog.saeloun.com/2022/08/09/ruby-introduces-regexp-timeout
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Regular expressions (regexps) are codes that demonstrates the contents of a string. They’re generally used to test a string’s patterns and determine which portions of the string are a match to the output desired.
- Syntax: Regexp.=~()
- Parameter: Regexp values
- Return: true – if two expressions matches string, if there is no match, then it will return False.
They are usually built with the /pat/ and %r{pat} literals
or the Regexp.new
constructor.
A regexp is usually confined within forward slashes (/). For example:
/par/ =~ 'parrot' #=> 0
/p/.match('parrot') #=> #<MatchData "p">
If a string has the pattern we are looking for, then it is said to be a match.
Here, the word, ‘parrot’ does not have the pattern ‘beak’, so it doesn’t match:
/beak/.match('parrot') #=> nil
Here, ‘parrot’ contains the pattern ‘par’, so it matches:
/par/.match('parrot') #=> #<MatchData "par">
Remember that any Regexp matching will display a RuntimeError when a timeout is set and exceeded. This is why, oftentimes, the codes may get exploited by malicious users for DoS or ReDoS.
Therefore, to prevent or mitigate the risk of DoS,
Regexp.timeout
is introduced by Ruby.
Timeout
There are two APIs to set timeout. They are:
- Timeout.timeout= it is the process-global configuration of timeout for Regexp matching.
- Timeout keyword of Regexp.new.= it is used when we want to try different timeout settings for some special Regexps
Timeout.timeout
Regexp.timeout = 4
q = 'a' * 25 + 'd' + 'a' * 4 + 's' #=> "aaaaaaaaaaaaaaaaaaaaaaaaadaaaas"
/(b|a+)*s/ =~ q #=> Regexp::TimeoutError is raised in four seconds
Timeout keyword of Regexp.new
re = Regexp.new("(b|a+)*c", timeout: 4)
q = 'a' * 25 + 'd' + 'a' * 4 + 's' #=> "aaaaaaaaaaaaaaaaaaaaaaaaadaaaas"
/(b|a+)*s/ =~ q #=> Regexp::TimeoutError is raised in four seconds
When we are using Regexp to run an untrusted output, its important to understand and use the timeout feature to prevent multiple backtracking.
If not done, the code will be prone to Denial-of-Service attack
as an attacker might exploit it by providing an input to Regexp
as the code might be otherwise matching an ineffecient Regexp
.
Let’s not forget that the timeout is not set by default as an appropriate limit is usually determined by the application needs and content.
Recommend
-
28
In this post, I'll try to explain the basics of regular expressions . Keep in mind that this sort-of tutorial is aimed for those who would like to learn regexps a bit better and/or are just starting a...
-
9
Regexp tutorial and cheat sheet yourbasic.org/golang A regular expression is a sequence of characters that define a search pattern.
-
19
REGEXP_LIKE – Happy thoughts whilst searching for multiple substrings in Oracle SQL Posted on
-
11
A quiz about RegExp.prototype.exec return values and numeric indexes (or indices or however you people pluralize that word).A quiz about RegExp.prototype.exec return values and numeric indexes (or indices or however you people pluralize that...
-
6
regexp 正则包 直立猿 · 大约2小时之前 · 12 次点击 · 预计阅读时间 5 分钟 · 不到1分钟之前 开始浏览 ...
-
7
Why Ruby’s Timeout is dangerous (and Thread.raise is terrifying) This is already documented in Timeout: Ruby’s most dangerous API
-
12
前端有的时候需要对 url 或者 http 请求进行处理,比如有的 api 不需要带 token 访问,有的直接请求第三方接口。之前处理的方式是简单的包含判断,后来发现这样不够严谨也容易出问题。正确的做法是用正则去匹配,但是自己写的也难保不会出错,所以就找了第三方...
-
15
Ruby Regex: difference between new and union with a single regexp advertisements I have simplified the examples. Say I have a string containin...
-
5
‘Cypress verify’ command gets executed as part of the ‘cypress open’ and ‘cypress run’ commands. Cypress now provides an option to override the verification timeout for slow machines with the environment variable CYPRESS_VERIFY_TIMEOUT. It de...
-
2
What is ReDoS? Regular expression Denial of Service (ReDoS) is a security vulnerability that can occur in a regular expression (regex) when the regex is applied to a long string. This attack is designed to make a system...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK