237 Web application security [3d]

Updated . Posted . Visible to the public.

Web applications are attacked constantly. This lesson covers the most common attacks like XSS, CSRF and SQL injection, how Rails protects you, and where you still need to be careful.

Important

Work on this lesson in advisor mode.

Learning goals

  • You can explain the CIA triad — confidentiality, integrity, availability — and sort any threat into it.
  • You can explain how the most common attacks on web applications work: cross-site scripting, cross-site request forgery, SQL injection, mass assignment, insecure direct object references (e.g. building a file path from user input), session hijacking.
  • You know which of these Rails prevents by default and where you still have to do the right thing yourself, e.g. with html_safe, strong parameters or file paths from parameters.
  • You can set a strict Content Security Policy for an app and explain what it prevents.
  • You can explain how vulnerable dependencies endanger an app and how we find and fix them, e.g. with bundler-audit, CVE advisories and Rails LTS.
  • You can explain what HTTPS guarantees against an attacker on the network — and what it doesn't.
  • You read code — your own, a colleague's or an agent's — through the eyes of an attacker and spot these vulnerabilities in a review.
  • You can use a static analyzer like Brakeman as a safety net and interpret its warnings.

Resources

Read what's new to you, skim what's familiar, skip what you already master. Stop when you can meet the learning goals.

Your agent can also generate an overview, a tutorial or an explanation for anything here, tailored to what you already know. Just ask.

The map

Rails

Content Security Policy

Vulnerable dependencies

HTTPS

Exercises

Controller review

Assume our application has some PDF files with documentation:

app/
config/
db/
documents/
  markdown.pdf
  search.pdf
  registration.pdf
lib/
...

We want to link to a document like this:

= link_to 'Markdown help', '/help?doc=markdown'

So we add a controller that delivers the PDF:

get '/help', to: 'help#download'
class HelpController < ApplicationController

  def download
    pdf_path = Rails.root + "/documents/" + params[:doc] + ".pdf"
    send_file pdf_path
  end

end

Is it a good idea to build the controller that way? Can you improve it?

Tip

Get into a habit of reading code through the eyes of an attacker with bad intentions.

XSS in Rails

  • Intentionally make MovieDB vulnerable to HTML code injection
  • Boot up a second Rails application on another port (e.g. rails server -p 4000 to boot it on http://localhost:4000). Assume this is a second domain under the attacker's control.
  • Exploit your vulnerability by sending MovieDB's cookies to the attacker's Rails app.

Brakeman analysis

Brakeman Show archive.org snapshot is a static analysis tool which checks Ruby on Rails applications for security vulnerabilities.

Read our corresponding guide, then run brakeman -I on your MovieDB.

  • If it doesn't find any issues, run it on one of the sample apps you checked out earlier.
  • In any case, add a new spec to your MovieDB. It should check that brakeman does not find any (non-ignored) issues.

Understand a few of the warnings and their specific impact on the application you scanned. Could an attacker leverage the issue if we don't address it?

bundler-audit

bundler-audit Show archive.org snapshot checks for vulnerable gem versions in your Gemfile.lock.

Run bundler-audit on your MovieDB. If it doesn't find any issues, run it on one of the sample apps you checked out earlier.

Understand a few of the warnings and their specific impact on the application you scanned. Could an attacker leverage the issue if we don't address it?

An attacker on your network

Assume an attacker takes over the router box on your local network. The attacker can see and change any network communication between your PC and the internet.

Unaware of the attacker, you purchase articles in an online shop. Answer the following questions for both cases (1) with HTTP and (2) with HTTPS:

  • Can the attacker see that you're accessing the shop?
  • Can the attacker see how often you're accessing the shop?
  • Can the attacker see what articles you're browsing?
  • Can the attacker copy your session cookies?
  • Can the attacker record the network traffic while you're making a request, then send the same bytes from their own machine and get the same response ("replay attack")?
  • Can the attacker set up a fake web app on your local network and respond to your requests to the shop?
  • Is it safe to give your credit card information to a shop with a valid SSL certificate?
Profile picture of Henning Koch
Henning Koch
Last edit
Michael Leimstädtner
Keywords
html_safe, safebuffer
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra Curriculum (2015-08-05 13:28)