Django jQuery ajax 发布到 https 失败

标签 django jquery google-chrome-extension tastypie

我正在编写一个 chrome 扩展,它使用 GET 和 POST ajax 调用对 Django 和 Tastypie 制作的 api。

GET ajax 调用成功,我可以访问数据。然而,POST 调用仅在生产中失败,因为 api 是通过 https://托管的,在本地环境中它可以工作 (htt://localhost:8000)。

我在 header 中提供了正确的CSRF token 。 看来错误发生在这里:https://github.com/toastdriven/django-tastypie/blob/master/tastypie/authentication.py#L259其中 API 需要 Referer header 来检查调用是否安全。

仅当 header 名称以 X- 开头时,似乎无法直接在我的 ajax header 中设置 Referer 值。

预先感谢您提供解决此问题的任何解决方案或提示。

马克西姆。

最佳答案

original source of that method 中解释了该检查的原因。 :

# Suppose user visits http://example.com/
# An active network attacker (man-in-the-middle, MITM) sends a
# POST form that targets https://example.com/detonate-bomb/ and
# submits it via JavaScript.
#
# The attacker will need to provide a CSRF cookie and token, but
# that's no problem for a MITM and the session-independent
# nonce we're using. So the MITM can circumvent the CSRF
# protection. This is true for any HTTP connection, but anyone
# using HTTPS expects better! For this reason, for
# https://example.com/ we need additional protection that treats
# http://example.com/ as completely untrusted. Under HTTPS,
# Barth et al. found that the Referer header is missing for
# same-domain requests in only about 0.2% of cases or less, so
# we can use strict Referer checking.

因此,您可能会也可能不会从推荐人检查中受益 - 由您决定。

如果您想覆盖它,只需将模型设置为使用 SessionAuthentication 的子类进行身份验证,并覆盖 is_authenticated(self, request, **kwargs) 函数满足您的需求。原始方法非常简洁,所以说实话,我只是复制粘贴它并删除有问题的 if request.is_secure(): block ,而不是欺骗父类(super class)认为该请求具有引用者。

关于Django jQuery ajax 发布到 https 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18198893/

相关文章:

javascript - 语义 UI 将普通侧边栏最小化为图标侧边栏

javascript - 我想编写一个重新排序用户界面?怎么做?

javascript - 如何阻止 chrome 减慢控制台/扩展发送的请求?

javascript - BrowserExtension webRequest.onBeforeRequest 返回 promise

javascript - 什么是 "chrome-extension://"

django - 如何在 Django model.object.values() 中对列字段进行排序

django - 如何将通用字典数据传递到 django 中的每个页面

python - Django:如何缓存一个函数

Django 模板 : Comparing current url with {% url xyz %}

php - 多个列表框值未使用 PHP 插入 MySQL 数据库