Files
wekan/models/lib/samlConfig.js
T
Lauri Ojansivu ad51a03731 Log in by full-page redirect by default with OAuth2/OIDC, Google & co, SAML and CAS.
Every login that chooses between a popup and a redirect opened a popup by
default. Google's sign-in pages, and other identity providers', can send
Cross-Origin-Opener-Policy, which makes Meteor's popup look closed at
once: the login was tried before the provider answered, and the user
was back on the sign-in page with no error. Popups are also blocked in
iframes and on some phones. The full-page redirect has no popup to lose.

- OAUTH2_LOGIN_STYLE (OAuth2/OIDC): redirect unless it says popup, in the
  server, the Admin Panel catalog and the samples.
- OAUTH_PROVIDERS_LOGIN_STYLE (Google, GitHub, Facebook, X, Meteor
  Developer, Weibo, Meetup): redirect by default, in the code, Dockerfile,
  snap, Sandstorm package and samples.
- SAML: both identity-provider profiles log in by redirect;
  SAML_LOGIN_FLOW=popup still chooses the popup, and the client no longer
  falls back to a popup while the configuration loads.
- CAS: redirects unless "popup": true. Its redirect could never finish:
  nothing called Meteor.initCas, which the package now does at startup.
  The sign-in page also passed username and password as loginWithCas's
  options and callback, so its callback never ran, and the CAS redirect
  page echoed the request address into HTML unescaped.

A redirect login's error now reaches the reloaded sign-in page, from
accounts-oauth's page-load hook and the SAML and CAS events, instead of
the page coming back silently.

Verified against a freshly built and prepared bundle with the local
identity-provider suite and no login style set: OIDC, SAML, CAS and all
seven Meteor providers log in by redirect, and each refusal is shown on
the sign-in page (31 of 31 with Chromium). Its LDAP wrong-password case
expected wording that changed in 679a8b3499 and is corrected.

Thanks to xet7 !
2026-10-08 15:18:59 +03:00

114 lines
5.9 KiB
JavaScript

// Shared field catalog for runtime resolution, Admin Panel controls and platform checks.
const { resolveConfigValue } = require('./configResolver');
const SAML_FIELDS = [
['enabled', 'SAML_ENABLED', 'boolean', false],
['provider', 'SAML_PROVIDER', 'text', 'default'],
['entryPoint', 'SAML_ENTRYPOINT', 'url', ''],
['issuer', 'SAML_ISSUER', 'text', ''],
['cert', 'SAML_CERT', 'textarea', ''],
['idpSLORedirectURL', 'SAML_IDPSLO_REDIRECTURL', 'url', ''],
['privateKeyFile', 'SAML_PRIVATE_KEYFILE', 'text', ''],
['publicCertFile', 'SAML_PUBLIC_CERTFILE', 'text', ''],
['identifierFormat', 'SAML_IDENTIFIER_FORMAT', 'text', 'urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress'],
['localProfileMatchAttribute', 'SAML_LOCAL_PROFILE_MATCH_ATTRIBUTE', 'text', ''],
['attributesSAML', 'SAML_ATTRIBUTES', 'text', 'sn,givenName,mail'],
['mergeExistingUsers', 'SAML_MERGE_EXISTING_USERS', 'boolean', false],
// Identity providers differ in WHAT they sign and in how the login page is
// opened. A profile sets all of that at once; each field below it can still
// be set on its own, and a field left at Default follows the profile.
['idpProfile', 'SAML_IDP_PROFILE', 'choice', 'standard', ['standard', 'signed-assertion-redirect']],
['wantResponseSigned', 'SAML_WANT_RESPONSE_SIGNED', 'boolean', null],
['wantAssertionsSigned', 'SAML_WANT_ASSERTIONS_SIGNED', 'boolean', null],
['loginFlow', 'SAML_LOGIN_FLOW', 'choice', null, ['popup', 'redirect']],
].map(([key, envVar, type, defaultValue, choices]) => ({ key, envVar, type, defaultValue, choices }));
// What each identity-provider profile means.
// standard the Response is signed (node-saml's default and
// WeKan's behaviour so far).
// signed-assertion-redirect the identity provider signs the Assertion and
// not the Response - SAML 2.0 allows either - so
// the Assertion signature is required instead.
// Both log in by full-page redirect: the browser leaves WeKan for the identity
// provider and comes back in the same window. It used to be a popup for
// `standard`; a popup is blocked in iframes and on some phones, and an
// identity provider's Cross-Origin-Opener-Policy can cut it off from WeKan so
// the login never finishes. SAML_LOGIN_FLOW=popup still chooses the popup.
const SAML_PROFILES = {
standard: { wantResponseSigned: true, wantAssertionsSigned: false, loginFlow: 'redirect' },
'signed-assertion-redirect': { wantResponseSigned: false, wantAssertionsSigned: true, loginFlow: 'redirect' },
};
const PROFILE_KEYS = ['wantResponseSigned', 'wantAssertionsSigned', 'loginFlow'];
function cleanSamlOverrides(input) {
const clean = {};
for (const [key, value] of Object.entries(input)) {
const field = SAML_FIELDS.find(item => item.key === key);
if (!field) throw new TypeError(`Unknown SAML setting: ${key}`);
if (value === '' || value === null) continue; // restore environment/default
if (field.type === 'boolean') {
if (typeof value !== 'boolean') throw new TypeError(`Invalid ${field.envVar}`);
clean[key] = value;
} else if (field.type === 'choice') {
if (!field.choices.includes(value)) throw new TypeError(`Invalid ${field.envVar}`);
clean[key] = value;
} else {
if (typeof value !== 'string' || value.length > (key === 'cert' ? 65536 : 4096)) {
throw new TypeError(`Invalid ${field.envVar}`);
}
if (value.trim()) clean[key] = value.trim();
}
}
return clean;
}
function resolveSamlConfig(overrides = {}, env = process.env) {
const config = {}, sources = {};
for (const field of SAML_FIELDS) {
const resolved = resolveConfigValue(field.envVar, overrides[field.key], { readEnv: key => env[key] });
const value = resolved.value === undefined ? field.defaultValue : resolved.value;
config[field.key] = field.type === 'boolean' && value !== null ? value === true || value === 'true' : value;
sources[field.key] = { source: resolved.source, value: config[field.key] };
}
// Fields left at Default follow the chosen profile. An unknown profile name
// (a mistyped environment variable) is kept so validation can refuse it.
const profile = SAML_PROFILES[config.idpProfile];
for (const key of PROFILE_KEYS) {
if (config[key] === null || config[key] === undefined) {
config[key] = profile ? profile[key] : SAML_PROFILES.standard[key];
sources[key] = { source: 'profile', value: config[key] };
}
}
return { config, sources };
}
function validateSamlConfig(config) {
if (!/^[a-zA-Z0-9_-]+$/.test(config.provider)) throw new TypeError('Invalid SAML_PROVIDER');
for (const key of ['entryPoint', 'idpSLORedirectURL']) {
if (!config[key]) continue;
const url = new URL(config[key]);
if (!['http:', 'https:'].includes(url.protocol) || url.username || url.password) {
throw new TypeError(`Invalid SAML ${key} URL`);
}
}
for (const key of ['privateKeyFile', 'publicCertFile']) {
if (config[key] && (config[key].startsWith('/') || config[key].includes('\\') || config[key].split('/').includes('..'))) {
throw new TypeError(`SAML ${key} must be a path inside private/`);
}
}
for (const field of SAML_FIELDS) {
if (field.type === 'choice' && !field.choices.includes(config[field.key])) {
throw new TypeError(`Invalid ${field.envVar}`);
}
}
// At least one signature must be required and verified. With neither, any
// unsigned assertion naming any user would log in as that user.
if (config.wantResponseSigned !== true && config.wantAssertionsSigned !== true) {
throw new TypeError('SAML needs SAML_WANT_RESPONSE_SIGNED or SAML_WANT_ASSERTIONS_SIGNED');
}
if (config.enabled && (!config.entryPoint || !config.issuer || !config.cert)) {
throw new TypeError('SAML_ENTRYPOINT, SAML_ISSUER and SAML_CERT are required');
}
return config;
}
module.exports = { SAML_FIELDS, SAML_PROFILES, cleanSamlOverrides, resolveSamlConfig, validateSamlConfig };