skip to content

A Rails controller uses Pundit's `after_action :verify_authorized`; what does that filter guarantee, and what can still slip through?

level: seniorimportance: should knowfreq 48%

answer

  1. a per-request flag, not a policy check
  2. after_action runs after the action body
  3. AuthorizationNotPerformedError, PolicyScopingNotPerformedError
  4. skip_authorization sets the flag too
  5. wrong record or wrong query still passes

basics

~10 s

verify_authorized only checks that authorize or skip_authorization was called during the action. It runs after the action, so side effects already happened, and it cannot tell whether the right record and predicate were checked.

solid answer

~40 s

`verify_authorized` raises `Pundit::AuthorizationNotPerformedError` unless the action called `authorize` or `skip_authorization`, which set a per-request flag. It is a development aid, and Pundit's own README says the app must behave the same without it. Three gaps follow. It runs as an `after_action`, so an action that forgot `authorize` has already deleted the post or sent the email before the error replaces the response. It checks that some call happened, not which: `authorize Post` in a `destroy` action, or `authorize @post, :show?`, passes. And `skip_authorization` passes by design. For index actions use `verify_policy_scoped`, which raises `Pundit::PolicyScopingNotPerformedError` unless the controller called `policy_scope` or `skip_policy_scope`; the README pattern runs it on `index` and `verify_authorized` elsewhere.

code

ruby · 23 lines
ruby
class ApplicationController < ActionController::Base
  include Pundit::Authorization
  after_action :verify_pundit_authorization

  private

  def verify_pundit_authorization
    if action_name == "index"
      verify_policy_scoped
    else
      verify_authorized
    end
  end
end

class PostsController < ApplicationController
  def destroy
    @post = Post.find(params[:id])
    authorize @post, :show?   # passes verify_authorized, checks the wrong rule
    @post.destroy!
    redirect_to posts_path, status: :see_other
  end
end

go deeper

for a junior

Know that verify_authorized raises when an action forgot to call authorize, and that skip_authorization opts an action out.

for a middle

Explain the per-request flags, the two errors, why index actions use verify_policy_scoped, and why the view helper does not count.

for a senior

Explain why an after_action check cannot undo side effects and cannot spot a wrong record or predicate, and what reviews and tests must cover instead.

for a principal

Decide which guarantees come from tooling, which from tests and review, and how to keep non-controller entry points authorized.

## What the filter actually checks Pundit tracks two booleans per controller instance, which means per request: - `authorize` (and `skip_authorization`) set `@_pundit_policy_authorized`; - `policy_scope` (and `skip_policy_scope`) set `@_pundit_policy_scoped`. `verify_authorized` is one line: raise `Pundit::AuthorizationNotPerformedError` unless the first flag is set. `verify_policy_scoped` does the same with the second flag and raises `Pundit::PolicyScopingNotPerformedError`, a subclass of `AuthorizationNotPerformedError`. Both are meant to be installed as `after_action` filters in `ApplicationController`. Pundit's README is blunt about the purpose: the mechanism "only exists to aid you while developing your application", it "is not some kind of failsafe mechanism or authorization mechanism", and you "should be able to remove these filters without affecting how your app works". ## Gap 1: it runs after the damage An `after_action` runs **after** the action body. Consider a forum moderator tool: 1. A developer adds `PostsController#purge` and forgets `authorize`. 2. A member calls it. The action runs and deletes the posts. 3. The `after_action` raises `AuthorizationNotPerformedError`, and the member sees an error page. The exception changes the response, not the database. Nothing is rolled back, emails already sent stay sent. The filter catches the mistake in development and tests, which is where it earns its keep, not in production. ## Gap 2: it checks that a call happened, not which call The flag is set by **any** `authorize` call, so these all pass: | Code in `destroy` | Why it passes | What it really checks | |---|---|---| | `authorize Post` | a call happened | `destroy?` on the class, not on this post | | `authorize @post, :show?` | a call happened | visibility, not deletion | | `authorize @topic` | a call happened | the topic's rule, not the post's | | `skip_authorization` | it sets the flag by design | nothing | So code review and policy tests still have to confirm that each action authorizes the **right record** with the **right predicate**. ## Gap 3: index actions and scopes An index that uses only `policy_scope` never calls `authorize`, so `verify_authorized` would raise there. The README pattern splits the two: 1. `after_action :verify_pundit_authorization` in `ApplicationController`; 2. inside it, `verify_policy_scoped` when `action_name == "index"`, otherwise `verify_authorized`. Calling `policy_scope` from the **view** does not count: the view helper resolves the scope without setting the controller flag. ## Gap 4: actions that never reach the filter `after_action` filters do not run when the action raises or when a `before_action` halts the chain. That is harmless for security (nothing ran) but means the filter says nothing about code paths outside controller actions, such as jobs, mailers, console scripts or Action Cable channels, which call domain code directly. Those need their own `Pundit.authorize(user, record, :query?)` calls or equivalent checks. ## Using `skip_authorization` honestly `skip_authorization` exists for actions that genuinely have nothing to authorize on some path. The README's example is a `show` action that looks a record up with `find_by`: when the record exists it calls `authorize record`, and when it does not it calls `skip_authorization` before rendering a not-found page. Used that way, the skip documents a decision. Used carelessly, it is the same as no check. Two habits keep it honest: 1. never call it unconditionally at the top of an action that changes data; 2. grep for it in review, since each occurrence is a place where the filter is switched off on purpose. ## How to use it well - Install it globally and opt out per action with `skip_authorization`, so a new action fails closed in development. - Use `skip_after_action :verify_authorized` for controllers that are deliberately public, such as a marketing page, rather than sprinkling skips in action bodies. - Treat a production `AuthorizationNotPerformedError` as a bug report: the side effects already happened, so investigate what the action did. - Pair it with policy unit tests and request tests that assert a refused user gets the forbidden response.

  • Why is verify_authorized an after_action rather than a before_action?
    The flag can only be set by code inside the action, after the record is loaded and `authorize` is called. A `before_action` would always see the flag unset. The cost of checking afterwards is that it detects a missing call only once the action has run, which is why it is a development safety net, not a guard.
  • A team wraps every action in a transaction so verify_authorized can roll back; is that a fix?
    Only partly. A rollback undoes database writes inside that transaction, but not emails, enqueued jobs committed elsewhere, cache writes or calls to other services, and it hides a programming error that should be fixed in the action. The real fix is an explicit `authorize` with the right record and predicate, covered by a test.

  • verify_authorized stops the action before it can change anything
  • verify_authorized confirms the correct predicate was checked
  • skip_authorization fails verify_authorized like a missing call
  • Calling policy_scope in the view satisfies verify_policy_scoped
  • With verify_authorized installed, policy tests are unnecessary