bur*_*ner 4 node.js csrf-protection express angularjs
我正在将AngularJS整合到hackathon-starter中.这是我在这里用以下test.html和test.controller.js 提到的
<div>
The record: {{record}}
</div>
<div align="right">
<button class="btn btn-lg btn-primary" ng-click="createRecord()" onclick="window.location.href='/order/shipping'">
<i class=""> Create record</i>
</button>
</div>
Run Code Online (Sandbox Code Playgroud)
test.controller.js
(function () {
'use strict';
var injectParams = ['$scope', '$location', '$http'];
function TestController($scope, $location, $http) {
$scope.record = {
interId: 1,
sku: '107k',
category: 'Useful'
};
function createRecord(record) {
return $http.post('/order/create', record).then(function (response) {
return response.data;
})
}
$scope.createRecord = function () {
var record = $scope.record;
createRecord(record)
.then(function (data) {
if (data.success) {
return $location.url('/shipping');
}
alert('Something wrong...');
});
}
};
TestController.$inject = injectParams;
angular.module('miniApp')
.controller('TestController', TestController);
}());
Run Code Online (Sandbox Code Playgroud)
如果csrf的值设置为false,它可以工作,如:
app.use(lusca({
csrf: false,
xframe: 'SAMEORIGIN',
xssProtection: true }));
Run Code Online (Sandbox Code Playgroud)
当csrf的值设置为true时,会出现错误: 错误:缺少CSRF令牌
解决此问题的一个选项是在lusca配置之前请求'/ order/create'路径,例如:
app.post('/order/create', passportConf.isAuthenticated, orderController.postCreateOrder);
app.use(lusca({
csrf: true,
Run Code Online (Sandbox Code Playgroud)
...
但这个解决方案不太优雅.
另一种选择是使用 CSRF中间件内的正则表达式将动态URL列入白名单.我试过这种方法,但我缺乏经验,如何正确地做到这一点.如何用白名单解决这个问题(具体例子)?
我可能错了,但应该可以在test.controller.js中传递csrf.怎么做我不知道.所以,如果有人提供具体的例子,那就太好了.
白名单的解决方案将被排除,因为我无法弄清楚如何使其工作.
据我所知lusca,内置的CSRF白名单并不容易配置,也没有hackathon-starter.从链接文章的措辞方式来看,听起来他们希望您自己在自己的自定义中间件中进行白名单.要做到这一点,我认为您可能需要放弃您的app.use(lusca({}))呼叫,而不是app.use()单独使用每个lusca中间件,如下所示:
var csrfMiddleware = lusca.csrf();
app.use(function(req, res, next) {
// Paths that start with /foo don't need CSRF
if (/^\/foo/.test(req.originalUrl)) {
next();
} else {
csrfMiddleware(req, res, next);
}
});
app.use(lusca.csp({ /* ... */}));
app.use(lusca.xframe('SAMEORIGIN'));
app.use(lusca.p3p('ABCDEF'));
app.use(lusca.hsts({ maxAge: 31536000 }));
app.use(lusca.xssProtection(true));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4235 次 |
| 最近记录: |