From 25941e0437e3128f7c1a55b12683e54c8037741c Mon Sep 17 00:00:00 2001 From: Shane C Date: Tue, 27 Aug 2024 11:57:45 -0400 Subject: [PATCH] fix middlewares --- api/src/middlewares/cors.ts | 2 +- api/src/middlewares/log.ts | 4 ++-- api/src/middlewares/ratelimit.ts | 2 +- api/src/middlewares/updateTokenExpiry.ts | 14 +++++++------- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/api/src/middlewares/cors.ts b/api/src/middlewares/cors.ts index c9a5871..833ec06 100644 --- a/api/src/middlewares/cors.ts +++ b/api/src/middlewares/cors.ts @@ -1,7 +1,7 @@ import { Request, Response, NextFunction } from "express"; import { app } from ".."; -app.use('*', (req: Request, res: Response, next: NextFunction) => { +app.use('*', (_req: Request, res: Response, next: NextFunction) => { res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, x-auth-user, x-auth-token'); res.header('Access-Control-Allow-Methods', '*'); diff --git a/api/src/middlewares/log.ts b/api/src/middlewares/log.ts index 5e13d81..5b09d25 100644 --- a/api/src/middlewares/log.ts +++ b/api/src/middlewares/log.ts @@ -1,7 +1,7 @@ -import { Request, Response } from "express"; +import { Request, Response, NextFunction } from "express"; import { app, logger } from ".."; -app.use('*', (req: Request, _res: Response, next: () => void) => { +app.use('*', (req: Request, _res: Response, next: NextFunction) => { logger.debug(`${req.method} ${req.url}`); next(); }); diff --git a/api/src/middlewares/ratelimit.ts b/api/src/middlewares/ratelimit.ts index 4bd8d83..0d9e437 100644 --- a/api/src/middlewares/ratelimit.ts +++ b/api/src/middlewares/ratelimit.ts @@ -20,7 +20,7 @@ class RateLimiter { const ip = req.ip; const reqId = ulid(); // ratelimit:ip_address_base64:route_base64 - const redisKey = `ratelimit:${Buffer.from(ip).toString('base64')}:${Buffer.from(this.route).toString('base64')}`; + const redisKey = `ratelimit:${Buffer.from(ip!).toString('base64')}:${Buffer.from(this.route).toString('base64')}`; const reqs = await redis.SCARD(redisKey); if (reqs >= this.limit) { logger.debug(`Ratelimiter: IP address exceeded ratelimit for ${this.route} [${this.limit}/${this.timeframe}]`); diff --git a/api/src/middlewares/updateTokenExpiry.ts b/api/src/middlewares/updateTokenExpiry.ts index d6e8daf..ece4d80 100644 --- a/api/src/middlewares/updateTokenExpiry.ts +++ b/api/src/middlewares/updateTokenExpiry.ts @@ -1,4 +1,4 @@ -import { Request } from "express"; +import { Request, Response, NextFunction } from "express"; import { Collection, Db } from "mongodb"; import { app, SESSION_LIFETIME } from ".."; @@ -8,17 +8,17 @@ export function initializeSessionsMiddleware(db: Db) { sessionsCollection = db.collection('sessions'); } -app.use('*', async (req: Request, next: () => void) => { +app.use('*', async (req: Request, _res: Response, next: NextFunction) => { next(); const user = req.header('x-auth-user'); const token = req.header('x-auth-token'); if (!user || !token) return; try { - const session = await sessionsCollection.findOne({ - user, - token, - expires: { $gt: new Date() } + const session = await sessionsCollection.findOne({ + user, + token, + expires: { $gt: new Date() } }); if (session) { @@ -27,7 +27,7 @@ app.use('*', async (req: Request, next: () => void) => { { $set: { expires: new Date(Date.now() + SESSION_LIFETIME) } } ); } - } catch(e) { + } catch(e) { console.error(e); } });