Sinatra / 1.4.3使用Rack :: Session :: Cookie警告

时间:2013-08-04 15:40:07

标签: ruby session sinatra

我的配置代码

require 'sinatra'

#set :environment, :production
enable :sessions
enable :logging
set run: true

case
  when production?
    set port: 8081
  when development?
    require 'sinatra/reloader'
    require 'better_errors'
    use BetterErrors::Middleware
    BetterErrors.application_root = __dir__
end

use Rack::Session::Cookie, key: 'N&wedhSDF',
    domain: "localhost",
    path: '/',
    expire_after: 14400,
    secret: '*&(^B234'

get '/' do
  erb :hello
end

它仍会显示警告:

SECURITY WARNING: No secret option provided to Rack::Session::Cookie.
This poses a security threat. It is strongly recommended that you
provide a secret to prevent exploits that may be possible from crafted
cookies. This will not be supported in future versions of Rack, and
future versions will even invalidate your existing user cookies.

但它没有出现在制作

问题是,即使Rack :: Session :: Cookie已经设置,为什么它仍会显示警告?

1 个答案:

答案 0 :(得分:35)

您正在使用两者

enable :sessions

makes Sinatra setup cookie based sessions

use Rack::Session::Cookie, ...

还会为您的应用添加会话,因此您最终会在中间件堆栈中使用两个实例Rack::Session::Cookie

警告由Sinatra包含的会话中间件生成。默认情况下,Sinatra doesn’t create a session secret when running in the development environment(至少在经典模式下,它适用于模块化应用程序),因此Rack会在开发过程中生成警告。

您应该只需要两种启用会话的方法之一,一起使用两种方式可能会导致它们以意想不到的方式进行交互。

要避免此警告,您可以使用session_secret选项为Sinatra会话明确设置机密:

enable :sessions
set :session_secret, '*&(^B234'

启用会话时,您还可以将选项哈希作为参数传递。而不是enable :sessions,请执行以下操作:

set :sessions, key: 'N&wedhSDF',
  domain: "localhost",
  path: '/',
  expire_after: 14400,
  secret: '*&(^B234'