Repository navigation
Conversation
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
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.
…srf-error # Conflicts: # CHANGELOG.md
…/4809-graceful-csrf-error
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
InvalidCSRFTokenErrorand rendered the 403 page (SentryLIGHTNING-NS).
The
:browserpipeline now usesLightningWeb.Plugs.CSRFProtectionWithFallback,which runs
Plug.CSRFProtectionunchanged. When an HTML form submissionfails 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.
(
application/x-www-form-urlencoded,multipart/form-data) areredirected. Everything else re-raises as before, so the editor's JSON
fetchcalls (run, retry, rename dataclip) still get a 403 rather thanfollowing a redirect to an HTML page.
this host. Otherwise to
/users/log_in, so it can't be used as an openredirect.
Plug.CSRFProtection.InvalidCSRFTokenErroris added toLightning.SentryEventFilter, as proposed in the issue.Closes #4809
Validation steps
/users/log_inin two tabs. Log in on the first, then submit theform in the second: you are returned to the page with "This form has
expired. Please try again." instead of the Forbidden page.
Automated:
test/lightning_web/plugs/csrf_protection_with_fallback_test.exs: a formreturns 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-inform 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
Config.CSRFcheck looks for the literal:protect_from_forgeryin the pipeline, so it flags the browser pipeline. I added a commented
# sobelow_skip ["Config.CSRF"]abovepipeline :browserrather thanworking around the check.
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.
:authenticated_jsonpipeline, which still return a 403. That follows theissue's proposal; happy to drop it if you'd rather keep those visible.
:authenticated_jsonkeeps 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!):
You can read more details in our
Responsible AI Policy
Pre-submission checklist
/reviewwith Claude Code)
(e.g.,
:owner,:admin,:editor,:viewer)