Skip to content

Add 'digest' option #62

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

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
13 changes: 10 additions & 3 deletions lib/cgi/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,19 @@ def create_new_id
# suffix:: the prefix to add to the session id when generating
# the filename for this session's FileStore file.
# Defaults to the empty string.
# digest:: the digest algorithm to hash the session id when
# generating the filename for this session's FileStore
# file. Defaults to "MD5".
def new_store_file(option={}) # :nodoc:
dir = option['tmpdir'] || Dir::tmpdir
prefix = option['prefix']
suffix = option['suffix']
require 'digest/md5'
md5 = Digest::MD5.hexdigest(session_id)[0,16]
algorithm = option['digest'] || 'MD5'
require 'digest'
digest = Digest(algorithm).hexdigest(session_id)[0,16]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we limit the size to 16 characters? This could be a security issue.

path = dir+"/"
path << prefix if prefix
path << md5
path << digest
path << suffix if suffix
if File::exist? path
hash = nil
Expand Down Expand Up @@ -410,6 +414,9 @@ class FileStore
# suffix:: the prefix to add to the session id when generating
# the filename for this session's FileStore file.
# Defaults to the empty string.
# digest:: the digest algorithm to hash the session id when
# generating the filename for this session's FileStore
# file. Defaults to "MD5".
#
# This session's FileStore file will be created if it does
# not exist, or opened if it does.
Expand Down
23 changes: 22 additions & 1 deletion test/cgi/test_cgi_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ def test_cgi_session_filestore
assert_equal(value1,session["key1"])
assert_equal(value2,session["key2"])
session.close

end

def test_cgi_session_pstore
update_env(
'REQUEST_METHOD' => 'GET',
Expand Down Expand Up @@ -92,6 +92,7 @@ def test_cgi_session_pstore
assert_equal(value2,session["key2"])
session.close
end if defined?(::PStore)

def test_cgi_session_specify_session_id
update_env(
'REQUEST_METHOD' => 'GET',
Expand Down Expand Up @@ -130,6 +131,7 @@ def test_cgi_session_specify_session_id
assert_equal("foo",session.session_id)
session.close
end

def test_cgi_session_specify_session_key
update_env(
'REQUEST_METHOD' => 'GET',
Expand Down Expand Up @@ -166,4 +168,23 @@ def test_cgi_session_specify_session_key
assert_equal(value2,session["key2"])
session.close
end

def test_cgi_session_filestore_digest
session_id = "banana"
path_md5 = session_file_store_path("tmpdir"=>@session_dir, "session_id"=>session_id)
assert_equal path_md5, session_file_store_path("tmpdir"=>@session_dir, "session_id"=>session_id)
path_sha512 = session_file_store_path("tmpdir"=>@session_dir, "session_id"=>session_id, "digest"=>"SHA512")
assert_not_equal path_sha512, path_md5
end

private

def session_file_store_path(options)
cgi = Object.new
session = CGI::Session.new(cgi, options)
session.delete
dbman = session.instance_variable_get(:@dbman)
assert_kind_of(CGI::Session::FileStore, dbman)
dbman.instance_variable_get(:@path)
end
end
Loading