PATH:
opt
/
alt
/
ruby34
/
share
/
ri
/
system
U:RDoc::TopLevel[ i I"security.rdoc:ETcRDoc::Parser::Simpleo:RDoc::Markup::Document:@parts[SS:RDoc::Markup::Heading: leveli: textI"Ruby Security;To:RDoc::Markup::BlankLine o:RDoc::Markup::Paragraph;[I"TThe Ruby programming language is large and complex and there are many security ;TI"Lpitfalls often encountered by newcomers and experienced Rubyists alike.;T@ o; ;[I"RThis document aims to discuss many of these pitfalls and provide more secure ;TI"#alternatives where applicable.;T@ o; ;[I"UPlease check the full list of publicly known CVEs and how to correctly report a ;TI"Hsecurity vulnerability, at: https://www.ruby-lang.org/en/security/ ;TI"EJapanese version is here: https://www.ruby-lang.org/ja/security/;T@ o; ;[ I"ASecurity vulnerabilities should be reported via an email to ;TI"4mailto:security@ruby-lang.org ({the PGP public ;TI"Ukey}[https://www.ruby-lang.org/security.asc]), which is a private mailing list. ;TI"5Reported problems will be published after fixes.;T@ S; ; i;I"+Marshal.load+;T@ o; ;[I"URuby's +Marshal+ module provides methods for serializing and deserializing Ruby ;TI"3object trees to and from a binary data format.;T@ o; ;[ I"NNever use +Marshal.load+ to deserialize untrusted or user supplied data. ;TI"NBecause +Marshal+ can deserialize to almost any Ruby object and has full ;TI"Rcontrol over instance variables, it is possible to craft a malicious payload ;TI"6that executes code shortly after deserialization.;T@ o; ;[ I"RIf you need to deserialize untrusted data, you should use JSON as it is only ;TI"Ucapable of returning 'primitive' types such as strings, arrays, hashes, numbers ;TI"Oand nil. If you need to deserialize other classes, you should handle this ;TI";manually. Never deserialize to a user specified class.;T@ S; ; i;I" YAML;T@ o; ;[I"RYAML is a popular human readable data serialization format used by many Ruby ;TI"Nprograms for configuration and database persistence of Ruby object trees.;T@ o; ;[I"RSimilar to +Marshal+, it is able to deserialize into arbitrary Ruby classes. ;TI"KFor example, the following YAML data will create an +ERB+ object when ;TI"2deserialized, using the `unsafe_load` method:;T@ o:RDoc::Markup::Verbatim;[I"!ruby/object:ERB ;TI"src: puts `uname` ;T:@format0o; ;[I"RBecause of this, many of the security considerations applying to Marshal are ;TI"Lalso applicable to YAML. Do not use YAML to deserialize untrusted data.;T@ S; ; i;I"Symbols;T@ o; ;[ I"USymbols are often seen as syntax sugar for simple strings, but they play a much ;TI"Pmore crucial role. The MRI Ruby implementation uses Symbols internally for ;TI"Rmethod, variable and constant names. The reason for this is that symbols are ;TI"Ssimply integers with names attached to them, so they are faster to look up in ;TI"hashtables.;T@ o; ;[I"OStarting in version 2.2, most symbols can be garbage collected; these are ;TI"Lcalled <i>mortal</i> symbols. Most symbols you create (e.g. by calling ;TI"+to_sym+) are mortal.;T@ o; ;[I"P<i>Immortal</i> symbols on the other hand will never be garbage collected. ;TI"*They are created when modifying code:;To:RDoc::Markup::List: @type:BULLET:@items[o:RDoc::Markup::ListItem:@label0;[o; ;[I"3defining a method (e.g. with +define_method+),;To;;0;[o; ;[I"Fsetting an instance variable (e.g. with +instance_variable_set+),;To;;0;[o; ;[I"<creating a variable or constant (e.g. with +const_set+);To; ;[ I"LC extensions that have not been updated and are still calling `SYM2ID` ;TI"#will create immortal symbols. ;TI"IBugs in 2.2.0: +send+ and +__send__+ also created immortal symbols, ;TI"Gand calling methods with keyword arguments could also create some.;T@ o; ;[ I"KDon't create immortal symbols from user inputs. Otherwise, this would ;TI"Rallow a user to mount a denial of service attack against your application by ;TI"Sflooding it with unique strings, which will cause memory to grow indefinitely ;TI"Muntil the Ruby process is killed or causes the system to slow to a halt.;T@ o; ;[I"TWhile it might not be a good idea to call these with user inputs, methods that ;TI"<used to be vulnerable such as +to_sym+, +respond_to?+, ;TI"Q+method+, +instance_variable_get+, +const_get+, etc. are no longer a threat.;T@ S; ; i;I"Regular expressions;T@ o; ;[ I"RRuby's regular expression syntax has some minor differences when compared to ;TI"Tother languages. In Ruby, the <code>^</code> and <code>$</code> anchors do not ;TI"Urefer to the beginning and end of the string, rather the beginning and end of a ;TI"*line*.;T@ o; ;[ I"?This means that if you're using a regular expression like ;TI"S<code>/^[a-z]+$/</code> to restrict a string to only letters, an attacker can ;TI"Ubypass this check by passing a string containing a letter, then a newline, then ;TI""any string of their choosing.;T@ o; ;[I"RIf you want to match the beginning and end of the entire string in Ruby, use ;TI"the anchors +\A+ and +\z+.;T@ S; ; i;I"+eval+;T@ o; ;[I"=Never pass untrusted or user controlled input to +eval+.;T@ o; ;[ I"NUnless you are implementing a REPL like +irb+ or +pry+, +eval+ is almost ;TI"Ucertainly not what you want. Do not attempt to filter user input before passing ;TI"Sit to +eval+ - this approach is fraught with danger and will most likely open ;TI"Jyour application up to a serious remote code execution vulnerability.;T@ S; ; i;I"+send+;T@ o; ;[I"U'Global functions' in Ruby (+puts+, +exit+, etc.) are actually private instance ;TI"Qmethods on +Object+. This means it is possible to invoke these methods with ;TI"A+send+, even if the call to +send+ has an explicit receiver.;T@ o; ;[I"RFor example, the following code snippet writes "Hello world" to the terminal:;T@ o;;[I""1.send(:puts, "Hello world") ;T;0o; ;[I"SYou should never call +send+ with user supplied input as the first parameter. ;TI">Doing so can introduce a denial of service vulnerability:;T@ o;;[I"6foo.send(params[:bar]) # params[:bar] is "exit!" ;T;0o; ;[I"OIf an attacker can control the first two arguments to +send+, remote code ;TI"execution is possible:;T@ o;;[I"J# params is { :a => "eval", :b => "...ruby code to be executed..." } ;TI"&foo.send(params[:a], params[:b]) ;T;0o; ;[I"SWhen dispatching a method call based on user input, carefully verify that the ;TI"Qmethod name. If possible, check it against a whitelist of safe method names.;T@ o; ;[I"ONote that the use of +public_send+ is also dangerous, as +send+ itself is ;TI"public:;T@ o;;[I"E1.public_send("send", "eval", "...ruby code to be executed...") ;T;0S; ; i;I"DRb;T@ o; ;[I"UAs DRb allows remote clients to invoke arbitrary methods, it is not suitable to ;TI"!expose to untrusted clients.;T@ o; ;[I"TWhen using DRb, try to avoid exposing it over the network if possible. If this ;TI"Uisn't possible and you need to expose DRb to the world, you *must* configure an ;TI"<appropriate security policy with <code>DRb::ACL</code>.;T: @file@:0@omit_headings_from_table_of_contents_below0
[+]
..
[+]
UnicodeNormalize
[+]
OptionParser
[+]
Integer
[+]
Ractor
[+]
Bundler
[-] page-implicit_conversion_rdoc.ri
[edit]
[+]
Digest
[+]
Addrinfo
[-] cache.ri
[edit]
[+]
FloatDomainError
[+]
TCPServer
[+]
KeyError
[+]
SystemCallError
[+]
RactorLocalSingleton
[+]
Prism
[+]
Exception
[+]
SystemStackError
[+]
Hash
[+]
Math
[+]
Rational
[+]
NEWS
[+]
Timeout
[-] page-fiber_md.ri
[edit]
[-] page-extension_rdoc.ri
[edit]
[+]
Shellwords
[-] page-maintainers_md.ri
[edit]
[-] page-marshal_rdoc.ri
[edit]
[+]
English
[+]
Process
[+]
GC
[-] page-extension_ja_rdoc.ri
[edit]
[+]
FiberError
[-] page-dig_methods_rdoc.ri
[edit]
[+]
Singleton
[+]
ARGF
[+]
StringScanner
[+]
Fcntl
[-] page-command_injection_rdoc.ri
[edit]
[+]
File
[+]
ArgumentError
[-] page-LEGAL.ri
[edit]
[+]
Interrupt
[+]
IPAddr
[+]
Benchmark
[+]
Enumerator
[+]
UDPSocket
[+]
Monitor
[-] page-NEWS_md.ri
[edit]
[+]
ENV
[+]
RubyVM
[-] page-index_md.ri
[edit]
[+]
WeakRef
[+]
Reline
[+]
Class
[+]
EncodingError
[-] page-COPYING_ja.ri
[edit]
[+]
TCPSocket
[+]
SOCKSSocket
[-] page-globals_rdoc.ri
[edit]
[+]
Net
[+]
Dir
[+]
Random
[+]
Signal
[-] page-format_specifications_rdoc.ri
[edit]
[-] page-distribution_md.ri
[edit]
[+]
MonitorMixin
[+]
Complex
[+]
SystemExit
[-] page-strftime_formatting_rdoc.ri
[edit]
[-] page-COPYING.ri
[edit]
[+]
Proc
[+]
UNIXSocket
[+]
StringIO
[+]
Refinement
[+]
Readline
[+]
Struct
[+]
RbConfig
[+]
PrettyPrint
[+]
RangeError
[-] page-contributing_md.ri
[edit]
[-] page-ractor_md.ri
[edit]
[+]
PTY
[+]
NameError
[+]
WIN32OLE
[+]
NoMethodError
[+]
Range
[+]
Date
[-] page-bsearch_rdoc.ri
[edit]
[+]
OLEProperty
[+]
NotImplementedError
[+]
FileTest
[+]
Regexp
[+]
Kernel
[+]
BasicObject
[-] page-packed_data_rdoc.ri
[edit]
[+]
Fiddle
[+]
UnboundMethod
[+]
Open3
[+]
optparse
[+]
Marshal
[+]
MatchData
[+]
regexp
[+]
Object
[+]
Coverage
[-] page-encodings_rdoc.ri
[edit]
[+]
ThreadError
[-] page-memory_view_md.ri
[edit]
[-] page-dtrace_probes_rdoc.ri
[edit]
[+]
NoMemoryError
[+]
UNIXServer
[+]
Resolv
[-] page-windows_md.ri
[edit]
[+]
date
[+]
TSort
[+]
EOFError
[+]
Method
[-] page-security_rdoc.ri
[edit]
[+]
URI
[+]
OpenStruct
[+]
FrozenError
[-] page-README_ja_md.ri
[edit]
[+]
IndexError
[+]
ERB
[+]
Comparable
[+]
PStore
[+]
Ripper
[+]
Gem
[+]
SecurityError
[+]
Pathname
[+]
Warning
[+]
Thread
[+]
LoadError
[+]
RegexpError
[+]
Float
[+]
Numeric
[+]
SyntaxSuggest
[+]
RDoc
[+]
Rake
[+]
StandardError
[+]
FalseClass
[-] page-README_md.ri
[edit]
[-] page-character_selectors_rdoc.ri
[edit]
[+]
Encoding
[+]
Time
[+]
YAML
[+]
MakeMakefile
[+]
UncaughtThrowError
[+]
Delegator
[-] page-signals_rdoc.ri
[edit]
[+]
Enumerable
[+]
StopIteration
[+]
ObjectSpace
[+]
ClosedQueueError
[+]
OpenSSL
[-] page-standard_library_md.ri
[edit]
[+]
ErrorHighlight
[+]
IO
[+]
BasicSocket
[+]
BigDecimal
[+]
Win32
[+]
Logger
[+]
PP
[+]
Continuation
[+]
SingleForwardable
[+]
CGI
[+]
JSON
[+]
fatal
[+]
yjit
[+]
Module
[+]
Data
[+]
TypeError
[+]
ThreadGroup
[+]
Zlib
[+]
OpenURI
[+]
SimpleDelegator
[+]
Find
[+]
syntax
[+]
ruby
[+]
SignalException
[+]
FileUtils
[+]
ScriptError
[+]
rjit
[+]
contributing
[+]
ZeroDivisionError
[+]
TrueClass
[+]
NoMatchingPatternKeyError
[+]
DidYouMean
[+]
RuntimeError
[+]
DateTime
[+]
CoreExtensions
[-] page-syntax_rdoc.ri
[edit]
[-] page-exceptions_md.ri
[edit]
[-] page-bug_triaging_rdoc.ri
[edit]
[+]
Etc
[+]
IOError
[+]
Errno
[+]
Fiber
[+]
Symbol
[+]
LocalJumpError
[+]
TracePoint
[+]
NilClass
[+]
Forwardable
[-] page-case_mapping_rdoc.ri
[edit]
[+]
Array
[+]
NoMatchingPatternError
[+]
IPSocket
[+]
SyntaxError
[+]
Binding
[+]
SocketError
[+]
Set
[+]
String
[+]
Socket
[+]
SecureRandom
[+]
Psych
[+]
Tempfile