🥊 fix: Reject Malformed LDAP Credentials Before Authentication (#18235)

This commit is contained in:
Danny Avila 2026-11-16 20:04:11 -05:00 • committed by GitHub
parent e1fc59c9cf
commit 3de814c0fa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 112 additions and 19 deletions

View file

@ -1,22 +1,24 @@
const passport = require('passport');
const { validateLdapCredentials } = require('@librechat/api');
const requireLdapAuth = (req, res, next) => {
passport.authenticate('ldapauth', (err, user, info) => {
if (err) {
console.log({
title: '(requireLdapAuth) Error at passport.authenticate',
parameters: [{ name: 'error', value: err }],
});
return next(err);
}
if (!user) {
console.log({
title: '(requireLdapAuth) Error: No user',
});
return res.status(404).send(info);
}
req.user = user;
next();
})(req, res, next);
};
const requireLdapAuth = (req, res, next) =>
validateLdapCredentials(req, res, () => {
passport.authenticate('ldapauth', (err, user, info) => {
if (err) {
console.log({
title: '(requireLdapAuth) Error at passport.authenticate',
parameters: [{ name: 'error', value: err }],
});
return next(err);
}
if (!user) {
console.log({
title: '(requireLdapAuth) Error: No user',
});
return res.status(404).send(info);
}
req.user = user;
next();
})(req, res, next);
});
module.exports = requireLdapAuth;

View file

@ -0,0 +1,74 @@
const express = require('express');
const passport = require('passport');
const request = require('supertest');
jest.mock('passport', () => ({ authenticate: jest.fn() }));
jest.mock('@librechat/api', () => jest.requireActual('../../../packages/api/src/middleware/ldap'));
const requireLdapAuth = require('./requireLdapAuth');
let app;
let bind;
beforeEach(() => {
jest.clearAllMocks();
bind = jest.fn();
passport.authenticate.mockImplementation((_strategy, callback) => (req) => {
bind(req.body);
callback(null, { id: 'ldap-user' });
});
app = express();
app.use(express.json());
app.post('/login', requireLdapAuth, (req, res) => res.json({ id: req.user.id }));
app.use((_error, _req, res, _next) => res.sendStatus(500));
});
describe.each(['email', 'password'])('LDAP %s validation', (field) => {
it.each([undefined, null, {}, [], 123, 0, true, false, ''])(
'rejects malformed credentials before invoking Passport: %p',
async (value) => {
const response = await request(app)
.post('/login')
.send({ email: 'alice', password: 'fixture-secret', [field]: value });
expect(response.status).toBe(400);
expect(response.body).toEqual({ message: 'Invalid credentials' });
expect(passport.authenticate).not.toHaveBeenCalled();
expect(bind).not.toHaveBeenCalled();
},
);
});
it('rejects a missing body before invoking Passport', async () => {
expect((await request(app).post('/login')).status).toBe(400);
expect(passport.authenticate).not.toHaveBeenCalled();
});
it.each(['alice', 'DOMAIN\\alice', 'alice@example.test'])(
'preserves valid LDAP identifiers and passwords verbatim: %s',
async (email) => {
const credentials = { email, password: ' fixture secret ' };
const response = await request(app).post('/login').send(credentials);
expect(response.status).toBe(200);
expect(response.body).toEqual({ id: 'ldap-user' });
expect(bind).toHaveBeenCalledWith(credentials);
},
);
it('preserves ordinary authentication failure behavior', async () => {
passport.authenticate.mockImplementation(
(_strategy, callback) => () => callback(null, false, { message: 'Invalid username/password' }),
);
const response = await request(app)
.post('/login')
.send({ email: 'alice', password: 'wrong-password' });
expect(response.status).toBe(404);
expect(response.body).toEqual({ message: 'Invalid username/password' });
});
it('forwards operational authentication errors to the request error boundary', async () => {
passport.authenticate.mockImplementation(
(_strategy, callback) => () => callback(new Error('fixture LDAP outage')),
);
expect(
(await request(app).post('/login').send({ email: 'alice', password: 'fixture-secret' })).status,
).toBe(500);
});

View file

@ -10,6 +10,7 @@ export * from './json';
export * from './sanitize';
export * from './capabilities';
export * from './auth';
export * from './ldap';
export {
TENANT_CONTEXT_REQUIRED,
createPreAuthTenantGuard,

View file

@ -0,0 +1,16 @@
import type { RequestHandler } from 'express';
/** LDAP libraries can throw outside Express when asynchronous binds receive non-string credentials. */
export const validateLdapCredentials: RequestHandler = (req, res, next) => {
const { email, password } = req.body ?? {};
if (
typeof email !== 'string' ||
email.length === 0 ||
typeof password !== 'string' ||
password.length === 0
) {
res.status(400).json({ message: 'Invalid credentials' });
return;
}
next();
};