Rails security: above and beyond the defaults

106
Rails Security Above and beyond the defaults

Transcript of Rails security: above and beyond the defaults

Page 1: Rails security: above and beyond the defaults

Rails Security

Above and beyond the defaults

Page 2: Rails security: above and beyond the defaults

kiskolabs.com

Page 3: Rails security: above and beyond the defaults

Ma#as Korhonen

• Twi%er: @ma-askorhonen.fi

• GitHub: ma-askorhonen

• Email: [email protected]

• Web: ma-askorhonen.fi

• Blog: randomerrata.com

Page 4: Rails security: above and beyond the defaults

I start too many side projects

Page 5: Rails security: above and beyond the defaults

Homebrew

No, not the package manager

Page 6: Rails security: above and beyond the defaults

beerstyles.co

An iOS app for

browsing beer style

guidelines

Page 7: Rails security: above and beyond the defaults

piranhas.co

Book price comparison

on the web and iOS

Page 8: Rails security: above and beyond the defaults
Page 9: Rails security: above and beyond the defaults

Disclaimers

Page 10: Rails security: above and beyond the defaults

I am not a cryptographer

Page 11: Rails security: above and beyond the defaults

I am not a white/grey/black hat hacker

Page 12: Rails security: above and beyond the defaults

I'm just a developer who wants to keep his

apps as secure as reasonably possible

Page 13: Rails security: above and beyond the defaults

On with the show

Page 14: Rails security: above and beyond the defaults

What's this talk about?

Page 15: Rails security: above and beyond the defaults

Mostly generic web applica*on

security (with some Rails specific

implementa6on details)

Page 16: Rails security: above and beyond the defaults

RISKS

Page 17: Rails security: above and beyond the defaults

“Why would anyone ever

hack my website?”

— straw man developer

Page 18: Rails security: above and beyond the defaults

Understand that the a+acks

affec/ng a large number of website

owners … are predominantly

automated.1

— Sucuri

1 h$ps://blog.sucuri.net/2015/02/why-websites-get-hacked.html

Page 19: Rails security: above and beyond the defaults

In our analyses, we have found that

it takes about 30 – 45 days for a

new website, with no content or

audience, to be iden7fied and added

to a bot crawler.1

— Sucuri

1 h$ps://blog.sucuri.net/2015/02/why-websites-get-hacked.html

Page 20: Rails security: above and beyond the defaults

“But there's nothing

valuable on my site”

— straw man developer

Page 21: Rails security: above and beyond the defaults

All websites have something of

value for a4ackers: reputa'on2

— Troy Hunt

2 h$ps://www.troyhunt.com/all-websites-have-something-of-value-for-a$ackers-reputa=on/

Page 22: Rails security: above and beyond the defaults

Every site on the

web is a target

Page 23: Rails security: above and beyond the defaults

Rails

Page 24: Rails security: above and beyond the defaults

Rails is a great base for a secure web

applica0on

Page 25: Rails security: above and beyond the defaults

Sane defaults

Page 26: Rails security: above and beyond the defaults

Rails's sane (security) defaults

• CSRF protec-on

• XSS protec-on

• Injec-on protec-on

• SQL

• HTML

• JavaScript

Page 27: Rails security: above and beyond the defaults

Rails's sane (security) defaults

• Default headers

• X-Frame-Options: SAMEORIGIN

• X-XSS-Protection: 1; mode=block

• X-Content-Type-Options: nosniff

Page 28: Rails security: above and beyond the defaults

Rails's sane (security) defaults

• Encrypted session store

• Encourages good development prac5ces

• has_secure_password (bcrypt hashed passwords)

• secrets.yml

• user inputs escaped by default

Page 29: Rails security: above and beyond the defaults

You get all this for “free” when you use Rails

• CSRF protec-on

• XSS protec-on

• Injec-on protec-on

• SQL

• HTML

• JavaScript

• Default headers

• X-Frame-Options

• X-XSS-Protection

• X-Content-Type-Options

• Encrypted session store

• Encourages good development prac-ces

• has_secure_password (bcrypt hashed passwords)

• secrets.yml

• etc

Page 30: Rails security: above and beyond the defaults

What more can we do?

Page 31: Rails security: above and beyond the defaults

HTTPS

Page 32: Rails security: above and beyond the defaults

I firmly believe that as web

developers it is our duty to use

HTTPS for everything possible

Page 33: Rails security: above and beyond the defaults

HTTPS: why?

Even if your site has “nothing valuable”, do you trust:

• every shady wifi hotspot a user might be using?

• all the world's ISPs

Page 34: Rails security: above and beyond the defaults

Beyond underhanded, Comcast and

other carriers are inser3ng their own

ads and no3fica3ons into their

customers’ data streams

— InfoWorld3

3 h$p://www.infoworld.com/ar4cle/2925839/net-neutrality/code-injec4on-new-low-isps.html

Page 35: Rails security: above and beyond the defaults

Google Inves+ga+on: Ad Injec+on Is

Infes+ng Millions of Devices

— Adver(singAge4

4 h$p://adage.com/ar1cle/digital/google-ad-injec1on-affec1ng-millions/305321/

Page 36: Rails security: above and beyond the defaults

HTTPS doesn't just provide privacy

and security, it also provides

integrity

Page 37: Rails security: above and beyond the defaults
Page 38: Rails security: above and beyond the defaults
Page 39: Rails security: above and beyond the defaults

Eventually, Chrome will show a Not

Secure warning for all pages served

over HTTP

— Eric Lawrence5

5 h$ps://developers.google.com/web/updates/2016/10/avoid-not-secure-warn

Page 40: Rails security: above and beyond the defaults

More technical reasons

• SSL/TLS is essen+ally mandatory with HTTP 2.0

• Some browser features are only available over HTTPS6

• Geoloca+on

• Service workers

• Fullscreen

• and others

6 h$ps://groups.google.com/a/chromium.org/forum/#!msg/blink-dev/2LXKVWYkOus/gT-ZamfwAKsJ

Page 41: Rails security: above and beyond the defaults

Visitors to your site will blame you,

not the shady ISP/hotspot

Page 42: Rails security: above and beyond the defaults

How?

Page 43: Rails security: above and beyond the defaults

HTTPS: cer*ficates

• Let's Encrypt7 is your friend

• Free 90 day cer8ficates

• Automated verifica8on and renewal

• AWS Cer8ficate Manager8 is your friend on AWS

• Free cer8ficates for AWS services

• Including wildcard cer8ficates!

• Paid cer8ficates go for as liHle as $5/year (! ~70 rand)9

9 h$ps://www.ssls.com/

8 h$ps://aws.amazon.com/cer3ficate-manager/

7 h$ps://letsencrypt.org/

Page 44: Rails security: above and beyond the defaults

HTTPS: force_ssl is your friend

Rails.application.configure do

...

# Force all access to the app over SSL,

# use Strict-Transport-Security, and

# use secure cookies.

config.force_ssl = true

...

end

Page 45: Rails security: above and beyond the defaults

HTTPS: configura/on

Ubuntu 16.04 LTS, Rails 5.0, Ruby 2.4, Phusion Passenger 5.1

Page 46: Rails security: above and beyond the defaults

Everything seems fine and dandy

Page 47: Rails security: above and beyond the defaults

But is it?

Page 48: Rails security: above and beyond the defaults

Qualys SSL Labs10

SSL Server Test

10 h%ps://www.ssllabs.com

Page 49: Rails security: above and beyond the defaults

Ah, a B.

Page 50: Rails security: above and beyond the defaults

I mean it's not bad…

Page 51: Rails security: above and beyond the defaults

…but we can do be-er

Page 52: Rails security: above and beyond the defaults

This server supports weak Diffie-

Hellman (DH) key exchange

parameters. Grade capped to B.

Learn more

— SSL Report

Page 53: Rails security: above and beyond the defaults

Mozilla SSL Configura0on Generator11

11 h$ps://mozilla.github.io/server-side-tls/ssl-config-generator/

Page 54: Rails security: above and beyond the defaults
Page 55: Rails security: above and beyond the defaults
Page 56: Rails security: above and beyond the defaults

server {

listen 443 ssl http2;

listen [::]:443 ssl http2;

# certs sent to the client in SERVER HELLO are concatenated in ssl_certificate

ssl_certificate /path/to/signed_cert_plus_intermediates;

ssl_certificate_key /path/to/private_key;

ssl_session_timeout 1d;

ssl_session_cache shared:SSL:50m;

ssl_session_tickets off;

# modern configuration. tweak to your needs.

ssl_protocols TLSv1.2;

ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:...';

ssl_prefer_server_ciphers on;

# HSTS (15768000 seconds = 6 months)

add_header Strict-Transport-Security max-age=15768000;

# OCSP Stapling ---

# fetch OCSP records from URL in ssl_certificate and cache them

ssl_stapling on;

ssl_stapling_verify on;

## verify chain of trust of OCSP response using Root CA and Intermediate certs

ssl_trusted_certificate /path/to/root_CA_cert_plus_intermediates;

resolver <IP DNS resolver>;

....

}

Page 57: Rails security: above and beyond the defaults

TADA !

Page 58: Rails security: above and beyond the defaults
Page 59: Rails security: above and beyond the defaults

One more thing: HSTS preload12

12 h%ps://hstspreload.org/

Page 60: Rails security: above and beyond the defaults

HSTS preload

1. Serve a valid cer+ficate.

2. Redirect from HTTP to HTTPS on the same host

3. Serve all subdomains over HTTPS.

4. Serve an HSTS header on the base domain for HTTPS requests

Page 61: Rails security: above and beyond the defaults

CSPContent Security Policy

Page 62: Rails security: above and beyond the defaults

Content Security Policy is an added

layer of security that helps to detect

and mi3gate certain types of

a5acks, including Cross Site

Scrip3ng (XSS) and data injec3on

a5acks.

— MDN

Page 63: Rails security: above and beyond the defaults

Content Security Policy: a header

which tells the browser where

assets (scripts, stylesheets, fonts,

and so on) can be loaded from.

— Me

Page 64: Rails security: above and beyond the defaults

Supported by all major browsers,

even Internet Explorer (kind of)

Page 65: Rails security: above and beyond the defaults
Page 66: Rails security: above and beyond the defaults

CSP: Why?

• Reduces the poten.al surface area for a3acks or malicious

injec.on of scripts

• Can help prevent malicious browser extensions and malware

from inser.ng crap into your pages.

• For example, the CSP on Piranhas.co has stopped some shady

browser extensions from injec.ng ads? onto the page.

Page 67: Rails security: above and beyond the defaults
Page 68: Rails security: above and beyond the defaults

static.cmptch.com

Page 69: Rails security: above and beyond the defaults

I'm not 100% sure what this is, but I'm 100% sure I don't want it on my site

Page 70: Rails security: above and beyond the defaults

Content Security Policies allow quite

fine grained control over what can

be loaded from where.

Page 71: Rails security: above and beyond the defaults

Simple example of a CSP

Content-Security-Policy: script-src 'self'

Only allow scripts from the same origin as the page

Page 72: Rails security: above and beyond the defaults

Simple example of a CSP

Content-Security-Policy: script-src 'self' https://apis.google.com

Same origin as the page and apis.google.com

Page 73: Rails security: above and beyond the defaults

Available direc,ves

• default-src: fallback policy

• script-src: which scripts the protected resource can execute

• style-src: which CSS applies to the protected resource

• img-src: where the protected resource can load images

• font-src: where the protected resource can load fonts

• and a lot more, if you have more esoteric needs

Page 74: Rails security: above and beyond the defaults

Repor&ng

The report-uri direc)ve lets you get JSON reports for viola)ons

{

"csp-report": {

"document-uri": "http://example.com/signup.html",

"referrer": "",

"blocked-uri": "http://example.com/css/style.css",

"violated-directive": "style-src cdn.example.com",

"original-policy": "style-src cdn.example.com; report-uri /_csp-reports"

}

}

Page 75: Rails security: above and beyond the defaults

report-uri.io

Free repor'ng endpoint and UI for CSP viola'ons

Page 76: Rails security: above and beyond the defaults
Page 77: Rails security: above and beyond the defaults

Where to start?

Page 78: Rails security: above and beyond the defaults

Adding a CSP header to a long

standing site can be … tricky

Having it there from the start is a lot easier

Page 79: Rails security: above and beyond the defaults

Where to start?

Content-Security-Policy: default-src *;

Allow all sources, but disallow unsafe inline assets (for example

scripts and styles).

Page 80: Rails security: above and beyond the defaults

SecureHeaders13

Security related headers all in one gem

13 h%ps://github.com/twi%er/secureheaders

Page 81: Rails security: above and beyond the defaults

Provides support for CSP headers and a lot more

Page 82: Rails security: above and beyond the defaults
Page 83: Rails security: above and beyond the defaults

The defaults are strict

but not ridiculously so

Page 84: Rails security: above and beyond the defaults

The not ridiculously strict defaults

Content-Security-Policy: default-src 'self' …continues

Strict-Transport-Security: max-age=631138519

X-Content-Type-Options: nosniff

X-Download-Options: noopen

X-Frame-Options: sameorigin

X-Permitted-Cross-Domain-Policies: none

X-Xss-Protection: 1; mode=block

Page 85: Rails security: above and beyond the defaults

The CSP which didn't fit in the last slide

Content-Security-Policy: default-src 'self' https:;

font-src 'self' https: data:;

img-src 'self' https: data:;

object-src 'none';

script-src https:;

style-src 'self' https: 'unsafe-inline'

You'll probably want to add report-uri to that

Page 86: Rails security: above and beyond the defaults

S"ll afraid?

Page 87: Rails security: above and beyond the defaults

Content-Security-Policy-Report-Only

Page 88: Rails security: above and beyond the defaults

CSP pro-)ps

• New projects

• Enforce the CSP from the beginning

• Report viola8ons from your staging or produc8on environment

• Old projects

• Add a CSP with all the sources you think you need

• Deploy it as Report Only, leave it for a week or two to uncover anything

you might have forgoBen about

• Deploy the enforced policy once you've accounted for all the viola8ons

Page 89: Rails security: above and beyond the defaults

HTTP, HTTPS, CSP, SSL, TLS, XSS,

CSRF, and so forth

Enough alphabet soup yet?

Page 90: Rails security: above and beyond the defaults

HPKP

Page 91: Rails security: above and beyond the defaults

HTTP Public Key PinningWhat is it and should you use it?

Page 92: Rails security: above and beyond the defaults

One more security HTTP header

Public-Key-Pins:

pin-sha256="cUPcTAZWKaASuYWhhneDttWpY3oBAkE3h2+soZS7sWs=";

pin-sha256="M8HztCzM3elUxkcjR2S5P4hhyBNf6lHkmjAHKhpGPWE=";

max-age=5184000; includeSubDomains;

report-uri="https://www.example.org/hpkp-report"

Lets you limit what public keys the

browser will trust in the future

Page 93: Rails security: above and beyond the defaults

Foot, meet gun

Page 94: Rails security: above and beyond the defaults

If you mess it up, you can lock out

users for days, weeks, or months

Page 95: Rails security: above and beyond the defaults

If you mess up on a produc0on site,

there is no undo bu'on

(aside from wai-ng for it to expire)

Page 96: Rails security: above and beyond the defaults

IMO, not worth it

The benefits are too small compared to the

massive damage you can poten7ally do.

Page 97: Rails security: above and beyond the defaults

However

Page 98: Rails security: above and beyond the defaults

Some &ps if you do go down this road…

Page 99: Rails security: above and beyond the defaults

Some &ps if you do go down this road…

1. Start with a very short expiry 1me (minutes)

2. Include pins for one or two backup keys

3. The backup keys should not touch the server un4l you need

them

• Keep them in cold storage, preferable secure and offline

4. You can also choose to pin a CA public key

Page 100: Rails security: above and beyond the defaults

Summary

Page 101: Rails security: above and beyond the defaults

Summary

• Rails defaults are pre/y good, but can (fairly easily) be 9ghtened

• You should use HTTPS

• Test that HTTPS is set-up correctly

• The Mozilla SSL Configura9on Generator is great

Page 102: Rails security: above and beyond the defaults

Summary

• Use a Content Security Policy, if only to reduce the surface area available

for a9acks

• The more strict its is, the fewer chances there are for third par?es to

mess with your site

• Use the SecureHeaders gem to manage the policy

• It requires more thought than the Rails defaults, but I think it's worth it

• Excep&on to most of the above: If you're working on your first Rails

app, you probably shouldn't add this complexity.

Page 103: Rails security: above and beyond the defaults

Summary

• HTTP Public Key Pinning can be an excellent way to shoot

yourself in the foot

• If used correctly, you can effec=vely prevent a rogue CA from

issuing certs for your domain

• I don't consider this a major vulnerability for most sites

Page 104: Rails security: above and beyond the defaults

Thanks. Ques,ons?

Page 105: Rails security: above and beyond the defaults

Thanks again

• Twi%er: @ma-askorhonen.fi

• GitHub: ma-askorhonen

• Email: [email protected]

• Web: ma-askorhonen.fi

• Blog: randomerrata.com

Page 106: Rails security: above and beyond the defaults

Resources

• HTTPS

• h#ps://letsencrypt.org

• h#ps://mozilla.github.io/server-side-tls/ssl-config-generator/

• h#ps://www.ssllabs.com

• CSP

• h#ps://report-uri.io

• h#ps://sco#helme.co.uk/content-security-policy-an-introduc>on/

• h#ps://developer.mozilla.org/en-US/docs/Web/Security/CSP/Using_Content_Security_Policy

• h#ps://github.com/twi#er/secureheaders

• HPKP

• h#ps://developer.mozilla.org/en-US/docs/Web/HTTP/Public_Key_Pinning