• Stars
    star
    360
  • Rank 117,463 (Top 3 %)
  • Language
    Python
  • License
    BSD 2-Clause "Sim...
  • Created over 15 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🍯 Generic honeypot utilities for use in django projects.

django-honeypot

Django application that provides utilities for preventing automated form spam.

Provides template tags, view decorators, and middleware to add and verify honeypot fields to forms.

Written by James Turk with contributions by Flavio Curella and Daniel Greenfeld.

Source: https://github.com/jamesturk/django-honeypot/

Requirements

  • python >= 3.7
  • django >= 2.2

(django-honeypot 0.7 supports Django 1.11 and Python 2.7)

Usage

settings.py

Be sure to add honeypot to INSTALLED_APPS in settings.py.

You will almost always need to define HONEYPOT_FIELD_NAME which is the name to use for the honeypot field. Some sophisticated bots will attempt to avoid fields named honeypot, so it may be wise to name the field something slightly more realistic such as "phonenumber" or "body2".

HONEYPOT_VALUE is an option that you can specify to populate the honeypot field, by default the honeypot field will be empty and any text entered into it will result in a failed POST. HONEYPOT_VALUE can be a string or a callable that takes no arguments.

HONEYPOT_VERIFIER is an advanced option that you can specify to validate the honeypot. The default verifier ensures that the contents of the honeypot field matches HONEYPOT_VALUE. Using a combination of a callable for HONEYPOT_VALUE and HONEYPOT_VERIFIER it is possible to implement a more advanced technique such as using timestamps.

Adding honeypot fields to specific forms and views

It is possible to add honeypot fields to specific forms and ensure that specific views check for a valid honeypotin request.POST. This can be accomplished by using the render_honeypot_field template tag:

At the top of a template file include the line:

{% load honeypot %}

And then within any form including the tag:

{% render_honeypot_field "field_name" %}

will render a honeypot field named "field_name" that is hidden by default. The name of the honeypot field will default to HONEYPOT_FIELD_NAME if one is not provided.

To ensure that the honeypot field is both present and correct you will need to use check_honeypot decorator from honeypot.decorators:

from honeypot.decorators import check_honeypot

@check_honeypot(field_name='hp_field_name')
def post_comment(request):
    ...

@check_honeypot
def other_post_view(request):
    ...

This decorator will ensure that a field exists in request.POST that is named 'field_name'. @check_honeypot without arguments will use the default HONEYPOT_FIELD_NAME.

Adding honeypot fields to class-based-views

The same as above for Adding honeypot fields to specific forms and views but add the decorator to the post method making use of django's method_decorator.

from django.utils.decorators import method_decorator
from honeypot.decorators import check_honeypot

@method_decorator(check_honeypot, name='post')
class MyView(FormView):
    ...

Adding honeypot fields site-wide

Sometimes it is desirable to add honeypots to all forms site-wide. This is particularly useful when dealing with apps that render their own forms. For this purpose three middlewares are provided, similar in functionality to django's own CSRF middleware.

All of these middleware live in honeypot.middleware.

HoneypotResponseMiddleware analyzes the output of all responses and rewrites any forms that use method="POST" to contain a honeypot field, just as if they had started with {% render_honeypot_field %}. Borrowing heavily from django.contrib.csrf.middleware.CsrfResponseMiddleware this middleware only rewrites responses with Content-Type text/html or application/xhtml+xml.

HoneypotViewMiddleware ensures that for all incoming POST requests to views request.POST contains a valid honeypot field as defined by the HONEYPOT_FIELD_NAME, HONEYPOT_VALUE, and HONEYPOT_VERIFIER settings. The result is the same as if every view in your project were decorated with @check_honeypot.

HoneypotMiddleware is a combined middleware that applies both HoneypotResponseMiddleware and HoneypotViewMiddleware, this is the easiest way to get honeypot fields site-wide and can be used in many if not most cases.

Customizing honeypot display

There are two templates used by django-honeypot that can be used to control various aspects of how the honeypot functionality is presented to the user.

honeypot/honeypot_field.html is used to render the honeypot field. It is given two context variables fieldname and value, corresponding to HONEYPOT_FIELD_NAME and HONEYPOT_VALUE or any overrides in effect (such as a custom field name passed to the template tag).

honeypot/honeypot_error.html is the error page rendered when a bad request is intercepted. It is given the context variable fieldname representing the name of the honeypot field.

More Repositories

1

jellyfish

πŸͺΌ a python library for doing approximate and phonetic matching of strings.
Jupyter Notebook
2,025
star
2

scrapeghost

πŸ‘» Experimental library for scraping websites using OpenAI's GPT API.
Python
1,421
star
3

spatula

A modern Python library for writing maintainable web scrapers.
Python
241
star
4

scrapelib

⛏ a library for scraping unreliable pages
Python
206
star
5

django-markupfield

πŸ“‘ a MarkupField for Django
Python
194
star
6

django-brainstorm

❌ deprecated brainstorm idea voting app
Python
58
star
7

django-layar

❌ deprecated helper for publishing data to Layar augmented reality browser from Django
Python
34
star
8

saucebrush

experiment in writing a simple data processing toolkit in python
Python
18
star
9

glftfont

πŸ”‘ simple library/example for using Freetype fonts within OpenGL
C++
16
star
10

cjellyfish

🎐 C implementations of Jellyfish's algorithms [deprecated]
C
14
star
11

django-markupwiki

❌ deprecated version of a simple django wiki based on django-markupfield
Python
10
star
12

polipoly

❌ deprecated simple library for dealing with political boundaries as defined by census.gov shapefiles
Python
9
star
13

mongoprof

πŸ•΅ command line mongo profiling utility
Python
6
star
14

oyster

❌ deprecated attempt to build proactive document cache
Python
6
star
15

jellyfish-testdata

🎐 cross-language test data for string comparison/encoding algorithms
3
star
16

gcr-cli

CLI for working with GitHub classroom repositories.
Python
3
star
17

go-jellyfish

🎐 a Go library for doing approximate and phonetic matching of strings
Go
3
star
18

graveyard

⚰ pieces of code that accumulate along the way
Python
2
star
19

dotfiles

βš™
Shell
2
star
20

ansible-django-uwsgi-nginx

simple django-uwsgi-nginx ansible role
2
star
21

django-simplekeys

πŸ”‘ simple but flexible API keys
Python
1
star
22

scad-designs

OpenSCAD
1
star
23

slack-render

render slack backups as static HTML
JavaScript
1
star
24

cookiecutters

template for creating a python package to my liking
CSS
1
star
25

photon

❌ obsolete ctypes+SDL experiment
Python
1
star
26

cpp_photon

❌ obsolete C++ API for development of OpenGL accelerated applications/games
1
star
27

rust-jellyfish

🎐 a Rust library for doing approximate and phonetic matching of strings, based on Python library of the same name
Rust
1
star
28

zengine-gewi

❌ deprecated GUI library written to use ZEngine
1
star
29

zengine

❌ obsolete 2D game API using OpenGL for fast 2D drawing and SDL for everything else
1
star
30

tripod-lambda

really lightweight scaffolding for AWS Lambda
Python
1
star
31

python-disqus

❌ obsolete python client library for Disqus 1.1 API
Python
1
star