require 'sidekiq'
require 'sidekiq/web'
Sidekiq::Web.use(Rack::Auth::Basic) do |user, password|
# Protect against timing attacks:
# - See https://codahale.com/a-lesson-in-timing-attacks/
# - See https://thisdata.com/blog/timing-attacks-against-string-comparison/
# - Use & (do not use &&) so that it doesn't short circuit.
# - Use digests to stop length information leaking
Rack::Utils.secure_compare(::Digest::SHA256.hexdigest(user), ::Digest::SHA256.hexdigest(ENV["SIDEKIQ_USER"])) &
Rack::Utils.secure_compare(::Digest::SHA256.hexdigest(password), ::Digest::SHA256.hexdigest(ENV["SIDEKIQ_PASSWORD"]))
end
# config/routes.rb
# This defines the authentication constraint
constraint = lambda do |request|
request.env['warden'].authenticate!({ scope: :admin_user })
end
# This mounts the route using the constraint.
# You could use any other path to make it less obvious
constraints constraint do
mount Sidekiq::Web => '/sidekiq'
end
require 'active_support/security_utils'
require 'sidekiq'
require 'sidekiq/web'
Sidekiq::Web.use(Rack::Auth::Basic) do |user, password|
# Protect against timing attacks:
# - See https://codahale.com/a-lesson-in-timing-attacks/
# - See https://thisdata.com/blog/timing-attacks-against-string-comparison/
# - Use & (do not use &&) so that it doesn't short circuit.
# - Use digests to stop length information leaking
ActiveSupport::SecurityUtils.secure_compare(user, ENV["SIDEKIQ_ADMIN_USER"]) &
ActiveSupport::SecurityUtils.secure_compare(password, ENV["SIDEKIQ_ADMIN_PASSWORD"])
end
主动支持4:
require 'active_support/security_utils'
require 'sidekiq'
require 'sidekiq/web'
Sidekiq::Web.use(Rack::Auth::Basic) do |user, password|
# Protect against timing attacks:
# - See https://codahale.com/a-lesson-in-timing-attacks/
# - See https://thisdata.com/blog/timing-attacks-against-string-comparison/
# - Use & (do not use &&) so that it doesn't short circuit.
# - Use digests to stop length information leaking
ActiveSupport::SecurityUtils.secure_compare(
::Digest::SHA256.hexdigest(user),
::Digest::SHA256.hexdigest(ENV["SIDEKIQ_ADMIN_USER"])
) &
ActiveSupport::SecurityUtils.secure_compare(
::Digest::SHA256.hexdigest(password),
::Digest::SHA256.hexdigest(ENV["SIDEKIQ_ADMIN_PASSWORD"])
)
end
class RouteConstraints::NoUserRequiredConstraint
include RouteConstraints::UserConstraint
def matches?(request)
!current_user(request).present?
end
end
class RouteConstraints::AdminRequiredConstraint
include RouteConstraints::UserConstraint
def matches?(request)
user = current_user(request)
user.present? && user.is_admin?
end
end
最后,您可以将约束添加到config/routes.rb:
MyApp::Application.routes.draw do
# other routes …
root :to => 'admin#dashboard', :constraints => RouteConstraints::AdminRequiredConstraint.new
root :to => 'home#welcome', :constraints => RouteConstraints::NoUserRequiredConstraint.new
end
9条答案
按热度按时间hc8w905p1#
将以下内容放入sidekiq初始化器中
在routes文件中:
6tqwzwtp2#
很抱歉迟到了,但是Sidekiq's wiki为Devise推荐了以下内容:
要允许任何经过身份验证的
User
,请执行以下操作:限制对
User.admin?
的访问This wiki post也有许多其他的安全方案。
这是使用Rails 5.1.3、Devise 4.3和Sidekiq 5.0测试的
wgx48brx3#
参见https://github.com/mperham/sidekiq/wiki/Monitoring下的“安全性”
Sidekiq::Web使用Rack::Protection来保护您的应用程序免受典型的Web攻击(例如CSRF,XSS等)。如果Rack::Protection发现您的请求不满足安全要求,它将使您的会话无效并引发
Forbidden
错误。其中一种可能的情况是,您的应用程序在反向代理之后工作,并且不向其传递重要的头(X-Forwarded-For
,X-Forwarded-Proto
)。这种情况和解决方案可以找到in this article和issue #2560.kpbpu0084#
如果您正在滚动自己的自定义身份验证,那么您可以使用下面的示例,该示例在此处的文档中引用。
zaq34kh65#
如果您正在使用Devise(或其他基于Warden的身份验证),则可以这样做,前提是您的应用中有一个AdminUser模型。
mrwjdhj36#
公认的答案是好的,但我认为它可以更安全地实现,正如Sidekiq文档所提到的(在我发布后,它被编辑以展示正确的解决方案)。
要保护您的应用免受计时攻击,请使用
ActiveSupport::SecurityUtils.secure_compare
。另外,使用
&
(不要使用&&
),这样它就不会短路。最后,使用扩展器来阻止长度信息泄漏(Active Support 5中的默认值为
secure_compare
)。因此,在一个初始化器文件中,通常在Rails项目的
config/initializers/sidekiq.rb
中,根据您的Active Support/Rails版本,编写以下内容。主动支持5+:由于Rails PR #24510,传递给
secure_compare
的参数默认会通过Digest::SHA256.hexdigest
。主动支持4:
jm81lzqq7#
如果您使用Sorcery进行身份验证,则可以使用how to use Rails routes constraints来保护某些路由。
这里复制从巫术维基冗余:
本教程展示了如何在Sorcery gem中使用Rails路由约束。感谢@anthonator写的!
首先,定义将用于所有约束的
UserConstraint
模块:然后,在定义了该模块之后,您可以指定特定的约束类。在这些示例中,第一个路由仅在没有用户登录的情况下才有效,第二个路由仅适用于作为管理员的登录用户:
最后,您可以将约束添加到
config/routes.rb
:b1payxdu8#
另一种选择是添加类似CanCan和基于角色的特殊访问。
cetgtptt9#
能够使用devise用户名/密码
这也适用于API
config/initializers/sidekiq.rb
config/routes.rb