Files
wekan/tests/oidcEmailDomainPolicy.test.cjs
T
Lauri Ojansivu eb49a4b1b6 Override every login environment variable in Admin Panel / People.
Each login method has its own section: LDAP, OAuth2 (OpenID Connect and
Oracle OIM), CAS and Header login are new or complete, and the Login pane
gets PASSWORD_LOGIN_ENABLED and ACCOUNTS_COMMON_LOGIN_EXPIRATION_IN_DAYS.
models/lib/authConfigCatalog.js lists all 103 variables; server/lib/
authConfig.js resolves each one at run time (Admin Panel value first, then
the environment variable) for the app and, through
globalThis.__wekanAuthEnv, for the wekan-ldap, wekan-oidc and
wekan-accounts-cas packages. OAuth2 and CAS service configurations and LDAP
background sync are reapplied when their settings are saved.

Passwords and client secrets (LDAP_AUTHENTIFICATION_PASSWORD, OAUTH2_SECRET)
never reach the browser: the page learns only whether one is set and where
it comes from. Other values have credentials written inside a URL masked on
the server, and a URL field refuses them.

Fixed on the way:
- The Admin Panel LDAP overrides never applied. The bridge read Settings with
  a server-side findOne(), which Meteor 3 refuses; the error was swallowed and
  the environment used. The settings are now held in an observed cache.
- LDAP Test connection never existed on the server (testConnection.js was not
  imported) and used the synchronous Meteor.user().
- PROPAGATE_OIDC_DATA=false turned the feature on.
- CAS read only the misspelled CASE_VALIDATE_URL; CAS_VALIDATE_URL now works
  and the old name still does.
- snap help named three group-filter keys snap set does not know.

Every catalog variable is now listed in the Dockerfile, the docker-compose
files, snap config and help, and start-wekan.sh/.bat; 14 were missing.
tests/authConfigCatalog.test.cjs pins resolution, secrets, input validation,
platform coverage, and - across the whole tree - that no code reads a login
variable outside the catalog or straight from process.env, and that no
server code calls the synchronous Settings.findOne() or Meteor.user().
tests/playwright/specs/admin-login-env-overrides.e2e.js drives the pages.

Thanks to xet7 !
2026-10-05 19:48:48 +03:00

73 lines
3.9 KiB
JavaScript

'use strict';
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const { isEmailDomainAllowed } = require('../packages/wekan-oidc/emailDomainPolicy');
assert.equal(isEmailDomainAllowed(undefined, undefined), true);
assert.equal(isEmailDomainAllowed(undefined, ''), true);
assert.equal(isEmailDomainAllowed('person@EXAMPLE.internal', ' example.internal, other.internal '), true);
assert.equal(isEmailDomainAllowed('person@other.internal', 'example.internal,other.internal'), true);
assert.equal(isEmailDomainAllowed('person@intranet', 'intranet'), true);
for (const email of [undefined, null, {}, '', '@example.internal', 'person@evil.internal',
'person@sub.example.internal', 'person@example.internal.evil', 'person@@example.internal',
'person@example.internal ', 'person@example.internal\n']) {
assert.equal(isEmailDomainAllowed(email, 'example.internal'), false, String(email));
}
for (const config of [' ', ',', 'example.internal,', '*.internal', 'example..internal',
'-example.internal', 'example-.internal', 'https://example.internal', {}]) {
assert.equal(isEmailDomainAllowed('person@example.internal', config), false, String(config));
}
const source = fs.readFileSync(path.join(__dirname, '../packages/wekan-oidc/oidc_server.js'), 'utf8');
// Read through authEnv: the Admin Panel / People / OAuth2 value, else the env var.
const guard = source.indexOf("if (!isEmailDomainAllowed(serviceData.email, authEnv('OAUTH2_ALLOWED_EMAIL_DOMAINS')))");
assert.ok(guard > source.indexOf('mergeWhitelistedClaims('));
assert.ok(guard < source.indexOf("await Meteor.callAsync('groupRoutineOnLogin'"));
assert.ok(guard < source.indexOf("await Meteor.callAsync('boardRoutineOnLogin'"));
assert.match(source.slice(guard, guard + 220), /throw new Meteor.Error\(403, 'Login forbidden'\)/);
// Exercise the actual OAuth callback against an intranet provider response.
// Denied logins must not reach either routine that mutates memberships.
const vm = require('node:vm');
const callbackSource = source.slice(source.indexOf("OAuth.registerService('oidc'"), source.indexOf('var userAgent'));
async function handshake(email, restriction) {
let callback;
const calls = [];
const context = {
OAuth: { registerService(name, version, options, handler) { callback = handler; } },
process: { env: {
OAUTH2_EMAIL_MAP: 'mail', OAUTH2_ID_MAP: 'sub',
OAUTH2_ALLOWED_EMAIL_DOMAINS: restriction,
} },
// The package's resolver (Admin Panel override, else the env var); no
// overrides here, so this scenario's environment.
authEnv: name => context.process.env[name],
isOracleOim: () => false,
getToken: async () => ({ access_token: 'fake-provider-token' }),
getUserInfo: async () => ({ sub: 'existing-or-new-user', mail: email }),
getTokenContent: () => null,
getConfiguration: async () => ({}),
mergeWhitelistedClaims() {}, isEmailDomainAllowed,
Meteor: {
Error: class extends Error { constructor(code, reason) { super(reason); this.error = code; } },
callAsync: async name => calls.push(name),
},
};
vm.runInNewContext(callbackSource, context);
try { return { result: await callback({}), calls }; }
catch (error) { return { error, calls }; }
}
(async () => {
const allowed = await handshake('person@example.internal', 'example.internal');
assert.equal(allowed.result.serviceData.email, 'person@example.internal');
assert.deepEqual(allowed.calls, ['groupRoutineOnLogin', 'boardRoutineOnLogin']);
for (const email of ['person@evil.internal', undefined]) {
const denied = await handshake(email, 'example.internal');
assert.equal(denied.error.error, 403);
assert.deepEqual(denied.calls, []);
}
const unrestricted = await handshake('person@other.internal', undefined);
assert.ok(unrestricted.result);
console.log('oidcEmailDomainPolicy: positive, negative and actual OAuth callback checks passed');
})().catch(error => { console.error(error); process.exitCode = 1; });