limited_sessions 4.2.0 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/CHANGELOG +6 -0
- data/MIT-LICENSE +1 -1
- data/README.md +205 -0
- data/lib/limited_sessions.rb +0 -4
- data/lib/limited_sessions/expiry.rb +2 -9
- data/lib/limited_sessions/self_cleaning_session.rb +1 -7
- data/lib/limited_sessions/version.rb +1 -1
- metadata +29 -30
- data/README +0 -232
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 372dbf49775aefdb7902b765bc5727c9506f6df46251d890e5a31e957ba70962
|
4
|
+
data.tar.gz: d2d4e1684ec8f9b183a5cdf3d4722e73d8ac8bd7d1b656b41c3fd0f3d2ed6a62
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 92285992bce470310c88b656caf2a62d26bfa9315656d13bab944e44241743eb1a7bb86d912f837a13328be682a402c021d89552124577ce26e6212d84c1dba6
|
7
|
+
data.tar.gz: 176aea0865080e0399d2cd5f17cd3a04bb3daeccc06e5be982ed4c422bbeee6b2c1b47aaa73d7ff3a514468dcda0a39adee855916d955ecb48ec43b39b098425
|
data/CHANGELOG
CHANGED
data/MIT-LICENSE
CHANGED
data/README.md
ADDED
@@ -0,0 +1,205 @@
|
|
1
|
+
# LimitedSessions
|
2
|
+
|
3
|
+
LimitedSessions provides two distinct features, each in a separate part:
|
4
|
+
|
5
|
+
* Rack-compatible middleware that expires sessions based on inactivity or maximum session length. The middleware supports any session storage type, including cookies, Redis, ActiveRecord, etc.
|
6
|
+
|
7
|
+
* Rails extension to the (now separate) ActiveRecord Session Store to auto-cleanup stale session records.
|
8
|
+
|
9
|
+
|
10
|
+
## Features
|
11
|
+
|
12
|
+
* For all session stores:
|
13
|
+
* Configurable session expiry time (eg: 2 hours from last page access)
|
14
|
+
* Optional hard maximum limit from beginning of session (eg: 24 hours)
|
15
|
+
|
16
|
+
* When using the ActiveRecord Session Store:
|
17
|
+
* DB-based handling of session expiry (activity and hard limits) instead of by session paramters
|
18
|
+
* Auto-cleaning of expired session records
|
19
|
+
|
20
|
+
|
21
|
+
## Requirements
|
22
|
+
|
23
|
+
* Rack and any Rack-compatible app (including Rails)
|
24
|
+
* Utilizing Rack's (or Rails') sessions
|
25
|
+
* For ActiveRecord session enhancements:
|
26
|
+
* Must be using the standard ActiveRecord::SessionStore
|
27
|
+
(`ActionDispatch::Session::ActiveRecordStore.session_store = :active_record_store`)
|
28
|
+
* Ensure your sessions table has an `updated_at` column
|
29
|
+
* If using hard session limits, a `created_at` column is needed too
|
30
|
+
|
31
|
+
|
32
|
+
## Compatibility
|
33
|
+
|
34
|
+
The middleware should be compatible with any framework using a recent version of Rack. It has been tested with Rack 2.x and Rails 5.2-6.1.
|
35
|
+
|
36
|
+
The optional ActiveRecord Session Store extension requires Rails.
|
37
|
+
|
38
|
+
If using Rack < 2.0.9 or Rails < 5.2, use LimitedSessions 4.x.
|
39
|
+
|
40
|
+
|
41
|
+
## Upgrading
|
42
|
+
|
43
|
+
No changes are required to upgrade from LimitedSessions 4.x to 5.0.
|
44
|
+
|
45
|
+
Upgrading `activerecord-session_store` from 1.x to 2.x may require changes. See its own upgrade instructions.
|
46
|
+
|
47
|
+
|
48
|
+
## Installation
|
49
|
+
|
50
|
+
Add this gem to your Gemfile or otherwise make it available to your app. Then, configure as required.
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
gem 'limited_sessions', '~> 5'
|
54
|
+
```
|
55
|
+
|
56
|
+
If storing sessions in the DB using ActiveRecord with AR Session Store:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
gem 'activerecord-session_store'
|
60
|
+
gem 'limited_sessions', '~> 5'
|
61
|
+
```
|
62
|
+
|
63
|
+
`activerecord-session_store` must be loaded first in order for `limited_sessions` to properly detect it.
|
64
|
+
|
65
|
+
|
66
|
+
## Configuration
|
67
|
+
|
68
|
+
### Rack Middleware with Rails
|
69
|
+
|
70
|
+
1. Add/update `config/initializers/session_store.rb` and append the following:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
config.middleware.insert_after ActionDispatch::Flash, LimitedSessions::Expiry, \
|
74
|
+
recent_activity: 2.hours, max_session: 24.hours
|
75
|
+
```
|
76
|
+
|
77
|
+
2. Configuration options.
|
78
|
+
|
79
|
+
The example above shows both configuration options. You may include one, both, or none.
|
80
|
+
|
81
|
+
#### Session activity timeout
|
82
|
+
Example: `recent_activity: 2.hours`
|
83
|
+
By default, the session activity timeout is disabled (`nil`).
|
84
|
+
|
85
|
+
#### Maximum session length
|
86
|
+
Example: `max_session: 24.hours`
|
87
|
+
By default, the maximum session length is disabled (`nil`).
|
88
|
+
|
89
|
+
|
90
|
+
### Rack Middleware apart from Rails
|
91
|
+
|
92
|
+
1. In `config.ru`, add the following *after* the middleware that handles your sessions.
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
use LimitedSessions::Expiry, recent_activity: 2.hours, max_session: 24.hours
|
96
|
+
```
|
97
|
+
|
98
|
+
2. For configuration options, see #2 above, under Rack Middleware with Rails.
|
99
|
+
|
100
|
+
|
101
|
+
### ActionRecord Session Store extension
|
102
|
+
|
103
|
+
1. If you don't already have an `updated_at` column on your sessions table, create a migration and add it. If you plan to use the hard session limit feature, you'll also need to add `created_at`.
|
104
|
+
|
105
|
+
2. Tell Rails to use your the new session store. Change `config/initializers/session_store.rb` to reflect the following:
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
Rails.application.config.session_store :active_record_store
|
109
|
+
ActionDispatch::Session::ActiveRecordStore.session_class = LimitedSessions::SelfCleaningSession
|
110
|
+
```
|
111
|
+
|
112
|
+
3. Configuration options.
|
113
|
+
|
114
|
+
Each of the following options should also be added to your initializer file from step 2.
|
115
|
+
|
116
|
+
#### Self-cleaning
|
117
|
+
By default, SelfCleaningSession will clean the sessions table every 1000 page views. Technically, it's a 1 in 1000 chance on each page. For most sites this is good. Higher traffic sites may want to increase it to 10000 or more. Set to 0 to disable self-cleaning.
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
LimitedSessions::SelfCleaningSession.self_clean_sessions = 1000
|
121
|
+
```
|
122
|
+
|
123
|
+
#### Session activity timeout
|
124
|
+
The default session activity timeout is 2 hours. This uses the `updated_at` column which will be updated on every page load.
|
125
|
+
|
126
|
+
This can also be disabled by setting to `nil`. However, the `updated_at` column is still required for self-cleaning and will effectively function as if set to `1.week`. If you really want it longer, set it to `1.year` or something.
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
LimitedSessions::SelfCleaningSession.recent_activity = 2.hours
|
130
|
+
```
|
131
|
+
|
132
|
+
#### Maximum session length
|
133
|
+
By default, maximum session length handling is disabled. When enabled, it uses the `created_at` column to do its work.
|
134
|
+
|
135
|
+
A value of `nil` disables this feature and `created_at` does not need to exist in this case.
|
136
|
+
|
137
|
+
```ruby
|
138
|
+
LimitedSessions::SelfCleaningSession.max_session = 12.hours
|
139
|
+
```
|
140
|
+
|
141
|
+
|
142
|
+
## Questions
|
143
|
+
|
144
|
+
* Do I need both the middleware and the ActiveRecord Session Store?
|
145
|
+
|
146
|
+
No. While it should work, it is not necessary to use both the middleware
|
147
|
+
and the ActiveRecord Session Store. If you are storing sessions via AR,
|
148
|
+
then use the ActiveRecord Session Store. If you are storing sessions any
|
149
|
+
other way, then use the middleware.
|
150
|
+
|
151
|
+
* I'm storing sessions in {Memcache, Redis, etc.} and they auto-expire sessions. Do I need this?
|
152
|
+
|
153
|
+
Maybe, maybe not. Normally, that auto-expire period is equivalent to LimitedSessions' :recent_activity. If that's all you want, then you don't need this. However, if you'd also like to put a maximum cap on session length, regardless of activity, then LimitedSessions' `:max_session` feature will still be useful.
|
154
|
+
|
155
|
+
* Can I use the middleware with ActiveRecord instead of the ActionRecord Session Store enhancement?
|
156
|
+
|
157
|
+
Yes. Session expiry (recent activity and max session length) should work fine in this circumstance. The only thing you won't get is self-cleaning of the AR sessions table.
|
158
|
+
|
159
|
+
* How are session expiry times tracked?
|
160
|
+
|
161
|
+
The middleware adds one or two keys to the session data: `:last_visit` and/or `:first_visit`.
|
162
|
+
|
163
|
+
The AR enhancement uses `updated_a`t and possibly `created_at`.
|
164
|
+
|
165
|
+
* How is this different from using the session cookie's own expires= value?
|
166
|
+
|
167
|
+
The cookie's own value puts the trust in the client to self-expire. If you really want to control session lengths, then you need to manage the values on the application side. LimitedSessions is fully compatible with the cookie's expires= value, however, and the two can be used together.
|
168
|
+
|
169
|
+
* What's the difference between `:recent_activity` and `:max_session`?
|
170
|
+
|
171
|
+
Recent activity requires regular access on your site. If it's set to 15 minutes, then a page must be loaded at least once every 15 minutes.
|
172
|
+
|
173
|
+
Max session is a cap on the session from the very beginning. If it's set to 12 hours, then even if a user is accessing the page constantly, and not triggering the recent activity timeout, after 12 hours their session would be reset anyway.
|
174
|
+
|
175
|
+
* What are the security implications of using LimitedSessions?
|
176
|
+
|
177
|
+
LimitedSessions enhances security by reducing risk of session cookie replay attacks. The specifics will depend on what cookie store you're using.
|
178
|
+
|
179
|
+
For Rails' default cookie store, `:max_session` handling is perhaps most valuable as it guarantees an end to the session. Rails' default behavior allows a session to last for an infinite time. If a cookie is somehow exposed, the holder of the cookie has an open-ended session. Note that signing and/or encryption do not mitigate this.
|
180
|
+
|
181
|
+
For any session store that uses a server-side database (AR, memcache, Redis, etc.), at least the user can formally logout and terminate the session. Auto-expiring sessions (memcache, Redis, AR w/SelfCleaningSession, etc.) will also expire if allowed to, but can also be maintained perpetually by ongoing access.
|
182
|
+
|
183
|
+
Since the cookie store doesn't expire ever, `:recent_activity` addresses this by making sessions expire similarly to if memcache, Redis, or something similar was being used.
|
184
|
+
|
185
|
+
It is recommended to use both aspects of LimitedSessions for best security.
|
186
|
+
|
187
|
+
* What are the performance implications of using LimitedSessions?
|
188
|
+
|
189
|
+
The middleware should have minimal impact.
|
190
|
+
|
191
|
+
The AR enhancement should result in an overall net gain in performance as the size of the AR sessions table will be kept to a smaller size. The 1 in 1000 hit (or whatever you've configured it to) may be slightly slower while the database cleanup is in progress.
|
192
|
+
|
193
|
+
|
194
|
+
## Contributing
|
195
|
+
|
196
|
+
1. Fork it ( https://github.com/zarqman/smart_assets/fork )
|
197
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
198
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
199
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
200
|
+
5. Create new Pull Request
|
201
|
+
|
202
|
+
|
203
|
+
## License
|
204
|
+
|
205
|
+
MIT
|
data/lib/limited_sessions.rb
CHANGED
@@ -1,16 +1,9 @@
|
|
1
|
-
# LimitedSessions
|
2
|
-
# (c) 2007-2017 t.e.morgan
|
3
|
-
# Made available under the MIT license
|
4
|
-
|
5
|
-
# This version is compatible with Rack 1.4-2.0 (possibly earlier; untested).
|
6
|
-
# Correspondingly, it is compatible with Rails 3.x-5.x.
|
7
|
-
|
8
1
|
module LimitedSessions
|
9
2
|
# Rack middleware that should be installed *after* the session handling middleware
|
10
3
|
class Expiry
|
11
4
|
DEFAULT_OPTIONS = {
|
12
|
-
:
|
13
|
-
:
|
5
|
+
recent_activity: nil, # eg: 2.hours
|
6
|
+
max_session: nil # eg: 24.hours
|
14
7
|
}
|
15
8
|
|
16
9
|
def initialize(app, options={})
|
@@ -1,9 +1,3 @@
|
|
1
|
-
# LimitedSessions
|
2
|
-
# (c) 2007-2017 t.e.morgan
|
3
|
-
# Made available under the MIT license
|
4
|
-
|
5
|
-
# This is the Rails 4-5.x version.
|
6
|
-
|
7
1
|
module LimitedSessions
|
8
2
|
class SelfCleaningSession < ActiveRecord::SessionStore::Session
|
9
3
|
|
@@ -29,7 +23,7 @@ module LimitedSessions
|
|
29
23
|
# If this is a problem, use a migration and rename the column.
|
30
24
|
def find_by_session_id(session_id)
|
31
25
|
consider_self_clean
|
32
|
-
active_session.current_session.where(:session_id
|
26
|
+
active_session.current_session.where(session_id: session_id).first
|
33
27
|
end
|
34
28
|
|
35
29
|
private
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: limited_sessions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 5.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- t.e.morgan
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-04-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -16,7 +16,7 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 2.0.9
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
22
|
version: '3'
|
@@ -26,7 +26,7 @@ dependencies:
|
|
26
26
|
requirements:
|
27
27
|
- - ">="
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version:
|
29
|
+
version: 2.0.9
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: '3'
|
@@ -50,20 +50,20 @@ dependencies:
|
|
50
50
|
requirements:
|
51
51
|
- - ">="
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version: '
|
53
|
+
version: '5.2'
|
54
54
|
- - "<"
|
55
55
|
- !ruby/object:Gem::Version
|
56
|
-
version: '
|
56
|
+
version: '6.2'
|
57
57
|
type: :development
|
58
58
|
prerelease: false
|
59
59
|
version_requirements: !ruby/object:Gem::Requirement
|
60
60
|
requirements:
|
61
61
|
- - ">="
|
62
62
|
- !ruby/object:Gem::Version
|
63
|
-
version: '
|
63
|
+
version: '5.2'
|
64
64
|
- - "<"
|
65
65
|
- !ruby/object:Gem::Version
|
66
|
-
version: '
|
66
|
+
version: '6.2'
|
67
67
|
description: 'LimitedSessions provides two core features to handle cookie-based session
|
68
68
|
expiry: 1) Rack Middleware for most session stores and 2) an ActiveRecord extension
|
69
69
|
for AR-based session stores. Sessions can be expired on inactivity and/or overall
|
@@ -76,7 +76,7 @@ extra_rdoc_files: []
|
|
76
76
|
files:
|
77
77
|
- CHANGELOG
|
78
78
|
- MIT-LICENSE
|
79
|
-
- README
|
79
|
+
- README.md
|
80
80
|
- Rakefile
|
81
81
|
- lib/limited_sessions.rb
|
82
82
|
- lib/limited_sessions/expiry.rb
|
@@ -117,7 +117,7 @@ files:
|
|
117
117
|
homepage: https://iprog.com/projects#limited_sessions
|
118
118
|
licenses: []
|
119
119
|
metadata: {}
|
120
|
-
post_install_message:
|
120
|
+
post_install_message:
|
121
121
|
rdoc_options: []
|
122
122
|
require_paths:
|
123
123
|
- lib
|
@@ -132,40 +132,39 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
132
132
|
- !ruby/object:Gem::Version
|
133
133
|
version: '0'
|
134
134
|
requirements: []
|
135
|
-
|
136
|
-
|
137
|
-
signing_key:
|
135
|
+
rubygems_version: 3.0.9
|
136
|
+
signing_key:
|
138
137
|
specification_version: 4
|
139
138
|
summary: Server-side session expiry via either Rack Middleware or ActiveRecord extension
|
140
139
|
test_files:
|
140
|
+
- test/dummy/app/controllers/application_controller.rb
|
141
|
+
- test/dummy/app/views/layouts/application.html.erb
|
141
142
|
- test/dummy/app/assets/javascripts/application.js
|
142
143
|
- test/dummy/app/assets/stylesheets/application.css
|
143
|
-
- test/dummy/app/controllers/application_controller.rb
|
144
144
|
- test/dummy/app/helpers/application_helper.rb
|
145
|
-
- test/dummy/
|
146
|
-
- test/dummy/config/
|
147
|
-
- test/dummy/config/boot.rb
|
148
|
-
- test/dummy/config/database.yml
|
149
|
-
- test/dummy/config/environment.rb
|
150
|
-
- test/dummy/config/environments/development.rb
|
145
|
+
- test/dummy/config/routes.rb
|
146
|
+
- test/dummy/config/locales/en.yml
|
151
147
|
- test/dummy/config/environments/production.rb
|
148
|
+
- test/dummy/config/environments/development.rb
|
152
149
|
- test/dummy/config/environments/test.rb
|
150
|
+
- test/dummy/config/environment.rb
|
151
|
+
- test/dummy/config/application.rb
|
152
|
+
- test/dummy/config/database.yml
|
153
|
+
- test/dummy/config/boot.rb
|
153
154
|
- test/dummy/config/initializers/backtrace_silencers.rb
|
154
|
-
- test/dummy/config/initializers/inflections.rb
|
155
155
|
- test/dummy/config/initializers/mime_types.rb
|
156
|
-
- test/dummy/config/initializers/secret_token.rb
|
157
156
|
- test/dummy/config/initializers/session_store.rb
|
158
157
|
- test/dummy/config/initializers/wrap_parameters.rb
|
159
|
-
- test/dummy/config/
|
160
|
-
- test/dummy/config/
|
158
|
+
- test/dummy/config/initializers/secret_token.rb
|
159
|
+
- test/dummy/config/initializers/inflections.rb
|
161
160
|
- test/dummy/config.ru
|
162
|
-
- test/dummy/
|
163
|
-
- test/dummy/
|
161
|
+
- test/dummy/script/rails
|
162
|
+
- test/dummy/Rakefile
|
163
|
+
- test/dummy/public/favicon.ico
|
164
164
|
- test/dummy/public/422.html
|
165
165
|
- test/dummy/public/500.html
|
166
|
-
- test/dummy/public/
|
167
|
-
- test/dummy/
|
166
|
+
- test/dummy/public/404.html
|
167
|
+
- test/dummy/log/test.log
|
168
168
|
- test/dummy/README.rdoc
|
169
|
-
- test/dummy/script/rails
|
170
169
|
- test/limited_sessions_test.rb
|
171
170
|
- test/test_helper.rb
|
data/README
DELETED
@@ -1,232 +0,0 @@
|
|
1
|
-
LimitedSessions
|
2
|
-
===============
|
3
|
-
Copyright 2007-2017 t.e.morgan.
|
4
|
-
License: MIT
|
5
|
-
|
6
|
-
Updates/info: http://iprog.com/projects#limited_sessions
|
7
|
-
Source: https://github.com/zarqman/limited_sessions
|
8
|
-
Contact: tm@iprog.com
|
9
|
-
|
10
|
-
|
11
|
-
LimitedSessions provides two distinct features, each in a separate part:
|
12
|
-
* Rack-compatible middleware that expires sessions based on inactivity or
|
13
|
-
maximum session length. The middleware supports any session storage type,
|
14
|
-
including cookies, Redis, ActiveRecord, etc.
|
15
|
-
* Rails 4+ extension to the (now separate) ActiveRecord Session Store to
|
16
|
-
auto-cleanup stale session records.
|
17
|
-
|
18
|
-
|
19
|
-
Notes on Rails and Rack versions:
|
20
|
-
The middleware should be compatible with any framework using a recent
|
21
|
-
version of Rack. It was tested with Rack 1.5 on Rails 4.2 and Rack 2.0 on
|
22
|
-
Rails 5.0 and 5.1.
|
23
|
-
|
24
|
-
The ActiveRecord Session Store extension requires Rails 4+ and the now
|
25
|
-
separate activerecord-session_store gem:
|
26
|
-
gem 'activerecord-session_store'
|
27
|
-
activerecord-session_store must be *before* limited_sessions in your Gemfile
|
28
|
-
in order for limited_sessions to auto-detect it.
|
29
|
-
|
30
|
-
The extension has been tested with the following combinations:
|
31
|
-
* Rails 4.2 + activerecord-session_store 0.1.2
|
32
|
-
* Rails 5.0 + activerecord-session_store 1.0.0
|
33
|
-
* Rails 5.1 + activerecord-session_store 1.1.0
|
34
|
-
|
35
|
-
|
36
|
-
Upgrading from previous versions:
|
37
|
-
Other than possibly requiring the activerecord-session_store gem as noted
|
38
|
-
above, no changes are required upgrading from limited_sessions 3.x to 4.0.
|
39
|
-
|
40
|
-
If upgrading from limited_sessions v2.x, please review the upgrade notes from
|
41
|
-
limited_sessions 3.x or build a new configuration using the instructions
|
42
|
-
below.
|
43
|
-
|
44
|
-
|
45
|
-
Features:
|
46
|
-
* For all session stores:
|
47
|
-
* Configurable session expiry time (eg: 2 hours from last page access)
|
48
|
-
* Optional hard maximum limit from beginning of session (eg: 24 hours)
|
49
|
-
* When using the ActiveRecord Session Store:
|
50
|
-
* DB-based handling of session expiry (activity and hard limits) instead of
|
51
|
-
by session paramters
|
52
|
-
* Auto-cleaning of expired session records
|
53
|
-
|
54
|
-
|
55
|
-
Requirements:
|
56
|
-
* Rack and any Rack-compatible app (including Rails 4 or 5)
|
57
|
-
* Utilizing Rack's (or Rails') sessions support
|
58
|
-
* For ActiveRecord session enhancements:
|
59
|
-
* Must be using the standard ActiveRecord::SessionStore
|
60
|
-
(ActionDispatch::Session::ActiveRecordStore.session_store = :active_record_store)
|
61
|
-
* Ensure your sessions table has an `updated_at` column
|
62
|
-
* If using hard session limits, a `created_at` column is needed too
|
63
|
-
|
64
|
-
|
65
|
-
Installation:
|
66
|
-
Add this gem to your Gemfile (Rails) or otherwise make it available to your
|
67
|
-
app. Then, configure as required.
|
68
|
-
|
69
|
-
gem 'limited_sessions', '~> 4.0'
|
70
|
-
|
71
|
-
|
72
|
-
Configuration:
|
73
|
-
Rack Middleware with Rails
|
74
|
-
1. Update your config/initializers/session_store.rb and append the
|
75
|
-
following:
|
76
|
-
|
77
|
-
config.middleware.insert_after ActionDispatch::Flash, LimitedSessions::Expiry, \
|
78
|
-
recent_activity: 2.hours, max_session: 24.hours
|
79
|
-
|
80
|
-
2. Configuration options.
|
81
|
-
The example above shows both configuration options. You may include
|
82
|
-
both, one, or none.
|
83
|
-
|
84
|
-
* Session activity timeout *
|
85
|
-
Example: recent_activity: 2.hours
|
86
|
-
By default, the session activity timeout is disabled (nil).
|
87
|
-
|
88
|
-
* Maximum session length *
|
89
|
-
Example: max_session: 24.hours
|
90
|
-
By default, the maximum session length is disabled (nil).
|
91
|
-
|
92
|
-
|
93
|
-
Rack Middleware apart from Rails
|
94
|
-
1. In your config.ru, add the following *after* the middleware that handles
|
95
|
-
your sessions.
|
96
|
-
|
97
|
-
use LimitedSessions::Expiry, recent_activity: 2.hours, max_session: 24.hours
|
98
|
-
|
99
|
-
2. See #2 above, under Rack Middleware with Rails, for Configuration options.
|
100
|
-
|
101
|
-
|
102
|
-
ActionRecord Session Store
|
103
|
-
1. If you don't already have an 'updated_at' column on your sessions table,
|
104
|
-
create a migration and add it. If you plan to use the hard session limit
|
105
|
-
feature, you'll also need to add 'created_at'.
|
106
|
-
|
107
|
-
2. Tell Rails to use your the new session store. Change
|
108
|
-
config/initializers/session_store.rb to reflect the following:
|
109
|
-
|
110
|
-
<YourApp>::Application.config.session_store :active_record_store
|
111
|
-
ActionDispatch::Session::ActiveRecordStore.session_class = LimitedSessions::SelfCleaningSession
|
112
|
-
|
113
|
-
3. Configuration options.
|
114
|
-
Each of the following options should also be added to your initializer
|
115
|
-
file from step 2.
|
116
|
-
|
117
|
-
|
118
|
-
* Self-cleaning *
|
119
|
-
By default, SelfCleaningSession will clean the sessions table about every
|
120
|
-
1000 page views. Technically, it's a 1 in 1000 chance on each page. For
|
121
|
-
most sites this is good. Higher traffic sites may want to increase it to
|
122
|
-
10000 or more. 0 will disable self-cleaning.
|
123
|
-
|
124
|
-
LimitedSessions::SelfCleaningSession.self_clean_sessions = 1000
|
125
|
-
|
126
|
-
|
127
|
-
* Session activity timeout *
|
128
|
-
The default session activity timeout is 2 hours. This uses the
|
129
|
-
'updated_at' column which will be updated on every page load.
|
130
|
-
|
131
|
-
This can also be disabled by setting to nil. However, the 'updated_at'
|
132
|
-
column is still required for self-cleaning and will effectively function
|
133
|
-
as if this was set to 1.week. If you really want it longer, set it to
|
134
|
-
1.year or something.
|
135
|
-
|
136
|
-
LimitedSessions::SelfCleaningSession.recent_activity = 2.hours
|
137
|
-
|
138
|
-
|
139
|
-
* Maximum session length *
|
140
|
-
By default, the maximum session length handling is disabled. When
|
141
|
-
enabled, it uses the 'created_at' column to do its work.
|
142
|
-
|
143
|
-
A value of nil disables this feature and 'created_at' does not need to
|
144
|
-
exist in this case.
|
145
|
-
|
146
|
-
LimitedSessions::SelfCleaningSession.max_session = 12.hours
|
147
|
-
|
148
|
-
|
149
|
-
Other questions:
|
150
|
-
Do I need both the middleware and the ActiveRecord Session Store?
|
151
|
-
No. While it should work, it is not necessary to use both the middleware
|
152
|
-
and the ActiveRecord Session Store. If you are storing sessions via AR,
|
153
|
-
then use the ActiveRecord Session Store. If you are storing sessions any
|
154
|
-
other way, then use the middleware.
|
155
|
-
|
156
|
-
I'm storing sessions in {Memcache, Redis, etc.} and they auto-expire
|
157
|
-
sessions. Do I need this?
|
158
|
-
Maybe, maybe not. Normally, that auto-expire period is equivalent to
|
159
|
-
LimitedSessions' :recent_activity. If that's all you want, then you don't
|
160
|
-
need this. However, if you'd also like to put a maximum cap on session
|
161
|
-
length, regardless of activity, then LimitedSessions' :max_session feature
|
162
|
-
will still be useful.
|
163
|
-
|
164
|
-
Can I use the middleware with ActiveRecord instead of the ActionRecord
|
165
|
-
Session Store enhancement?
|
166
|
-
Yes; session expiry (recent activity and max session length) should work
|
167
|
-
fine in this circumstance. The only thing you won't get is self-cleaning of
|
168
|
-
the AR sessions table.
|
169
|
-
|
170
|
-
How are session expiry times tracked?
|
171
|
-
The middleware adds one or two keys to the session data: :last_visit and/or
|
172
|
-
:first_visit.
|
173
|
-
The AR enhancement uses 'updated_at' and possibly 'created_at'.
|
174
|
-
|
175
|
-
How is this different from using the session cookie's own expires= value?
|
176
|
-
The cookie's own value puts the trust in the client to self-expire. If you
|
177
|
-
really want to control session lengths, then you need to manage the values
|
178
|
-
on the application side. LimitedSessions is fully compatible with the
|
179
|
-
cookie's expires= value, however, and the two can be used together.
|
180
|
-
|
181
|
-
What's the difference between :recent_activity and :max_session?
|
182
|
-
Recent activity requires regular access on your site. If it's set to 15
|
183
|
-
minutes, then a page must be loaded at least once every 15 minutes.
|
184
|
-
|
185
|
-
Max session is a cap on the session from the very beginning. If it's set to
|
186
|
-
12 hours, then even if a user is accessing the page constantly, and not
|
187
|
-
triggering the recent activity timeout, after 12 hours their session would
|
188
|
-
be reset anyway.
|
189
|
-
|
190
|
-
What are the security implications of using LimitedSessions?
|
191
|
-
LimitedSessions enhances security by reducing risk of session cookie replay
|
192
|
-
attacks. The specifics will depend on what cookie store you're using.
|
193
|
-
|
194
|
-
For Rails' default cookie store, :max_session handling is perhaps most
|
195
|
-
valuable as it guarantees an end to the session. Rails' default behavior
|
196
|
-
allows a session to last for an infinite time. If a cookie is somehow
|
197
|
-
exposed, the holder of the cookie has an open-ended session. Note that
|
198
|
-
signing and/or encryption do not mitigate this.
|
199
|
-
|
200
|
-
For any session store that uses a server-side database (AR, memcache, Redis,
|
201
|
-
etc.), at least the user can formally logout and terminate the session.
|
202
|
-
Auto-expiring sessions (memcache, Redis, AR w/SelfCleaningSession, etc.)
|
203
|
-
will also expire if allowed to, but can also be maintained perpetually by
|
204
|
-
ongoing access.
|
205
|
-
|
206
|
-
Since the cookie store doesn't expire ever, :recent_activity addresses this
|
207
|
-
by making sessions expire similarly to if memcache, Redis, or something
|
208
|
-
similar was being used.
|
209
|
-
|
210
|
-
It is recommended to use both halves of LimitedSessions for best security.
|
211
|
-
|
212
|
-
What are the performance implications of using LimitedSessions?
|
213
|
-
The middleware should have minimal impact.
|
214
|
-
|
215
|
-
The AR enhancement should result in an overall net gain in performance as
|
216
|
-
the size of the AR sessions table will be kept to a smaller size. The 1 in
|
217
|
-
1000 hit (or whatever you've configured it to) may be slightly slower while
|
218
|
-
the database cleanup is in progress.
|
219
|
-
|
220
|
-
Is the AR enhancement compatible with the legacy 'sessid' column?
|
221
|
-
No. Please rename that column to 'session_id'.
|
222
|
-
|
223
|
-
|
224
|
-
Other Notes:
|
225
|
-
This version has been tested on Rack 1.5-2.0 and Rails 4.2-5.1. It should be
|
226
|
-
compatible with a broad spectrum of data and session stores. If you find a
|
227
|
-
bug, I'd love to hear about it -- preferably via a new issue on GitHub (bonus
|
228
|
-
points for a pull request). Likewise, give me a shout if you have a suggestion
|
229
|
-
or just want to tell me that it works. Thanks for checking limited_sessions
|
230
|
-
out!
|
231
|
-
|
232
|
-
--t (tm@iprog.com; https://iprog.com/)
|