Skip to content

Commit dee8b33

Browse files
committed
fix: add possessive quantifier to regex match for formatted strings
(Fixes https://github.com/faker-ruby/faker/security/code-scanning/14) Adds a possessive quantifier (an extra `+`) to the regex to prevent backtracking. This is to prevent the issue 'Polynomial regular expression used on uncontrolled data code'. It seems a bit overkill, given how these formatted strings are used, but I don't think it hurts either. See reference: https://ruby-doc.org/3.4.1/Regexp.html#class-Regexp-label-Greedy-2C+Lazy-2C+or+Possessive+Matching
1 parent 2258715 commit dee8b33

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

lib/faker.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ def fetch_all(key)
133133
# formatted translation: e.g., "#{first_name} #{last_name}".
134134
def parse(key)
135135
fetched = fetch(key)
136-
parts = fetched.scan(/(\(?)#\{([A-Za-z]+\.)?([^}]+)\}([^#]+)?/).map do |prefix, kls, meth, etc|
136+
137+
parts = fetched.scan(/(\(?)#\{([A-Za-z]+\.)?([^}]+)\}([^#]++)?/).map do |prefix, kls, meth, etc|
137138
# If the token had a class Prefix (e.g., Name.first_name)
138139
# grab the constant, otherwise use self
139140
cls = kls ? Faker.const_get(kls.chop) : self

0 commit comments

Comments
 (0)