Skip to content

Commit

Permalink
Make sure the cookie session store always defines an expiry when sign…
Browse files Browse the repository at this point in the history
…ing the session data
  • Loading branch information
ellmetha committed Jul 22, 2024
1 parent e64653c commit 57f74ae
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
22 changes: 22 additions & 0 deletions spec/marten/http/session/store/cookie_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,28 @@ describe Marten::HTTP::Session::Store::Cookie do
store = Marten::HTTP::Session::Store::Cookie.new("bad")
store.load.should be_empty
end

it "returns the expected hash if the session key is not expired yes" do
store = Marten::HTTP::Session::Store::Cookie.new(nil)
store["foo"] = "bar"
store.save

Timecop.freeze(Time.local + Time::Span.new(seconds: Marten.settings.sessions.cookie_max_age - 10)) do
new_store = Marten::HTTP::Session::Store::Cookie.new(store.session_key.not_nil!)
new_store.load.should eq({"foo" => "bar"})
end
end

it "returns an empty session data hash if the session key cannot be decrypted because it is expired" do
store = Marten::HTTP::Session::Store::Cookie.new(nil)
store["foo"] = "bar"
store.save

Timecop.freeze(Time.local + Time::Span.new(seconds: Marten.settings.sessions.cookie_max_age + 10)) do
new_store = Marten::HTTP::Session::Store::Cookie.new(store.session_key.not_nil!)
new_store.load.should be_empty
end
end
end

describe "#save" do
Expand Down
5 changes: 4 additions & 1 deletion src/marten/http/session/store/cookie.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ module Marten

def save : Nil
@modified = true
@session_key = encryptor.encrypt(session_hash.to_json)
@session_key = encryptor.encrypt(
value: session_hash.to_json,
expires: Time.local + Time::Span.new(seconds: Marten.settings.sessions.cookie_max_age),
)
end

def clear_expired_entries : Nil
Expand Down

0 comments on commit 57f74ae

Please sign in to comment.