如何在用户未登录时使用 AJAX 和 CakePHP 3 检查 CSRF 令牌?
How to check CSRF token using AJAX and CakePHP 3 when user is not logged in?
所以我让我的网站上线了,我正在进入 public 人们并不总是友善的领域。我刚开始学习 CSRF,当我让我的 cakephp 3 网站上线时,我发现它是我需要的东西。 As seen here!
我将 csrf 组件和安全组件添加到我的网站,但我有 1 个主要问题。现在,当用户想要注册时,他们不能。我使用 stripe 的自定义表单发送付款,但也使用 ajax 添加用户到我的数据库。首先添加用户,然后处理付款并将订单保存到数据库。
根据 stripe docs,我在单击提交按钮后将令牌以隐藏值的形式添加到表单中,但不禁注意到我的新安全性不允许这种情况发生。
由于我使用 ajax 将 post 数据发送到我的用户控制器并在提交时添加表单输入,
如何检查 csrf 令牌并确保没有安全漏洞而不禁用相关操作的安全性?
将不胜感激如何完成此操作的示例,因为在 cakephp 3 中似乎缺少执行此操作的示例。我也很难弄清楚一切是如何工作的,因为 cakephp 3 automagic 添加了表单和 cookie 的令牌。我不确定 how/where/what 检查一下。
According to stripe docs I add the token in a hidden value to the form after I click the submit button and can't help but notice that my new security is not allowing this to happen.
当发布到另一个站点时,Cake 的 CSRF 令牌将无效。
Since I am using ajax to send the post data to my users controller and adding a form input on submit,
How do I check the csrf token and make sure there isn't a security leak without disabling the security for the actions involved?
CSRF 令牌在名为 csrfToken
的 cookie 中可用,因此请在您的 javascript 中读取该令牌并为您的 AJAX 请求设置 X-CSRF-Token
header . CsrfCompoment
将进行检查。
对于传递 X-CSRF-Token
,在您的 Ajax 请求中使用 beforeSend
参数,并定义 csrfToken
cookie 的值。
$.ajax({
url: '/foo/bar',
type: 'POST',
dataType: 'HTML',
data: data,
beforeSend: function(xhr){
xhr.setRequestHeader('X-CSRF-Token', csrfToken);
},
})
.done(function(data) {
alert('done !');
});
using js function:
function getCookie(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
}
...
then
$.ajax
({
type: "Post",
url: "URL_HERE",
data: {some_data},
beforeSend: function(xhr){
xhr.setRequestHeader('X-CSRF-Token', getCookie('csrfToken'));
},
success: function (e) {
},
errors: function () {
}
});
所以我让我的网站上线了,我正在进入 public 人们并不总是友善的领域。我刚开始学习 CSRF,当我让我的 cakephp 3 网站上线时,我发现它是我需要的东西。 As seen here!
我将 csrf 组件和安全组件添加到我的网站,但我有 1 个主要问题。现在,当用户想要注册时,他们不能。我使用 stripe 的自定义表单发送付款,但也使用 ajax 添加用户到我的数据库。首先添加用户,然后处理付款并将订单保存到数据库。
根据 stripe docs,我在单击提交按钮后将令牌以隐藏值的形式添加到表单中,但不禁注意到我的新安全性不允许这种情况发生。
由于我使用 ajax 将 post 数据发送到我的用户控制器并在提交时添加表单输入,
如何检查 csrf 令牌并确保没有安全漏洞而不禁用相关操作的安全性?
将不胜感激如何完成此操作的示例,因为在 cakephp 3 中似乎缺少执行此操作的示例。我也很难弄清楚一切是如何工作的,因为 cakephp 3 automagic 添加了表单和 cookie 的令牌。我不确定 how/where/what 检查一下。
According to stripe docs I add the token in a hidden value to the form after I click the submit button and can't help but notice that my new security is not allowing this to happen.
当发布到另一个站点时,Cake 的 CSRF 令牌将无效。
Since I am using ajax to send the post data to my users controller and adding a form input on submit,
How do I check the csrf token and make sure there isn't a security leak without disabling the security for the actions involved?
CSRF 令牌在名为 csrfToken
的 cookie 中可用,因此请在您的 javascript 中读取该令牌并为您的 AJAX 请求设置 X-CSRF-Token
header . CsrfCompoment
将进行检查。
对于传递 X-CSRF-Token
,在您的 Ajax 请求中使用 beforeSend
参数,并定义 csrfToken
cookie 的值。
$.ajax({
url: '/foo/bar',
type: 'POST',
dataType: 'HTML',
data: data,
beforeSend: function(xhr){
xhr.setRequestHeader('X-CSRF-Token', csrfToken);
},
})
.done(function(data) {
alert('done !');
});
using js function:
function getCookie(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
}
...
then
$.ajax
({
type: "Post",
url: "URL_HERE",
data: {some_data},
beforeSend: function(xhr){
xhr.setRequestHeader('X-CSRF-Token', getCookie('csrfToken'));
},
success: function (e) {
},
errors: function () {
}
});