LoginSignup
9
9

More than 5 years have passed since last update.

Padrino 0.11 以降で Better Errors を使う

Last updated at Posted at 2013-09-01

Padrino 0.12.0 以降では protect_from_csrf に except オプションを指定します。

app/app.rb
module Sample
  class App < Padrino::Application
    #...
    set :protect_from_csrf, except: %r{/__better_errors/\d+/\w+\z}

以降は、Padrino 0.11.x でのお話です。


Better Errors の導入自体は以前書いた記事の通りでいいのですが、protect_from_csrf が有効となっている場合 ( Padrino 0.11 からデフォルトで有効 ) には、Better Errors が表示するページからの HXR が通らずページ右側が表示されません。

これでは Better Errors を使っている意味がまるっきり無くなってしまうので、とりあえず、以下のパッチで回避しました。

1.指定したパスを保護対象外とするパッチを lib の下に置く

lib/rack_protection.rb
if Padrino.env == :development
  module Rack
    module Protection
      class AuthenticityToken
        def accepts_with_allowed_path?(env)
          if defined?(Padrino::Application.exclude_from_protection)
            [Padrino::Application.exclude_from_protection].flatten.each do |path|
              return true if Regexp.new(path).match(env['REQUEST_PATH'])
            end
          end
          accepts_without_allowed_path?(env)
        end
        alias_method_chain :accepts?, :allowed_path
      end
    end
  end
end 

2.保護対象外とするパスを Better Errors の設定と一緒に指定する

config/boot.rb
if Padrino.env == :development
  # Setup better_errors
  Padrino::Application.use BetterErrors::Middleware
  BetterErrors.application_root = PADRINO_ROOT
  BetterErrors::Middleware.allow_ip! ENV['TRUSTED_IP'] if ENV['TRUSTED_IP']
  Padrino::Application.set :exclude_from_protection, '/__better_errors' # Append this
  # ...
end

Rack:: Protection 側をいじるのは嫌だったのですが、これしか思いつきませんでした…。

9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9