3
我正在使用Ruby和Sinatra來開發應用程序。
我爲了使用由齒條所提供的會話變量使用如何使機架會話cookie安全?
enable :sessions
。我怎樣才能讓所有的會話cookie成爲HTTPOnly?這是默認的嗎?我找不到關於此的任何文檔。
我正在使用Ruby和Sinatra來開發應用程序。
我爲了使用由齒條所提供的會話變量使用如何使機架會話cookie安全?
enable :sessions
。我怎樣才能讓所有的會話cookie成爲HTTPOnly?這是默認的嗎?我找不到關於此的任何文檔。
而不是enable :sessions
:
use Rack::Session::Cookie, {:httponly => true }
我建議使用encrypted_cookie寶石相反,它更安全。作爲一個例子,這裏就是我可能有一個項目:
# app/main.rb
module Example
class App < Sinatra::Base # this class in its own file
# stuff here
end
end
# app/config.rb
require "main"
module Example
def self.app #
Rack::Builder.app do
cookie_settings = {
:key => 'usr',
:path => "/",
:expire_after => 86400, # In seconds, 1 day.
:secret => ENV["COOKIE_KEY"], # load this into the environment of the server
:httponly => true
}
cookie_settings.merge!(:secure => true) if ENV["RACK_ENV"] == "production"
# AES encryption of cookies
use Rack::Session::EncryptedCookie, cookie_settings
# other stuff here
run App
end
end
end
# config.ru
require "app/config"
run Example.app # this in the rackup file
(解釋,爲什麼我就躺在它這種方式 - 這種stucture的讓我分裂的應用程序和使用更方便在測試中只需要app/config.rb。YMMV)
非常好。我的應用程序的流程要簡單得多。我可以在我的config.ru文件中使用Rack :: Builder.app語句,而不是啓用:會話?另外,我通過編寫set:session_secret,ENV ['SESSION_KEY'] ||來使用會話密鑰「development_secret」。我可以使用機架會話cookie以相同的方式執行此操作嗎? –
是的,不要使用啓用會話以及這個,只是生成器。你也可以執行'||'技巧,但是因爲env變量會隨着時間的推移而增加(因爲它們非常有用),所以我把變量命令放在一個rake任務中,該任務具有設置所有env變量的先決任務具有發展價值。 – iain
'Rack :: Session :: EncryptedCookie'確實提供':httponly'選項嗎? –