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

Ruby Sinatra/1.4.3使用Rack::Session::Cookie警告,ruby,session,sinatra,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::M

我的配置代码

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,它仍会显示警告?

您同时使用这两个选项

enable :sessions
哪个,以及

它还将会话添加到应用程序中,因此在中间件堆栈中有两个
Rack::Session::Cookie

该警告由Sinatra包含的会话中间件生成。默认情况下,Sinatra(至少在经典模式下,它适用于模块化应用程序),因此Rack会在开发过程中生成警告

您应该只需要启用会话的两种方法中的一种,同时使用这两种方法可能会导致它们以意外的方式进行交互

为了避免警告,您可以使用
会话\u secret
选项显式设置Sinatra会话的机密:

enable :sessions
set :session_secret, '*&(^B234'
您还可以在启用会话时将选项哈希作为参数传递。执行以下操作,而不是启用:会话:

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

你能解释一下
session\u secret
的设置是什么吗?我一直在寻找答案,但我在任何地方都找不到。我最好的猜测是,它将其用作哈希的盐,这样人们就不能伪造饼干了。@Piccolo是的,它是为了防止人们伪造饼干,但它被用作a中的键,而不是盐。查看更多信息。非常感谢!我现在正在调查。还有,有什么原因不能在我的应用程序开始时生成新的密钥吗?@Piccolo,我很确定这样做的后果是,每次重新启动应用程序时,会话都会被破坏,因此人们必须重新登录。。。除其他可能存储在会话中的内容外,我的问题是如何使用这两种方法来启用会话。谢谢你弄明白了!
set :sessions, key: 'N&wedhSDF',
  domain: "localhost",
  path: '/',
  expire_after: 14400,
  secret: '*&(^B234'