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

feat(ldapauth): sanitize input for group search filter #97

Merged
merged 1 commit into from
Dec 24, 2021
Merged
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
42 changes: 21 additions & 21 deletions lib/ldapauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,25 @@ var getOption = function(obj, keys) {
return undefined;
};

/**
* Sanitize LDAP special characters from input
*
* {@link https://tools.ietf.org/search/rfc4515#section-3}
*
* @private
* @param {string} input - String to sanitize
* @returns {string} Sanitized string
*/
var sanitizeInput = function(input) {
return input
.replace(/\*/g, '\\2a')
.replace(/\(/g, '\\28')
.replace(/\)/g, '\\29')
.replace(/\\/g, '\\5c')
.replace(/\0/g, '\\00')
.replace(/\//g, '\\2f');
};

/**
* Create an LDAP auth class. Primary usage is the `.authenticate` method.
*
Expand Down Expand Up @@ -139,8 +158,8 @@ function LdapAuth(opts) {
var groupSearchFilter = opts.groupSearchFilter;
opts.groupSearchFilter = function(user) {
return groupSearchFilter
.replace(/{{dn}}/g, user[opts.groupDnProperty])
.replace(/{{username}}/g, user.uid);
.replace(/{{dn}}/g, sanitizeInput(user[opts.groupDnProperty] || ''))
.replace(/{{username}}/g, sanitizeInput(user.uid || ''));
};
}

Expand Down Expand Up @@ -284,25 +303,6 @@ LdapAuth.prototype._search = function(searchBase, options, callback) {
});
};

/**
* Sanitize LDAP special characters from input
*
* {@link https://tools.ietf.org/search/rfc4515#section-3}
*
* @private
* @param {string} input - String to sanitize
* @returns {string} Sanitized string
*/
var sanitizeInput = function(input) {
return input
.replace(/\*/g, '\\2a')
.replace(/\(/g, '\\28')
.replace(/\)/g, '\\29')
.replace(/\\/g, '\\5c')
.replace(/\0/g, '\\00')
.replace(/\//g, '\\2f');
};

/**
* Find the user record for the given username.
*
Expand Down