Skip to content

Commit

Permalink
[#7028] Use the actual view function to exempt request from CSRF
Browse files Browse the repository at this point in the history
The request.endpoint does not always match the actual view function, eg
organization.edit or custom_group.edit both point to group.edit, which
is what the csrf library expects in the exempt list
  • Loading branch information
amercader committed Sep 29, 2022
1 parent 724a173 commit 4e8c452
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions ckan/config/middleware/flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from logging.handlers import SMTPHandler
from typing import Any, Iterable, Optional, Union, cast

from flask import Blueprint, send_from_directory
from flask import Blueprint, send_from_directory, current_app
from flask.ctx import _AppCtxGlobals
from flask.sessions import SessionInterface
from flask_multistatic import MultiStaticFlask
Expand Down Expand Up @@ -416,7 +416,11 @@ def ckan_before_request() -> Optional[Response]:
# Disable CSRF protection if user was logged in via the Authorization
# header
if g.get("login_via_auth_header"):
csrf.exempt(f"ckan.views.{request.endpoint}")
# Get the actual view function, as it might not match the endpoint,
# eg "organization.edit" -> "group.edit", or custom dataset types
view = current_app.view_functions.get(request.endpoint)
dest = f"{view.__module__}.{view.__name__}"
csrf.exempt(dest)

# Set the csrf_field_name so we can use it in our templates
g.csrf_field_name = config.get_value("WTF_CSRF_FIELD_NAME")
Expand Down

0 comments on commit 4e8c452

Please sign in to comment.