Skip to content

CSRF exempt for WebhookView when authenticate via user/pass #5009

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

Closed
wants to merge 3 commits into from

Conversation

humitos
Copy link
Member

@humitos humitos commented Dec 17, 2018

I think this bug was introduced at f95cae3

Closes #4986

@humitos humitos requested a review from a team December 17, 2018 13:12
@humitos humitos added this to the 2.9 milestone Dec 26, 2018
@agjohnson
Copy link
Contributor

I'm waffling on this PR.

Removing this would likely(?) be opening ourselves up to CSRF attacks that use a logged in user's session to perform actions on our API. Without a CSRF token from RTD, a malicious site could use JS to interact with our API on behalf of the logged in user. We can mitigate this by requiring a CSRF token from RTD, in much the same way that we protect our dashboard forms against malicious JS. But i agree that this is extra burden on the user that makes for a worse UX.

This is one of the reasons why I lean towards divorcing user sessions/auth and API auth, though I suppose that offering token auth for a case like this removes the burden of CSRF tokens and session auth on API requests.

@humitos
Copy link
Member Author

humitos commented Jan 3, 2019

I agree with all you said here.

Removing this would likely(?) be opening ourselves up to CSRF attacks that use a logged in user's session to perform actions on our API

I'm exempting only the API view for the Webhooks not all the API endpoints. It's still "dangerous" because a malicious JS could trigger a crazy amount of builds, but it won't be able to change anything in the project.

On the other hand, if we are going to not allow this, we need to update our docs at

https://docs.readthedocs.io/en/latest/webhooks.html#authentication

and reply to @ianw at #4986.

This is one of the reasons why I lean towards divorcing user sessions/auth and API auth, though I suppose that offering token auth for a case like this removes the burden of CSRF tokens and session auth on API requests.

This is the solution to me as well, and APIv3 will behave in this way.

@ianw
Copy link

ianw commented Jan 21, 2019

This is one of the reasons why I lean towards divorcing user sessions/auth and API auth, though I suppose that offering token auth for a case like this removes the burden of CSRF tokens and session auth on API requests.

By this you mean JWT type tokens for the user? Rather than project-specific tokens?

As an infrastructure project hosting many projects that want to publish to RTD via us, it is much easier for us to run things via a single authenticated user and to publish. It means for our users they simply add the CI user to their project (so it can publish) and add the publishing job with their non-secret project id to their CI release pipeline. We only need to manage the one one secret that is the CI user password. We could do a non-expiring token just the same.

We can take the alternative approach of each project using their per-project token to trigger builds, but it means managing many more secrets. Each client project needs to then get their token, encrypt it and write a job to use it. And because we don't want to leak the tokens we don't publish the logs, so then it becomes another step if debugging where projects probably need to get infrastructure admins involved.

@agjohnson agjohnson modified the milestones: 2.9, 3.2 Jan 25, 2019
@agjohnson agjohnson added Bug A bug Accepted Accepted issue on our roadmap labels Jan 25, 2019
@agjohnson agjohnson modified the milestones: 3.2, 3.3 Jan 25, 2019
@humitos humitos mentioned this pull request Feb 11, 2019
@gorshunovr
Copy link
Contributor

Team, please, review this patch. Thank you very much.

ericholscher
ericholscher previously approved these changes Feb 19, 2019
Copy link
Member

@ericholscher ericholscher left a comment

Choose a reason for hiding this comment

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

I think this makes sense for the webhook view. I agree on not exposing it for all API views, but this is breaking a documented use case that users depend on, so I'm 👍

This way, these webhook can be called from a command line and
authenticate them via session (user/password)
@humitos
Copy link
Member Author

humitos commented Feb 19, 2019

Upgrade the PR to solve conflicts. Ready to merge once tests pass.

# We want to avoid CSRF checking when authenticating by user/password on
# this API endpoint so we can make a request like:
# curl -X POST -d "branches=branch" -u user:pass -e URL /api/v2/webhook/test-builds/{pk}/
authentication_classes = [CsrfExemptSessionAuthentication]
Copy link
Member

@ericholscher ericholscher Feb 19, 2019

Choose a reason for hiding this comment

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

I thought we were doing basic auth here, not session auth?

Seems like it should be using https://www.django-rest-framework.org/api-guide/authentication/#basicauthentication

Copy link
Member

@ericholscher ericholscher left a comment

Choose a reason for hiding this comment

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

Holding off on approval until I understand the session auth changes.

ericholscher added a commit that referenced this pull request Feb 19, 2019
@stsewd
Copy link
Member

stsewd commented Feb 19, 2019

I guess we can close this one. Problem fixed in #5311

@stsewd stsewd deleted the humitos/webhook/csrf-exempt branch February 19, 2019 19:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Accepted Accepted issue on our roadmap Bug A bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants