Slide 1

Slide 1 text

Securing Your Rails App Mike Nicholaides @nicholaides promptworks.com

Slide 2

Slide 2 text

http://promptworks.com! @promptworks

Slide 3

Slide 3 text

http://xkcd.com/327/

Slide 4

Slide 4 text

Security

Slide 5

Slide 5 text

Security is hard

Slide 6

Slide 6 text

Seriously. It’s really hard.

Slide 7

Slide 7 text

Are we secure?

Slide 8

Slide 8 text

Awareness

Slide 9

Slide 9 text

Know your risks • data • system • network • identity

Slide 10

Slide 10 text

Know your environment •server •network •hardware

Slide 11

Slide 11 text

Know your perimeter •ops architecture •libraries & services

Slide 12

Slide 12 text

Security Strategy

Slide 13

Slide 13 text

Minimize risks Security Strategy

Slide 14

Slide 14 text

Visibility Security Strategy

Slide 15

Slide 15 text

Audit Logs •key transactions •blocked access attempts •append-only logs

Slide 16

Slide 16 text

Alerting •business anomalies •usage anomalies

Slide 17

Slide 17 text

Have a plan Security Strategy

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Security as part of the process Security Strategy

Slide 20

Slide 20 text

That’s great, but what about my Rails app?

Slide 21

Slide 21 text

Users can submit arbitrary input Fundamental Security Problem:

Slide 22

Slide 22 text

POST /users HTTP/1.1 Host: innocent-corporation.com Content-Type: application/json Content-Length: 26 ! { "user": { "name": "Joe Hacker", "admin": true } }

Slide 23

Slide 23 text

Don’t trust user input

Slide 24

Slide 24 text

• params • cookies • request headers • url path • incoming emails • uploaded files • input from other services • scraped from the web Don’t trust user input

Slide 25

Slide 25 text

Blacklisting

Slide 26

Slide 26 text

Blacklisting: Good class Account < ActiveRecord::Base validates :subdomain, exclusion: { in: ['www', 'mail'] } end

Slide 27

Slide 27 text

Blacklisting: Good “WWW” “ www” “” “wwww”

Slide 28

Slide 28 text

Blacklisting: Bad blacklist = /SELECT|INSERT|UPDATE|DELETE/ ! name = params[:name].gsub(blacklist, '') ! Account.where("name = #{name}")

Slide 29

Slide 29 text

Blacklisting: Bad select selSELECTect SELECT/**/id FROM ... TRUNCATE, DROP, ALTER

Slide 30

Slide 30 text

Blacklisting •occasionally effective •must know ALL bad inputs

Slide 31

Slide 31 text

Whitelisting

Slide 32

Slide 32 text

Whitelisting class User property :admin, Boolean property :name, String property :address, String ! attr_accessible :name, :address end

Slide 33

Slide 33 text

Whitelisting •most effective •least flexible

Slide 34

Slide 34 text

Sanitization <%= sanitize(@user.bio) %>

About Me

I'm a resourceful developer

About Me

performXSS();

I'm a resourceful developer

Slide 35

Slide 35 text

DON’T ROLL YOUR OWN

Slide 36

Slide 36 text

1756

Slide 37

Slide 37 text

Sanitization •effective, but hard •don’t roll your own

Slide 38

Slide 38 text

Safe Data Handling becomes performXSS(); <script> performXSS(); </script>

Slide 39

Slide 39 text

Safe Data Handling becomes 'N Sync WHERE band = '\'N Sync'

Slide 40

Slide 40 text

Safe Data Handling /songs?band=Mary%20Kate%20%26%20Ashley becomes Mary Kate & Ashley

Slide 41

Slide 41 text

Safe Data Handling • super effective • don’t roll your own (if you can help it)

Slide 42

Slide 42 text

Semantic Checks if current_user.owns? account if account.balance > 0

Slide 43

Slide 43 text

Handling User Input • Blacklist • Whitelist • Sanitization • Safe data handling • Semantic checks

Slide 44

Slide 44 text

Recommendations

Slide 45

Slide 45 text

Learn

Slide 46

Slide 46 text

Rails Security Guide

Slide 47

Slide 47 text

Code Review

Slide 48

Slide 48 text

Code Review • Trace user-controllable data • Signatures of common vulnerabilities • review risky code

Slide 49

Slide 49 text

Brakeman

Slide 50

Slide 50 text

Lean on the community

Slide 51

Slide 51 text

The Ruby Toolbox

Slide 52

Slide 52 text

Don’t roll your own •sanitization •escaping/encoding •authentication •cryptography

Slide 53

Slide 53 text

Devise

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

http://www.schneierfacts.com

Slide 56

Slide 56 text

http://www.schneierfacts.com

Slide 57

Slide 57 text

Use Rails

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

Keep Rails up-to-date

Slide 60

Slide 60 text

Ruby on Rails: Security

Slide 61

Slide 61 text

Keep gems up-to-date

Slide 62

Slide 62 text

bundle outdated Outdated gems included in the bundle: * awesome_print (1.2.0 > 1.1.0) * axiom-types (0.0.5 > 0.0.4) * builder (3.2.2 > 3.1.4) * cliver (0.3.1 > 0.2.2) * coderay (1.1.0 > 1.0.9) * database_cleaner (1.2.0 > 1.0.1) * descendants_tracker (0.0.3 > 0.0.1) * guard (2.0.3 > 1.8.3) * guard-rubocop (1.0.0 > 0.2.2) * haml_coffee_assets (1.14.1 > 1.14.0) ...

Slide 63

Slide 63 text

bundler-audit Name: actionpack Version: 3.2.10 Advisory: OSVDB-91452 Criticality: Medium URL: http://www.osvdb.org/show/osvdb/91452 Title: XSS vulnerability in sanitize_css in Action Pack Solution: upgrade to ~> 2.3.18, ~> 3.1.12, >= 3.2.13 ! Name: actionpack Version: 3.2.10 Advisory: OSVDB-91454 Criticality: Medium URL: http://osvdb.org/show/osvdb/91454 Title: XSS Vulnerability in the `sanitize` helper of Ruby on Rails Solution: upgrade to ~> 2.3.18, ~> 3.1.12, >= 3.2.13

Slide 64

Slide 64 text

Keep your code clean

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

http://agileinaflash.blogspot.com/

Slide 67

Slide 67 text

Regression tests for vulnerabilities

Slide 68

Slide 68 text

Outside security audit

Slide 69

Slide 69 text

Security is hard

Slide 70

Slide 70 text

http://xkcd.com/538/

Slide 71

Slide 71 text

@nicholaides mike@promptworks.com