Files
wekan/tests/ldapOptionalUserFilter.test.cjs
T
Lauri Ojansivu a944be1817 LDAP background sync imports users again, and keeps the interval it is given.
After the 12.47 fix, #6771 comment 6197931378 showed the background sync
running and importing nobody: "filter": "(&(&(objectclass=person))(uid=\2a))",
"Search result count 0". importNewUsers() asked for every user with the string
'*', and the RFC 4515 escaping added for login names on 2026-09-27
(679a8b3499) turned the wildcard into a literal asterisk. The wildcard is now
reachable only through ALL_LDAP_USERS (userSearchValue.js), a Symbol that a
login request cannot carry, so a typed '*' is still escaped.

The sync also ran far more often, or less often, than set: later.js is a
calendar, and every(n).minute() is "minutes 0, n, 2n ... of each hour", an
interval only when n divides 60. 2 hours and 1 day both became hourly, 90 s
became every minute, and 3600 (seconds, typed as milliseconds) every 4
seconds. Milliseconds now become an interval later keeps exactly - seconds
and minutes dividing 60, hours dividing 24, whole days - pinned to the start
of the unit; anything else is rounded to the nearest one with a warning, and
an interval under a minute is warned about as probably seconds.

tests/ldapImportAllUsers.test.cjs reproduces the reported filter and checks
the wildcard, the still-escaped typed '*', and that no code searches the
string '*'. tests/ldapSyncSchedule.test.cjs checks every interval up to 36
hours reaches later only as an every(n) it keeps, and with the real later.js
that 1 min ... 1 week keep their spacing.

Thanks to tromlet and xet7 !
2026-10-15 20:45:49 +03:00

47 lines
3.5 KiB
JavaScript

'use strict';
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const source = fs.readFileSync(path.join(__dirname, '../packages/wekan-ldap/server/ldap.js'), 'utf8');
const body = source.split(' getUserFilter(username) {')[1].split('\n async bindUserIfNecessary')[0].replace(/\n }\s*$/, '');
const { userSearchValue, describeUserSearch } = require('../packages/wekan-ldap/server/userSearchValue');
const getUserFilter = new Function('escapeLdapFilterValue', 'Log', 'userSearchValue', `return function(username) {${body}}`)(value => value, { error() {} }, userSearchValue);
for (const value of [undefined, null, '']) {
assert.equal(getUserFilter.call({ options: { User_Search_Filter: value, User_Search_Field: 'sAMAccountName' } }, 'alice'), '(&(sAMAccountName=alice))');
}
for (const value of ['objectClass=person', '(objectClass=person)']) {
assert.equal(getUserFilter.call({ options: { User_Search_Filter: value, User_Search_Field: 'uid,mail' } }, 'alice'), '(&(objectClass=person)(|(uid=alice)(mail=alice)))');
}
for (const value of [true, 123, {}]) {
assert.throws(() => getUserFilter.call({ options: { User_Search_Filter: value, User_Search_Field: 'uid' } }, 'alice'), /LDAP_USER_SEARCH_FILTER must be a string/);
}
console.log('ldapOptionalUserFilter: unset optional filter, configured restrictions and invalid-type failures verified');
// Reproduce the actual post-bind search sequence from the LDAP implementation.
(async () => {
const AsyncFunction = Object.getPrototypeOf(async function() {}).constructor;
// These methods use only the established structured logger as an external dependency.
const Log = { info() {}, debug() {} };
for (const filter of [undefined, null, '', '(objectClass=person)']) {
const events = [];
const ldap = {
options: { Authentication: true, Authentication_UserDN: 'service@example.test', Authentication_Password: 'test-only-password', User_Search_Filter: filter, User_Search_Field: 'sAMAccountName' },
getUserFilter, getUserAttributes: () => undefined,
bind: async (dn) => { assert.equal(dn, 'service@example.test'); events.push('bind'); },
searchAll: async (base, options) => { events.push('search'); assert.equal(options.filter, filter ? '(&(objectClass=person)(sAMAccountName=alice))' : '(&(sAMAccountName=alice))'); return [{dn:'cn=alice'}]; },
};
const bindBody = source.split(' async bindIfNecessary() {')[1].split('\n async searchUsers(')[0].replace(/\n }\s*$/, '');
const searchBody = source.split(' async searchUsers(username) {')[1].split('\n async getUserById(')[0].replace(/\n }\s*$/, '');
ldap.bindIfNecessary = new AsyncFunction('Log', bindBody).bind(ldap, Log);
// #6548: the search details go to the LDAP logger (log_debug), not Log.debug.
ldap.searchUsers = new AsyncFunction('Log', 'log_debug', 'describeUserSearch', 'username', searchBody).bind(ldap, Log, () => {}, describeUserSearch);
assert.deepEqual(await ldap.searchUsers('alice'), [{dn:'cn=alice'}]);
assert.deepEqual(events, ['bind','search']);
ldap.domainBinded = false; events.length = 0;
ldap.bind = async () => { throw new Error('invalid credentials'); };
await assert.rejects(ldap.searchUsers('alice'), /invalid credentials/);
assert.equal(events.length, 0, 'failed bind must never proceed to search');
}
console.log('ldapOptionalUserFilter: actual bind/search sequence and failed-bind isolation verified');
})().catch(error => { console.error(error); process.exitCode = 1; });