CSRF 공격을 막기 위해서 서버에서 토큰을 클라이언트로 발행한다. 클라이언트에서 글 작성 후 데이터 전송 시 서버가 발행한 토큰을 같이 신고한다. 서버에서 생성한 토큰과 맞는지 값을 비교한 후에 토큰이 일치하는 경우에만 글을 작성하도록 구현한다.
express my-node-csrf --view=hbs --css=sass
https://www.npmjs.com/package/csurf
npm i csurf
The default value is a function that reads the token from the following locations, in order:
- req.body._csrf - typically generated by the body-parser module.
- req.query._csrf - a built-in from Express.js to read from the URL query string.
- req.headers['csrf-token'] - the CSRF-Token HTTP request header.
- req.headers['xsrf-token'] - the XSRF-Token HTTP request header.
- req.headers['x-csrf-token'] - the X-CSRF-Token HTTP request header.
- req.headers['x-xsrf-token'] - the X-XSRF-Token HTTP request header