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

Add ability to specify CSRF token in header #925

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

matthew-white
Copy link
Member

If a non-GET request is sent, and a session cookie is used to authenticate, then the CSRF token associated with the session must also be provided. Right now, the CSRF token is expected to be in the request body. For example, Frontend provides the CSRF token from the iframe form in the SubmissionDownload component. Enketo also provides the CSRF token when using cookie auth.

However, I think the expectation that the CSRF token is in the request body doesn't work for all requests. The main issue that comes to mind is file uploads. For example, I think requiring the CSRF token to be in the request body would be an obstacle to using cookie auth to upload a form definition or form attachment.

This PR makes it possible to use cookie auth in such cases by allowing the CSRF token to be specified in a header instead of the request body.

What has been done to verify that this works as intended?

New tests.

Why is this the best possible solution? Were any other approaches considered?

I don't love that there are now two ways to specify the CSRF token. I thought about removing support for CSRF tokens in the request body so that there would be only one way to specify the token (via the header). However, that wouldn't work for HTML form submission like from the iframe form. I also didn't want to change anything about our Enketo integration.

I also don't think this PR adds that much complexity. Only two lines of code were changed.

If a user of cookie auth has a choice between specifying the token in the header vs. in the request body, specifying it in the header is probably slightly better. That's because it doesn't require the token to be removed from the request body.

How does this change affect users? Describe intentional changes to behavior and behavior that could have accidentally been affected by code changes. In other words, what are the regression risks?

During regression testing, we will verify that the parts of Central that rely on cookie auth continue to work.

Does this change require updates to the API documentation? If so, please update docs/api.md as part of this PR.

I don't think changes are needed. We discourage the use of cookie auth in the API docs, and we don't document any requirements around the CSRF token.

Before submitting this PR, please make sure you have:

  • run make test-full and confirmed all checks still pass OR confirm CircleCI build passes
  • verified that any code from external sources are properly credited in comments or that everything is internally sourced

Copy link
Member Author

Choose a reason for hiding this comment

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

I wrapped some tests in a describe block, so the diff will look nicer if you hide whitespace.

@@ -298,18 +298,6 @@ describe('preprocessors', () => {
)
)).should.be.rejectedWith(Problem, { problemCode: 401.2 }));

it('should reject cookie auth with incorrect CSRF token for non-GET requests', () =>
Copy link
Member Author

Choose a reason for hiding this comment

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

This test was moved below.

);
context.body.should.eql({ __csrf: 'secretcsrf', other: 'data' });
});
});

it('should url-decode the CSRF token', () =>
Copy link
Member Author

Choose a reason for hiding this comment

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

This test ("should url-decode the CSRF token") was moved below.

@matthew-white matthew-white self-assigned this Jul 21, 2023
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.

None yet

1 participant