免费制作logo的软件有哪些seo标题关键词优化
如果你的网站中允许匿名用户通过POST方式提交表单, 比如用户注册表, 评论表或者留下用户联系方式的表单,你一定要防止机器人或爬虫程序恶意提交大量的垃圾数据到你的数据库中。这种情况不是可能会发生,而是一定会发生。一种解决这种问题的方式就是在表单中加入人机交互验证码(CAPTCHA), 另一种方式就是在表单中加入honeypot隐藏字段,然后在视图中对隐藏字段的值进行验证。两种验证方式的目的都是一样,防止机器人或程序通过伪装成人来提交数据。今天我们就来详细介绍下如何在表单中添加honeypot增加安全性。
Honeypot的工作原理
Honeypot又名蜜罐,其实本质上是种陷阱。我们在表单中故意通过CSS隐藏一些字段, 这些字段一般人是不可见的。然而机器人或程序会以为这些字段也是必需的字段(required), 所以会补全后提交表单,这就中了我们的陷阱。在视图中我们可以通过装饰器对用户提交的表单数据进行判断,来验证表单的合法性。比如honeypot字段本来应该为空的,现在居然有内容了,显然这是机器人或程序提交的数据,我们可以拒绝其请求。
Django中如何实现表单honeypot验证?
Django表单中添加honeypot,一共分两步:
1. 编写模板标签(templatetags),在包含模板的表单中生成honeypot字段。
2. 编写装饰器(decorators.py), 对POST请求发送来的表单数据进行验证。
由于honeypot的功能所有app都可以用到,我们创建了一个叫common的app。整个项目的目录结构如下所示。只有标蓝色的4个文件,是与honeypot相关的。
编写模板标签
我们在common目录下新建templatetags目录(包含一个空的__init__.py),然后在新建common_tags_filters.py, 添加如下代码。
from django import templatefrom django.conf import settingsfrom django.template.defaultfilters import stringfilter
register = template.Library()# used to render honeypot field@register.inclusion_tag('common/snippets/honeypot_field.html')def render_honeypot_field(field_name=None):"""
Renders honeypot field named field_name (defaults to HONEYPOT_FIELD_NAME).
"""if not field_name:
field_name = getattr(settings, 'HONEYPOT_FIELD_NAME', 'name1')
value = getattr(settings, 'HONEYPOT_VALUE', '')if callable(value):
value = value()return {'fieldname': field_name, 'value': value}
我们现在来看下上面这段代码如何工作的。我们创建了一个名为render_honeypot_field的模板标签,用于在模板中生成honeypot字段。honeypot字段名是settings.py里HONEYPOT_FIELD_NAME,如果没有此项设置,默认值为name1。honeypot字段的默认值是HONEYPOT_VALUE, 如果没有此项设置,默认值为空字符串''。然后这个函数将fieldname和value传递给如下模板片段。
# common/snippets/honeypot_field.html
class="form-control" style="display: none;">type="text" name="{{ fieldname }}" value="{{ value }}" />
在Django模板的表单中生成honeypot字段只需按如下操作:
{% load common_tags_filters %}
{% load static %}method="post" action="">{% csrf_token %}
{% render_honeypot_field %}
{% form.as_p %}
相关阅读
Django基础(16): 模板标签(tags)的分类及如何自定义模板标签
编写装饰器
在common文件下新建decorators.py, 添加如下代码。我们编写了check_honeypot和honeypot_exempt两个装饰器,前者给需要对honeypot字段进行验证的视图函数使用,后者给不需要对honeypot字段进行验证的视图函数使用。
#common/decorators.py
from functools import wrapsfrom django.conf import settingsfrom django.http import HttpResponseBadRequest, HttpResponseForbidden, HttpResponseRedirectfrom django.template.loader import render_to_stringfrom django.contrib.auth.decorators import user_passes_testdef honeypot_equals(val):"""
Default verifier used if HONEYPOT_VERIFIER is not specified.
Ensures val == HONEYPOT_VALUE or HONEYPOT_VALUE() if it's a callable.
"""expected = getattr(settings, 'HONEYPOT_VALUE', '')if callable(expected):
expected = expected()return val == expecteddef verify_honeypot_value(request, field_name):"""
Verify that request.POST[field_name] is a valid honeypot.
Ensures that the field exists and passes verification according to
HONEYPOT_VERIFIER.
"""verifier = getattr(settings, 'HONEYPOT_VERIFIER', honeypot_equals)if request.method == 'POST':
field = field_name or settings.HONEYPOT_FIELD_NAMEif field not in request.POST or not verifier(request.POST[field]):
response = render_to_string('common/snippets/honeypot_error.html',{'fieldname': field})return HttpResponseBadRequest(response)def check_honeypot(func=None, field_name=None):"""
Check request.POST for valid honeypot field.
Takes an optional field_name that defaults to HONEYPOT_FIELD_NAME if
not specified.
"""# hack to reverse arguments if called with str paramif isinstance(func, str):
func, field_name = field_name, funcdef wrapper(func):@wraps(func)def inner(request, *args, **kwargs):
response = verify_honeypot_value(request, field_name)if response:return responseelse:return func(request, *args, **kwargs)return innerif func is None:def decorator(func):return wrapper(func)return decoratorreturn wrapper(func)def honeypot_exempt(func):"""
Mark view as exempt from honeypot validation
"""# borrowing liberally from django's csrf_exempt@wraps(func)def wrapper(*args, **kwargs):return func(*args, **kwargs)
wrapper.honeypot_exempt = True
return wrapper
上面代码最重要的就是verify_honeypot_value函数了。如果用户通过POST方式提交的表单里没有honeypot字段或该字段的值不等于settings.py中的默认值,则验证失败并返回如下错误:
# common/snippets/honeypot_error.html
/span>html>lang="en">
400 Bad POST RequestWe have detected a suspicious request. Your request is aborted.
定义好装饰器后,我们对需要处理POST表单的视图函数加上@check_honeypot就行了,是不是很简单?
from common.decorators import check_honeypot@check_honeypotdef signup(request):if request.method == "POST":
form = SignUpForm(request.POST)if form.is_valid():
user = form.save()
login(request, user)return HttpResponseRedirect(reverse('users:profile'))else:
form = SignUpForm()return render(request, "users/signup.html", {"form": form, })
相关阅读
一文看懂Python系列之装饰器(decorator)(工作面试必读)
Django基础(26): 常用装饰器应用场景及正确使用方法
参考
本文核心代码参考了James Sturk的Django-honeypot项目。原项目地址如下所示:
https://github.com/jamesturk/django-honeypot/
大江狗
2020.9.24
喜欢我们的公众号就留言或点个赞吧?