2017-12-14 8 views
0

フォームを送信した後、私はいつも「ForbiddenError:無効なcsrfトークン」を取得しても、この基本テストアプリケーションでcsurfを使用できません。csurf(Node.js)を使用してcsrfトークンを検証できません

私は間違っていますか?私はin the documentation for csurf与えられた例に従うことを試みている(私はTLSも使用しているが、結果に影響を与える可能性がある)。

'use strict' 

var https = require('https'); 
var express = require('express'); 
var cookieParser = require('cookie-parser') 
var csrf = require('csurf') 
var bodyParser = require('body-parser') 
var fs = require('fs'); 
var oauth = require('./oauth.js'); 
var argv = require('minimist')(process.argv.slice(2), { 
    alias: {p: 'port'}, 
    default: {port: 80}, 
}); 

// setup route middlewares 
var csrfProtection = csrf({ cookie: true }) 
var parseForm = bodyParser.urlencoded({ extended: false }) 

var app = express(); 

app.use(cookieParser()); 

// Init SSL 
var sslPath = '/etc/letsencrypt/live/censored/'; 
var options = { 
    key: fs.readFileSync(sslPath + 'privkey.pem'), 
    cert: fs.readFileSync(sslPath + 'fullchain.pem') 
}; 

app.get('/some-form', csrfProtection, function(req, res){ 
     res.send('<form action="/process" method="POST">' + 
        '<input type="hidden" name="_csrf" value="' + req.csrfToken() + '">' + 
        'Favorite color: <input type="text" name="favoriteColor">' + 
        '<button type="submit">Submit</button>' + 
        '</form>'); 
}); 

app.post('/process', csrfProtection, function(req, res){ 
     res.send('<p>Your favorite color is "' + req.body.favoriteColor + '".'); 
}); 


app.get("/", function(req, res) { 
    res.send('Hello World!'); 
}); 
app.listen(argv.port, function() { 
    console.log('listening on port ' + argv.port); 
}); 

https.createServer(options, app).listen(443); 

答えて

0

parseFormを使用するのを忘れましたが、一度すべて正常に機能しました。

関連する問題