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

Implement CSRF token verification for OAuth example #2534

Merged
merged 3 commits into from
Nov 17, 2024

Conversation

loganbnielsen
Copy link
Contributor

Addresses #2511

Copy link

@iggyzuk iggyzuk left a comment

Choose a reason for hiding this comment

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

Looks good and it works, I used it in my project.

// Create session to store csrf_token
let mut session = Session::new();
session
.insert("csrf_token", &csrf_token)
Copy link

Choose a reason for hiding this comment

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

Since "csrf_token" is used in two placed it might be good to define a constant value. static CSRF_TOKEN: &str = "csrf_token"; similar to COOKIE_NAME. I suppose "user" also needs one.

.context("unexpected error retrieving CSRF cookie value")?;

// Attach the session cookie to the response header
let cookie = format!("{COOKIE_NAME}={cookie}; SameSite=Lax; Path=/");
Copy link

@emmanuelgautier emmanuelgautier Mar 24, 2024

Choose a reason for hiding this comment

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

The cookie security can be improved setting HttpOnly and Secure.

Copy link

Choose a reason for hiding this comment

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

In my case setting SameSite to Strict breaks something "Application error: unexpected error getting cookie name". It happens when the login is authorized, so csrf_token_validation_workflow is unable to find cookie COOKIE_NAME. Note: I'm using Google OAuth2, not Discord.

Choose a reason for hiding this comment

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

My bad, you are right. We can not use Strict here since the cookie is sent after a redirect. First comment updated.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@loganbnielsen can you add the mentioned options? I get that we don't need to make the whole thing production-ready, but if these settings are more secure let's add them to the example.

Choose a reason for hiding this comment

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

All good for the HttpOnly and Secure flags. This comment can be resolved.

@loganbnielsen
Copy link
Contributor Author

Just wanted to follow up if there were any changes expected before this can be merged. I think there's some nit improvements which can be made which is understandable.

This example (along with others already merged) is an MVP to set up security and probably doesn't need to be industry ready.

As long as there are no fundamental security mistakes, I think merging it would be best with potential enhancements provided in subsequent PRs being encouraged

We can link the Leptos OAUTH example so users have 2 implementations they can check review before implementing into their own app

@loganbnielsen
Copy link
Contributor Author

Noticed that this has been open for almost a year. Lmk if I can do anything to make this mergeable

Copy link
Collaborator

@mladedav mladedav left a comment

Choose a reason for hiding this comment

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

Thanks for pinging us, let's finish this.

examples/oauth/src/main.rs Show resolved Hide resolved
.context("unexpected error retrieving CSRF cookie value")?;

// Attach the session cookie to the response header
let cookie = format!("{COOKIE_NAME}={cookie}; SameSite=Lax; Path=/");
Copy link
Collaborator

Choose a reason for hiding this comment

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

@loganbnielsen can you add the mentioned options? I get that we don't need to make the whole thing production-ready, but if these settings are more secure let's add them to the example.

@loganbnielsen
Copy link
Contributor Author

updated

@mladedav
Copy link
Collaborator

Thanks. I'll just try to run it tomorrow when I get to a PC and if there aren't any issues, I'll merge it.

@loganbnielsen
Copy link
Contributor Author

Sounds good, I sometimes hit a race condition when I run it where the token read attempt occurs before the write has happened so you might need to try a second time if you get that error (I think exponential back off for second would fix this but feels out of scope for a minimal example)

Copy link
Collaborator

@mladedav mladedav left a comment

Choose a reason for hiding this comment

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

It works for me so I'll merge this. Thanks again for fixing the example.

@mladedav mladedav merged commit 7e59625 into tokio-rs:main Nov 17, 2024
18 checks passed
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.

5 participants