Files
wekan/tests/ldapTestConnectionProbe.test.cjs
T
Lauri Ojansivu 8ab7d46714 Trust the operating system's certificate authorities for TLS.
Since v12.15 the LDAP server's certificate is verified by default, and
Node.js verified it only against the Mozilla list it is built with. A
company root CA installed on the server with update-ca-certificates was
unknown to WeKan, so after upgrading from v12.14 the snap refused LDAPS
logins with UNABLE_TO_VERIFY_LEAF_SIGNATURE.

server/main.js now adds the operating system's CAs to Node's own before any
application code runs: tls.getCACertificates('system'), and the distribution
CA bundle files Go's crypto/x509 reads. The bundle file matters in the snap:
on Debian and Ubuntu snapd keeps the host's /etc/ssl, but the per-CA links
update-ca-certificates makes point into /usr/local/share/ca-certificates,
which the snap sees from its base. Verification is not relaxed - a CA the
system does not have is still refused - and --use-bundled-ca keeps Node's
list only. It covers LDAPS, mail, webhooks and OAuth2 in every launcher.

A refused LDAP certificate now says what to do, in the log and in Admin
Panel's Test connection: install the CA on the server, or set LDAP_CA_CERT.
No hint suggests turning verification off.

tests/systemCaCertificates6762.test.cjs runs a TLS server with a certificate
from a test private CA (its key is not kept) and checks the reported error
without the CA, verification with it in the system bundle, refusal without
it, --use-bundled-ca, and the hints. Two LDAP guards follow the extended
error handling; prune-unreachable-npm.mjs leaves the regex guard's reviewed
list, as it no longer builds a RegExp.

Thanks to fvb-sb and xet7 !

Fixes #6762.
2026-10-12 11:14:34 +03:00

72 lines
3.8 KiB
JavaScript

'use strict';
// Admin Panel / People / LDAP / Test connection without a service account
// (maintainer decision of 2026-10-08). ldapts opens its socket lazily, so the
// old test - connect, then bind only when LDAP_AUTHENTIFICATION is set -
// reported success for a host that does not even resolve. Now a test without
// credentials does an anonymous base search of LDAP_BASEDN and succeeds only
// when the directory answered.
//
// Run: node --test tests/ldapTestConnectionProbe.test.cjs
const {test} = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const net = require('node:net');
const path = require('node:path');
const ROOT = path.join(__dirname, '..');
const read = rel => fs.readFileSync(path.join(ROOT, rel), 'utf8');
const {connectionProbe, anonymousSearchOptions} = require('../packages/wekan-ldap/server/testConnectionProbe');
test('with a service account the bind is the proof', () => {
assert.deepEqual(connectionProbe({Authentication: true, BaseDN: 'dc=example,dc=org'}), {kind: 'bind'});
assert.deepEqual(connectionProbe({Authentication: true}), {kind: 'bind'});
});
test('without one, an anonymous base search of LDAP_BASEDN', () => {
assert.deepEqual(connectionProbe({Authentication: false, BaseDN: ' dc=example,dc=org '}),
{kind: 'anonymous-search', baseDN: 'dc=example,dc=org'});
// 'true' as a string is not a configured service account: bindIfNecessary
// compares with === true too, so the two must agree.
assert.equal(connectionProbe({Authentication: 'true', BaseDN: 'o=x'}).kind, 'anonymous-search');
assert.deepEqual(anonymousSearchOptions(),
{scope: 'base', filter: '(objectClass=*)', attributes: ['1.1'], sizeLimit: 1},
'the base entry only, and no attributes of anybody');
});
test('negative: no service account and no base DN is an error, never success', () => {
for (const options of [{}, {BaseDN: ''}, {BaseDN: ' '}, {Authentication: false, BaseDN: undefined}, undefined]) {
const probe = connectionProbe(options);
assert.ok(probe.error, JSON.stringify(options));
assert.equal(probe.kind, undefined);
}
});
test('the method runs the probe before it can answer success', () => {
const src = read('packages/wekan-ldap/server/testConnection.js');
const probeAt = src.indexOf('connectionProbe(ldap.options)');
const successAt = src.indexOf("message: 'Connection_success'");
assert.ok(probeAt !== -1 && probeAt < successAt);
assert.match(src, /if \(probe\.error\) \{\s*throw new Meteor\.Error\('LDAP_not_tested'/);
assert.match(src, /await ldap\.client\.search\(probe\.baseDN, anonymousSearchOptions\(\)\)/);
// The directory's own message reaches the Admin Panel as the reason - with,
// since #6762, what to do about a refused certificate appended to it.
assert.match(src, /throw new Meteor\.Error\(error\.name \|\| error\.message, hint \? `\$\{error\.message\} \$\{hint\}` : error\.message\)/);
});
// The reported case, against the library WeKan ships: no directory listening.
// The old flow did no operation at all here; the anonymous search fails.
const LDAPTS = path.join(ROOT, 'packages/wekan-ldap/.npm/package/node_modules/ldapts');
test('negative, real ldapts: the anonymous search fails when nothing answers', {skip: !fs.existsSync(LDAPTS) && 'ldapts not installed (run a Meteor build first)'}, async () => {
const {Client} = require(LDAPTS);
// A port that was just free: bind it, close it, connect to it.
const port = await new Promise(resolve => {
const server = net.createServer().listen(0, '127.0.0.1', () => {
const {port: free} = server.address();
server.close(() => resolve(free));
});
});
const client = new Client({url: `ldap://127.0.0.1:${port}`, connectTimeout: 2000, timeout: 2000});
await assert.rejects(client.search('dc=example,dc=org', anonymousSearchOptions()));
await client.unbind().catch(() => {});
});