Skip to content

Commit e217f63

Browse files
committed
Make Webhook views CSRF exempt
This way, these webhook can be called from a command line and authenticate them via session (user/password)
1 parent 4cf6a2e commit e217f63

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

readthedocs/restapi/authentication.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# -*- coding: utf-8 -*-
2+
from rest_framework.authentication import SessionAuthentication
3+
4+
5+
class CsrfExemptSessionAuthentication(SessionAuthentication):
6+
"""
7+
Session authentication class exempt of CSRF.
8+
9+
DRF by default when using a ``SessionAuthentication`` it enforces CSRF.
10+
11+
See: https://github.com/encode/django-rest-framework/blob/3.9.0/rest_framework/authentication.py#L134-L144
12+
"""
13+
14+
def enforce_csrf(self, request):
15+
return

readthedocs/restapi/views/integrations.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
from readthedocs.integrations.utils import normalize_request_payload
3030
from readthedocs.projects.models import Project
3131

32+
from ..authentication import CsrfExemptSessionAuthentication
33+
34+
3235
log = logging.getLogger(__name__)
3336

3437
GITHUB_EVENT_HEADER = 'HTTP_X_GITHUB_EVENT'
@@ -418,6 +421,11 @@ class WebhookView(APIView):
418421
be.
419422
"""
420423

424+
# We want to avoid CSRF checking when authenticating by user/password on
425+
# this API endpoint so we can make a request like:
426+
# curl -X POST -d "branches=branch" -u user:pass -e URL /api/v2/webhook/test-builds/{pk}/
427+
authentication_classes = [CsrfExemptSessionAuthentication]
428+
421429
VIEW_MAP = {
422430
Integration.GITHUB_WEBHOOK: GitHubWebhookView,
423431
Integration.GITLAB_WEBHOOK: GitLabWebhookView,

0 commit comments

Comments
 (0)