Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move LDAP Escape to login handler #14234

Merged
merged 1 commit into from
Apr 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions app/ldap/server/ldap.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { settings } from '../../settings';
import { Logger } from '../../logger';
import ldapjs from 'ldapjs';
import Bunyan from 'bunyan';
import ldapEscape from 'ldap-escape';

const logger = new Logger('LDAP', {
sections: {
Expand Down Expand Up @@ -183,7 +182,6 @@ export default class LDAP {
}

getUserFilter(username) {
username = ldapEscape.filter`${ username }`;
const filter = [];

if (this.options.User_Search_Filter !== '') {
Expand Down Expand Up @@ -320,9 +318,6 @@ export default class LDAP {
}

isUserInGroup(username, userdn) {
username = ldapEscape.filter`${ username }`;
userdn = ldapEscape.dn`${ userdn }`;

if (!this.options.group_filter_enabled) {
return true;
}
Expand Down Expand Up @@ -484,7 +479,6 @@ export default class LDAP {
}

authSync(dn, password) {
rodrigok marked this conversation as resolved.
Show resolved Hide resolved
dn = ldapEscape.dn`${ dn }`;
logger.auth.info('Authenticating', dn);

try {
Expand Down
12 changes: 8 additions & 4 deletions app/ldap/server/loginHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { Logger } from '../../logger';
import { slug, getLdapUsername, getLdapUserUniqueID, syncUserData, addLdapUser } from './sync';
import LDAP from './ldap';

import ldapEscape from 'ldap-escape';

const logger = new Logger('LDAPHandler', {});

function fallbackDefaultAccountSystem(bind, username, password) {
Expand Down Expand Up @@ -46,23 +48,25 @@ Accounts.registerLoginHandler('ldap', function(loginRequest) {
const ldap = new LDAP();
let ldapUser;

const escapedUsername = ldapEscape.filter`${ loginRequest.username }`;

try {
ldap.connectSync();
const users = ldap.searchUsersSync(loginRequest.username);
const users = ldap.searchUsersSync(escapedUsername);

if (users.length !== 1) {
logger.info('Search returned', users.length, 'record(s) for', loginRequest.username);
logger.info('Search returned', users.length, 'record(s) for', escapedUsername);
throw new Error('User not Found');
}

if (ldap.authSync(users[0].dn, loginRequest.ldapPass) === true) {
if (ldap.isUserInGroup(loginRequest.username, users[0].dn)) {
if (ldap.isUserInGroup(escapedUsername, users[0].dn)) {
rodrigok marked this conversation as resolved.
Show resolved Hide resolved
ldapUser = users[0];
} else {
throw new Error('User not in a valid group');
}
} else {
logger.info('Wrong password for', loginRequest.username);
logger.info('Wrong password for', escapedUsername);
}
} catch (error) {
logger.error(error);
Expand Down