django-csrf-protect-form

Enable CSRF protection only for HTML forms


Keywords
django html view csrf protect
License
Other
Install
pip install django-csrf-protect-form==0.1.0

Documentation

https://travis-ci.org/dex4er/django-csrf-protect-form.svg?branch=master https://readthedocs.org/projects/django-csrf-protect-form/badge/?version=latest

django-csrf-protect-form

The CSRF middleware and template tag from Django framework provides easy-to-use protection against Cross Site Request Forgeries. This protector has some inconveniences for XHR POST requests.

This module enables CSRF protection only for HTML forms when content type of the request is one of the following:

  • application/x-www-form-urlencoded
  • multipart/form-data
  • text/plain

It is generally safe to exclude XHR requests from CSRF protection, because XHR requests can only be made from the same origin. Check your CORS configuration before using this module. Use django-cors-headers module to protect your site with CORS.

Installation

Install with pip or pipenv:

pip install django-csrf-protect-form

Configuration

You can set a list of content types which have CSRF protection enabled. The default value is:

CSRF_PROTECT_FORM_CONTENT_TYPE = [
  'application/x-www-form-urlencoded',
  'multipart/form-data',
  'text/plain',
]

Usage

views.py

from django_csrf_protect_form import csrf_protect_form

@csrf_protect_form
def hello(request):
    return HttpResponse("<html><head></head><body>Hello, world!</body></html>")

or:

urls.py

from django_csrf_protect_form import csrf_protect_form
from .views import hello

urlpatterns = [
    url('hello/', csrf_protect_form(hello)),
]

Documentation

See http://django-csrf-protect-form.readthedocs.org/

License

Copyright © 2019, Piotr Roszatycki

This software is distributed under the GNU Lesser General Public License (LGPL 3 or greater).