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

Warn on dropping sessions #1900

Merged
merged 2 commits into from
Aug 4, 2023
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
8 changes: 7 additions & 1 deletion rack-protection/lib/rack/protection/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,13 @@ def session(env)
end

def drop_session(env)
session(env).clear if session? env
return unless session? env

session(env).clear

return if ["1", "true"].include?(ENV["RACK_PROTECTION_SILENCE_DROP_SESSION_WARNING"])

warn env, "session dropped by #{self.class}"
end

def referrer(env)
Expand Down
12 changes: 12 additions & 0 deletions rack-protection/spec/lib/rack/protection/protection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@
expect(io.string).not_to match(/prevented.*Origin/)
end

it 'drops the session and warns if reaction is to drop_session' do
io = StringIO.new
mock_app do
use Rack::Protection, reaction: :drop_session, logger: Logger.new(io)
run DummyApp
end
session = { foo: :bar }
post('/', {}, 'rack.session' => session, 'HTTP_ORIGIN' => 'http://malicious.com')
expect(io.string).to match(/session dropped by Rack::Protection::HttpOrigin/)
expect(session).not_to have_key(:foo)
end

it 'passes errors to reaction method if specified' do
io = StringIO.new
Rack::Protection::Base.send(:define_method, :special) { |*args| io << args.inspect }
Expand Down