class_eval is a Ruby method that evaluates code in the context of a class or module. It is used to define methods or alter class behaviour dynamically at runtime, which can also widen code injection risk if untrusted input reaches the evaluated string.
What class_eval does in Ruby
class_eval is a Ruby metaprogramming method that evaluates code against a class or module object, so the evaluated code can add methods, change behaviour, or redefine existing members at runtime. That flexibility is powerful, but it also expands the attack surface when the input being evaluated is not fully controlled.
Why class_eval is security-sensitive
class_eval changes not just what code runs, but where it runs. Because the evaluated string or block executes in the class or module context, it can alter method lookup, override behaviour, and create a much broader blast radius than a normal helper or utility method. In security terms, that makes it a high-risk pattern whenever developers treat dynamic input as safe code.
When untrusted content reaches class_eval, the main danger is code injection. The vulnerability is not unique to Ruby, but Ruby’s metaprogramming style makes the impact especially severe because injected code may redefine authorization checks, suppress logging, or introduce hidden backdoors inside core application classes.
How class_eval is typically used
Developers often use class_eval to generate boilerplate dynamically, build DSLs, or extend frameworks at load time. In well-controlled codebases, this can reduce repetition and make APIs more expressive. The trade-off is that the more dynamic the implementation becomes, the harder it is to reason about the final class shape, especially during code review and security testing.
class_eval is most defensible when the evaluated content is static, locally defined, and reviewed like source code. If the text comes from configuration, user input, stored records, templates, or network data, the technique stops being a convenience feature and becomes a code execution boundary that needs the same scrutiny as any other interpreter entry point.
Safer alternatives and guardrails
Safer designs usually avoid evaluating strings entirely and prefer explicit method definitions, dispatch tables, or other constrained metaprogramming patterns. If runtime behaviour truly needs to vary, the safest approach is to keep the decision logic data-driven and reserve executable code for trusted source files that are version-controlled and reviewed.
Where class_eval cannot be removed, the guardrail is strict trust separation: treat every input source as hostile unless it is proven otherwise, and narrow the evaluated surface so it cannot reach sensitive class methods or privileged internals. A small amount of flexibility is rarely worth the cost of an unconstrained interpreter entry point.
When class_eval becomes a real security problem
The risk becomes material when class_eval is used to transform external data into executable Ruby inside a long-lived application process. In that situation, an attacker who can influence the string being evaluated may be able to execute arbitrary code, persist malicious behaviour by redefining methods, or alter security controls in ways that are difficult to detect after the fact.
Failure mechanism: Untrusted input is concatenated into evaluated Ruby, then executed inside a class or module context where it can redefine behaviour or invoke dangerous methods.
Impact: The application may suffer remote code execution, integrity loss, hidden logic changes, credential exposure, or durable compromise of business-critical classes.
Standards & Framework Alignment
This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.
MITRE ATT&CK addresses the attack and risk surface, while OWASP ASVS, CIS Controls v8 and NIST SP 800-53 Rev 5 set the governance and control requirements practitioners need to meet.
| Framework | Control / Reference | Relevance |
|---|---|---|
| OWASP ASVS | V15 — Secure Coding and Architecture | class_eval is a secure-coding pattern with direct injection and maintainability implications. |
| Recommendation — Avoid string-based evaluation and prefer explicit, reviewable code paths for dynamic behaviour. | ||
| CIS Controls v8 | CIS-16 — Application Software Security | Dynamic code evaluation is an application-security weakness that secure SDLC controls should address. |
| Recommendation — Review and restrict risky language features during secure development and code review. | ||
| NIST SP 800-53 Rev 5 | SI-10 — Information Input Validation | Untrusted data reaching eval-style code is an input-validation failure leading to code injection. |
| Recommendation — Validate and constrain all external input before it can influence executable code paths. | ||
| MITRE ATT&CK | T1059 — Command and Scripting Interpreter | class_eval can become an interpreter-based code execution path when attacker-controlled text is evaluated. |
| Recommendation — Monitor for script-like execution paths and investigate any attacker-controlled eval usage. | ||
Practitioner Guidance
Why practitioners should care: class_eval is not just a maintainability choice, it is a trust-boundary decision. Review it as a code execution sink, not as a normal metaprogramming convenience, and require a clear justification whenever it appears in production code.
Common misunderstanding: Teams sometimes assume that class_eval is safe because the code is “internal” or “framework-controlled.” That assumption fails as soon as any part of the evaluated payload is built from variable data, interpolated strings, or content that crosses an application boundary.
Practitioner takeaway: Prefer explicit Ruby constructs over string-based evaluation, and when dynamic behaviour is unavoidable, keep the evaluated content fully trusted, minimal, and reviewable.
Related resources from NHI Mgmt Group
- What breaks when machine identities are not governed like first-class identities?
- What breaks when enterprise agents are not treated as first-class identities?
- How do organisations know if their AI eval rubric is actually useful?
- Why do storage-class permissions matter so much in Kubernetes security?
Deepen Your Knowledge
Free weekly newsletter
Subscribe to the NHI & AI Identity Journal
The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.
Bonus 33% off our NHI Course when you subscribe.
Reviewed and updated by the NHIMG editorial team on November 25, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org