mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-01 08:34:53 +02:00
CSRF prevention
This commit is contained in:
parent
993eab5ac1
commit
4e4b8744b5
3 changed files with 43 additions and 8 deletions
24
server/middlewares/csrfProtection.ts
Normal file
24
server/middlewares/csrfProtection.ts
Normal file
|
@ -0,0 +1,24 @@
|
|||
import { NextFunction, Request, Response } from "express";
|
||||
|
||||
export function csrfProtectionMiddleware(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
) {
|
||||
const csrfToken = req.headers["x-csrf-token"];
|
||||
|
||||
// Skip CSRF check for GET requests as they should be idempotent
|
||||
if (req.method === "GET") {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!csrfToken || csrfToken !== "x-csrf-protection") {
|
||||
res.status(403).json({
|
||||
error: "CSRF token missing or invalid"
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
next();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue