Summary
Active Support's safe_constantize can build a regular expression from an unescaped constant name when constant lookup raises LoadError. If untrusted input reaches this code, crafted patterns may trigger defects in Ruby's regular expression compiler and crash the server process.
Affected context
The issue affects Rails applications using affected Active Support behavior when constant lookup raises LoadError and the requested constant name is used to build a regular expression without escaping. Action Cable can expose this path by passing a channel value from a subscribe command to safe_constantize; reachability depends on the application mounting Action Cable and accepting connections from untrusted clients without an additional authentication check.
Exploit preconditions
- Attack surface: An application path that invokes Active Support's
safe_constantizewith a value that can be influenced by a client - Required access: For the Action Cable path, an unauthenticated connection may be sufficient if the application accepts untrusted clients without an added authentication check
- Trigger point: Constant lookup raises
LoadError, causing the requested name to be used in regular-expression construction - Why exploitation is possible: The name is not escaped before it becomes part of the pattern, allowing crafted input to affect the expression compiled by Ruby
Why this happens
The underlying bug is failure to neutralize regular-expression metacharacters in data used to construct a pattern. When safe_constantize handles a LoadError, it builds a regular expression using the requested constant name without escaping it. This is an injection weakness associated with CWE-74: Improper Neutralization of Special Elements in Output Used by a Downstream Component.
Potential impact
- Crafted input may trigger a defect in Ruby's regular-expression compiler
- The affected request may crash the server process
- Repeated requests could keep the application unavailable
General mitigation guidance
Defensive approaches focus on preventing untrusted values from controlling regular-expression syntax and limiting the effect of failures.
- Escape user-controlled text before incorporating it into regular expressions, or avoid constructing patterns from such text
- Prefer explicit allowlists for values that select constants or channels
- Authenticate and authorize clients before processing sensitive Action Cable subscription operations
- Handle parsing and lookup failures in a way that limits their effect on application availability
- Apply resource and process isolation appropriate to the service's availability requirements
How teams detect issues like this
- Use static analysis to identify regular expressions constructed from untrusted values without escaping
- Review
safe_constantizeand similar lookup calls for inputs originating from requests, channel messages, or other external sources - Test error paths in which constant lookup raises
LoadError, including their behavior under malformed input - Enforce code-review or CI policies that require escaping or allowlisting when dynamic patterns are used
- Monitor for repeated request failures or unexpected server process restarts

