Skip to content

Commit 43696ef

Browse files
committed
Add 'digest' option
The option specifies the digest algorithm to hash the session id when generating the filename for this session's FileStore file. It is defaulted to "MD5" because of backward compatibility for now.
1 parent 82818c4 commit 43696ef

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

lib/cgi/session.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,19 @@ def create_new_id
210210
# suffix:: the prefix to add to the session id when generating
211211
# the filename for this session's FileStore file.
212212
# Defaults to the empty string.
213+
# digest:: the digest algorithm to hash the session id when
214+
# generating the filename for this session's FileStore
215+
# file. Defaults to "MD5".
213216
def new_store_file(option={}) # :nodoc:
214217
dir = option['tmpdir'] || Dir::tmpdir
215218
prefix = option['prefix']
216219
suffix = option['suffix']
217-
require 'digest/md5'
218-
md5 = Digest::MD5.hexdigest(session_id)[0,16]
220+
algorithm = option['digest'] || 'MD5'
221+
require 'digest'
222+
digest = Digest(algorithm).hexdigest(session_id)[0,16]
219223
path = dir+"/"
220224
path << prefix if prefix
221-
path << md5
225+
path << digest
222226
path << suffix if suffix
223227
if File::exist? path
224228
hash = nil
@@ -410,6 +414,9 @@ class FileStore
410414
# suffix:: the prefix to add to the session id when generating
411415
# the filename for this session's FileStore file.
412416
# Defaults to the empty string.
417+
# digest:: the digest algorithm to hash the session id when
418+
# generating the filename for this session's FileStore
419+
# file. Defaults to "MD5".
413420
#
414421
# This session's FileStore file will be created if it does
415422
# not exist, or opened if it does.

test/cgi/test_cgi_session.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ def test_cgi_session_filestore
5555
assert_equal(value1,session["key1"])
5656
assert_equal(value2,session["key2"])
5757
session.close
58-
5958
end
59+
6060
def test_cgi_session_pstore
6161
update_env(
6262
'REQUEST_METHOD' => 'GET',
@@ -92,6 +92,7 @@ def test_cgi_session_pstore
9292
assert_equal(value2,session["key2"])
9393
session.close
9494
end if defined?(::PStore)
95+
9596
def test_cgi_session_specify_session_id
9697
update_env(
9798
'REQUEST_METHOD' => 'GET',
@@ -130,6 +131,7 @@ def test_cgi_session_specify_session_id
130131
assert_equal("foo",session.session_id)
131132
session.close
132133
end
134+
133135
def test_cgi_session_specify_session_key
134136
update_env(
135137
'REQUEST_METHOD' => 'GET',
@@ -166,4 +168,23 @@ def test_cgi_session_specify_session_key
166168
assert_equal(value2,session["key2"])
167169
session.close
168170
end
171+
172+
def test_cgi_session_filestore_digest
173+
session_id = "banana"
174+
path_md5 = session_file_store_path("tmpdir"=>@session_dir, "session_id"=>session_id)
175+
assert_equal path_md5, session_file_store_path("tmpdir"=>@session_dir, "session_id"=>session_id)
176+
path_sha512 = session_file_store_path("tmpdir"=>@session_dir, "session_id"=>session_id, "digest"=>"SHA512")
177+
assert_not_equal path_sha512, path_md5
178+
end
179+
180+
private
181+
182+
def session_file_store_path(options)
183+
cgi = Object.new
184+
session = CGI::Session.new(cgi, options)
185+
session.delete
186+
dbman = session.instance_variable_get(:@dbman)
187+
assert_kind_of(CGI::Session::FileStore, dbman)
188+
dbman.instance_variable_get(:@path)
189+
end
169190
end

0 commit comments

Comments
 (0)