ソース読み

ブラウザ以外から GET すると session が初期化されちゃう件について。

reset_session なる手続きの定義

以下で定義されてるようなんですが

  • actionpack/lib/action_controller/metal/rack_delegation.rb
  • actionpack/lib/action_dispatch/http/request.rb

下側が実際リクエストを捌く方々になるのかどうか。あるいは actiopack/lib/action_controller/metal/request_forgery_protection.rb の handle_unverified_request という手続きからも reset_session 呼び出されますね。

      # This is the method that defines the application behavior when a request is found to be unverified.
      # By default, \Rails resets the session when it finds an unverified request.
      def handle_unverified_request
        reset_session
      end

unverified な意味を確認する必要ありそげ。
上記は同じソースファイルの verify_authenticity_token から呼び出されますね。

      # The actual before_filter that is used. Modify this to change how you handle unverified requests.
      def verify_authenticity_token
        unless verified_request?
          logger.warn "WARNING: Can't verify CSRF token authenticity" if logger
          handle_unverified_request
        end
      end

WARNING 出たら session は初期化されるのか。出力されてないのに初期化、ってことはこの経路ではない、ってことなのかな。

うーん

request_forgery_protection.rb で以下なコメントを発見。

      # Returns true or false if a request is verified. Checks:
      #
      # * is it a GET request?  Gets should be safe and idempotent
      # * Does the form_authenticity_token match the given token value from the params?
      # * Does the X-CSRF-Token header match the form_authenticity_token

GET で session 初期化されてるんですがorz

もすこし時間あるので

curl で云々してみるか。と言いつつブラウザとスマホのアクセスを見比べてため息をつくなど。ちなみに GET new なブラウザからのリクエストが以下。

Started GET "/vm_operations/new" for 
Processing by VmOperationsController#new as HTML
*** debug ***
0
#<Conn:0x007f14396fefb0>
#<User:0x007f1439715fa8>
*** debug ***
  Rendered vm_operations/new.html.erb within layouts/application (1.3ms)
Completed 200 OK in 29ms (Views: 18.8ms | ActiveRecord: 1.3ms)

スマホからのソレが以下。

Started GET "/vm_operations/new.json?auth_token=-H_KpyMqqusvp-hkLmpA" for 
Processing by VmOperationsController#new as JSON
  Parameters: {"auth_token"=>"-H_KpyMqqusvp-hkLmpA"}
*** debug ***


#<User:0x007f14392b94c8>
*** debug ***
NoMethodError (undefined method `id' for nil:NilClass):
Completed 500 Internal Server Error in 35ms

Conn な id 参照して落ちてます。ちなみに User は current_user を puts してます。
しかし参った。