Skip to content

Commit

Permalink
Adding CSRF Validation for schemas (ansible#15027)
Browse files Browse the repository at this point in the history
* Adding CSRF Validation for schemas

* Changing retrieve of scheme to avoid importing new library

* check if CSRF_TRUSTED_ORIGINS exists before accessing it

---------

Signed-off-by: Bruno Sanchez <[email protected]>
  • Loading branch information
brsanche authored Apr 24, 2024
1 parent f5f8566 commit 7dc7754
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
25 changes: 25 additions & 0 deletions awx/main/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging

# Django
from django.core.checks import Error
from django.utils.translation import gettext_lazy as _

# Django REST Framework
Expand Down Expand Up @@ -954,3 +955,27 @@ def logging_validate(serializer, attrs):


register_validate('logging', logging_validate)


def csrf_trusted_origins_validate(serializer, attrs):
if not serializer.instance or not hasattr(serializer.instance, 'CSRF_TRUSTED_ORIGINS'):
return attrs
if 'CSRF_TRUSTED_ORIGINS' not in attrs:
return attrs
errors = []
for origin in attrs['CSRF_TRUSTED_ORIGINS']:
if "://" not in origin:
errors.append(
Error(
"As of Django 4.0, the values in the CSRF_TRUSTED_ORIGINS "
"setting must start with a scheme (usually http:// or "
"https://) but found %s. See the release notes for details." % origin,
)
)
if errors:
error_messages = [error.msg for error in errors]
raise serializers.ValidationError(_('\n'.join(error_messages)))
return attrs


register_validate('system', csrf_trusted_origins_validate)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class Migration(migrations.Migration):

dependencies = [
('main', '0189_inbound_hop_nodes'),
]
Expand Down

0 comments on commit 7dc7754

Please sign in to comment.