Skip to content

rails engine before_action: Get context from rack env - #358

Open
bitterpanda63 wants to merge 1 commit into
mainfrom
fix-before-action-context-bug
Open

rails engine before_action: Get context from rack env#358
bitterpanda63 wants to merge 1 commit into
mainfrom
fix-before-action-context-bug

Conversation

@bitterpanda63

@bitterpanda63 bitterpanda63 commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator

for: #357

@aikido-pr-checks

Copy link
Copy Markdown

Summary by Aikido

Security Issues: 0 Quality Issues: 0 Resolved Issues: 0

🐛 Bugfixes

  • Retrieved Rails request context from Rack environment during controller actions
  • Skipped blocking checks when middleware context was unavailable
  • Handled missing contexts safely in attack protection middleware

More info

@codecov

codecov Bot commented Aug 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

# Read from the Rack env, not the fiber-local current_context: that one is nil when the
# action runs in another thread (ActionController::Live) or when no middleware ran at
# all (ActionController::TestCase). See https://github.com/AikidoSec/firewall-ruby/issues/357
before_action { request.env[Aikido::Zen::ENV_KEY]&.update_request(request) }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 High - ActionController::Live requests run outside Zen's request context

The new before_action stops dereferencing a nil context in ActionController::Live by reading from request.env, but it never rebinds that env-backed context to Aikido::Zen.current_context in the controller thread. All request-scoped protections still consume current_context (enable_idor_protection, track_user, set_tenant_id, and Sink#scan), and several scanners explicitly skip when it is nil, so streaming actions now become reachable while SQLi/SSRF/path traversal/shell checks, user-based blocking, and IDOR protection silently stop applying. This change therefore converts Live endpoints from a hard failure into an unprotected execution path.

Show fix

When controller callbacks/actions run, copy request.env[Aikido::Zen::ENV_KEY] into Aikido::Zen.current_context for the duration of that execution and restore the previous value afterward. That rebinding needs to happen before calling enable_idor_protection/update_request so controller-thread helpers and all sink scanners observe the same request context; add a regression test with an ActionController::Live action that calls track_user or set_tenant_id and exercises a protected sink.

Reply to ignore this finding.
More info

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant