Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10-10CG] Ensure we have a valid csrf token when making POST requests in 10-10CG #34395

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

coope93
Copy link
Contributor

@coope93 coope93 commented Jan 30, 2025

Note: Delete the description statements, complete each step. None are optional, but can be justified as to why they cannot be completed as written. Provide known gaps to testing that may raise the risk of merging to production.

Are you removing, renaming or moving a folder in this PR?

  • No, I'm not changing any folders (skip to TeamSites and delete the rest of this section)
  • Yes, I'm removing, renaming or moving a folder

If the folder you changed contains a manifest.json, search for its entryName in the content-build registry.json (the entryName there will match).

If an entry for this folder exists in content-build and you are:

  1. Deleting a folder:

    1. First search vets-website for all instances of the entryName in your manifest.json and remove them in a separate PR. Look particularly for references in src/applications/static-pages/static-pages-entry.js and src/platform/forms/constants.js. If you do not do this, other applications will break!
      • Add the link to your merged vets-website PR here
    2. Then, Delete the application entry in registry.json and merge that PR before this one
      • Add the link to your merged content-build PR here
  2. Renaming or moving a folder: Update the entry in the registry.json, but do not merge it until your vets-website changes here are merged. The content-build PR must be merged immediately after your vets-website change is merged in to avoid CI errors with content-build (and Tugboat).

⚠️ TeamSites ⚠️

Examples of a TeamSite: https://va.gov/health and https://benefits.va.gov/benefits/. This scenario is also referred to as the "injected" header and footer. You can reach out in the #sitewide-public-websites Slack channel for questions.

Did you change site-wide styles, platform utilities or other infrastructure?

Summary

  • Pulling out the logic we introduced to make a HEAD request to generate a new csrf token when it is absent from localStorage in the fetchFacilities function.
  • Created a new ensureValidCSRFToken function that checks if the csrf token is in local storage, and if it is not it makes the HEAD request to generate a new one.
  • Using this new function in the fetchFacilities function and in the ApplicationDownloadLink component that makes a POST call to generate a pdf for download.
  • This is a first step to use this method in more than one place, and the eventual goal is to integrate it into the existing apiRequest function.
  • I wanted to get it in and prove that it is working for the ApplicationDownloadLink since we have consistently been seeing 403 invalid csrf errors for years on this call, and I think getting it in to validate it here sooner is better than waiting to ensure we have the right approach to introduce it sitewide via the apiRequest function.
  • 1010 Health Apps

Related issue(s)

Testing done

  • Updated the unit tests to for the new function and where it is used
  • We will manually validate the request is being sent when there is no csrf token in a RI before merging

Screenshots

No UI changes

What areas of the site does it impact?

10-10CG

Acceptance criteria

Quality Assurance & Testing

  • I fixed|updated|added unit tests and integration tests for each feature (if applicable).
  • No sensitive information (i.e. PII/credentials/internal URLs/etc.) is captured in logging, hardcoded, or specs
  • Linting warnings have been addressed
  • Documentation has been updated (link to documentation *if necessary)
  • Screenshot of the developed feature is added
  • Accessibility testing has been performed

Error Handling

  • Browser console contains no warnings or errors.
  • Events are being sent to the appropriate logging solution
  • Feature/bug has a monitor built into Datadog or Grafana (if applicable)

Authentication

  • Did you login to a local build and verify all authenticated routes work as expected with a test user

@coope93 coope93 changed the title Coope93 ensure valid csrf token [10-10CG] Ensure we have a valid csrf token when making POST requests in 10-10CG Jan 30, 2025
});
};

export const ensureValidCSRFToken = async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same logic we had in the fetchFacilities, just moved it here so it can be reused. Maybe there is a better place than actions? It's really a helper, but I'm also hoping it does not stay here super long as we will want to eventually get it somewhere more global.

@@ -155,69 +141,6 @@ describe('CG fetchFacilities action', () => {
expect(apiRequestStub.callCount).to.equal(1);
});
});

context('no csrfToken in localStorage', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is all tested in the unit spec for the new ensureValidCSRFToken

@coope93 coope93 marked this pull request as ready for review January 30, 2025 19:57
@coope93 coope93 requested review from a team as code owners January 30, 2025 19:57
Copy link

@va-vfs-bot va-vfs-bot left a comment

Choose a reason for hiding this comment

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

Sentry call found

Sentry captures a lot of data, and we want to make sure that we only keep information that will be useful for troubleshooting issues. This means that PII should not be recorded.

What you can do

Review your call to Sentry and see if you can reasonably reduce any information that is included, or wait for a VSP review.

event: 'caregivers-10-10cg-fetch-csrf-token-empty',
});

Sentry.withScope(scope => {

Choose a reason for hiding this comment

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

Sentry found

});

Sentry.withScope(scope => {
scope.setLevel(Sentry.Severity.Log);

Choose a reason for hiding this comment

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

Sentry found


Sentry.withScope(scope => {
scope.setLevel(Sentry.Severity.Log);
Sentry.captureMessage(`${message} Calling ${url} to generate new one.`);

Choose a reason for hiding this comment

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

Sentry found


return apiRequest(`${environment.API_URL}${url}`, { method: 'HEAD' })
.then(() => {
Sentry.withScope(scope => {

Choose a reason for hiding this comment

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

Sentry found

return apiRequest(`${environment.API_URL}${url}`, { method: 'HEAD' })
.then(() => {
Sentry.withScope(scope => {
scope.setLevel(Sentry.Severity.Log);

Choose a reason for hiding this comment

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

Sentry found

.then(() => {
Sentry.withScope(scope => {
scope.setLevel(Sentry.Severity.Log);
Sentry.captureMessage(

Choose a reason for hiding this comment

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

Sentry found

});
})
.catch(error => {
Sentry.withScope(scope => {

Choose a reason for hiding this comment

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

Sentry found

})
.catch(error => {
Sentry.withScope(scope => {
scope.setLevel(Sentry.Severity.Log);

Choose a reason for hiding this comment

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

Sentry found

Sentry.withScope(scope => {
scope.setLevel(Sentry.Severity.Log);
scope.setExtra('error', error);
Sentry.captureMessage(

Choose a reason for hiding this comment

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

Sentry found

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.

2 participants