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

Kok*_*zzu 12 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
Run Code Online (Sandbox Code Playgroud)

它仍然显示警告:

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.
Run Code Online (Sandbox Code Playgroud)

但它没有出现在生产上

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

mat*_*att 35

你正在使用两者

enable :sessions
Run Code Online (Sandbox Code Playgroud)

使Sinatra设置基于cookie的会话,和

use Rack::Session::Cookie, ...
Run Code Online (Sandbox Code Playgroud)

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

警告由Sinatra包含的会话中间件生成.默认情况下,Sinatra 在开发环境中运行时不会创建会话密钥(至少在经典模式下,它适用于模块化应用程序),因此Rack会在开发中生成警告.

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

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

enable :sessions
set :session_secret, '*&(^B234'
Run Code Online (Sandbox Code Playgroud)

您还可以在启用会话时将选项哈希作为参数传递.而不是enable :sessions,这样做:

set :sessions, key: 'N&wedhSDF',
  domain: "localhost",
  path: '/',
  expire_after: 14400,
  secret: '*&(^B234'
Run Code Online (Sandbox Code Playgroud)