Skip to content

AUT-4272: migrate from csurf to csurf sync (v2)#2804

Closed
sw-mattw wants to merge 7 commits into
mainfrom
AUT-4272/migrate-from-csurf-to-csurf-sync-v2
Closed

AUT-4272: migrate from csurf to csurf sync (v2)#2804
sw-mattw wants to merge 7 commits into
mainfrom
AUT-4272/migrate-from-csurf-to-csurf-sync-v2

Conversation

@sw-mattw

@sw-mattw sw-mattw commented May 27, 2025

Copy link
Copy Markdown
Contributor

What

This PR includes the same changes as #2793 with some modifications to the redirect behaviour (see below).

In #2793, if a session expired and a user submitted a form the CSRF plaintext error would be rendered which was not user friendly. The additional work under this PR handle this case, rendering the standard "session expired" page in this scenario. Additionally, if a session is valid but the token is missing or invalid, the standard "internal server error" page will now be rendered. For further details on these additions, see the description of 98c398d.

Release

We will perform a controlled release of this change as it affects all frontend pages (in this repo) with forms. We will pause the pipeline following deployment to staging, and then following deployment to integration, testing on these environments before proceeding.

Testing

Deployed to authdev1, and:

  1. Ran through sign in journey (via orch stub) successfully
  2. Ran through password reset journey (via orch stub) successfully
  3. Cleared cookies / site data, directly navigated to the anonymous cookies page on the deployed env, successfully submitted form.
  4. On the /enter-email page, changed the CSRF token hidden input value slightly, submitted form, received the "Sorry, there is a problem" 500 page (expected as the token in the hidden input did not match that in the session).
  5. On the /enter-email page, waited for the session to expire (5m on dev), submitted form, received the "session expired" page (as expected - token comparison fails as there is no session for the token to be stored in).
  6. On the /enter-email page, changed the CSRF token hidden input value slightly, waited for the session to expire (5m on dev), submitted form, received the "session expired" page (as expected - token comparison fails as although the hidden input value is wrong, there is no session for the token to be stored in).
  7. On the /enter-email page, waited for the session to expire (5m on dev), clicked the "Back" link, received the "session expired" page (as expected).

For each of these, observed aps cookie being set (session) and the token in the hidden input.

How to review

  1. Code review
  2. Deploy to a dev env
  3. Run through typical journeys (e.g., sign in, sign up)
  4. Run through anonymous journey (e.g., cookies) to confirm form submits as expected
  5. Optionally, let the session expire on a page with a form, submit form, check the "session expired" page is rendered

Checklist

  • Performance analyst has been notified of the change. - N/A
  • A UCD review has been performed. - N/A
  • Any necessary changes to the acceptance tests have been made. - N/A, no CSRF references
  • Documentation has been updated to reflect these changes.

Related PRs

sw-mattw added 5 commits May 27, 2025 10:22
Note that this helper is no longer used as `csrf-sync` does not accept cookie options as the session is used for token storage.
Refreshing per request was too fragile - we had issues when an optional script was not found causing the session token to refresh meaning the token in the form submission did not match (causing a 403). This means that with per request refresh if we make a mistake with an optional script and it 404s then the entire app is broken. This may also be problematic with slow page loads and double submits.

Note that having a token per session mimics the previous implementation with `csurf` and is simpler than per request. This creates a session even when not authenticated, so works with the anonymous pages (e.g., cookies, contact-us).

Other reference:
https://security.stackexchange.com/a/22936
@sw-mattw sw-mattw self-assigned this May 27, 2025
@sw-mattw sw-mattw force-pushed the AUT-4272/migrate-from-csurf-to-csurf-sync-v2 branch from 98c398d to f01288f Compare May 27, 2025 14:33
Note that if the session is invalid (e.g., if it has expired) then we wish to render the standard "session expired" page. Previously in this case we would render the CSRF error text which is not user friendly.

This commit renders the "session expired" page if a CSRF error has occurred, but this is due to an invalid session. If the session is still active, then we render the "internal server error" page. The user can use the browsers back button to return to the form on the previous page, the token in the "hidden" input will be reset to a valid value automatically, allowing the user to resubmit the form successfully.
@sw-mattw sw-mattw force-pushed the AUT-4272/migrate-from-csurf-to-csurf-sync-v2 branch from f01288f to 9747a66 Compare May 28, 2025 09:05
…endered

If a session is valid but the csrf token is invalid, we render the "internal server error" page.

If a session is invalid, we render the "session expired" page.
@sw-mattw sw-mattw marked this pull request as ready for review May 28, 2025 10:15
@sw-mattw sw-mattw requested review from a team as code owners May 28, 2025 10:15
@sonarqubecloud

Copy link
Copy Markdown

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant