-
-
Save mpaccione/239dc7ea09c9e39225cff626b71e1dcb to your computer and use it in GitHub Desktop.
CSRF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CSRFProtection: (req, publicRoutes = false) => { | |
if (req.headers['x-sboauth']) { | |
const userProfile = jwt_decode(req.headers['x-sboauth']); | |
console.log('============ USER PROFILE FROM SBOAUTH ============'); | |
console.log({ userProfile }); | |
req.user = userProfile; | |
return; | |
} | |
if (publicRoutes) { | |
const methods = publicRoutes.methods || oneOrMany(publicRoutes.method); | |
if (!isUrlMatch(publicRoutes, req.path) || !isMethodMatch(methods, req.method)) { | |
console.log('============== CSRF PROTECTION ==============='); | |
console.log(req.headers); | |
console.log(req.cookies); | |
if (req.originalUrl.includes('/users/my/profile') && req.headers['x-csrf']) { | |
// Library API is on a different domain so have to reduce security | |
const userProfile = jwt_decode(req.headers['x-csrf']); | |
console.log('============ USER PROFILE FROM X-CSRF ============'); | |
console.log({ userProfile }); | |
req.user = userProfile; | |
return; | |
} | |
if (req.headers['x-csrf'] === req.cookies['c']) { | |
const userProfile = jwt_decode(req.cookies['c']); | |
console.log('============ USER PROFILE FROM COOKIES ============'); | |
console.log({ userProfile }); | |
req.user = userProfile; | |
return; | |
} | |
} | |
} | |
console.log('CSRF Security Failure'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment