Node基礎之CSRF 篇

CSRF 跨站請求偽造,這是一個非常常見的攻擊手法

防禦 CSRF

1
npm install --save csurf

引入方式

1
var csurf = require('csurf')

那使用方式其實很簡單,它採用 middleware 的作法

1
2
3
4
5
6
7
8
9
10
11
12
var cookieParser = require('cookie-parser')

var csrfProtection = csrf({ cookie: true })

app.get('/form', csrfProtection, function (req, res) {
// pass the csrfToken to the view
res.render('send', { csrfToken: req.csrfToken() }) // 傳給 View csrf 的 token
})

app.post('/process', parseForm, csrfProtection, function (req, res) {
res.send('data is being processed')
})

在表單傳送上必須夾帶 CSRF 的資訊

*注意若使用了 CSURF 之後 PostMan 就會無法動作唷