Skip to content

Send stale browser forms back to log in instead of a 403 page - #5294

Open
manziosee wants to merge 7 commits into
OpenFn:mainfrom
manziosee:fix/4809-graceful-csrf-error
Open

manziosee wants to merge 7 commits into
OpenFn:mainfrom
manziosee:fix/4809-graceful-csrf-error

Conversation

@manziosee

@manziosee manziosee commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Description

This PR stops a stale browser form from showing the default "Forbidden"
page.

Submitting a form with a CSRF token from an earlier session, most often a
log-in form re-submitted after pressing back or left open in a second tab,
raised InvalidCSRFTokenError and rendered the 403 page (Sentry
LIGHTNING-NS).

The :browser pipeline now uses LightningWeb.Plugs.CSRFProtectionWithFallback,
which runs Plug.CSRFProtection unchanged. When an HTML form submission
fails the check, it redirects back to the page the form came from, with
"This form has expired. Please try again." The request is still rejected
and never reaches the router; only the response changes.

  • Only HTML forms get the redirect. Form-encoded requests
    (application/x-www-form-urlencoded, multipart/form-data) are
    redirected. Everything else re-raises as before, so the editor's JSON
    fetch calls (run, retry, rename dataclip) still get a 403 rather than
    following a redirect to an HTML page.
  • Where it redirects: to the referer's path, only when the referer is
    this host. Otherwise to /users/log_in, so it can't be used as an open
    redirect.
  • Sentry: Plug.CSRFProtection.InvalidCSRFTokenError is added to
    Lightning.SentryEventFilter, as proposed in the issue.

Closes #4809

Validation steps

  1. Open /users/log_in in two tabs. Log in on the first, then submit the
    form in the second: you are returned to the page with "This form has
    expired. Please try again." instead of the Forbidden page.
  2. Or log in, press Back, and submit the cached log-in form again.
  3. A normal log-in still works.

Automated:

  • test/lightning_web/plugs/csrf_protection_with_fallback_test.exs: a form
    returns to a same-site referer (query string kept), an off-site or missing
    referer goes to log in, and a JSON request still gets a 403.
  • user_session_controller_test.exs: stale and missing tokens on the log-in
    form redirect with the message and don't log in; a valid token from the
    real form still logs in.
  • sentry_event_filter_test.exs: the new filter entry.

These run with CSRF checks turned back on
(put_private(conn, :plug_skip_csrf_protection, false)).

Additional notes for the reviewer

  1. Sobelow's Config.CSRF check looks for the literal :protect_from_forgery
    in the pipeline, so it flags the browser pipeline. I added a commented
    # sobelow_skip ["Config.CSRF"] above pipeline :browser rather than
    working around the check.
  2. The message differs slightly from the issue's suggestion ("Your session
    expired"). A logged-in user submitting a stale form on another page (for
    example two-factor setup) is still logged in, so "This form has expired"
    is accurate for both cases.
  3. The Sentry filter now also silences CSRF failures from the
    :authenticated_json pipeline, which still return a 403. That follows the
    issue's proposal; happy to drop it if you'd rather keep those visible.
  4. :authenticated_json keeps plain :protect_from_forgery.

AI Usage

Please disclose whether you've used AI anywhere in this PR (it's cool, we just
want to know!):

  • I have used Claude Code
  • I have used another model
  • I have not used AI

You can read more details in our
Responsible AI Policy

Pre-submission checklist

  • I have performed an AI review of my code (we recommend using /review
    with Claude Code)
  • I have implemented and tested all related authorization policies.
    (e.g., :owner, :admin, :editor, :viewer)
  • I have updated the changelog.
  • I have ticked a box in "AI usage" in this PR

Submitting a form with a CSRF token from an earlier session, most often
a log-in form re-submitted after pressing back or left open in a second
tab, raised InvalidCSRFTokenError and showed the default Forbidden page.

The browser pipeline now uses CSRFProtectionWithFallback, which runs
Plug.CSRFProtection unchanged and turns its failure into a redirect to
/users/log_in with "Your session expired. Please try again." The request
is still rejected. The JSON pipeline keeps the plain 403, and the
exception is added to SentryEventFilter so what remains stops reaching
Sentry.

Fixes OpenFn#4809
The fallback redirected every CSRF failure in the browser pipeline,
including the editor's JSON fetch calls (run, retry, rename dataclip),
which would then follow a redirect to an HTML page instead of seeing an
error. Only form-encoded submissions now get the redirect; anything else
re-raises, as before.

A form now goes back to the page it was submitted from when the referer
names this host, so a logged-in user isn't bounced to the projects page.
Off-site or missing referers go to /users/log_in, so this can't be used
as an open redirect. The message becomes "This form has expired. Please
try again.", which is accurate whether or not the user is logged in.
MDEx rendered AI assistant messages in the legacy LiveView component,
which OpenFn#4908 removed. Nothing has called it since, and mix hex.audit now
fails on five advisories published for MDEx 0.13.3 on 30 Nov 2026, so
every PR's lint job fails.

Remove the dependency instead of upgrading it to 0.15.0, and drop mdex
and mdex_native from the lock file with mix deps.unlock --unused.

Closes OpenFn#4948
@github-project-automation github-project-automation Bot moved this to New Issues in Core Dec 2, 2026
hex.audit fails on every branch while MDEx 0.13.3 is in the lock file.
This brings in OpenFn#5295's change so CI passes here too; it drops out of
this PR's diff once OpenFn#5295 is merged and main is merged in.
yaml_elixir 2.12.1 expands nested YAML aliases without a limit, so a
crafted file can exhaust memory. The advisory fails mix hex.audit in CI
lint. 2.13.0 carries the fix and fits the existing ~> 2.12 constraint,
so only mix.lock changes.

This branch has not been deployed

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

Labels

None yet

Projects

Status: New Issues

Development

Successfully merging this pull request may close these issues.

Handle CSRF token error gracefully

1 participant