Created
August 5, 2024 14:04
-
-
Save nsdevaraj/af48734a929c9d0137309e3078997080 to your computer and use it in GitHub Desktop.
Prevention of Authorization Bypass Attacks
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
import { Request, Response, NextFunction } from 'express'; | |
interface User { | |
id: string; | |
roles: string[]; | |
} | |
function checkPermission(permission: string) { | |
return (req: Request, res: Response, next: NextFunction) => { | |
const user = req.user as User; | |
if (user && user.roles.includes(permission)) { | |
next(); | |
} else { | |
res.status(403).json({ error: "Permission denied" }); | |
} | |
}; | |
} | |
app.get('/sensitive-data', checkPermission('view_sensitive_data'), (req: Request, res: Response) => { | |
// Handle the request | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment