【发布时间】:2018-05-10 04:07:05
【问题描述】:
在我的应用程序中,我有一个来自 official docs 的代码,除了一个区别:我发送 xsrfToken 以响应 POST 请求,而不是 GET。
var cookieParser = require('cookie-parser')
var csrf = require('csurf')
var bodyParser = require('body-parser')
var express = require('express')
// setup route middlewares
var csrfProtection = csrf({ cookie: true })
var parseForm = bodyParser.urlencoded({ extended: false })
var app = express()
// we need this because "cookie" is true in csrfProtection
app.use(cookieParser())
app.post('/getCsrfToken', /*csrfProtection,*/ function (req, res) {
// check credentials from request.body
// and then
res.render('send', { csrfToken: req.csrfToken() }) //EXCEPTION: csrfToken is not a function
})
app.post('/process', parseForm, csrfProtection, function (req, res) {
res.send('data is being processed')
})
我正面临着鸡蛋母鸡的问题:如果我启用 csrfProtection,我无法在没有令牌的情况下进入端点的代码,但如果我禁用它,req.csrfToken 将变为未定义。
我需要 gerCsrfToken 端点为 POST,因为我不想将密码公开为 url 参数。
【问题讨论】:
标签: javascript node.js express csrf csrf-protection