Skip to content
This repository was archived by the owner on Oct 5, 2023. It is now read-only.

Commit

Permalink
Initial commit with length-hiding middleware and railtie
Browse files Browse the repository at this point in the history
  • Loading branch information
bradleybuda committed Aug 2, 2013
0 parents commit 303c1a5
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in breach-mitigation-rails.gemspec
gemspec
22 changes: 22 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2013 BBA, Inc.

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# breach-mitigation-rails

Makes Rails applications less susceptible to the BREACH / CRIME
attacks. See [breachattack.com](http://breachattack.com/) for details.

## How it works

This gem implements two of the suggestion mitigation strategies from
the paper:

*Masking Secrets*: The Rails CSRF token is 'masked' by encrypting it
with a 32-byte one-time pad, and the pad and encrypted token are
returned to the browser, instead of the "real" CSRF token. This only
protects the CSRF token from an attacker; it does not protect other
data on your pages (see the paper for details on this).

*Length Hiding*: The BreachMitigation::LengthHiding middleware
appends an HTML comment up to 2k in length to the end of all HTML
documents served by your app. As noted in the paper, this does not
prevent plaintext recovery, but it can slow the attack and it's
relatively inexpensive to implement. Unlike the CSRF token masking,
length hiding protects the entire page body from recovery.

## Warning!

BREACH and CRIME are **complicated and wide-ranging attacks**, and this
gem offers only partial protection for Rails applications. If you're
concerned about the security of your web app, you should review the
BREACH paper and look for other, application-specific things you can
do to prevent or mitigate this class of attacks.

## Installation

Add this line to your Rails Gemfile:

gem 'breach-mitigation-rails'

And then execute:

$ bundle

TODO And then?

## Contributing

Pull requests are welcome, either to enhance the existing mitigation
strategies or to add new ways to mitigate against the attack.

## License

MIT - see LICENSE.txt
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require "bundler/gem_tasks"
23 changes: 23 additions & 0 deletions breach-mitigation-rails.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'breach_mitigation/version'

Gem::Specification.new do |spec|
spec.name = "breach-mitigation-rails"
spec.version = BreachMitigation::VERSION
spec.authors = ["Bradley Buda"]
spec.email = ["[email protected]"]
spec.description = %q{Mitigates the BREACH and CRIME attacks on TLS in Rails applications}
spec.summary = %q{Uses length-hiding and CSRF token masking to make it more difficult for an attacker to recover plaintext from HTTP responses. See README.md for details.}
spec.homepage = "https://github.com/meldium/breach-mitigation-rails"
spec.license = "MIT"

spec.files = `git ls-files`.split($/)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
end
1 change: 1 addition & 0 deletions lib/breach-mitigation-rails.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'breach_mitigation/railtie' if defined?(Rails)
50 changes: 50 additions & 0 deletions lib/breach_mitigation/length_hiding.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module BreachMitigation
class LengthHiding
def initialize(app)
@app = app
end

def call(env)
status, headers, body = @app.call(env)

# Only pad HTML documents
if headers['Content-Type'] =~ /text\/html/
# Copy the existing response to a new object
response = Rack::Response.new(body, status, headers)

# Append to that response
response.write random_html_comment

body.close if body.respond_to? :close
response.finish
else
[status, headers, body]
end
end

private

# Append a comment from 0 to MAX_LENGTH bytes in size to the
# response body. See section 3.1 of "BREACH: Reviving the CRIME
# attack". This should make BREACH attacks take longer, but does
# not fully protect against them. The longer MAX_LENGTH is, the
# more effective the mitigation is, however longer lengths mean
# more time spent in this middleware and more data on the wire.

MAX_LENGTH = 2048
ALPHABET = ('a'..'z').to_a

def random_html_comment
# The length of the padding should be strongly random, but the
# data itself doesn't need to be strongly random; it just needs
# to be resistant to compression
length = SecureRandom.random_number(1024)

# TODO make this faster
junk = ''
length.times { junk << ALPHABET.sample }

"\n<!-- This is a random-length HTML comment: #{junk} -->"
end
end
end
9 changes: 9 additions & 0 deletions lib/breach_mitigation/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'breach_mitigation/length_hiding'

module BreachMitigation
class Railtie < Rails::Railtie
initializer "breach-mitigation-rails.insert_middleware" do |app|
app.config.middleware.use "BreachMitigation::LengthHiding"
end
end
end
3 changes: 3 additions & 0 deletions lib/breach_mitigation/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module BreachMitigation
VERSION = "0.0.1"
end

0 comments on commit 303c1a5

Please sign in to comment.