Skip to content

Instantly share code, notes, and snippets.

@nsdevaraj
Created August 5, 2024 14:04
Show Gist options
  • Save nsdevaraj/af48734a929c9d0137309e3078997080 to your computer and use it in GitHub Desktop.
Save nsdevaraj/af48734a929c9d0137309e3078997080 to your computer and use it in GitHub Desktop.
Prevention of Authorization Bypass Attacks
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