Skip to content
This repository has been archived by the owner. It is now read-only.

Commit

Permalink
Add HSTS header
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Nov 4, 2010
1 parent 7ff8100 commit 3543fcc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/rack/ssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ def initialize(app)

def call(env)
if scheme(env) == 'https'
@app.call(env)
status, headers, body = @app.call(env)
headers = hsts_headers.merge(headers)
[status, headers, body]
else
redirect_to_https(env)
end
Expand All @@ -30,7 +32,12 @@ def scheme(env)
def redirect_to_https(env)
req = Request.new(env)
location = req.url.sub(/^http:/, 'https:')
[301, {'Content-Type' => "text/html", 'Location' => location}, []]
[301, hsts_headers.merge({'Content-Type' => "text/html", 'Location' => location}), []]
end

# http://tools.ietf.org/html/draft-hodges-strict-transport-sec-02
def hsts_headers
{ 'Strict-Transport-Security' => "max-age=16070400; includeSubDomains" }
end
end
end
5 changes: 5 additions & 0 deletions test/test_ssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ def test_redirects_http_to_https
assert last_response.redirect?
assert_equal "https://example.org/path?key=value", last_response.headers['Location']
end

def test_strict_transport_security_header
get "https://example.org/path?key=value"
assert_equal "max-age=16070400; includeSubDomains", last_response.headers['Strict-Transport-Security']
end
end

0 comments on commit 3543fcc

Please sign in to comment.