Simplify Session.sweep
method example in security doc [ci skip]
This commit is contained in:
parent
3351347190
commit
d0051708c6
@ -211,15 +211,11 @@ Another countermeasure is to _save user-specific properties in the session_, ver
|
||||
|
||||
NOTE: _Sessions that never expire extend the time-frame for attacks such as cross-site request forgery (CSRF), session hijacking, and session fixation._
|
||||
|
||||
One possibility is to set the expiry time-stamp of the cookie with the session ID. However the client can edit cookies that are stored in the web browser so expiring sessions on the server is safer. Here is an example of how to _expire sessions in a database table_. Call `Session.sweep("20 minutes")` to expire sessions that were used longer than 20 minutes ago.
|
||||
One possibility is to set the expiry time-stamp of the cookie with the session ID. However the client can edit cookies that are stored in the web browser so expiring sessions on the server is safer. Here is an example of how to _expire sessions in a database table_. Call `Session.sweep(20.minutes)` to expire sessions that were used longer than 20 minutes ago.
|
||||
|
||||
```ruby
|
||||
class Session < ApplicationRecord
|
||||
def self.sweep(time = 1.hour)
|
||||
if time.is_a?(String)
|
||||
time = time.split.inject { |count, unit| count.to_i.send(unit) }
|
||||
end
|
||||
|
||||
where("updated_at < ?", time.ago.to_s(:db)).delete_all
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user