{"id":110,"date":"2014-05-30T10:10:55","date_gmt":"2014-05-30T01:10:55","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2014\/05\/30\/express_angularjs_csrf"},"modified":"2022-10-26T09:46:16","modified_gmt":"2022-10-26T00:46:16","slug":"express_angularjs_csrf","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2014\/05\/30\/express_angularjs_csrf\/","title":{"rendered":"express + AngularJS \u3067CSRF\u306e\u8a2d\u5b9a"},"content":{"rendered":"
\u6700\u8fd1\u30ab\u30c3\u30d7\u30e9\u30fc\u30e1\u30f3\u306b\u7d0d\u8c46\u3092\u5165\u308c\u308b\u306e\u304c\u597d\u304d\u3067\u3001\u30ab\u30c3\u30d7\u30e9\u30fc\u30e1\u30f3\u306b\u7d0d\u8c463\u30d1\u30c3\u30af\u3068\u304b\u30c9\u30d0\u3063\u3068\u5165\u308c\u3066\u3001\u305d\u3053\u306b\u5375\u3092\u5165\u308c\u3066\u98df\u3079\u305f\u308a\u3059\u308b\u3002
\u898b\u305f\u76ee\u306f\u60aa\u3044\u3051\u3069\u610f\u5916\u3068\u3044\u3051\u308b\u306e\u3067\u3001\u8208\u5473\u3042\u308b\u65b9\u306f\u304a\u8a66\u3057\u3042\u308c\u3002<\/p>\n
\u3068\u3053\u308d\u3067\u6700\u8fd1\u306f Node.js<\/a> \u3068\u304b express<\/a> \u3068\u304b mongodb<\/a> \u3068\u304b AngularJS<\/a> \u3092\u52c9\u5f37\u4e2d\u3002 AngularJS \u3067\u30c7\u30fc\u30bf\u66f4\u65b0\u3092\u3057\u3088\u3046\u3068\u3057\u305f\u3068\u3053\u308d\u3001express \u5074\u3067\u4e0b\u8a18\u306e\u3088\u3046\u306a\u30a8\u30e9\u30fc\u304c\u51fa\u305f\u3002<\/p>\n CSRF<\/a> \u306e\u30a8\u30e9\u30fc\u3002 \u3053\u3093\u306a\u611f\u3058\u3067\u5024\u304c\u5165\u3063\u3066\u3044\u305f\u306a\u30fc\u3068\u3002<\/p>\n AngularJS \u3067\u306f\u305d\u3046\u3044\u3046\u8a2d\u5b9a\u3057\u3066\u306a\u3044\u3057\u3001\u30a8\u30e9\u30fc\u304c\u51fa\u308b\u306e\u3082\u7d0d\u5f97\u3002 \u307e\u305a\u3001CSRF middleware \u3092\u8ffd\u52a0\u3002 Cross Site Request Forgery (XSRF) Protection<\/a><\/p>\n Angular provides a mechanism to counter XSRF. When performing XHR requests, the $http service reads a token from a cookie (by default, XSRF-TOKEN) and sets it as an HTTP header (X-XSRF-TOKEN). <\/p>\n<\/blockquote>\n AngularJS \u306e $http \u30b5\u30fc\u30d3\u30b9\u3067\u306f\u3001cookie \u306e XSRF-TOKEN \u3092\u8aad\u3093\u3067\u3001HTTP \u30d8\u30c3\u30c0\u30fc\u306b X-XSRF-TOKEN \u3068\u3057\u3066\u30bb\u30c3\u30c8\u3059\u308b\u306e\u3067\u3001express \u5074\u3067 cookie \u306e XSRF-TOKEN \u3068\u3057\u3066\u5024\u3092\u5165\u308c\u3066\u3084\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<\/p>\n express 3.x docs - csrf()<\/a><\/p>\n The default value function checks req.body generated by the bodyParser() middleware, req.query generated by query(), and the "X-CSRF-Token" header field. <\/p>\n<\/blockquote>\n CSRF middleware \u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u306f\u3001req.body \u3084 req.query \u3084\u30d8\u30c3\u30c0\u30fc\u306e X-CSRF-Token \u3092\u898b\u306b\u884c\u304f\u3002 *1 express \u306e 4.x \u3067\u306f\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u3067 x-xsrf-token \u3082\u898b\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u3066\u3001\u3053\u306e\u8a2d\u5b9a\u304c\u5fc5\u8981\u306a\u3044\u3088\u3046\u306a\u306e\u3067\u6ce8\u610f\u3002 The default function checks four possible token locations: \u3068\u3001\u3053\u3053\u307e\u3067\u66f8\u3044\u3066\u5b9f\u969b\u306b\u30c7\u30e2\u30a2\u30d7\u30ea\u3067\u4f7f\u3063\u3066\u3044\u308b csrf.js \u3092\u898b\u3066\u307f\u305f\u3089\u3061\u3083\u3093\u3068<\/p>\n \u304c\u5165\u3063\u3066\u3044\u305f\uff01
\u3053\u3061\u3089<\/a> \u306e\u30c7\u30e2\u30a2\u30d7\u30ea\u3092\u30d9\u30fc\u30b9\u306b\u3044\u308d\u3044\u308d\u3068\u3044\u3058\u3063\u3066\u5909\u66f4\u3057\u306a\u304c\u3089\u3001\u6319\u52d5\u3092\u78ba\u8a8d\u3057\u3064\u3064\u52c9\u5f37\u3057\u3066\u308b\u3051\u3069\u3001\u3069\u308c\u3082\u52c9\u5f37\u3057\u59cb\u3081\u3067\u3064\u307e\u305a\u304f\u3053\u3068\u304c\u591a\u3044\u3002
\u4eca\u56de\u306f\u3001AngularJS \u3067\u30c7\u30fc\u30bf\u306e\u66f4\u65b0\u3092\u3057\u3088\u3046\u3068\u3057\u305f\u3068\u304d\u306b\u3001express \u5074\u3067 CSRF \u30a8\u30e9\u30fc\u304c\u51fa\u305f\u306e\u3067\u3001\u305d\u306e\u5185\u5bb9\u3068\u5bfe\u51e6\u65b9\u6cd5\u3092\u307e\u3068\u3081\u3066\u307f\u305f\u3002<\/p>\nCSRF \u30a8\u30e9\u30fc<\/h2>\n
Error: Forbidden\n at Object.exports.error (\/home\/nodejs\/em_test\/node_modules\/express\/node_modules\/connect\/lib\/utils.js:63:13)\n at createToken (\/home\/nodejs\/em_test\/node_modules\/express\/node_modules\/connect\/lib\/middleware\/csrf.js:82:55)\n at Object.handle (\/home\/nodejs\/em_test\/node_modules\/express\/node_modules\/connect\/lib\/middleware\/csrf.js:48:24)\n at next (\/home\/nodejs\/em_test\/node_modules\/express\/node_modules\/connect\/lib\/proto.js:193:15)\n at Object.handle (\/home\/nodejs\/em_test\/node_modules\/view-helpers\/index.js:65:5)\n at next (\/home\/nodejs\/em_test\/node_modules\/express\/node_modules\/connect\/lib\/proto.js:193:15)\n at Object.handle (\/home\/nodejs\/em_test\/node_modules\/connect-flash\/lib\/flash.js:21:5)\n at next (\/home\/nodejs\/em_test\/node_modules\/express\/node_modules\/connect\/lib\/proto.js:193:15)\n at SessionStrategy.strategy.pass (\/home\/nodejs\/em_test\/node_modules\/passport\/lib\/middleware\/authenticate.js:314:9)\n at \/home\/nodejs\/em_test\/node_modules\/passport\/lib\/strategies\/session.js:61:12\nPUT \/admin\/expenses\/537bfa12db0460ff19812b80 500 490ms<\/code><\/pre>\n
\nexpress \u3067\u306f\u3001\u78ba\u304b\u306b CSRF \u306e\u8a2d\u5b9a\u3057\u3066\u3001\u30d5\u30a9\u30fc\u30e0\u306b\u4e0b\u8a18\u306e\u884c\u3092\u66f8\u3044\u3066\u304a\u308a\u3001<\/p>\ninput(type="hidden", name="_csrf", value="#{csrf_token}")<\/code><\/pre>\n
<input type="hidden" name="_csrf" value="hSPcdgooM3t461mJRl98Zx6o9KeNLU18ncXEA="><\/code><\/pre>\n
\u3068\u3044\u3046\u3053\u3068\u3067\u3001express + AngularJS \u3067\u306e CSRF \u306e\u8a2d\u5b9a\u65b9\u6cd5\u3092\u8abf\u3079\u3066\u3084\u3063\u3066\u307f\u305f\u3002
\u53c2\u8003URL\uff1a
Using CSRF protection with Express and AngularJS<\/a>
ExpressJS, AngularJS and Cross-site request forgery (csrf) protection<\/a><\/p>\n\u5bfe\u5fdc\u65b9\u6cd5<\/h2>\n
1. express \u30a2\u30d7\u30ea\u306b CSRF middleware \u3092\u8a2d\u5b9a<\/h5>\n
\u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u6271\u3048\u306a\u3044\u3068\u30c0\u30e1\u306a\u306e\u3067\u3001session() \u306e\u5f8c\u306b\u8ffd\u52a0\u3059\u308b\u3053\u3068\u3002
\u4eca\u56de\u306e\u30c7\u30e2\u30a2\u30d7\u30ea\u306b\u306f\u65e2\u306b\u8a2d\u5b9a\u6e08\u307f\u3060\u3063\u305f\u3051\u3069\u3002<\/p>\napp.use(express.csrf());<\/code><\/pre>\n
2. AngularJS \u7528\u306b cookie \u8a2d\u5b9a<\/h5>\n
\n
app.use(function(req, res, next){\n res.locals.csrf_token = req.csrfToken();\n res.cookie('XSRF-TOKEN', req.csrfToken()); \/\/ \u3053\u306e\u884c\u3092\u8ffd\u52a0\n next();\n});<\/code><\/pre>\n
3. CSRF middleware \u306e token \u8a2d\u5b9a *1<\/h5>\n
\n
AngularJS \u306f X-XSRF-TOKEN \u3092\u4f7f\u3046\u306e\u3067\u3001express \u5074\u3067\u30d8\u30c3\u30c0\u30fc\u306e X-XSRF-TOKEN \u3092\u898b\u306b\u884c\u304f\u3088\u3046\u306b\u8a2d\u5b9a\u3057\u3066\u3084\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002
\u65e2\u5b58\u306e\u51e6\u7406\u306b\u3001Angular \u306b\u5fc5\u8981\u306a req.headers['x-xsrf-token'] \u3092\u8ffd\u52a0\u3067\u898b\u306b\u884c\u304f\u30d5\u30a1\u30f3\u30af\u30b7\u30e7\u30f3\u4f5c\u6210\u3002 <\/p>\nvar csrfValue = function(req) {\n var token = (req.body && req.body._csrf)\n || (req.query && req.query._csrf)\n || (req.headers['x-csrf-token'])\n || (req.headers['x-xsrf-token']); \/\/ \u3053\u306e\u884c\u3092\u8ffd\u52a0\u3002\n return token;\n};<\/code><\/pre>\n
\n
app.use(express.csrf({value: csrfValue}));<\/code><\/pre>\n
csrf(options)<\/a><\/p>\n\n
_csrf parameter in req.body generated by the body-parser middleware.
_csrf parameter in req.query generated by query().
x-csrf-token and x-xsrf-token header fields.<\/p>\n<\/blockquote>\n || (req.headers['x-xsrf-token']);<\/code><\/pre>\n
\u4f7f\u3063\u3066\u3044\u308b express \u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u306f\u30013.4.8\u306a\u3093\u3060\u3051\u3069\u3002
2013\u5e74\u306e7\u6708\u306b\u5bfe\u5fdc\u3055\u308c\u3066\u3044\u308b\u3063\u307d\u3044\u2026\u3002
Update csrf.js \u00b7 b236bc3 \u00b7 senchalabs\/connect<\/a> <\/p>\n