@riocrypto/common-server 1.0.1903 → 1.0.1905
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/index.d.ts +1 -1
- package/build/index.js +1 -1
- package/build/middlewares/verify-csrf-token.d.ts +2 -0
- package/build/middlewares/verify-csrf-token.js +45 -0
- package/package.json +1 -1
- package/build/middlewares/csrf-protection.d.ts +0 -5
- package/build/middlewares/csrf-protection.js +0 -19
package/build/index.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ export * from "./middlewares/not-sandbox";
|
|
|
6
6
|
export * from "./middlewares/set-trace";
|
|
7
7
|
export * from "./middlewares/require-min-role";
|
|
8
8
|
export * from "./middlewares/require-min-admin-role";
|
|
9
|
-
export * from "./middlewares/csrf-
|
|
9
|
+
export * from "./middlewares/verify-csrf-token";
|
|
10
10
|
export * from "./services/apiKey";
|
|
11
11
|
export * from "./services/password";
|
|
12
12
|
export * from "./services/logger";
|
package/build/index.js
CHANGED
|
@@ -22,7 +22,7 @@ __exportStar(require("./middlewares/not-sandbox"), exports);
|
|
|
22
22
|
__exportStar(require("./middlewares/set-trace"), exports);
|
|
23
23
|
__exportStar(require("./middlewares/require-min-role"), exports);
|
|
24
24
|
__exportStar(require("./middlewares/require-min-admin-role"), exports);
|
|
25
|
-
__exportStar(require("./middlewares/csrf-
|
|
25
|
+
__exportStar(require("./middlewares/verify-csrf-token"), exports);
|
|
26
26
|
__exportStar(require("./services/apiKey"), exports);
|
|
27
27
|
__exportStar(require("./services/password"), exports);
|
|
28
28
|
__exportStar(require("./services/logger"), exports);
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.verifyCsrfToken = void 0;
|
|
16
|
+
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
17
|
+
const secret_manager_client_1 = require("../clients/secret-manager-client");
|
|
18
|
+
const common_1 = require("@riocrypto/common");
|
|
19
|
+
const verifyCsrfToken = (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
20
|
+
const CSRF_TOKEN_SECRET = yield secret_manager_client_1.secretManagerClient.getSecretValue("CSRF_TOKEN_SECRET");
|
|
21
|
+
if (!CSRF_TOKEN_SECRET) {
|
|
22
|
+
throw new common_1.SecretManagerError();
|
|
23
|
+
}
|
|
24
|
+
// Check for the presence of accessToken or adminAccessToken cookies
|
|
25
|
+
const hasAccessToken = req.cookies.accessToken || req.cookies.adminAccessToken;
|
|
26
|
+
// Only apply CSRF check if accessToken or adminAccessToken cookies are present
|
|
27
|
+
if (hasAccessToken &&
|
|
28
|
+
["POST", "PUT", "PATCH", "DELETE"].includes(req.method.toUpperCase())) {
|
|
29
|
+
const csrfToken = req.body.csrfToken;
|
|
30
|
+
if (!csrfToken) {
|
|
31
|
+
return res.status(403).json({ error: "CSRF token missing" });
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
jsonwebtoken_1.default.verify(csrfToken, CSRF_TOKEN_SECRET);
|
|
35
|
+
next(); // Token is valid, proceed to the next middleware
|
|
36
|
+
}
|
|
37
|
+
catch (err) {
|
|
38
|
+
return res.status(403).json({ error: "Invalid CSRF token" });
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
next(); // Skip CSRF check if no relevant cookies are present
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
exports.verifyCsrfToken = verifyCsrfToken;
|
package/package.json
CHANGED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
/// <reference types="qs" />
|
|
2
|
-
import { Request, Response, NextFunction } from "express";
|
|
3
|
-
declare const csrfProtection: import("express-serve-static-core").RequestHandler<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>;
|
|
4
|
-
declare const csrfErrorHandler: (err: any, req: Request, res: Response, next: NextFunction) => void;
|
|
5
|
-
export { csrfProtection, csrfErrorHandler };
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.csrfErrorHandler = exports.csrfProtection = void 0;
|
|
7
|
-
const csurf_1 = __importDefault(require("csurf"));
|
|
8
|
-
// Initialize CSRF protection middleware
|
|
9
|
-
const csrfProtection = (0, csurf_1.default)();
|
|
10
|
-
exports.csrfProtection = csrfProtection;
|
|
11
|
-
// Error handling middleware for CSRF token errors
|
|
12
|
-
const csrfErrorHandler = (err, req, res, next) => {
|
|
13
|
-
if (err.code !== "EBADCSRFTOKEN")
|
|
14
|
-
return next(err);
|
|
15
|
-
// CSRF token errors
|
|
16
|
-
res.status(403);
|
|
17
|
-
res.send("Form tampered with");
|
|
18
|
-
};
|
|
19
|
-
exports.csrfErrorHandler = csrfErrorHandler;
|