Summary
A guard bypass in Ruby's ERB template engine allows reconstructed ERB objects from Marshal.load to reach unprotected code evaluation methods.
In applications that deserialize untrusted data while erb is loaded, an attacker can abuse ERB#def_module as a code execution sink despite existing protections in ERB#result and ERB#run.
Affected context
This vulnerability appears in Ruby applications that:
- Use ERB as a templating engine
- Have
erbloaded in the process - Perform
Marshal.loaddeserialization on data that is not strictly trusted or validated
It manifests when deserialized ERB instances can call public methods like def_method, def_module, or def_class that evaluate stored template source with eval() and do not enforce the same initialization guard as result and run.
Exploit preconditions
- Attack surface: Ruby application code paths that invoke
Marshal.loadon attacker-controlled or partially untrusted serialized objects. - Required access: Ability to supply or influence serialized Ruby objects that will be deserialized with
Marshal.loadwhileerbis available. - Trigger point: Reconstruction of an ERB instance and subsequent invocation of
ERB#def_module(or similar methods) that evaluate@srcviaeval()without checking the initialization guard. - Why exploitation is possible: The guard (
@_init) introduced to protectERB#resultandERB#runis not enforced indef_method,def_module, ordef_class, leaving alternate code evaluation paths exposed to unsafe deserialized state.
Why this happens
The underlying bug stems from incomplete application of a security guard designed to mitigate unsafe deserialization in ERB.
Ruby introduced an @_init instance variable check to ensure only legitimately initialized ERB objects could execute template code via result and run.
However, ERB still exposed other public methods (def_method, def_module, def_class) that internally use eval() on the @src template source, but these methods were not wrapped with the same guard.
This mismatch maps to CWE-502 (Unsafe Deserialization)—where application state derived from untrusted serialized data can control execution paths—and CWE-693 (Protection Mechanism Failure) because the intended defense only protects some sinks, leaving equivalent sinks unguarded.
Potential impact
- An attacker may achieve arbitrary Ruby code execution within the application process.
- Malicious templates could be defined as methods or modules, enabling persistent logic injection.
- Application data and secrets could be read, modified, or exfiltrated.
- The compromised process could be used as a pivot for broader system or network compromise.
- Integrity of business logic and templating behavior could be subverted by attacker-defined methods.
General mitigation guidance
Effective mitigation focuses on both deserialization hygiene and consistent application of security guards across all execution paths.
Pattern-based guidance includes:
- Treat deserialized objects as untrusted input; avoid using
Marshal.loadon data that originates outside a tightly controlled trust boundary. - Prefer safer data formats (e.g., JSON with explicit schema validation) when exchanging data between systems or persisting state.
- Ensure that all code-evaluation sinks in templating engines are subject to uniform guard checks, not just the most commonly used methods.
- Apply defense-in-depth controls such as reducing ERB exposure on deserialization paths and limiting dynamic method/module/class creation from template sources.
- Run application processes with least privilege and constrained capabilities so that, even if template code runs, its ability to affect the host or environment is minimized.
How teams detect issues like this
High-level detection typically combines code analysis and configuration review:
- Use static analysis or grep-based rules to flag
Marshal.loadusage on data that can originate from user input, network messages, or shared storage. - During code review, trace the lifecycle of serialized data and verify that deserialization does not rehydrate complex objects like ERB templates in untrusted contexts.
- Look for calls to
ERB#def_method,ERB#def_module, andERB#def_classand assess whether they might be reachable via deserialized instances. - Establish policy checks in CI/CD pipelines that discourage unsafe deserialization (e.g., custom linters or security checks for Marshal usage).
- Complement source-level review with runtime monitoring for unusual dynamic method/module/class definitions originating from templates.

