Restrict mock login to local requests

This commit is contained in:
Terncode committed 2026-11-16 20:37:05 +01:00
1 parent 10066d17de
commit 0a1f1cee1e
1 file changed
+20 -3
+20 -3
View File
@@ -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 {