Skip to content

Commit

Permalink
escape special LDAP chars
Browse files Browse the repository at this point in the history
  • Loading branch information
ahaenggli committed Mar 19, 2022
1 parent db8ca1a commit 1e49434
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased] (in 'dev')

## [1.7.0] - 2022-03-19

### Changed

- to support #ext#-users the following changes were necessary:
Expand Down Expand Up @@ -34,6 +36,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- converted schema csv files from utf-16 to utf-8
- handle cn=subschema like any other ldap entries instead of fixed search attributes
- register an error handler for the server (EventEmitter)
- escape LDAP special chars `,=+<>#;\` with an additional backslash

## [1.6.0] - 2021-12-19

Expand Down
3 changes: 2 additions & 1 deletion graph_azuread.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const cca = new msal.ConfidentialClientApplication({
clientSecret: config.AZURE_APP_SECRET,
}
});

/**
* Acquires token with client credentials.
* @param {object} tokenRequest
Expand Down Expand Up @@ -98,7 +99,7 @@ graph.loginWithUsernamePassword = async function loginWithUsernamePassword(usern
let credential = new aIdentity.UsernamePasswordCredential(
config.AZURE_TENANTID,
config.AZURE_APP_ID,
username,
encodeURIComponent(username),
encodeURIComponent(password)
);

Expand Down
6 changes: 6 additions & 0 deletions helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,11 @@ helper.ReadFile = function (file, encoding = 'utf8') {
else return "";
};

helper.escapeLDAPspecialChars = function escapeLDAPspecialChars(str) {
return str.replace(/[,=+<>#;\\]/g, '\\$&');
};

helper.unescapeLDAPspecialChars = function escapeLDAPspecialChars(str) {
return str.replace(/\\([,=+<>#;\\])/g, '$1');
};
module.exports = helper;
13 changes: 4 additions & 9 deletions ldapwrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ function removeSpecialChars(str) {
return diacritic.clean(str).replace(/[^A-Za-z0-9._\s]+/g, '-');
}

function escapeLDAPspecialChars(str) {
return str.replace(/[,=+<>#;\\]/g, '\\$&');
}

ldapwrapper.do = async function () {
helper.log("ldapwrapper.js", "start");

Expand Down Expand Up @@ -375,14 +371,13 @@ ldapwrapper.do = async function () {
user.userPrincipalName = user.mail;

if (userPrincipalName.indexOf("#EXT#") > -1) {
userPrincipalName = escapeLDAPspecialChars(userPrincipalName.substring(0, userPrincipalName.indexOf("#EXT#")));
userPrincipalName = userPrincipalName.substring(0, userPrincipalName.indexOf("#EXT#"));
} else {

let issuers = user.identities.filter(x => x.hasOwnProperty('issuer') && x.signInType == 'userPrincipalName');
helper.warn(issuers);
issuers.forEach(issuer => userPrincipalName = userPrincipalName.replace('@' + issuer.issuer, ''));
userPrincipalName = userPrincipalName.replace('#EXT#', '');
userPrincipalName = escapeLDAPspecialChars(userPrincipalName);
userPrincipalName = userPrincipalName.replace('#EXT#', '');
}

AzureADuserExternal = 1;
Expand Down Expand Up @@ -412,6 +407,8 @@ ldapwrapper.do = async function () {
// userPrincipalName = userPrincipalNameClean;
}

userPrincipalName = helper.escapeLDAPspecialChars(userPrincipalName);

let upName = config.LDAP_USERRDN + "=" + userPrincipalName + "," + config.LDAP_USERSDN;
upName = upName.toLowerCase();

Expand Down Expand Up @@ -521,8 +518,6 @@ ldapwrapper.do = async function () {
});

db[upName] = customizer.ModifyLDAPUser(db[upName], user);


}
}

Expand Down
5 changes: 2 additions & 3 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ server.bind(SUFFIX, async (req, res, next) => {
helper.log("server.js", "server.bind", dn);

// dn bind
var username = dn.replace(config.LDAP_USERRDN + "=", '').replace("," + config.LDAP_USERSDN, '');
var username = helper.unescapeLDAPspecialChars(dn.replace(config.LDAP_USERRDN + "=", '').replace("," + config.LDAP_USERSDN, ''));
var pass = req.credentials;

if (config.LDAP_BINDUSER && config.LDAP_BINDUSER.toString().split("||").indexOf(username + '|' + pass) > -1) {
Expand All @@ -192,7 +192,7 @@ server.bind(SUFFIX, async (req, res, next) => {

var userAttributes = db[dn]; // removeSensitiveAttributes(req.dn, dn, db[dn]);//

if (!userAttributes || !userAttributes.hasOwnProperty("sambaNTPassword") || !userAttributes.hasOwnProperty("AzureADuserPrincipalName")) {
if (!userAttributes || !userAttributes.hasOwnProperty("sambaNTPassword") || !userAttributes.hasOwnProperty("AzureADuserPrincipalName")) {
helper.log("server.js", "server.bind", username, "Failed login -> mybe not synced yet?");
return next(new ldap.InvalidCredentialsError());
} else {
Expand Down Expand Up @@ -238,7 +238,6 @@ server.bind(SUFFIX, async (req, res, next) => {
helper.error("server.js", "server.bind", username, " -> Failed login");
return next(new ldap.InvalidCredentialsError());
}

}
}
}
Expand Down

0 comments on commit 1e49434

Please sign in to comment.