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

Improves the trim trask to prevent session fixation. #216

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions lib/tasks/database.rake
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ namespace 'db:sessions' do
task :trim => [:environment, 'db:load_config'] do
cutoff_period = (ENV['SESSION_DAYS_TRIM_THRESHOLD'] || 30).to_i.days.ago
ActiveRecord::SessionStore::Session.
where("updated_at < ?", cutoff_period).
delete_all
where("updated_at < ?", cutoff_period).or(
ActiveRecord::SessionStore::Session.where("created_at < ?", cutoff_period)
).delete_all
end

desc "Upgrade current sessions in the database to the secure version"
Expand Down
10 changes: 8 additions & 2 deletions test/tasks/database_rake_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,22 @@ def test_trim_task
session.updated_at = 5.minutes.until(cutoff_period)
end

Session.create!(data: "fixed session") do |session|
session.created_at = 5.minutes.until(cutoff_period)
end

recent_session = Session.create!(data: "recent") do |session|
session.updated_at = 5.minutes.since(cutoff_period)
end

Rake.application.invoke_task 'db:sessions:trim'

old_session_count = Session.where("updated_at < ?", cutoff_period).count
obsolete_session_count = Session.where("updated_at < ?", cutoff_period).count
fixed_session_count = Session.where("created_at < ?", cutoff_period).count
retained_session = Session.find(recent_session.id)

assert_equal 0, old_session_count
assert_equal 0, obsolete_session_count
assert_equal 0, fixed_session_count
assert_equal retained_session, recent_session
end

Expand Down
Loading