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

Conversation

jdelStrother
Copy link
Contributor

It took me quite a while to discover that the reason my sessions would suddenly get dropped was from rack-protection's SessionHijacking. How about warning when that happens (in common with the other reactions like deny and report) ?

@jdelStrother jdelStrother force-pushed the session-hijack-logging branch from 476ed1f to 166295e Compare March 7, 2023 10:35
Copy link
Member

@dentarg dentarg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this make sense. Do we have any tests for the other cases where we warn?

@jdelStrother
Copy link
Contributor Author

Oh sorry - turns out we do. I'll add one for SessionHijacking too

@jdelStrother jdelStrother force-pushed the session-hijack-logging branch 2 times, most recently from 1d0cbc4 to d2f2633 Compare March 7, 2023 16:27
@dentarg
Copy link
Member

dentarg commented Mar 11, 2023

Looks like only SessionHijacking has :drop_session as the default reaction, so only a change for that when using rack-protection out-of-the-box, but a change for anyone who is using the :drop_session reaction for anything else.

You can configure the logging option if you don't like the added logging. Though that is not very easy to do when using rack-protection from Sinatra as all protections share the same options.

Oh, :drop_session is the default reaction used by Sinatra:

options[:reaction] ||= :drop_session

Will this be too much noise in production? 🤔

Any thoughts from @sinatra/sinatras-helpers?

@jkowens
Copy link
Member

jkowens commented Apr 1, 2023

@dentarg yeah I'm not sure which way to go on this. I can see how it would be nice to get this warning, but it also could potentially be very noisy. I suppose all rack protection logging being configured to use its own file would be a decent option tho if that is a problem.

@jkowens jkowens self-requested a review April 7, 2023 21:07
session(env).clear if session? env
return unless session? env

warn env, "session dropped by #{self.class}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to make it easy to silence this warning. Maybe via an ENV?

WDYT @jkowens ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds like a really good idea 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jdelStrother any interest in continuing here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really get why dropping sessions might be a noisy warning that needs silencing, but deny and report warnings are fine and should always be printed. Could you clarify?

If we're adding an ENV var, any thoughts on naming? RACK_PROTECTION_WARN_ON_DROPPED_SESSIONS=0 ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because it is the default in Sinatra

We can warn by default but there should be an easy way to get back the previous behavior (no warning)

So it should be named "silence ..." something. The RACK_PROTECTION prefix is a good call 👍🏻

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't think it should just silence all warnings, along the lines of this ?

diff --git a/rack-protection/lib/rack/protection/base.rb b/rack-protection/lib/rack/protection/base.rb
index 6f7e81fb..1ffc37c6 100644
--- a/rack-protection/lib/rack/protection/base.rb
+++ b/rack-protection/lib/rack/protection/base.rb
@@ -10,7 +10,7 @@ module Rack
   module Protection
     class Base
       DEFAULT_OPTIONS = {
-        reaction: :default_reaction, logging: true,
+        reaction: :default_reaction, logging: ENV["RACK_PROTECTION_SILENCE_REACTIONS"] != "1",
         message: 'Forbidden', encryptor: Digest::SHA1,
         session_key: 'rack.session', status: 403,
         allow_empty_referrer: true,

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I think it should be specific to drop session

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, see what you think to the latest push.

@sinatra sinatra deleted a comment from Rubymoda Jul 25, 2023
@sinatra sinatra deleted a comment from Rubymoda Jul 25, 2023
@jdelStrother jdelStrother force-pushed the session-hijack-logging branch from d2f2633 to a91bd23 Compare July 25, 2023 08:42
Co-authored-by: Patrik Ragnarsson <patrik@starkast.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants