mirror of
https://github.com/fosrl/pangolin.git
synced 2025-05-13 05:40:38 +01:00
24 lines
548 B
TypeScript
24 lines
548 B
TypeScript
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();
|
|
}
|