Skip to main content

Enable CSRF protection only for HTML forms

Project description

https://img.shields.io/pypi/v/django-csrf-protect-form.svg https://travis-ci.org/dex4er/django-csrf-protect-form.svg?branch=master https://readthedocs.org/projects/django-csrf-protect-form/badge/?version=latest https://img.shields.io/pypi/pyversions/django-csrf-protect-form.svg https://img.shields.io/pypi/djversions/django-csrf-protect-form.svg

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).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-csrf-protect-form-0.1.0.tar.gz (16.8 kB view hashes)

Uploaded Source

Built Distribution

django_csrf_protect_form-0.1.0-py2.py3-none-any.whl (6.0 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page