标签: django-csrf

django 网站上 https 上传未设置 CSRF Coo​​kie 或 CSRF 验证失败

和周围的很多人一样,我在使用 CSRF 和 Django 时遇到了很多问题。

这是上下文:
- 我创建了一个 https 网站,用户可以在其中上传文件
- 我使用 Django 1.4.2 创建了这个网站
- 我创建了一个应用程序 *file_manager* 可以做我想要的
- 我是仅通过管理网址使用此应用程序

如果我django.middleware.csrf.CsrfViewMiddlewareMIDDLEWARE_CLASSESmy 中禁用,则settings.py一切正常。
我可以在 Debian Squeeze 下的命令行中使用 cURL 在我的模板上上传文件,该文件命中服务器,没问题。
然而,它似乎并不安全。

所以我启用django.middleware.csrf.CsrfViewMiddleware 它不再起作用。我收到有关 CSRF 验证的各种错误。

我相信我已经消除了通常的嫌疑人(我希望至少
):{% csrf_token %}
- - RequestContext
-CsrfViewMiddlewaresettings.py

您会在下面找到该过程中涉及的所有文件(我希望):

视图.py

from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.template import Context, loader, RequestContext
from django.shortcuts import render, get_object_or_404, redirect, render_to_response
from django.core.urlresolvers import reverse …
Run Code Online (Sandbox Code Playgroud)

django django-csrf

3
推荐指数
1
解决办法
6023
查看次数

Django 1.4中的CSRF保护

我试图通过"Django Book"来学习django,我遇到了CSRF保护问题.我在这里找到了很多建议,但似乎没有一个适合我.

使用Chrome我收到消息:CSRF token missing or incorrect.
使用Internet Explorer我收到消息:CSRF cookie not set.

如果我'django.middleware.csrf.CsrfViewMiddleware'在settings.py中注释掉,一切似乎都有效(虽然当然没有任何东西被邮寄到虚假的地址.)我已经尝试csrf_protect在我的视图上放置一个装饰器,但它没有帮助.我也尝试过对这个调用进行评论,但send_mail我仍然遇到CSRF失败,显然这是导致问题的ContactForm.

(我正在使用django 1.4.1.)

我需要做什么?

views.py

from django.shortcuts import render_to_response
from django.http import HttpResponse, HttpResponseRedirect
from contact.forms import ContactForm
from django.template import RequestContext
from django.core.mail import send_mail

def contact(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            send_mail(
                cd['subject'],
                cd['message'],
                cd.get('email', 'noreply@example.com'),
                ['siteowner@example.com'],
            )
            return HttpResponseRedirect('/contact/thanks/')
    else:
        form = ContactForm()
    return render_to_response('contact_form.html', {'form': form}, context_instance=RequestContext(request)) …
Run Code Online (Sandbox Code Playgroud)

django django-csrf

3
推荐指数
1
解决办法
837
查看次数

Django - 403 Forbidden CSRF验证失败

我在Django有一个联系表格供我的网站使用,当我在本地进行测试时它工作正常,但现在当我尝试提交我的联系表单"live"时,总是会出现403 Forbidden CSRF验证失败.

视图:

def contact(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            send_mail(
                cd['subject'],
                cd['message'],
                cd.get('email', 'noreply@example.org'),
                ['example@gmail.com'],
            )
            return HttpResponseRedirect('/thanks/')
    else:
        form = ContactForm()
    return render(request, 'contact/contact.html', {'form': form})
Run Code Online (Sandbox Code Playgroud)

contact.html

{% extends 'site_base.html' %}

{% block head_title %}Contact{% endblock %}

{% block body %}

      <h2>Contact Us</h2>
      <p>To send us a message, fill out the below form.</p>

    {% if form.errors %}
        <p style="color: red;">
            Please correct the error{{ form.errors|pluralize }} below.
        </p>
    {% endif …
Run Code Online (Sandbox Code Playgroud)

python django csrf django-csrf

3
推荐指数
1
解决办法
8246
查看次数

同一域上的多个 Django 站点 - CSRF 失败

我有两个应用程序在同一域的不同端口上运行,都使用 csrf 中间件。

当我登录其中一个应用程序时,POST另一个应用程序的所有提交都会失败。
我认为是因为它们SESSION_COOKIE_DOMAIN是相同的。

我尝试更改SESSION_COOKIE_NAME,但是,两个站点上的'csrftoken'表单POST请求中都使用了 cookie,无论现在有一个具有我指定名称的新 cookie。

当我使用 AJAX 发布信息并从具有新名称的 cookie 获取 csrf 令牌时,它可以工作,但是,表单提交失败,CSRF 验证失败。

django cookies csrf django-csrf csrf-protection

3
推荐指数
1
解决办法
2040
查看次数

(Django)针对在Chrome中运行的AJAX请求的CSRF验证,而不是Firefox

正如标题所述,我的(Django)CSRF验证在Chrome中运行但不在Firefox中,我想知道为什么我可以解决这个问题.

我将其包含在我的base.html文件的head标记中,我的应用程序中的所有其他文件都从该标记扩展:

base.html,head标签的底部

    <script>
    $(document).ready(function() {

        function getCookie(name) {
            var cookieValue = null;
            if (document.cookie && document.cookie != '') {
                var cookies = document.cookie.split(';');
                for (var i = 0; i < cookies.length; i++) {
                    var cookie = jQuery.trim(cookies[i]);
                    // Does this cookie string begin with the name we want?
                    if (cookie.substring(0, name.length + 1) == (name + '=')) {
                        cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                        break;
                    }
                }
            }
            return cookieValue;
        }
        var csrftoken = getCookie('csrftoken');
        function …
Run Code Online (Sandbox Code Playgroud)

django ajax firefox jquery django-csrf

3
推荐指数
1
解决办法
1352
查看次数

Nginx + gunicorn Django 1.9 CSRF 验证失败

背景:
我正在尝试使用 django配置 cloudflare灵活SSL。
浏览器 <-HTTPS-> Cloudflare <-HTTP-> Nginx <--> Gunicorn

问题:
我的CSRF 验证失败。管理面板登录请求中止- 目前这是我网站上唯一的 POST 请求。(相信我,我在这里浏览了大量帖子和要点,但似乎没有什么对我有用:()

配置:

Nginx -

listen 80;
server_name domain.com;

real_ip_header X-Forwarded-For;
set_real_ip_from 103.21.244.0/22;
set_real_ip_from 103.22.200.0/22;
set_real_ip_from 103.31.4.0/22;
set_real_ip_from 104.16.0.0/12;
set_real_ip_from 108.162.192.0/18;
set_real_ip_from 131.0.72.0/22;
set_real_ip_from 141.101.64.0/18;
set_real_ip_from 162.158.0.0/15;
set_real_ip_from 172.64.0.0/13;
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 188.114.96.0/20;
set_real_ip_from 190.93.240.0/20;
set_real_ip_from 197.234.240.0/22;
set_real_ip_from 198.41.128.0/17;
set_real_ip_from 199.27.128.0/21;
set_real_ip_from 2400:cb00::/32;
set_real_ip_from 2606:4700::/32;
set_real_ip_from 2803:f800::/32;
set_real_ip_from 2405:b500::/32;
set_real_ip_from 2405:8100::/32;
real_ip_header CF-Connecting-IP;

location / {
    proxy_pass http://unix:/var/webapps/run/SBWebsite.sock;
    proxy_set_header Host $host; …
Run Code Online (Sandbox Code Playgroud)

python django nginx django-csrf

3
推荐指数
2
解决办法
3801
查看次数

Django csrf令牌如何工作?

我不清楚使用Django表单的csrf令牌。我在表单提交中拥有此文件,并且看到它是动态生成的。如果捕获我与提琴手的会话并尝试提交没有该令牌的表单,则会收到403错误。但是我不明白的是,我可以使用提琴手使用相同的令牌提交尽可能多的数据,因此我不理解此令牌的安全性。如果有人入侵您的表单,他们可以使用相同的令牌。

我是否缺少一些其他步骤来确保令牌始终是唯一的?

python django django-csrf

3
推荐指数
2
解决办法
1668
查看次数

django rest 框架 - 会话身份验证与令牌身份验证,csrf

我使用默认设置设置了 DRF。我的 ajax 客户端使用会话身份验证工作正常。我希望另一个远程服务器使用与 javascript 客户端相同的 API。

我的登录代码很简单:

class Login(APIView):
    def post(self, request, *args, **kwargs):

        user = authenticate(username=username, password=password)

        if user is None:
            return Response(status=status.HTTP_401_UNAUTHORIZED)

        login(request, user)
        # ...
Run Code Online (Sandbox Code Playgroud)

问题是当我使用来自另一台主机的客户端时,例如 python requests,我收到 CSRF 错误。根据 DRF 文档,我认为我应该改用令牌身份验证。

问题:

  1. 为什么我需要令牌认证?sessionid cookie 已经是一个令牌,为什么我不能同时用于 ajax 客户端和软件客户端?因此避免为令牌使用另一个单独的数据库表。

  2. 由于我只想使用会话身份验证,如何仅对 ajax 客户端强制执行 CSRF?

django django-csrf django-rest-framework

3
推荐指数
1
解决办法
3491
查看次数

将django.contrib.auth.views.login设置为csrf_exempt

我正在开发一个与我的Django应用程序交互的刚刚学习的iOS应用程序.

我正在登录部分:由于csrf保护,我的客户端无法登录Django应用程序.

对于其他视图我只是添加csrf_exempt装饰器来禁用它,但对于内置django.contrib.auth.views.login

python iphone django django-csrf ios

2
推荐指数
1
解决办法
2060
查看次数

Django csrf令牌

views.py

from django.core.context_processors import csrf
context.update(csrf(request))
{'csrf_token': <django.utils.functional.__proxy__ object at 0xae0f4ec>}
Run Code Online (Sandbox Code Playgroud)

我正在尝试将csrf标记添加到我的forms.im中,在上面的视图中生成csrf标记.但是csrf_token值给出了一些代理对象,如上面所示而不是string.I m使用django 1.3.提前感谢任何类型的救命.

django csrf django-csrf

2
推荐指数
1
解决办法
3461
查看次数