/

CVE-2026-41316

Severity:

HIGH RISK

(8.1)

Guard Bypass in ERB Deserialization (CWE-502/693)

CVE-2026-41316

Deferred

Vulnerability type

Category:

Unsafe Deserialization and Guard Bypass

CWE:

CWE-693, CWE-502

Impact:

Remote Code Execution

Severity:

HIGH RISK

(8.1)

CVSS version:

3.1

Catch this CVE while code is being written

Detect vulnerable code directly in pull requests, so CVEs are fixed before merge.

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 erb loaded in the process
  • Perform Marshal.load deserialization 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.load on attacker-controlled or partially untrusted serialized objects.
  • Required access: Ability to supply or influence serialized Ruby objects that will be deserialized with Marshal.load while erb is available.
  • Trigger point: Reconstruction of an ERB instance and subsequent invocation of ERB#def_module (or similar methods) that evaluate @src via eval() without checking the initialization guard.
  • Why exploitation is possible: The guard (@_init) introduced to protect ERB#result and ERB#run is not enforced in def_method, def_module, or def_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.load on 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.load usage 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, and ERB#def_class and 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.

Sources

SourceLink
github.comgithub.com/ruby/erb/security/advisories/GHSA-q339-8rmv-2mhv
access.redhat.comaccess.redhat.com/errata/RHSA-2026:18030
access.redhat.comaccess.redhat.com/errata/RHSA-2026:18039
access.redhat.comaccess.redhat.com/errata/RHSA-2026:18065
access.redhat.comaccess.redhat.com/errata/RHSA-2026:20596
access.redhat.comaccess.redhat.com/errata/RHSA-2026:20606
access.redhat.comaccess.redhat.com/errata/RHSA-2026:20614
access.redhat.comaccess.redhat.com/errata/RHSA-2026:20670
access.redhat.comaccess.redhat.com/errata/RHSA-2026:26312
access.redhat.comaccess.redhat.com/errata/RHSA-2026:26655
access.redhat.comaccess.redhat.com/errata/RHSA-2026:33462
access.redhat.comaccess.redhat.com/errata/RHSA-2026:33478
access.redhat.comaccess.redhat.com/errata/RHSA-2026:35834
access.redhat.comaccess.redhat.com/errata/RHSA-2026:37238
access.redhat.comaccess.redhat.com/security/cve/CVE-2026-41316
bugzilla.redhat.combugzilla.redhat.com/show_bug.cgi?id=2461369
security.access.redhat.comsecurity.access.redhat.com/data/csaf/v2/vex/2026/cve-2026-41316.json

Frequently asked

questions

It can be, if a remote attacker can supply serialized data that the application deserializes with Marshal.load while ERB is loaded. Exploitability depends on whether deserialization is exposed through network or other remote interfaces.

The attacker must control or heavily influence the serialized data consumed by Marshal.load. They do not need direct shell access but do need a path to feed crafted Ruby marshaled objects into the application.

The safest pattern is to avoid using Marshal.load on untrusted or semi-trusted data and to ensure that all ERB code-evaluation methods enforce consistent initialization checks. Replacing Marshal-based persistence or messaging with simpler, validated data formats significantly reduces risk.

Adopt a design stance that treats deserialization as a high-risk operation: minimize object rehydration, prefer explicit data models over executable state, apply uniform guards to every code execution sink, and enforce secure coding policies—backed by automated checks—around templating engines and eval-like constructs.

Find out what's already
exploitable in your codebase.