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

ENFORCE CSRF verification in API to be accessed by a browser strictly #11978

Merged
merged 10 commits into from
Mar 17, 2024

Conversation

thesujai
Copy link
Contributor

@thesujai thesujai commented Mar 12, 2024

Summary

Enforces CSRF Verification more strongly in post api endpoints which are supposed to be accessed only by browser

References

Fixes #7389

Reviewer guidance

I performed Manual QA on the following API's:(except for setupwizard, i was confused with the actual api of that)

  1. api/auth/session/
  2. api/auth/facility/
  3. api/content/contentrequest/
  4. api/auth/signup/
  5. api/tasks/tasks/
  6. api/device/deviceprovision/

I ran curl commands like:

curl -X POST "http://localhost:8000/api/auth/facility/" -H  "accept: application/json" -H  "authorization: Basic c3VqYWk6c3VqYWk=" -H  "Content-Type: application/json"  -d "{  \"name\": \"string\"}"

I did the above for all API's i have changed here, without my changes i was in the API but with it i wasn't able to access it


Testing checklist

  • Contributor has fully tested the PR manually
  • If there are any front-end changes, before/after screenshots are included
  • Critical user journeys are covered by Gherkin stories
  • Critical and brittle code paths are covered by unit tests

PR process

  • PR has the correct target branch and milestone
  • PR has 'needs review' or 'work-in-progress' label
  • If PR is ready for review, a reviewer has been added. (Don't use 'Assignees')
  • If this is an important user-facing change, PR or related issue has a 'changelog' label
  • If this includes an internal dependency change, a link to the diff is provided

Reviewer checklist

  • Automated test coverage is satisfactory
  • PR is fully functional
  • PR has been tested for accessibility regressions
  • External dependency files were updated if necessary (yarn and pip)
  • Documentation is updated
  • Contributor is in AUTHORS.md

@github-actions github-actions bot added DEV: backend Python, databases, networking, filesystem... APP: Setup Wizard Re: Setup Wizard (facility import, superuser creation, settings, etc.) SIZE: small labels Mar 12, 2024
Copy link
Member

@rtibbles rtibbles left a comment

Choose a reason for hiding this comment

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

This is looking promising - a few places where I don't think it's needed, and a few more places where I think it may be.

I would like to see some unit tests of these API endpoints just to guarantee this CSRF enforcement in the future.

kolibri/core/auth/api.py Outdated Show resolved Hide resolved
kolibri/core/auth/api.py Outdated Show resolved Hide resolved
kolibri/core/auth/api.py Outdated Show resolved Hide resolved
kolibri/core/auth/api.py Show resolved Hide resolved
@@ -1387,6 +1388,7 @@ class Meta:
fields = ("contentnode_id", "contentnode_id__in")


@method_decorator(csrf_protect, name="dispatch")
Copy link
Member

Choose a reason for hiding this comment

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

Good catch - I think we should actually just add the IsAuthenticated permissions class to this viewset, as it requires a logged in user in the get_queryset method, so would just give a 500 for an anonymous user, I'd guess?

This might be worth doing in 0.16 instead, if you could open a separate PR to add just that onto the release-v0.16.x branch.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok i will add IsAuthenticated permissions to this in a seperate PR
And will remove csrf changes from this

Copy link
Member

Choose a reason for hiding this comment

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

Thank you!

kolibri/core/tasks/api.py Outdated Show resolved Hide resolved
kolibri/plugins/setup_wizard/api.py Show resolved Hide resolved
@rtibbles rtibbles self-assigned this Mar 12, 2024
@thesujai
Copy link
Contributor Author

I have added some unit tests for the API's those are modified by this PR. If any of them are not doing the thing or is not at the right file, let me know, i will change it.

Copy link
Member

@rtibbles rtibbles left a comment

Choose a reason for hiding this comment

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

Excellent - love the tests, just a little bit more cleanup to do, and clarity over the GET behaviour of the session viewset.

@rtibbles
Copy link
Member

Excellent work, thank you!

@rtibbles rtibbles merged commit 0370a3a into learningequality:develop Mar 17, 2024
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
APP: Setup Wizard Re: Setup Wizard (facility import, superuser creation, settings, etc.) DEV: backend Python, databases, networking, filesystem... SIZE: medium SIZE: small
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Django REST Framework does not check CSRF token on endpoints not requiring authentication
2 participants