-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfile.security.html
170 lines (115 loc) · 11.3 KB
/
file.security.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0, user-scalable=no'>
<meta name='apple-touch-fullscreen' content='yes'>
<meta name='apple-mobile-web-app-capable' content='yes'>
<meta name='apple-mobile-web-app-status-bar-style' content='rgba(228,228,228,1.0)'>
<title>File: Security — Ruby-2.6.10</title>
<link rel='stylesheet' type='text/css' href='../css/y_fonts.css' />
<link rel='stylesheet' type='text/css' href='../css/highlight.github.css' />
<link rel='stylesheet' type='text/css' href='../css/y_style.css' />
<link rel='stylesheet' type='text/css' href='../css/y_list.css' />
<link rel='stylesheet' type='text/css' href='../css/y_color.css' />
<script type='text/javascript'>
var pathId = "security",
relpath = '';
var t2Info = {
CSEP: '.',
ISEP: '#',
NSEP: '::'
};
</script>
<script type='text/javascript' charset='utf-8' src='../js/highlight.pack.js'></script>
<script type='text/javascript' charset='utf-8' src='../js/y_app.js'></script>
</head>
<body>
<svg id='y_wait' class viewBox='0 0 90 90'></svg>
<div id='settings' class='hidden'></div>
<div id='y_list' class='d h'>
<header id='list_header'></header>
<nav id= 'list_nav' class='y_nav l_nav'>
<ul id='list_items'></ul>
</nav>
</div>
<div id='y_toc' class='f h'>
<header id='toc_header'></header>
<nav id= 'toc_nav' class='y_nav t_nav'>
<ol id='toc_items'></ol>
</nav>
</div>
<div id='y_main' tabindex='-1'>
<header id='y_header'>
<div id='y_menu'>
<a id='home_no_xhr' href='/'>Home</a> »
<a href='.'>Ruby-2.6.10</a> »
<a href='_index.html'>Index</a> »
<span class='title'><a id='t2_doc_top' href='#'>File: Security ▲</a></span>
</div>
<a id='list_href' href="file_list.html"></a>
<div id='y_measure_em' class='y_measure'></div>
<div id='y_measure_vh' class='y_measure'></div>
<span id='y_measure_50pre' class='y_measure'><code>123456789_123456789_123456789_123456789_123456789_</code></span>
</header>
<div id='content' class='file'>
<h1 id="label-Ruby+Security">Ruby Security</h1>
<p>The Ruby programming language is large and complex and there are many security pitfalls often encountered by newcomers and experienced Rubyists alike.</p>
<p>This document aims to discuss many of these pitfalls and provide more secure alternatives where applicable.</p>
<p>Please check the full list of publicly known CVEs and how to correctly report a security vulnerability, at: <a href="https://www.ruby-lang.org/en/security">www.ruby-lang.org/en/security</a>/ Japanese version is here: <a href="https://www.ruby-lang.org/ja/security">www.ruby-lang.org/ja/security</a>/</p>
<p>Security vulnerabilities should be reported via an email to <a href="mailto:[email protected]">[email protected]</a> (<a href="https://www.ruby-lang.org/security.asc" target="_parent" title="the PGP public key">the PGP public key</a>), which is a private mailing list. Reported problems will be published after fixes.</p>
<h2 id="label-24SAFE"><code>$SAFE</code></h2>
<p>Ruby provides a mechanism to restrict what operations can be performed by Ruby code in the form of the <code>$SAFE</code> variable.</p>
<p>However, <code>$SAFE</code> does not provide a secure environment for executing untrusted code.</p>
<p>If you need to execute untrusted code, you should use an operating system level sandboxing mechanism. On Linux, ptrace or LXC can be used to sandbox potentially malicious code. Other similar mechanisms exist on every major operating system.</p>
<h2 id="label-Marshal.load"><code>Marshal.load</code></h2>
<p>Ruby’s <code>Marshal</code> module provides methods for serializing and deserializing Ruby object trees to and from a binary data format.</p>
<p>Never use <code>Marshal.load</code> to deserialize untrusted or user supplied data. Because <code>Marshal</code> can deserialize to almost any Ruby object and has full control over instance variables, it is possible to craft a malicious payload that executes code shortly after deserialization.</p>
<p>If you need to deserialize untrusted data, you should use JSON as it is only capable of returning ‘primitive’ types such as strings, arrays, hashes, numbers and nil. If you need to deserialize other classes, you should handle this manually. Never deserialize to a user specified class.</p>
<h2 id="label-YAML">YAML</h2>
<p>YAML is a popular human readable data serialization format used by many Ruby programs for configuration and database persistence of Ruby object trees.</p>
<p>Similar to <code>Marshal</code>, it is able to deserialize into arbitrary Ruby classes. For example, the following YAML data will create an <code>ERB</code> object when deserialized:</p>
<pre class="code ruby"><code class="ruby">!ruby/object:ERB
src: puts `uname`</code></pre>
<p>Because of this, many of the security considerations applying to Marshal are also applicable to YAML. Do not use YAML to deserialize untrusted data.</p>
<h2 id="label-Symbols">Symbols</h2>
<p>Symbols are often seen as syntax sugar for simple strings, but they play a much more crucial role. The MRI Ruby implementation uses Symbols internally for method, variable and constant names. The reason for this is that symbols are simply integers with names attached to them, so they are faster to look up in hashtables.</p>
<p>Starting in version 2.2, most symbols can be garbage collected; these are called <em>mortal</em> symbols. Most symbols you create (e.g. by calling <code>to_sym</code>) are mortal.</p>
<p><em>Immortal</em> symbols on the other hand will never be garbage collected. They are created when modifying code:</p>
<ul><li>
<p>defining a method (e.g. with <code>define_method</code>),</p>
</li><li>
<p>setting an instance variable (e.g. with <code>instance_variable_set</code>),</p>
</li><li>
<p>creating a variable or constant (e.g. with <code>const_set</code>)</p>
</li></ul>
<p>C extensions that have not been updated and are still calling <code>SYM2ID</code> will create immortal symbols. Bugs in 2.2.0: <code>send</code> and <tt>__send__</tt> also created immortal symbols, and calling methods with keyword arguments could also create some.</p>
<p>Don’t create immortal symbols from user inputs. Otherwise, this would allow a user to mount a denial of service attack against your application by flooding it with unique strings, which will cause memory to grow indefinitely until the Ruby process is killed or causes the system to slow to a halt.</p>
<p>While it might not be a good idea to call these with user inputs, methods that used to be vulnerable such as <code>to_sym</code>, <code>respond_to?</code>, <code>method</code>, <code>instance_variable_get</code>, <code>const_get</code>, etc. are no longer a threat.</p>
<h2 id="label-Regular+expressions">Regular expressions</h2>
<p>Ruby’s regular expression syntax has some minor differences when compared to other languages. In Ruby, the <code>^</code> and <code>$</code> anchors do not refer to the beginning and end of the string, rather the beginning and end of a <strong>line</strong>.</p>
<p>This means that if you’re using a regular expression like <code>/^[a-z]+$/</code> to restrict a string to only letters, an attacker can bypass this check by passing a string containing a letter, then a newline, then any string of their choosing.</p>
<p>If you want to match the beginning and end of the entire string in Ruby, use the anchors <code>\A</code> and <code>\z</code>.</p>
<h2 id="label-eval"><code>eval</code></h2>
<p>Never pass untrusted or user controlled input to <code>eval</code>.</p>
<p>Unless you are implementing a REPL like <code>irb</code> or <code>pry</code>, <code>eval</code> is almost certainly not what you want. Do not attempt to filter user input before passing it to <code>eval</code> - this approach is fraught with danger and will most likely open your application up to a serious remote code execution vulnerability.</p>
<h2 id="label-send"><code>send</code></h2>
<p>‘Global functions’ in Ruby (<code>puts</code>, <code>exit</code>, etc.) are actually private instance methods on <code>Object</code>. This means it is possible to invoke these methods with <code>send</code>, even if the call to <code>send</code> has an explicit receiver.</p>
<p>For example, the following code snippet writes “Hello world” to the terminal:</p>
<pre class="code ruby"><code class="ruby"><span class='int'>1</span>.<span class='id identifier rubyid_send'>send</span>(<span class='symbeg'>:</span><span class='id identifier rubyid_puts'>puts</span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>Hello world</span><span class='tstring_end'>"</span></span>)</code></pre>
<p>You should never call <code>send</code> with user supplied input as the first parameter. Doing so can introduce a denial of service vulnerability:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_foo'>foo</span>.<span class='id identifier rubyid_send'>send</span>(<span class='id identifier rubyid_params'>params</span>[<span class='symbeg'>:</span><span class='id identifier rubyid_bar'>bar</span>]) <span class='comment'># params[:bar] is "exit!"</span></code></pre>
<p>If an attacker can control the first two arguments to <code>send</code>, remote code execution is possible:</p>
<pre class="code ruby"><code class="ruby"><span class='comment'># params is { :a => "eval", :b => "...ruby code to be executed..." }
</span><span class='id identifier rubyid_foo'>foo</span>.<span class='id identifier rubyid_send'>send</span>(<span class='id identifier rubyid_params'>params</span>[<span class='symbeg'>:</span><span class='id identifier rubyid_a'>a</span>]<span class='comma'>,</span> <span class='id identifier rubyid_params'>params</span>[<span class='symbeg'>:</span><span class='id identifier rubyid_b'>b</span>])</code></pre>
<p>When dispatching a method call based on user input, carefully verify that the method name. If possible, check it against a whitelist of safe method names.</p>
<p>Note that the use of <code>public_send</code> is also dangerous, as <code>send</code> itself is public:</p>
<pre class="code ruby"><code class="ruby"><span class='int'>1</span>.<span class='id identifier rubyid_public_send'>public_send</span>(<span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>send</span><span class='tstring_end'>"</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>eval</span><span class='tstring_end'>"</span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>...ruby code to be executed...</span><span class='tstring_end'>"</span></span>)</code></pre>
<h2 id="label-DRb">DRb</h2>
<p>As DRb allows remote clients to invoke arbitrary methods, it is not suitable to expose to untrusted clients.</p>
<p>When using DRb, try to avoid exposing it over the network if possible. If this isn’t possible and you need to expose DRb to the world, you <strong>must</strong> configure an appropriate security policy with <code>DRb::ACL</code>.</p>
<div id='footer'></div>
</div> <!-- content -->
</div> <!-- y_main -->
</body>
</html>