Skip to content

Instantly share code, notes, and snippets.

@mpaccione
Created August 18, 2021 03:34
Show Gist options
  • Save mpaccione/239dc7ea09c9e39225cff626b71e1dcb to your computer and use it in GitHub Desktop.
Save mpaccione/239dc7ea09c9e39225cff626b71e1dcb to your computer and use it in GitHub Desktop.
CSRF
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