Skip to content
Snippets Groups Projects

Skip CSRF check for query-only GraphQL requests

Merged Heinrich Lee Yu requested to merge 379326-graphql-skip-csrf-for-queries into master
All threads resolved!

What does this MR do and why?

When a GraphQL request only contains queries, it can be requested using GET which skips CSRF checks.

We can skip the same checks even if they are requested with a POST since these should not mutate any data.

This is a follow-up to !175955 (merged). That MR prevents CSRF mismatches from logging out the user, but the mismatches still happen and the user will see alerts about failures loading data within the page.

This MR prevents most of them, since most if not all of these GraphQL requests that are triggered on page load are read-only queries.

References

Please include cross links to any resources that are relevant to this MR. This will give reviewers and future readers helpful context to give an efficient review of the changes introduced.

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Heinrich Lee Yu

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • Author Maintainer

    @atevans @grzesiek what do you think of this change?

    I think to fully solve the CSRF problem, we'd have to store the token outside the session as described in https://github.com/demarches-simplifiees/demarches-simplifiees.fr/blob/5b4f7f9ae9eaf0ac94008b62f7047e4714626cf9/doc/adr-csrf-forgery.md#using-a-long-lived-cookie-for-csrf-tokens. This is a much bigger change that also requires Rails 7.1 which we're still in the process of upgrading to.

    So to mitigate most of the problems, I think we can do this change where we skip CSRF check for query-only requests. This should allow pages restored from cache to load properly. Though the user would still encounter an error when they try to submit a comment or any other mutation. They would still need to refresh for that.

  • Heinrich Lee Yu requested review from @grzesiek and @atevans

    requested review from @grzesiek and @atevans

  • Andrew Evans approved this merge request

    approved this merge request

  • Before you set this MR to auto-merge

    This merge request will progress on pipeline tiers until it reaches the last tier: pipelinetier-3. We will trigger a new pipeline for each transition to a higher tier.

    Before you set this MR to auto-merge, please check the following:

    • You are the last maintainer of this merge request
    • The latest pipeline for this merge request is pipelinetier-3 (You can find which tier it is in the pipeline name)
    • This pipeline is recent enough (created in the last 8 hours)

    If all the criteria above apply, please set auto-merge for this merge request.

    See pipeline tiers and merging a merge request for more details.

    • Resolved by Heinrich Lee Yu

      I was able to reproduce the functionality locally. Approved! :white_check_mark: After reading the code, I agree we're not making a huge change here, security-wise. It should help further reduce cases where users get logged out due to an unexpected CSRF miss.

      Agree that moving to cookie-based storage for CSRF tokens would be a big change. There may be alternate solutions we can try - maybe frontend can check for the newest CSRF token on page restoration before launching any further requests? Could be hard to implement. It's probably worth discussing further.

  • E2E Test Result Summary

    allure-report-publisher generated test report!

    e2e-test-on-cng: :white_check_mark: test report for f6b56b32

    expand test summary
    +------------------------------------------------------------------+
    |                          suites summary                          |
    +-------------+--------+--------+---------+-------+-------+--------+
    |             | passed | failed | skipped | flaky | total | result |
    +-------------+--------+--------+---------+-------+-------+--------+
    | Verify      | 51     | 0      | 15      | 0     | 66    | ✅     |
    | Create      | 140    | 0      | 19      | 1     | 159   | ✅     |
    | Govern      | 84     | 0      | 10      | 0     | 94    | ✅     |
    | Monitor     | 8      | 0      | 12      | 0     | 20    | ✅     |
    | Package     | 30     | 0      | 14      | 0     | 44    | ✅     |
    | Plan        | 86     | 0      | 8       | 0     | 94    | ✅     |
    | Secure      | 2      | 0      | 5       | 0     | 7     | ✅     |
    | Data Stores | 33     | 0      | 10      | 0     | 43    | ✅     |
    | Analytics   | 2      | 0      | 0       | 0     | 2     | ✅     |
    | Fulfillment | 2      | 0      | 7       | 0     | 9     | ✅     |
    | Growth      | 0      | 0      | 2       | 0     | 2     | ➖     |
    | Manage      | 1      | 0      | 9       | 0     | 10    | ✅     |
    | Release     | 5      | 0      | 1       | 0     | 6     | ✅     |
    | Ai-powered  | 0      | 0      | 2       | 0     | 2     | ➖     |
    | ModelOps    | 0      | 0      | 1       | 0     | 1     | ➖     |
    | Configure   | 0      | 0      | 3       | 0     | 3     | ➖     |
    +-------------+--------+--------+---------+-------+-------+--------+
    | Total       | 444    | 0      | 118     | 1     | 562   | ✅     |
    +-------------+--------+--------+---------+-------+-------+--------+

    e2e-test-on-gdk: :white_check_mark: test report for f6b56b32

    expand test summary
    +------------------------------------------------------------------+
    |                          suites summary                          |
    +-------------+--------+--------+---------+-------+-------+--------+
    |             | passed | failed | skipped | flaky | total | result |
    +-------------+--------+--------+---------+-------+-------+--------+
    | Create      | 135    | 0      | 20      | 0     | 155   | ✅     |
    | Govern      | 80     | 0      | 12      | 0     | 92    | ✅     |
    | Data Stores | 33     | 0      | 10      | 0     | 43    | ✅     |
    | Verify      | 50     | 0      | 16      | 0     | 66    | ✅     |
    | Package     | 25     | 0      | 13      | 0     | 38    | ✅     |
    | Growth      | 0      | 0      | 2       | 0     | 2     | ➖     |
    | Manage      | 1      | 0      | 9       | 0     | 10    | ✅     |
    | Ai-powered  | 0      | 0      | 2       | 0     | 2     | ➖     |
    | Secure      | 4      | 0      | 3       | 0     | 7     | ✅     |
    | Monitor     | 8      | 0      | 12      | 0     | 20    | ✅     |
    | Plan        | 82     | 0      | 8       | 0     | 90    | ✅     |
    | Configure   | 0      | 0      | 3       | 0     | 3     | ➖     |
    | Fulfillment | 2      | 0      | 7       | 0     | 9     | ✅     |
    | Release     | 5      | 0      | 1       | 0     | 6     | ✅     |
    | ModelOps    | 0      | 0      | 1       | 0     | 1     | ➖     |
    | Analytics   | 2      | 0      | 0       | 0     | 2     | ✅     |
    +-------------+--------+--------+---------+-------+-------+--------+
    | Total       | 427    | 0      | 119     | 0     | 546   | ✅     |
    +-------------+--------+--------+---------+-------+-------+--------+
  • Grzegorz Bizon
  • Heinrich Lee Yu requested review from @grzesiek

    requested review from @grzesiek

  • added 1 commit

    • f6b56b32 - Skip CSRF check for query-only GraphQL requests

    Compare with previous version

  • Heinrich Lee Yu reset approvals from @atevans by pushing to the branch

    reset approvals from @atevans by pushing to the branch

  • Heinrich Lee Yu requested review from @grzesiek

    requested review from @grzesiek

  • Adil Farrukh mentioned in issue #414501

    mentioned in issue #414501

  • Andrew Evans approved this merge request

    approved this merge request

  • Grzegorz Bizon approved this merge request

    approved this merge request

  • Heinrich Lee Yu resolved all threads

    resolved all threads

  • Heinrich Lee Yu enabled automatic add to merge train when checks pass

    enabled automatic add to merge train when checks pass

  • Heinrich Lee Yu mentioned in commit be0b27ca

    mentioned in commit be0b27ca

  • added workflowproduction label and removed workflowcanary label

  • Please register or sign in to reply
    Loading