Skip to content

Commit

Permalink
Add HSTS preload option
Browse files Browse the repository at this point in the history
  • Loading branch information
gorism committed Jul 20, 2015
1 parent 60aa16f commit 07c0bf8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ pkg
Gemfile.lock

## PROJECT::SPECIFIC
.ruby-gemset
.ruby-version
5 changes: 3 additions & 2 deletions lib/rack/ssl-enforcer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def call(env)

if redirect_required?
call_before_redirect
modify_location_and_redirect
modify_location_and_redirect
elsif ssl_request?
status, headers, body = @app.call(env)
flag_cookies_as_secure!(headers) if @options[:force_secure_cookies]
Expand Down Expand Up @@ -195,10 +195,11 @@ def flag_cookies_as_secure!(headers)

# see http://en.wikipedia.org/wiki/Strict_Transport_Security
def set_hsts_headers!(headers)
opts = { :expires => 31536000, :subdomains => true }
opts = { :expires => 31536000, :subdomains => true, :preload => false }
opts.merge!(@options[:hsts]) if @options[:hsts].is_a? Hash
value = "max-age=#{opts[:expires]}"
value += "; includeSubDomains" if opts[:subdomains]
value += "; preload" if opts[:preload]
headers.merge!({ 'Strict-Transport-Security' => value })
end

Expand Down
9 changes: 7 additions & 2 deletions test/rack-ssl-enforcer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -789,17 +789,22 @@ def self.startup
end

context ':hsts' do
setup { mock_app :hsts => { :expires => '500', :subdomains => false } }
setup { mock_app :hsts => { :expires => '500', :subdomains => false, :preload => true } }

should 'set expiry option' do
get 'https://www.example.org/'
assert_equal "max-age=500", last_response.headers["Strict-Transport-Security"]
assert last_response.headers["Strict-Transport-Security"].include?("max-age=500")
end

should 'not include subdomains' do
get 'https://www.example.org/'
assert !last_response.headers["Strict-Transport-Security"].include?("includeSubDomains")
end

should 'set preload option' do
get 'https://www.example.org'
assert last_response.headers["Strict-Transport-Security"].include?("preload")
end
end

context ':force_secure_cookie == false' do
Expand Down

0 comments on commit 07c0bf8

Please sign in to comment.