{"id":234814,"date":"2024-06-21T02:56:14","date_gmt":"2024-06-21T02:56:14","guid":{"rendered":"https:\/\/michigandigitalnews.com\/index.php\/2024\/06\/21\/github-understanding-unsafe-deserialization-vulnerabilities-in-ruby-projects\/"},"modified":"2025-06-25T17:16:33","modified_gmt":"2025-06-25T17:16:33","slug":"github-understanding-unsafe-deserialization-vulnerabilities-in-ruby-projects","status":"publish","type":"post","link":"https:\/\/michigandigitalnews.com\/index.php\/2024\/06\/21\/github-understanding-unsafe-deserialization-vulnerabilities-in-ruby-projects\/","title":{"rendered":"Github: Understanding Unsafe Deserialization Vulnerabilities in Ruby Projects"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div>\n<figure class=\"figure mt-2\">&#13;<br \/>\n                        <a href=\"https:\/\/image.blockchain.news:443\/features\/24B6EBDC6093F0C1F639A6A7DA12473E2D2C5C390185833B0F398CC7FCE1368C.jpg\">&#13;<br \/>\n                            <img decoding=\"async\" class=\"rounded\" src=\"https:\/\/image.blockchain.news:443\/features\/24B6EBDC6093F0C1F639A6A7DA12473E2D2C5C390185833B0F398CC7FCE1368C.jpg\" alt=\"Github: Understanding Unsafe Deserialization Vulnerabilities in Ruby Projects\"\/>&#13;<br \/>\n&#13;<br \/>\n                        <\/a>&#13;<br \/>\n                    <\/figure>\n<p>Unsafe deserialization vulnerabilities in Ruby projects can enable attackers to execute arbitrary commands on remote servers by sending JSON data. According to <a rel=\"nofollow\" href=\"https:\/\/github.blog\/2024-06-20-execute-commands-by-sending-json-learn-how-unsafe-deserialization-vulnerabilities-work-in-ruby-projects\/\">The GitHub Blog<\/a>, these vulnerabilities occur when the deserialization process allows the instantiation of arbitrary classes or class-like structures specified in the serialized data.<\/p>\n<h2>How Unsafe Deserialization Works<\/h2>\n<p>In Ruby, unsafe deserialization vulnerabilities are often exploited through libraries that support polymorphism, such as the Oj JSON serialization library. Attackers can chain multiple classes together to execute code on the system under attack. These classes, known as gadgets, are combined into a gadget chain to form a larger exploit.<\/p>\n<p>For instance, when using the Oj library for deserializing JSON, a project can be vulnerable if it includes a construct like:<\/p>\n<pre><code>data = Oj.load(untrusted_json)<\/code><\/pre>\n<p>The Oj library, by default, supports the instantiation of classes specified in JSON, which can be disabled by using <code>Oj.safe_load<\/code> instead.<\/p>\n<p>To demonstrate how this works, consider a class named <code>SimpleClass<\/code> with a <code>hash<\/code> method that executes a command:<\/p>\n<pre><code>class SimpleClass&#13;\n  def initialize(cmd)&#13;\n    @cmd = cmd&#13;\n  end&#13;\n&#13;\n  def hash&#13;\n    system(@cmd)&#13;\n  end&#13;\nend<\/code><\/pre>\n<p>A JSON payload to instantiate this class might look like:<\/p>\n<pre><code>{&#13;\n    \"^o\": \"SimpleClass\",&#13;\n    \"cmd\": \"open -a calculator\"&#13;\n}<\/code><\/pre>\n<p>Loading this JSON with <code>Oj.load<\/code> would not trigger the <code>hash<\/code> method directly, but placing the class inside a hash as the key can trigger the method:<\/p>\n<pre><code>Oj.load(json_payload)<\/code><\/pre>\n<p>This would execute the command specified in the <code>@cmd<\/code> member variable.<\/p>\n<h2>Building a Detection Gadget<\/h2>\n<p>To detect unsafe deserialization vulnerabilities, one can build a detection gadget chain. For example, a class like <code>Gem::Requirement<\/code> can be used, which has a <code>hash<\/code> method that calls <code>to_s<\/code> on an internal member. By creating a suitable JSON payload, one can trigger this chain to detect vulnerabilities.<\/p>\n<p>The detection gadget can also be extended to a full-fledged remote code execution (RCE) chain. This involves using classes and methods that are part of Ruby or its dependencies to execute arbitrary commands.<\/p>\n<h2>Preventing Unsafe Deserialization<\/h2>\n<p>To prevent such vulnerabilities, it is crucial to use safe deserialization methods. For example, using <code>Oj.safe_load<\/code> instead of <code>Oj.load<\/code> can prevent the instantiation of arbitrary classes. Additionally, tools like CodeQL can help detect unsafe deserialization by analyzing the source code for vulnerable patterns.<\/p>\n<p>For developers with access to the source code, GitHub\u2019s code scanning with CodeQL can identify unsafe deserialization sinks. If the source code is not accessible, detection gadgets can be used to identify vulnerabilities remotely.<\/p>\n<p>Understanding how unsafe deserialization works and implementing secure coding practices can help avoid these vulnerabilities. For more detailed examples and detection methodologies, refer to the <a rel=\"nofollow\" href=\"https:\/\/github.blog\/2024-06-20-execute-commands-by-sending-json-learn-how-unsafe-deserialization-vulnerabilities-work-in-ruby-projects\/\">original blog post<\/a> by The GitHub Blog.<\/p>\n<p><span><i>Image source: Shutterstock<\/i><\/span>                    <!-- Divider --><\/p>\n<p>                    <!-- Divider --><\/p>\n<p>                    <!-- Author info START --><br \/>\n                    <!-- Author info END --><br \/>\n                    <!-- Divider -->\n                <\/div>\n<p>[ad_2]<br \/>\n<br \/><a href=\"https:\/\/blockchain.news\/news\/understanding-unsafe-deserialization-vulnerabilities-in-ruby-projects\">Source link <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] &#13; &#13; &#13; &#13; &#13; Unsafe deserialization vulnerabilities in Ruby projects can enable attackers to execute arbitrary commands on remote servers by sending JSON<\/p>\n","protected":false},"author":1,"featured_media":234815,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[171],"tags":[],"_links":{"self":[{"href":"https:\/\/michigandigitalnews.com\/index.php\/wp-json\/wp\/v2\/posts\/234814"}],"collection":[{"href":"https:\/\/michigandigitalnews.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michigandigitalnews.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michigandigitalnews.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/michigandigitalnews.com\/index.php\/wp-json\/wp\/v2\/comments?post=234814"}],"version-history":[{"count":0,"href":"https:\/\/michigandigitalnews.com\/index.php\/wp-json\/wp\/v2\/posts\/234814\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/michigandigitalnews.com\/index.php\/wp-json\/wp\/v2\/media\/234815"}],"wp:attachment":[{"href":"https:\/\/michigandigitalnews.com\/index.php\/wp-json\/wp\/v2\/media?parent=234814"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michigandigitalnews.com\/index.php\/wp-json\/wp\/v2\/categories?post=234814"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michigandigitalnews.com\/index.php\/wp-json\/wp\/v2\/tags?post=234814"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}