Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Evolution of Rails Security

The Evolution of Rails Security

Rails has a reputation for being secure by default, but how deserved is that reputation? Let’s take a look back at some of the low points in Rails security history: from the first Rails CVE, to the controversial GitHub mass assignment, the 2013 Rails apocalypse, and more recent remote code execution issues. Then we’ll cheer ourselves up with the many cool security features Rails has added over the years! We’ll cover auto-escaping, strong parameters, default security headers, secret storage, and less well-known features like per-form CSRF tokens and upcoming Content Security Policy support.

Justin Collins

April 18, 2018
Tweet

More Decks by Justin Collins

Other Decks in Programming

Transcript

  1. @presidentbeef Ugh, About Me 8 years of application security (AT&T

    Interactive, Twitter, SurveyMonkey) 8 years working on Brakeman OSS (Static analysis security tool for Rails) 4 years working on (More pro static analysis security tool for Rails)
  2. @presidentbeef If you’re using RESTful routing, pay special attention to

    the changes to route generation and recognition. The previous use of the semicolon in URLs has been replaced with a regular /. For instance /person/1;edit has become /person/1/edit. This change was made as several libraries, including mongrel, mistakenly treated semi-colons as query string seperators[sic] and some browsers and http libraries misbehaved.
  3. @presidentbeef Cross-Site Request Forgery Protection “Synchronizer Token Pattern” 1. Save

    a CSRF token to the session 2. Insert the CSRF token in forms 3. Match tokens on POST/DELETE/PATCH
  4. @presidentbeef CSRF Token in Form <form accept-charset="UTF-8" action="/users" class="new_user" id="new_user"

    method="post"> <input name="authenticity_token" type="hidden" value="NvyZey4G/GuBEr6RQJTrsfX1iiptwSBWiu9XOo9h4cE=" />
  5. @presidentbeef Better Cross-Site Request Forgery Protection 1. Save a CSRF

    token to the session 2. Add meta-tags for JavaScript to read tokens 3. Insert the CSRF token in forms 4. Send token with XHR requests 5. Match tokens on POST/DELETE/PATCH/XHR
  6. @presidentbeef Speculated GitHub Code class PublicKeyController < ApplicationController before_filter :authorize_user

    def update @current_key = PublicKey.find_by_id params[:public_key][:id] @current_key.update_attributes(params[:public_key]) end end https://gist.github.com/peternixey/1978249
  7. @presidentbeef Mass Assignment Over Time Rails 2 Optional white/black list

    in models Rails 3.1 Option to require whitelist in models Rails 3.2.3 Whitelist is default in new apps Rails 4 Strong Parameters
  8. @presidentbeef YAML Can Serialize Arbitrary Ruby Objects 2.5.0 :001 >

    require 'yaml' => true 2.5.0 :002 > YAML.dump(Object.new) => "--- !ruby/object {}\n" 2.5.0 :003 > YAML.load(_) => #<Object:0x000000000222dbf0>
  9. @presidentbeef Log a Payload example.com/users/?x=<%25%3D+%60ls%60+%25> Started GET "/users/?x=%3C%25%3D+%60ls%60+%25%3E" for 127.0.0.1

    at 2018-04-17 23:37:52 -0700 Processing by UsersController#index as HTML Parameters: {"x"=>"<%= `ls` %>"} Rendered users/index within layouts/application (0.6ms) Completed 200 OK in 16.9ms (Views: 16.4ms | ActiveRecord: 0.0ms)
  10. @presidentbeef Sanitizing vs. Escaping sanitize "<Some <script>prompt(1)</script>> input" => ">

    input" h "<Some <script>prompt(1)</script>> input" => "&lt;Some &lt;script&gt;prompt(1)&lt;/script&gt;&gt; input"
  11. @presidentbeef CVEs Related to Sanitizing Methods • November 2009 -

    No CVE • CVE-2011-2931 • CVE-2012-3465 • CVE-2013-1855 • CVE-2013-1857 • CVE-2015-7579 • CVE-2015-7580 • CVE-2018-8048 • CVE-2018-3741
  12. @presidentbeef Parameter Log Filtering Started GET "/?password=[FILTERED]" for 127.0.0.1 at

    2018-04-15 11:12:36 -0700 Processing by HomeController#index as HTML Parameters: {"password"=>"[FILTERED]"} Rendered home/index.html.erb within layouts/application (0.0ms) Completed 200 OK in 4.2ms (Views: 4.1ms | ActiveRecord: 0.0ms)
  13. @presidentbeef Rails 3.1: config.force_ssl In Rails 3.1, this just redirected

    requests to HTTPS As of Rails 5.2, this setting: • Redirects requests to HTTPS • Sets secure flag on all cookies • Sends the Strict-Transport-Security header
  14. @presidentbeef Rails 4.0: Encrypted Session Cookies Rails 3: Session cookie

    is marshalled and signed Rails 4: Session cookie is JSON, signed, and encrypted
  15. @presidentbeef Rails 4.0: Default Headers config.action_dispatch.default_headers = { 'X-Frame-Options' =>

    'SAMEORIGIN', 'X-XSS-Protection' => '1; mode=block', 'X-Content-Type-Options' => 'nosniff' }
  16. @presidentbeef Rails 5.2: Default Headers config.action_dispatch.default_headers = { 'X-Frame-Options' =>

    'SAMEORIGIN', 'X-XSS-Protection' => '1; mode=block', 'X-Content-Type-Options' => 'nosniff', 'X-Download-Options' => 'noopen', 'X-Permitted-Cross-Domain-Policies' => 'none', 'Referrer-Policy' => 'strict-origin-when-cross-origin' }
  17. @presidentbeef Rails 5.2: Content-Security-Policy # config/initializers/content_security_policy.rb Rails.application.config.content_security_policy do |policy| policy.default_src

    :self, :https policy.font_src :self, :https, :data policy.img_src :self, :https, :data policy.object_src :none policy.script_src :self, :https policy.style_src :self, :https # Specify URI for violation reports policy.report_uri "/csp-violation-report-endpoint" end