Ruby Security Handbook
Ruby Security Handbook
Security Handbook
INTRODUCTION
Damn, but security is hard.
It’s not always obvious what needs doing, and the payoffs of good security are at best
obscure. Who is surprised when it falls off our priority lists?
We’d like to offer a little help if you don’t mind. And by « help » we don’t mean « pitch
you our product »—we genuinely mean it.
Sqreen’s mission is to empower engineers to build secure, reliable web applications. We’ve
put our security knowledge to work in compiling an actionable list of best practices to
help you get a grip on your security priorities. It’s all on the following pages.
We hope your find if useful. If you do, share it with your network. And if you don’t, please
take to Twitter to complain loudly—it’s the best way to get our attention.
!2
INTRODUCTION
Damn, but security is hard.
It’s not always obvious what needs doing, and the payoffs of good security are at best
obscure. Who is surprised when it falls off our priority lists?
We’d like to offer a little help if you don’t mind. And by « help » we don’t mean « pitch
you our product »—we genuinely mean it.
Sqreen’s mission is to empower engineers to build secure, reliable web applications. We’ve
put our security knowledge to work in compiling an actionable list of best practices to
help you get a grip on your security priorities. It’s all on the following pages.
We hope your find if useful. If you do, share it with your network. And if you don’t, please
take to Twitter to complain loudly—it’s the best way to get our attention.
!2
CODE
Learn more:
JSON.load() documentation:
http://ruby-doc.org/stdlib-2.4.1/libdoc/json/rdoc/JSON.html#method-i-load - http://
bit.ly/2ooWwtD
Psych documentation:
https://ruby-doc.org/stdlib-2.4.1/libdoc/psych/rdoc/Psych.html#method-c-safe_load -
http://bit.ly/2BXYx9y
Great blog post explaining the vulnerability in depth:
http://blog.codeclimate.com/blog/2013/01/10/rails-remote-code-execution-
vulnerability-explained/ - http://bit.ly/2ws12vI
Module documentation:
https://ruby-doc.org/stdlib-2.4.1/libdoc/fileutils/rdoc/FileUtils.html - http://bit.ly/
2LL05DO
https://ruby-doc.org/core-2.4.1/File.html - http://bit.ly/2Na8NQN
!3
System documentation:
https://ruby-doc.org/core-2.4.1/Kernel.html#method-i-system - http://bit.ly/2C2ZgGA
Learn how to responsibly use backticks and other child process methods:
https://medium.com/zendesk-engineering/running-a-child-process-in-ruby-properly-
febd0a2b6ec8 - http://bit.ly/2MGADo3
Read more
https://github.com/cryptosphere/rbnacl - http://bit.ly/2wsDeId
http://ruby-doc.org/stdlib-2.4.1/libdoc/openssl/rdoc/OpenSSL.html - http://bit.ly/
2NE7VR2
https://en.wikipedia.org/wiki/Scrypt - http://bit.ly/2PQMgqr
http://crypto.stackexchange.com/questions/43272/why-is-writing-your-own-
encryption-discouraged - http://bit.ly/2onlGIX
!4
Also, you should know which template syntax can introduce XSS vulnerabilities. For
instance, mis-use of ERB’s html_safe method can actually open your app up to XSS
attacks.
ERB:
http://api.rubyonrails.org/classes/ERB/Util.html - http://bit.ly/2C2d1FH
https://makandracards.com/makandra/2579-everything-you-know-about-html_safe-is-
wrong - http://bit.ly/2wqPhFW
HAML documentation:
http://haml.info/docs/yardoc/Haml/Options.html#escape_html-instance_method -
http://bit.ly/2PRQfD5
http://haml.info/docs/yardoc/file.REFERENCE.html#escaping_html - http://bit.ly/
2wroARB
SLIM documentation:
http://www.rubydoc.info/gems/slim/frames#Available_options - http://bit.ly/2N1o1aG
http://www.rubydoc.info/gems/slim/frames#Output__ - http://bit.ly/2Pk4ZJT
https://blogs.dropbox.com/tech/2016/09/how-dropbox-securely-stores-your-
passwords/ - http://bit.ly/2PPilPw
!5
✔ Don't implement your own session management
It’s easy to write bad session management code that is vulnerable to session fixation
attacks. Use Devise, or another well-tested solution instead.
Read more:
Details on how session fixation attacks work:
https://www.owasp.org/index.php/Session_fixation - http://bit.ly/2MEUd4c
Learn more:
✔ Go hack yourself
Once in a while, the entire technical team should sit together and spend time targeting all
parts of the application, looking for vulnerabilities. This is a great time to test for account
isolation, token unicity, unauthenticated paths, etc. You will heavily rely on your browser’s
web console, curl, and 3rd party tools such as Burp.
Learn more:
Burp - http://bit.ly/2onKSPy
Red Team: Pwning the Hearts and Minds one Ticket at a Time -http://bit.ly/2MCvD3P
!6
✔ Keep secrets away from code
Never commit secrets in your code. They should be handled separately in order to
prevent them accidentally being shared or exposed. This allows a clear separation between
your environments (typically development, staging and production).
Learn more:
12 Factor App - http://bit.ly/2PMGypq
Snyk - http://bit.ly/2PiGahI
Sqreen - http://bit.ly/2LHmd1w
Ruby Advisory - http://bit.ly/2wBcq7K
!7
Learn more about SQL injections:
https://en.wikipedia.org/wiki/SQL_injection - http://bit.ly/2C22hqH
✔ Run it unprivileged
If an attacker successfully attacks your application, having it running as a restricted-user
will make it harder for the attacker to take over the host and/or to bounce to other
services. Privileged users are root on Unix systems, and Administrator or System on
Windows systems
Brakeman - http://bit.ly/2NAPZqy
Rubocop - http://bit.ly/2C0xDhk
Awesome Static Analysis - http://bit.ly/2PmyvPp
!8
Arachni - http://bit.ly/2MWfgP9
OWASP Zed - http://bit.ly/2Ny177w
Acunetix - http://bit.ly/2wpuany
Read more:
OWASP Secure Software Development Lifecycle Project - http://bit.ly/2PRgNED
Microsoft Security Development Lifecycle - http://bit.ly/2wsiTml
Learn more:
Secure headers library - http://bit.ly/2ws1QAE
Check your headers and other security configs - http://bit.ly/2C2LLXH
Check your headers and more - http://bit.ly/2ojI2ei
HTTP Security Headers - http://bit.ly/2LIHO9U
!9
Learn more:
ERB: http://api.rubyonrails.org/classes/ERB/Util.html - http://bit.ly/2C2d1FH
https://makandracards.com/makandra/2579-everything-you-know-about-html_safe-
iswrong - http://bit.ly/2Ny7oAn
!10
INFRASTRUCTURE
https://observatory.mozilla.org/ - https://mzl.la/2PMIqP5
!11
https://www.ssllabs.com/ - http://bit.ly/2wu3u44
https://diogomonica.com/2015/12/29/from-double-f-to-double-a/ - http://bit.ly/
2C1Uauc
http://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html - https://amzn.to/
2omecWF
https://cloud.google.com/compute/docs/access/create-enable-service-accounts-for-
instances - http://bit.ly/2PfXCDo
Learn more:
https://letsencrypt.org/ - http://bit.ly/2wvISsi
https://certbot.eff.org/ - http://bit.ly/2POHaeh
https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-
encrypt-on-ubuntu-14-04 - https://do.co/2C1LWT3
https://www.digitalocean.com/community/tutorials/how-to-secure-apache-with-let-s-
encrypt-on-ubuntu-14-04 - https://do.co/2PlIQLq
!12
https://qbox.io/blog/welcome-to-the-elk-stack-elasticsearch-logstash-kibana - http://
bit.ly/2MEyJV8
https://www.loggly.com/ - http://bit.ly/2C1u2jc
Tools:
HashiCorp Vault - http://bit.ly/2wu37Hu
Keywhiz - http://bit.ly/2ooRSvG
AWS CloudHS - https://amzn.to/2wxp8Ex
AWS Key Management Service (KMS) - https://amzn.to/2MGWNGG
http://techblog.netflix.com/2017/03/netflix-security-monkey-on-google-cloud.html -
http://bit.ly/2NvtuDC
https://cloudsploit.com/events - http://bit.ly/2PL5q13
http://ossec.github.io/ - http://bit.ly/2onNywA
https://security.stackexchange.com/a/19386 - http://bit.ly/2NvtIKY
https://github.com/glensc/monitoring-plugin-check_domain - http://bit.ly/2MECFF9
!13
✔ Protect your servers and infrastructure from scanners
Your servers will be scanned in order to fingerprint your application and locate open
services, misconfigurations, etc. You can setup tools to keep these scanners away from
your servers.
https://www.sqreen.com/ - http://bit.ly/2MDSMTm
https://www.digitalocean.com/community/tutorials/how-to-protect-ssh-with-fail2ban-
on-ubuntu-14-04 - https://do.co/2MV8eKt
https://www.sqreen.com/ - http://bit.ly/2MDSMTm
https://www.owasp.org/index.php/Blocking_Brute_Force_Attacks - http://bit.ly/
2wqhH1O
https://security.stackexchange.com/questions/94432/should-i-implement-incorrect-
password-delay-in-a-website-or-a-webservice - http://bit.ly/2wsMTh4
!14
Chef: https://github.com/chef/chef-vault - http://bit.ly/2oknMtg
Puppet: https://puppet.com/blog/encrypt-your-data-using-hiera-eyaml - http://bit.ly/
2C1NlJj
Salt: https://docs.saltstack.com/en/latest/ref/renderers/all/salt.renderers.gpg.html -
http://bit.ly/2wvk6bL
Ansible: http://docs.ansible.com/ansible/playbooks_vault.html - http://bit.ly/2N2uPEF
https://www.ubuntu.com/usn/ - http://bit.ly/2POJstX
https://help.ubuntu.com/community/AutomaticSecurityUpdates - http://bit.ly/2wsNgsZ
https://access.redhat.com/security/vulnerabilities - https://red.ht/2MXEfBB
https://martinfowler.com/bliki/ImmutableServer.html - http://bit.ly/2PlNh92
https://hackernoon.com/configuration-management-is-an-antipattern-
e677e34be64c#.n68b1i3eo - http://bit.ly/2wpE8F8
!15
PROTECTION
https://stripe.com/ - http://bit.ly/2BULIwI
https://www.braintreepayments.com - http://bit.ly/2wxe763
https://www.pcisecuritystandards.org/pdfs/pciscc_ten_common_myths.pdf - http://bit.ly/
2NuCLM1
https://medium.com/@folsen/accepting-payments-is-getting-harder-1b2f342e4ea#.
897akko4q - http://bit.ly/2BXHN2h
https://duo.com/ - https://duo.sc/2LEODJu
https://auth0.com/ - http://bit.ly/2wu0UM9
https://nakedsecurity.sophos.com/2016/08/18/nists-new-password-rules-what-you-
need-to-know/ - http://bit.ly/2wu5yZW
Learn more:
https://cloudsecurityalliance.org/ - http://bit.ly/2C3nTDa
https://en.wikipedia.org/wiki/ISO/IEC_27001:2013 - http://bit.ly/2C3mIDK
!16
https://en.wikipedia.org/wiki/Payment_Card_Industry_Data_Security_Standard - http://
bit.ly/2NxBS5x
Places to start:
https://www.docker.com/docker-security - https://dockr.ly/2MDdm6l
https://docs.docker.com/docker-cloud/builds/image-scan/ - https://dockr.ly/2N7Sp2S
!17
https://jpetazzo.github.io/2015/05/27/docker-images-vulnerabilities/ - http://bit.ly/
2MHTr6E
https://www.slideshare.net/MichaelCherny/security-best-practices-for-kubernetes-
deployment - http://bit.ly/2LGSkym
Tools:
Akamai - http://bit.ly/2optav8
Clouflare - http://bit.ly/2C0sB4t
OVH - http://bit.ly/2PQnFC7
!18
MONITORING
Learn more:
http://docs.aws.amazon.com/general/latest/gr/aws-security-audit-guide.html - https://
amzn.to/2N3ojxt
http://searchenterpriselinux.techtarget.com/tip/Creating-an-inventory-with-nmap-
network-scanning - http://bit.ly/2PkNcCf
https://github.com/Netflix/security_monkey - http://bit.ly/2olV0rZ
https://www.sqreen.com/ - http://bit.ly/2MDSMTm
https://www.linode.com/docs/security/using-fail2ban-for-security#email-alerts - http://
bit.ly/2omxRWD
http://alerta.io/ - http://bit.ly/2MBU1T6
https://www.sqreen.com/ - http://bit.ly/2MDSMTm
!19
✔ Get notified when your app is under attack
You will be attacked. Make sure you have a monitoring system in place that will detect
security events targeting your application before it’s too late. Knowing when your
application is starting to get massively scanned is key to stop more advanced attacks.
https://www.sqreen.com/ - http://bit.ly/2MDSMTm
https://www.linode.com/docs/security/using-fail2ban-for-security#email-alerts - http://
bit.ly/2omxRWD
http://alerta.io/ - http://bit.ly/2MBU1T6
!20
Start your free trial at sqreen.com
!21 www.sqreen.com