Files
wekan/tests/ldapEmailFieldmap.test.cjs
T
Lauri Ojansivu 2f7b49d8de Fix FerretDB migration non-finite doubles, LDAP email mapping, and export listener leak.
#6481 (Nissulya): after MongoDB 3.x -> FerretDB v1 migration, cards with a
non-finite `sort` (±Infinity / NaN, written by old WeKan before #6472) failed to
insert: FerretDB/SQLite rejects non-finite doubles ("invalid value { sort: +Inf }
(infinity values are not allowed)"), aborting the document and leaving boards
stuck on the loading spinner. sanitizeNonFinite() only made the bare mongoexport
tokens PARSE; the parsed value was still a JS Infinity/NaN. Added clampNonFinite()
in the migration tool to reset every non-finite number to a finite 0 before
insert, and a `nonfinite-sort-repair` startup schema-upgrade step that heals the
same corruption in an already-running database (MongoDB or FerretDB), using a
predicate built from finite literals only so it is safe on FerretDB too.

#6481 (Nissulya): LDAP `mail` -> `email` fieldmap returned `undefined` (worked in
6.09). getDataToSyncUserData's email branch read the mapped attribute
case-SENSITIVELY (ldapUser.hasOwnProperty / ldapUser[field]) while the rest of
wekan-ldap uses the case-insensitive getLDAPValue(); a directory returning the
attribute with a different case (exposed by ldapjs -> ldapts, which preserves the
server's case) missed it. The email branch (and the addLdapUser fallback) now
read via getLDAPValue().

#6480 (uusijani): board/CSV export paused on socket backpressure with
`res.once('drain', resolve); res.once('error', reject)`, which leaked one 'error'
listener per backpressured write ("MaxListenersExceededWarning: 11 error
listeners added to [ServerResponse]"). writeWithBackpressure() now removes both
listeners once either settles.

Added tests and negative tests for all three fixes.

Thanks to Nissulya, uusijani and xet7 !

Fixes #6480,
Fixes #6481.
2026-07-19 18:36:11 +03:00

89 lines
3.9 KiB
JavaScript

'use strict';
// Regression test for #6481: LDAP `mail`->`email` fieldmap returned `undefined`
// after upgrading (works in 6.09). Everywhere else in wekan-ldap reads LDAP
// attributes case-INSENSITIVELY via getLDAPValue(), but getDataToSyncUserData's
// `email` branch used case-SENSITIVE `ldapUser.hasOwnProperty(ldapField)` /
// `ldapUser[ldapField]`. When the directory returns the attribute with a
// different case than the fieldmap key (common with AD, and exposed by the move
// from ldapjs — which lowercased keys — to ldapts, which preserves the server's
// case), the own-property lookup missed it and no email was synced. The fix
// reads the mapped attribute through getLDAPValue() like the rest of the code.
//
// Run: node tests/ldapEmailFieldmap.test.cjs
const assert = require('assert');
const fs = require('fs');
const path = require('path');
let passed = 0;
function test(name, fn) { fn(); passed += 1; console.log(' ok -', name); }
// Mirrors the getLDAPValue accessor wekan-ldap defines on Object.prototype:
// first case-insensitive key match wins.
function getLDAPValue(obj, prop) {
for (const key in obj) {
if (key.toLowerCase() === String(prop).toLowerCase()) return obj[key];
}
return undefined;
}
// Mirrors the FIXED `email` branch of getDataToSyncUserData(): read the mapped
// attribute case-insensitively, then push one or many addresses.
function emailsFromFieldmap(ldapUser, ldapField) {
const emailList = [];
const ldapValue = getLDAPValue(ldapUser, ldapField);
if (ldapValue === undefined || ldapValue === null || ldapValue === '') return emailList;
if (typeof ldapValue === 'object') {
for (const item of Array.from(ldapValue)) emailList.push({ address: item, verified: true });
} else {
emailList.push({ address: ldapValue, verified: true });
}
return emailList;
}
console.log('ldapEmailFieldmap:');
test('mail attribute returned with different case is still mapped to email', () => {
// directory returns "Mail" (AD-style), fieldmap key is "mail"
const ldapUser = { cn: 'Xy Zebounisso', Mail: 'bxy1234@example.org' };
const emails = emailsFromFieldmap(ldapUser, 'mail');
assert.deepStrictEqual(emails, [{ address: 'bxy1234@example.org', verified: true }]);
});
test('exact-case mail still works', () => {
const emails = emailsFromFieldmap({ mail: 'a@b.com' }, 'mail');
assert.deepStrictEqual(emails, [{ address: 'a@b.com', verified: true }]);
});
test('multi-valued mail attribute produces one entry per address', () => {
const emails = emailsFromFieldmap({ MAIL: ['a@b.com', 'c@d.com'] }, 'mail');
assert.deepStrictEqual(emails, [
{ address: 'a@b.com', verified: true },
{ address: 'c@d.com', verified: true },
]);
});
test('NEGATIVE: a genuinely absent attribute yields no email (no crash)', () => {
assert.deepStrictEqual(emailsFromFieldmap({ cn: 'x' }, 'mail'), []);
assert.deepStrictEqual(emailsFromFieldmap({ mail: '' }, 'mail'), []);
});
// Source guard: the real branch must read via getLDAPValue, not the old
// case-sensitive own-property access.
test('source: getDataToSyncUserData email branch is case-insensitive (getLDAPValue)', () => {
const src = fs.readFileSync(path.join(__dirname, '..', 'packages', 'wekan-ldap', 'server', 'sync.js'), 'utf8');
// strip line comments so the "must be gone" checks see CODE only (the fix's
// explanatory comment intentionally quotes the old tokens).
const branch = src.slice(src.indexOf("case 'email'"), src.indexOf('default:'))
.replace(/\/\/.*$/gm, '');
assert.ok(/getLDAPValue\(ldapField\)/.test(branch),
'email branch must read the mapped attribute via getLDAPValue');
assert.ok(!/ldapUser\.hasOwnProperty\(ldapField\)/.test(branch),
'the case-sensitive hasOwnProperty(ldapField) lookup must be gone');
assert.ok(!/ldapUser\[ldapField\]/.test(branch),
'the case-sensitive ldapUser[ldapField] index must be gone');
});
console.log(`\n${passed} passed`);