【发布时间】:2017-04-04 06:17:53
【问题描述】:
如何保护从 angular2 应用程序到 Express 服务器的 post 调用?
在我的 angular2 应用程序中,我有以下 HTTP 帖子。
const headers = new Headers();
headers.append('Content-Type', 'application/json');
const data = {
email: this.form.value.email
};
this.http.post('http://localhost:8080/api/user/email', data, {
headers: headers
})
现在我想确保只有我的 angular 2 应用程序可以对用户 api 进行 post 调用。我结合 Express 和 Angular 2 对 csrf 进行了一些研究。在我的 Angular 2 应用程序中,我对 app.module.ts 文件进行了以下实现。
import { HttpModule, XSRFStrategy, CookieXSRFStrategy } from '@angular/http';
providers: [ {
provide: XSRFStrategy,
useValue: new CookieXSRFStrategy('csrftoken', 'X-CSRFToken')
} ]
我认为这是将 XSRFStrategy 实施到 Angular 2 的正确方法?
对于 Express 中的实现,我遵循了一些教程,但没有任何成功。我收到的大部分时间:
ForbiddenError: invalid csrf token
如何在我的 Express api 中实现 CSRF 部分。这是我的 Express 配置:
// call the packages we need
var express = require('express'); // call express
var app = express(); // define our app using express
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var csrf = require('csurf');
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
var port = process.env.PORT || 8080; // set our port
// ROUTES FOR OUR API
// =============================================================================
var router = express.Router();
router.use(function (req, res, next) {
console.log('Something is happening.');
res.setHeader("Access-Control-Allow-Origin", "http://localhost:4200");
res.setHeader("Access-Control-Allow-Credentials", "true");
res.setHeader("Access-Control-Allow-Methods", "POST");
res.setHeader("Access-Control-Allow-Headers", "Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers");
res.setHeader('Content-Type', 'application/json');
next();
});
app.use('/api', router);
router.post('/user/email', function (req, res) {
..... [how to make sure that this post can only be fired from my angular 2 application ]
}
更新 #1
做了更多研究,在 Angular 2 文档中发现了以下内容:
//By default, Angular will look for a cookie called `'XSRF-TOKEN'`, and set
//* an HTTP request header called `'X-XSRF-TOKEN'` with the value of the cookie on each request,
所以在我的 Express 应用程序中,我添加了以下部分:
const cookieOptions = {
key: 'XSRF-TOKEN',
secure: false,
httpOnly: false,
maxAge: 3600000
}
var csrfProtection = csrf({
cookie: cookieOptions
})
在发布路线中,我实施了如下保护:
router.post('/user/email', csrfProtection, function (req, res) {
console.log('post incomming');
}):
我收到了以下响应标头
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers
Access-Control-Allow-Methods:POST
Access-Control-Allow-Origin:http://localhost:4200
Connection:keep-alive
Content-Length:1167
Content-Type:text/html; charset=utf-8
Date:Mon, 21 Nov 2016 20:07:12 GMT
set-cookie:XSRF-TOKEN=O4JKkjAZRik2H7ml0DoxDc8s; Max-Age=3600000; Path=/
X-Content-Type-Options:nosniff
X-Powered-By:Express
以及请求头:
Accept:*/*
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8,nl;q=0.6
Connection:keep-alive
Content-Length:38
content-type:application/json
Host:localhost:8080
Origin:http://localhost:4200
Referer:http://localhost:4200/profile/users
【问题讨论】:
-
你能从express server端的浏览器中找到一个名字类似于csrf的cookie吗?
-
你好祖拜尔。我用收到的回复更新了我的问题。
标签: javascript security express angular csrf