fix: resolve CORS TypeScript error and format server files
Type defaultCorsOptions as CorsOptions so allowedHeaders is compatible with the cors middleware. Format files from develop merge for CI.
This commit is contained in:
1 parent
33f8641f3a
commit
ef049b6521
4 files changed
+13
-9
No files matched your search
@@ -7,8 +7,7 @@ import { AppError } from "@/utils/AppError.js";
|
|||||||
const multerErrorStatus = (error: MulterError): number => (error.code === "LIMIT_FILE_SIZE" ? 413 : 400);
|
const multerErrorStatus = (error: MulterError): number => (error.code === "LIMIT_FILE_SIZE" ? 413 : 400);
|
||||||
|
|
||||||
const handleErrors = (error: unknown, req: Request, res: Response, _next: NextFunction) => {
|
const handleErrors = (error: unknown, req: Request, res: Response, _next: NextFunction) => {
|
||||||
const status =
|
const status = error instanceof MulterError ? multerErrorStatus(error) : error instanceof AppError ? error.status || 500 : 500;
|
||||||
error instanceof MulterError ? multerErrorStatus(error) : error instanceof AppError ? error.status || 500 : 500;
|
|
||||||
const message = error instanceof AppError ? error.message : error instanceof Error ? error.message : "Server error";
|
const message = error instanceof AppError ? error.message : error instanceof Error ? error.message : "Server error";
|
||||||
const service = error instanceof AppError ? error.service : error instanceof MulterError ? "uploadMiddleware" : "unknownService";
|
const service = error instanceof AppError ? error.service : error instanceof MulterError ? "uploadMiddleware" : "unknownService";
|
||||||
const method = error instanceof AppError ? error.method : "unknownMethod";
|
const method = error instanceof AppError ? error.method : "unknownMethod";
|
||||||
|
|||||||
@@ -30,7 +30,13 @@ class AuthRoutes {
|
|||||||
this.router.patch("/users/:userId/password", verifyJWT, isAllowed(["superadmin"]), this.authController.editUserPasswordById);
|
this.router.patch("/users/:userId/password", verifyJWT, isAllowed(["superadmin"]), this.authController.editUserPasswordById);
|
||||||
this.router.delete("/users/:userId", verifyJWT, isAllowed(["admin", "superadmin"]), this.authController.deleteUserById);
|
this.router.delete("/users/:userId", verifyJWT, isAllowed(["admin", "superadmin"]), this.authController.deleteUserById);
|
||||||
|
|
||||||
this.router.patch("/user", verifyJWT, imageUpload.single("profileImage"), isAllowed(["admin", "superadmin", "user"]), this.authController.editUser);
|
this.router.patch(
|
||||||
|
"/user",
|
||||||
|
verifyJWT,
|
||||||
|
imageUpload.single("profileImage"),
|
||||||
|
isAllowed(["admin", "superadmin", "user"]),
|
||||||
|
this.authController.editUser
|
||||||
|
);
|
||||||
this.router.delete("/user", verifyJWT, isAllowed(["admin", "superadmin", "user"]), this.authController.deleteUser);
|
this.router.delete("/user", verifyJWT, isAllowed(["admin", "superadmin", "user"]), this.authController.deleteUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+3
-3
@@ -1,6 +1,6 @@
|
|||||||
import express from "express";
|
import express from "express";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import cors from "cors";
|
import cors, { type CorsOptions } from "cors";
|
||||||
import helmet from "helmet";
|
import helmet from "helmet";
|
||||||
import compression from "compression";
|
import compression from "compression";
|
||||||
import cookieParser from "cookie-parser";
|
import cookieParser from "cookie-parser";
|
||||||
@@ -30,12 +30,12 @@ export const createApp = ({
|
|||||||
}) => {
|
}) => {
|
||||||
const allowedOrigin = envSettings.clientHost;
|
const allowedOrigin = envSettings.clientHost;
|
||||||
const app = express();
|
const app = express();
|
||||||
const defaultCorsOptions = {
|
const defaultCorsOptions: CorsOptions = {
|
||||||
origin: allowedOrigin,
|
origin: allowedOrigin,
|
||||||
methods: "GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS",
|
methods: "GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS",
|
||||||
allowedHeaders: ["Content-Type", "Authorization", "Accept-Language"],
|
allowedHeaders: ["Content-Type", "Authorization", "Accept-Language"],
|
||||||
credentials: true,
|
credentials: true,
|
||||||
} as const;
|
};
|
||||||
const publicStatusPageCorsOrigin = createStatusPageCorsOrigin(allowedOrigin, services.statusPagesRepository);
|
const publicStatusPageCorsOrigin = createStatusPageCorsOrigin(allowedOrigin, services.statusPagesRepository);
|
||||||
|
|
||||||
app.use(generalApiLimiter);
|
app.use(generalApiLimiter);
|
||||||
|
|||||||
@@ -6,10 +6,9 @@ import { AppError } from "../../../src/utils/AppError.ts";
|
|||||||
|
|
||||||
// Reaches into the multer instance config to exercise the fileFilter/limits in isolation.
|
// Reaches into the multer instance config to exercise the fileFilter/limits in isolation.
|
||||||
// Guards against regressing GHSA-9xvg-x28f-m78m (pre-auth DoS via unbounded uploads).
|
// Guards against regressing GHSA-9xvg-x28f-m78m (pre-auth DoS via unbounded uploads).
|
||||||
const config = (imageUpload as unknown as { fileFilter: Function; limits: { fileSize: number; files: number } });
|
const config = imageUpload as unknown as { fileFilter: Function; limits: { fileSize: number; files: number } };
|
||||||
|
|
||||||
const makeFile = (mimetype: string): Express.Multer.File =>
|
const makeFile = (mimetype: string): Express.Multer.File => ({ fieldname: "profileImage", originalname: "x", mimetype }) as Express.Multer.File;
|
||||||
({ fieldname: "profileImage", originalname: "x", mimetype }) as Express.Multer.File;
|
|
||||||
|
|
||||||
const runFilter = (mimetype: string): Promise<{ error: unknown; accepted: unknown }> =>
|
const runFilter = (mimetype: string): Promise<{ error: unknown; accepted: unknown }> =>
|
||||||
new Promise((resolve) => {
|
new Promise((resolve) => {
|
||||||
|
|||||||
Reference in new issue
Block a user