lograge: extracting status does fail with devise when authentication fails
(I’m using rails 4.0.3, devise 3.2.3 and lograge 0.3.0.)
I’m not sure if this is really devise devise specific. When I make a request that fails authentication it returns a 401 Unauthorized. lograge does log this with status=0 though. When I look at payload in https://github.com/roidrage/lograge/blob/master/lib/lograge/log_subscriber.rb#L56-L65, I get
{:controller=>"TestsController", :action=>"index", :params=>{"action"=>"index", "controller"=>"tests"}, :format=>"*/*", :method=>"GET", :path=>"/tests"}
which is obviously the reason why status=0 gets logged.
With lograge disabled I get this log entry by rails:
Started GET "/tests" for 127.0.0.1 at 2014-03-14 10:45:49 +0100
Processing by TestsController#index as */*
Completed 401 Unauthorized in 8ms
I’m wondering if I am overlooking something here and if this can be somehow fixed. Anyone else seeing this issue?
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Reactions: 3
- Comments: 23 (1 by maintainers)
Status code 0 instead of 401 here as well, lograge
0.11.2, Rails5.2.4.3, Devise4.7.3the PR was reverted lol
I ran into this issue too and dug a bit deeper. It happens because devise sets the status here:
https://github.com/plataformatec/devise/blob/master/lib/devise/controllers/helpers.rb#L77
which is usually called by rails here:
https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/log_subscriber.rb#L21
However, lograge uses its own LogSubscriber to process the notifications from rails:
https://github.com/roidrage/lograge/blob/master/lib/lograge/log_subscriber.rb#L8
The easy fix would be to call
ActionController::Base.log_process_action(payload)from the lograge code, however, this doesn’t seem right to me.I spent some time investigating this. This issue uncovered a number of issues:
Devise shouldn’t be using
ActionController::Base.log_process_actionto mutatepayload; that wasn’t really the design of that API call (https://github.com/rails/rails/blob/06dd162fb3ae67f202a0a59da1ce94317d0a3e22/actionpack/lib/action_controller/metal/instrumentation.rb#L96-L98). The workaround in https://github.com/roidrage/lograge/pull/194 forces a status code if there isn’t one, but it’s not necessarily the correct status code. It will always display a 401 error code if no status or exception is available. I can make Devise throw a 404 in the failure app, but the logs will always show a 401.https://github.com/heartcombo/devise/pull/4375 causes the wrong error codes to be reported because
append_info_to_payloaddoesn’t have access to thepayload[:exception]object. The exception is added later in https://github.com/rails/rails/blob/997770f5955a36f0c800388c4592c961e184aec4/activesupport/lib/active_support/notifications/instrumenter.rb#L26:FailureApp, the Rails log subscriber would never actually see it. It looks like the Devise failures are directed to a Rails metal controller that isn’t instrumented with the standard logger. I can add notifications via this change:However, this generates two log entries: one for
/users/sign_inand one for an internal/unauthenticatedendpoint:Ideally, we’d combine these two entries into one, but I’m not sure whether that’s easy right now. This may be a limitation of the way all this instrumentation works. I think the request is going from Rails -> Rack -> Warden -> Warden failure app. As far as the Rails controller goes, it finishes processing the request with a
nilstatus code, and that’s when Lograge gets called. But then the Warden middleware runs and returns the correct status code.Sadly not it seems. I’ll reopen this and take a look today.
The devise issue is caused by the problem described here: plataformatec/devise#4375.
Despite the apparent failure by lograge to accommodate this I’m reluctant to handle the special case caused by devise.
@brucew If you’re experiencing issues outside of devise - post a reproduction for me to investigate.
I came across this issue today. Does anyone have a solution for it?
It’s merged! https://github.com/plataformatec/devise/pull/4375