-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Add option to set the session middleware when enabling sessions #1161
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1380,11 +1380,12 @@ end | |
Note that `enable :sessions` actually stores all data in a cookie. This | ||
might not always be what you want (storing lots of data will increase your | ||
traffic, for instance). You can use any Rack session middleware: in order to | ||
do so, do **not** call `enable :sessions`, but instead pull in your | ||
middleware of choice as you would any other middleware: | ||
do so, do **not** call `enable :sessions`, but instead call `set | ||
:sessions` with your middleware of choice passed in as the value for | ||
`:session_store` along with any other options: | ||
|
||
```ruby | ||
use Rack::Session::Pool, :expire_after => 2592000 | ||
set :sessions, :session_store => Rack::Session::Pool, :expire_after => 2592000 | ||
|
||
get '/' do | ||
"value = " << session[:value].inspect | ||
|
@@ -2098,14 +2099,7 @@ set :protection, :except => [:path_traversal, :session_hijacking] | |
``` | ||
|
||
By default, Sinatra will only set up session based protection if `:sessions` | ||
has been enabled. Sometimes you want to set up sessions on your own, though. In | ||
that case you can get it to set up session based protections by passing the | ||
`:session` option: | ||
|
||
```ruby | ||
use Rack::Session::Pool | ||
set :protection, :session => true | ||
``` | ||
has been enabled. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick "have been enabled". Also, this is still true if you use the middleware option. I don't want to ignore that use-case as many applications probably are doing it that way, and likely read it from here. We should consider the IMO this would mean having different sections under the "Using Sessions" chapter. Likely, we would start out with As well, the section below this "Available settings" might also need to be updated wrt the "sessions" option. WDYT? tl;dr I'm happy to support both cases independently. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good. To enable session based protection using the middleware option the following code would need to be used though (as described in #1038):
or
The code sample used before was incorrect, it didn't work. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about to provide a regression test (if there aren't any) to ensure this behavior works? |
||
|
||
### Available Settings | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1709,7 +1709,8 @@ def setup_sessions(builder) | |
options = {} | ||
options[:secret] = session_secret if session_secret? | ||
options.merge! sessions.to_hash if sessions.respond_to? :to_hash | ||
builder.use Rack::Session::Cookie, options | ||
session_store = options.delete(:session_store) { Rack::Session::Cookie } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we be concerned about mutating the options hash at this point? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think I see an issue with it, since the original sessions hash is preserved. |
||
builder.use session_store, options | ||
end | ||
|
||
def detect_rack_handler | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the
use
example still works I'd like to show both (since both should be supported).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call 👍