From 7069d5929e54997b7ee8daecaeb2d69f4d85b2aa Mon Sep 17 00:00:00 2001 From: Alex Yang <32620988+DatProJack@users.noreply.github.com> Date: Wed, 28 Aug 2024 09:46:50 -0500 Subject: [PATCH 1/2] proxy fixes: --- src/app.ts | 5 ++-- src/middleware/rateLimiter.ts | 4 +-- src/middleware/validator-middleware.ts | 34 ++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 src/middleware/validator-middleware.ts diff --git a/src/app.ts b/src/app.ts index 109e5ca..29958cd 100644 --- a/src/app.ts +++ b/src/app.ts @@ -9,6 +9,7 @@ import databaseMiddleware from "./middleware/database-middleware"; import morgan from "morgan"; import bodyParser from "body-parser"; import errorHandler from "./middleware/error-handler"; +import { rateLimiter } from "./middleware/rateLimiter"; import attendeeRouter from "./services/attendee/attendee-router"; import checkinRouter from "./services/checkin/checkin-router"; @@ -31,13 +32,13 @@ AWS.config.update({ }); const app = express(); -app.enable("trust proxy"); +app.set("trust proxy", true); // to prevent server-side caching/returning status code 200 // (we can remove this later) app.disable("etag"); -// app.use(rateLimiter); +app.use(rateLimiter); // app.use(customCors); app.use(cors()); diff --git a/src/middleware/rateLimiter.ts b/src/middleware/rateLimiter.ts index d681fe3..79532f1 100644 --- a/src/middleware/rateLimiter.ts +++ b/src/middleware/rateLimiter.ts @@ -12,8 +12,8 @@ export const rateLimiter = ( res: Response, next: NextFunction ) => { - console.log(req.hostname); - if (req.hostname === "localhost" || req.hostname === "127.0.0.1") { + // console.log(req.ip); + if (req.ip === "127.0.0.1") { next(); } else { limiter(req, res, next); diff --git a/src/middleware/validator-middleware.ts b/src/middleware/validator-middleware.ts new file mode 100644 index 0000000..5900238 --- /dev/null +++ b/src/middleware/validator-middleware.ts @@ -0,0 +1,34 @@ +import { Request, Response, NextFunction } from "express"; +import { ZodObject, ZodError } from "zod"; +import { StatusCodes } from "http-status-codes"; + +export default function validatorMiddleware(validator: ZodObject) { + return function (req: Request, res: Response, next: NextFunction) { + try { + // Validate req.body against the given validator + const validatedData = validator.parse(req.body); + + // If validation succeeds, replace req.body with the validated data + req.body = validatedData; + + // Proceed to the next middleware + next(); + } catch (error) { + if (error instanceof ZodError) { + // If it's a Zod validation error, format it and send a 400 response + res.status(StatusCodes.BAD_REQUEST).json({ + message: "Validation failed", + errors: error.errors.map((err) => ({ + path: err.path.join("."), + message: err.message, + })), + }); + + throw error; + } else { + // For other types of errors, pass to the default error handler + next(error); + } + } + }; +} From 40cc45a2a89e7d86d4a55f56cfa56a3e85130138 Mon Sep 17 00:00:00 2001 From: Alex Yang <32620988+aletya@users.noreply.github.com> Date: Wed, 28 Aug 2024 09:47:52 -0500 Subject: [PATCH 2/2] Delete src/middleware/validator-middleware.ts --- src/middleware/validator-middleware.ts | 34 -------------------------- 1 file changed, 34 deletions(-) delete mode 100644 src/middleware/validator-middleware.ts diff --git a/src/middleware/validator-middleware.ts b/src/middleware/validator-middleware.ts deleted file mode 100644 index 5900238..0000000 --- a/src/middleware/validator-middleware.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { Request, Response, NextFunction } from "express"; -import { ZodObject, ZodError } from "zod"; -import { StatusCodes } from "http-status-codes"; - -export default function validatorMiddleware(validator: ZodObject) { - return function (req: Request, res: Response, next: NextFunction) { - try { - // Validate req.body against the given validator - const validatedData = validator.parse(req.body); - - // If validation succeeds, replace req.body with the validated data - req.body = validatedData; - - // Proceed to the next middleware - next(); - } catch (error) { - if (error instanceof ZodError) { - // If it's a Zod validation error, format it and send a 400 response - res.status(StatusCodes.BAD_REQUEST).json({ - message: "Validation failed", - errors: error.errors.map((err) => ({ - path: err.path.join("."), - message: err.message, - })), - }); - - throw error; - } else { - // For other types of errors, pass to the default error handler - next(error); - } - } - }; -}