Mur*_*cho 3 javascript csrf node.js stripe-payments sails.js
我正在设置Stripe以使用我的sails.js服务器,并且为了使用Stripe的webhooks,我需要为我提供给Stripe的URL禁用CSRF.
是否可以使某些URL免于sails.js中的CSRF POST要求?我能为CSRF找到的唯一配置是全局打开它,并查看csrf钩子的源代码(https://github.com/balderdashy/sails/blob/master/lib/hooks/csrf/index. js)看起来如果我尝试提供自定义对象,它无论如何都会被全局设置替换.
谢谢
因此,通过阅读问题中链接的csrf钩子后,我设法解决了这个问题.
自v0.11.0起:
如果您尝试在csrf.js配置文件中提供具有设置的对象,则挂钩只会使用"default on"覆盖所有设置.csrf对象最终看起来像这样
{
grantTokenViaAjax: true,
protectionEnabled: true,
origin: '-',
routesDisabled: '-'
}
Run Code Online (Sandbox Code Playgroud)
为了向对象添加路由免除,您需要在设置完成后执行此操作,因此我在config/bootstrap.js中执行此操作.所以添加路线" http://yourhost.com/webhooks/testhook/ ":
// In bootstrap.js
sails.config.csrf.routesDisabled = "/webhooks/testhook";
Run Code Online (Sandbox Code Playgroud)
如果要添加多个钩子,可以将它们添加到相同的字符串中,逗号分隔:
// In bootstrap.js
sails.config.csrf.routesDisabled = "/webhooks/testhook,/webhooks/anotherhook";
Run Code Online (Sandbox Code Playgroud)
所以Murcho的解决方案正在运行,但实际上,sails v0.11只有一个配置文件:
在config/csrf.js,激活csrf保护的行后面是这个注释块:
/****************************************************************************
* *
* You may also specify more fine-grained settings for CSRF, including the *
* domains which are allowed to request the CSRF token via AJAX. These *
* settings override the general CORS settings in your config/cors.js file. *
* *
****************************************************************************/
// module.exports.csrf = {
// grantTokenViaAjax: true,
// origin: ''
// }
Run Code Online (Sandbox Code Playgroud)
你只需要在那里添加一个配置对象来扩展默认值:
module.exports.csrf = {
"routesDisabled": "/webhooks/testhook,/webhooks/anotherhook"
}
Run Code Online (Sandbox Code Playgroud)