# Class Pollution in Ruby: A Deep Dive into Exploiting Recursive Merges

DevFeed: [Class Pollution in Ruby: A Deep Dive into Exploiting Recursive Merges](<https://devfeed.tech/articles/class-pollution-in-ruby-a-deep-dive-into-exploiting-recursive-merges-92053.md>)

Original publisher: [Read original article](<https://blog.doyensec.com/2024/10/02/class-pollution-ruby.html>)

Published: 2024-10-01T22:00:00Z

Content type: article

Language: en

Sources: [Doyensec](<https://devfeed.tech/sources/doyensec.md>)

Topics: [Ruby](<https://devfeed.tech/topics/ruby.md>), [class](<https://devfeed.tech/topics/class.md>), [Inheritance](<https://devfeed.tech/topics/inheritance.md>), [superclass](<https://devfeed.tech/topics/superclass.md>), [Polymorphism](<https://devfeed.tech/topics/polymorphism.md>)

Tags: [about](<https://devfeed.tech/tags/about.md>), [admin](<https://devfeed.tech/tags/admin.md>), [allows](<https://devfeed.tech/tags/allows.md>), [alter](<https://devfeed.tech/tags/alter.md>), [application](<https://devfeed.tech/tags/application.md>), [auditing](<https://devfeed.tech/tags/auditing.md>), [automation](<https://devfeed.tech/tags/automation.md>), [base](<https://devfeed.tech/tags/base.md>), [based](<https://devfeed.tech/tags/based.md>), [behavior](<https://devfeed.tech/tags/behavior.md>), [behaviors](<https://devfeed.tech/tags/behaviors.md>), [blog](<https://devfeed.tech/tags/blog.md>), [blog-post](<https://devfeed.tech/tags/blog-post.md>), [can](<https://devfeed.tech/tags/can.md>), [cases](<https://devfeed.tech/tags/cases.md>), [class](<https://devfeed.tech/tags/class.md>), [classes](<https://devfeed.tech/tags/classes.md>), [deep](<https://devfeed.tech/tags/deep.md>), [deep-dive](<https://devfeed.tech/tags/deep-dive.md>), [dive](<https://devfeed.tech/tags/dive.md>), [doyensec](<https://devfeed.tech/tags/doyensec.md>), [doyensecurity](<https://devfeed.tech/tags/doyensecurity.md>), [exploit](<https://devfeed.tech/tags/exploit.md>), [hacking](<https://devfeed.tech/tags/hacking.md>), [infosec](<https://devfeed.tech/tags/infosec.md>), [offensive](<https://devfeed.tech/tags/offensive.md>), [pentest](<https://devfeed.tech/tags/pentest.md>), [pentester](<https://devfeed.tech/tags/pentester.md>), [pollution](<https://devfeed.tech/tags/pollution.md>), [reverse-engineering](<https://devfeed.tech/tags/reverse-engineering.md>), [ruby](<https://devfeed.tech/tags/ruby.md>), [security](<https://devfeed.tech/tags/security.md>), [vulnerability](<https://devfeed.tech/tags/vulnerability.md>)

## AI overview

Recursive merges that assign data to Ruby object attributes can alter behavior beyond the supplied values, and recursive traversal can escape an object to affect parent or unrelated classes. The article distinguishes hash-only merges, which it says cannot pollute classes, from nonrecursive attribute merges, which affect one object, and recursive attribute merges, which can have broader effects. Its example shows instance-level method injection enabling privilege escalation or code execution when protected methods are evaluated. ActiveSupport's `deep_merge` works on hashes and becomes risky in the article's example when merged values are then assigned as object attributes. Hashie merges object attributes and blocks direct method replacement, but the article describes an exception involving the `_` attribute that can bypass an authorization check. Further examples demonstrate poisoning a parent class variable to redirect HTTP requests and repeatedly targeting subclasses to alter a signing key; the latter is random and may over-infect classes. The article concludes that recursive merges of untrusted data can compromise application behavior across requests.

## Source excerpt

Introduction In this post, we are going to explore a rarely discussed class of vulnerabilities in Ruby, known as class pollution. This concept is inspired by the idea of prototype pollution in JavaScript, where recursive merges are exploited to poison the prototype of objects, leading to unexpected behaviors. This idea was initially discussed in a blog post about prototype pollution in Python, in which the researcher used recursive merging to poison class variables and eventually global variables via the __globals__ attribute. In Ruby, we can categorize class pollution into three main cases: Merge on Hashes: In this scenario, class pollution isn't possible because the merge operation is confined to the hash itself. Merge on Attributes (Non-Recursive): Here, we can poison the instance variables of an object, potentially replacing methods by injecting return values. This pollution is limited to the object itself and does not affect the class. current_obj.instance_variable_set("@#{key}", new_object) current_obj.singleton_class.attr_accessor key Merge on Attributes (Recursive): In this case, the recursive nature of the merge allows us to escape the object context and poison attributes or methods of parent classes or even unrelated classes, leading to a broader impact on the application. Merge on Attributes Let's start by examining a code example where we exploit a recursive merge to modify object methods and alter the application's behavior. This type of pollution is limited to the object itself. require 'json' # Base class for both Admin and Regular users class Person attr_accessor :name, :age, :details def initialize(name:, age:, details:) @name = name @age = age @details = details end # Method to merge additional data into the object def merge_with(additional) recursive_merge(self, additional) end # Authorize based on the `to_s` method result def authorize if to_s == "Admin" puts "Access granted: #{@name} is an admin." else puts "Access denied: #{@name} is not an adm