Module: RivetCms::SafePattern
- Defined in:
- lib/rivet_cms/safe_pattern.rb
Overview
Compiles and matches admin-supplied regex patterns without letting a pathological pattern hang the process. Ruby 3.2+ supports a per-regexp timeout; on older Rubies patterns still work, bounded only by MAX_LENGTH.
Constant Summary collapse
- MAX_LENGTH =
200- TIMEOUT_SECONDS =
1
Class Method Summary collapse
Class Method Details
.match?(source, value) ⇒ Boolean
19 20 21 22 23 24 25 26 27 |
# File 'lib/rivet_cms/safe_pattern.rb', line 19 def match?(source, value) compile(source).match?(value.to_s) rescue RegexpError, ArgumentError false rescue => error raise unless defined?(Regexp::TimeoutError) && error.is_a?(Regexp::TimeoutError) false end |
.valid?(source) ⇒ Boolean
10 11 12 13 14 15 16 17 |
# File 'lib/rivet_cms/safe_pattern.rb', line 10 def valid?(source) return false if source.to_s.length > MAX_LENGTH compile(source) true rescue RegexpError, ArgumentError false end |