Skip to content

fix(security): enforce CSRF protection on REST API endpoints#1885

Open
snowbird91 wants to merge 1 commit intotjcsl:devfrom
snowbird91:fix/enforce-csrf-on-api
Open

fix(security): enforce CSRF protection on REST API endpoints#1885
snowbird91 wants to merge 1 commit intotjcsl:devfrom
snowbird91:fix/enforce-csrf-on-api

Conversation

@snowbird91
Copy link
Copy Markdown

remove CsrfExemptSessionAuthentication which disabled CSRF checking for all session-auth API requests. replaced with DRF's built in SessionAuthentication which enforces CSRF validation.

Proposed changes

  • removed CsrfExemptSessionAuthentication class from intranet/apps/api/authentication.py
  • replaced the custom class with rest_framework.authentication.SessionAuthentication in intranet/settings/init.py

Brief description of rationale

CsrfExemptSessionAuthentication overrides enforce_csrf() to be a no-op, meaning any session-authenticated API request bypasses CSRF protection. a malicious webpage could make API calls on behalf of a logged-in user by exploiting their session cookie. replacing with DRF's built in SessionAuthentication enforces CSRF validation which fixes this vulnerability.

Copilot AI review requested due to automatic review settings April 9, 2026 23:20
@snowbird91 snowbird91 requested a review from a team as a code owner April 9, 2026 23:20
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a CSRF vulnerability in the REST API by removing a custom session authentication class that disabled CSRF checks and switching to DRF’s built-in SessionAuthentication, which enforces CSRF validation for session-authenticated requests.

Changes:

  • Removed CsrfExemptSessionAuthentication (which made CSRF enforcement a no-op) from the API authentication module.
  • Updated DRF DEFAULT_AUTHENTICATION_CLASSES to use rest_framework.authentication.SessionAuthentication instead of the CSRF-exempt custom class.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
intranet/settings/init.py Switches DRF default session auth to built-in SessionAuthentication to enforce CSRF on session-based API requests.
intranet/apps/api/authentication.py Removes the CSRF-exempt session authentication implementation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 616 to 620
"DEFAULT_AUTHENTICATION_CLASSES": (
# "intranet.apps.api.authentication.ApiBasicAuthentication", # Disabled for security
"intranet.apps.api.authentication.CsrfExemptSessionAuthentication", # exempts CSRF checking on API
"rest_framework.authentication.SessionAuthentication",
"oauth2_provider.contrib.rest_framework.OAuth2Authentication",
),
@snowbird91 snowbird91 force-pushed the fix/enforce-csrf-on-api branch from 8c349a7 to e5a1584 Compare April 10, 2026 00:00
@snowbird91 snowbird91 force-pushed the fix/enforce-csrf-on-api branch from e5a1584 to 1367e11 Compare April 10, 2026 00:10
@coveralls
Copy link
Copy Markdown

Coverage Status

coverage: 79.214% (-0.05%) from 79.264% — snowbird91:fix/enforce-csrf-on-api into tjcsl:dev

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants