Skip to content

Commit eec721b

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 ebb0f78 commit eec721b

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

lib/cgi/session.rb

Lines changed: 9 additions & 2 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 "SHA256".
213216
def new_store_file(option={}) # :nodoc:
214217
dir = option['tmpdir'] || Dir::tmpdir
215218
prefix = option['prefix']
216219
suffix = option['suffix']
220+
algorithm = option['digest'] || 'SHA256'
217221
require 'digest'
218-
sha256 = Digest::SHA256.hexdigest(session_id)[0,16]
222+
digest = Digest(algorithm).hexdigest(session_id)[0,16]
219223
path = dir+"/"
220224
path << prefix if prefix
221-
path << sha256
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,23 @@ def test_cgi_session_specify_session_key
168168
assert_equal(value2,session["key2"])
169169
session.close
170170
end
171+
172+
def test_cgi_session_filestore_digest
173+
session_id = "banana"
174+
path_default = session_file_store_path("tmpdir"=>@session_dir, "session_id"=>session_id)
175+
assert_equal path_default, 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_default
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
171190
end

0 commit comments

Comments
 (0)