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

CSRF token in url-encoded body is url-decoded twice #1260

Open
alxndrsn opened this issue Nov 3, 2024 · 4 comments
Open

CSRF token in url-encoded body is url-decoded twice #1260

alxndrsn opened this issue Nov 3, 2024 · 4 comments
Labels
refactor Improves code without altering behavior

Comments

@alxndrsn
Copy link
Contributor

alxndrsn commented Nov 3, 2024

  1. by body-parser, and
  2. by authHandler

Introduced in 36b97ff


$ curl http://localhost:8383/v1/projects/1/forms/1/submissions.csv.zip -H 'Cookie: session=<valid-session-token>' -H 'x-forwarded-proto: https' -H 'content-type: application/x-www-form-urlencoded' --data '__csrf=%25ea'
{"message":"Internal Server Error"}
@matthew-white
Copy link
Member

IIRC we have seen that CSRF tokens sometimes end up URL-encoded in the __csrf cookie (I think just $ and/or ! characters in the token). I think Enketo will then pass that encoded value back to Backend as-is without decoding it first. After Enketo encodes its request body, the encoded token will end up encoded a second time, requiring Backend to decode it twice. We considered changing how that works in Enketo (i.e., having Enketo decode the value of the __csrf cookie), but decided that it wasn't needed. Valid tokens don't contain %, which I think means they can be decoded any number of times without issue.

@alxndrsn
Copy link
Contributor Author

alxndrsn commented Nov 4, 2024

they can be decoded any number of times without issue.

As you say, risk-free for valid tokens 🙂

Handling for invalid tokens added at #1265

@matthew-white
Copy link
Member

I'm going to go ahead and close this issue now that #1265 is approved. Feel free to reopen if there's more to discuss!

@alxndrsn
Copy link
Contributor Author

alxndrsn commented Nov 9, 2024

It looks like valid tokens do not need URL-decoding at all, so I'd say there's still an option to simplify the code by removing a round of decoding.

@alxndrsn alxndrsn reopened this Nov 9, 2024
@matthew-white matthew-white added the refactor Improves code without altering behavior label Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor Improves code without altering behavior
Projects
None yet
Development

No branches or pull requests

2 participants