From 0a1f1cee1ed98e0550d3b77e176a00b4d177d285 Mon Sep 17 00:00:00 2001 From: Terncode Date: Mon, 16 Nov 2026 20:37:05 +0100 Subject: [PATCH] Restrict mock login to local requests --- src/ts/server/routes/auth.ts | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/ts/server/routes/auth.ts b/src/ts/server/routes/auth.ts index d4b5dc9..467c7a7 100644 --- a/src/ts/server/routes/auth.ts +++ b/src/ts/server/routes/auth.ts @@ -304,7 +304,9 @@ export function authRoutes( })); if (mockLogin) { - app.get('/mocks', wrap(server, async () => { + const mockLoginGuard = createMockLoginGuard(server); + + app.get('/mocks', mockLoginGuard, wrap(server, async () => { const accounts: IAccount[] = []; const mostUsed = await Account.find({ @@ -352,7 +354,7 @@ export function authRoutes( return uniqById(accounts, acc => acc._id.toString()).map(a => [a!._id, `${a!.name} ${a!.roles.length ? `(${a!.roles.join(' ')})`: ''}`]); })); - app.post('/create-account', wrap(server, async (data) => { + app.post('/create-account', mockLoginGuard, wrap(server, async (data) => { const accountCount = await Account.countDocuments(); const account = await Account.create({ @@ -369,12 +371,27 @@ export function authRoutes( .catch(e => done(e))) ); - app.get('/local', passport.authenticate('local', { successRedirect: '/', failureRedirect: '/failed-login' })); + app.get('/local', mockLoginGuard, passport.authenticate('local', { successRedirect: '/', failureRedirect: '/failed-login' })); } return app; } +function isLoopbackAddress(address: string | undefined) { + return address === '127.0.0.1' || address === '::1' || address === '::ffff:127.0.0.1'; +} + +// mock login gives access to any account, outside of development allow it only for direct local requests +const createMockLoginGuard = (server: ServerConfig): RequestHandler => (req, res, next) => { + if (DEVELOPMENT || (isLoopbackAddress(req.socket.remoteAddress) && !req.get('X-Forwarded-For'))) { + next(); + } + else { + createFromRequest(server, req).warn('Blocked mock login request', req.originalUrl); + res.sendStatus(403); + } +}; + function createOptions( req: Request, connectOnly: boolean, server: ServerConfig, settings: Settings, checkers: SuspiciousCheckers ): CreateAccountOptions {