Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make secure cookie flag optional #22

Merged
merged 1 commit into from
Apr 20, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ To set HSTS expiry and subdomain inclusion (defaults: one year, true). Strict op
config.middleware.use Rack::SslEnforcer, :hsts => { :expires => 500, :subdomains => false }
config.middleware.use Rack::SslEnforcer, :hsts => true # equivalent to { :expires => 31536000, :subdomains => true }

Finally you might want to share a cookie based session between http and https.
This is not possible by default with Rack::SslEnforcer for security reasons.
See: [http://en.wikipedia.org/wiki/HTTP_cookie#Cookie_hijacking]

Nevertheless, you can set the option :force_secure_cookies to false in order to be able to share a cookie based session between http and https:

config.middleware.use Rack::SslEnforcer, :only => "/login", :force_secure_cookies => false

But be aware that if you do so, you have to make sure that the content of you cookie is encoded.
This can be done using a coder with Rack::Session::Cookie.
See: [https://github.com/rack/rack/blob/master/lib/rack/session/cookie.rb#L28-42]


== TODO

Expand Down
3 changes: 2 additions & 1 deletion lib/rack/ssl-enforcer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class SslEnforcer

def initialize(app, options = {})
@app, @options = app, options
$stderr.puts "WARN -- : The option :force_secure_cookies is set to false so make sure your cookies are encoded and that you understand the consequences (see documentation)" if options[:force_secure_cookies]==false
end

def call(env)
Expand All @@ -19,7 +20,7 @@ def call(env)
[301, { 'Content-Type' => 'text/html', 'Location' => location }, [body]]
elsif ssl_request?(env)
status, headers, body = @app.call(env)
flag_cookies_as_secure!(headers)
flag_cookies_as_secure!(headers) unless @options[:force_secure_cookies]==false
set_hsts_headers!(headers) if @options[:hsts] && !@options[:strict]
[status, headers, body]
else
Expand Down
12 changes: 12 additions & 0 deletions test/rack-ssl-enforcer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -700,5 +700,17 @@ class TestRackSslEnforcer < Test::Unit::TestCase
assert !last_response.headers["Strict-Transport-Security"].include?("includeSubDomains")
end
end

context 'that has force_secure_cookie option set to false' do
$stderr = StringIO.new
setup { mock_app :force_secure_cookies => false }

should 'not secure cookies but warn the user of the consequences' do
get 'https://www.example.org/users/123/edit'
assert_equal ["id=1; path=/", "token=abc; path=/; secure; HttpOnly"], last_response.headers['Set-Cookie'].split("\n")
$stderr.rewind
assert_equal "WARN -- : The option :force_secure_cookies is set to false so make sure your cookies are encoded and that you understand the consequences (see documentation)\n", $stderr.read
end
end

end