mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 20:45:00 -05:00
Extract the pure, security- and correctness-critical logic of the recent features into Meteor-free models/lib/* modules (so production and tests share one implementation) and add plain-Node tests with positive AND negative cases, wired into test:unit:node: - models/lib/mongoSelectorSafety.js hasWhere() — the windowed card publication refuses a client selector carrying $where (server-side JS). Tests: safe selectors pass; $where at top level / in $and / deep in arrays / in $elemMatch is rejected. (was inline in server/publications/cardsWindow.js) - models/lib/cardsLoading.js resolveCardsLoadingMode / parseCardsLoadingEnv / windowCountId — CARDS_LOADING maps to 'lazy' only for exactly "lazy" (garbage never enables the experimental mode); a falsy swimlane id collapses to one count-doc id (listBody uses undefined, listHeader uses ''). (used by server/cards-loading.js, server/models/settings.js, client/lib/lazyCards.js) - models/lib/base64Chunk.js encodeAligned/encodeFinal — streaming an attachment into the JSON export as base64 in any chunk sizes reconstructs exactly the whole-file base64; aligned pieces never carry padding. (used by Exporter.buildStream) - models/lib/backupPaths.js filesRootFrom/scheduleText — WRITABLE_PATH -> files root (no double /files suffix, Windows-safe) and the synced-cron schedule text (daily/weekly/monthly + defaults, unknown frequency falls back to daily). (used by server/methods/backup.js) 41 new assertions across 4 files, all passing; whole test:unit:node suite green.
529 lines
17 KiB
JavaScript
529 lines
17 KiB
JavaScript
import { Meteor } from 'meteor/meteor';
|
|
import { check } from 'meteor/check';
|
|
import { Accounts } from 'meteor/accounts-base';
|
|
import { Email } from 'meteor/email';
|
|
import { ServiceConfiguration } from 'meteor/service-configuration';
|
|
import { WebApp } from 'meteor/webapp';
|
|
import Settings from '/models/settings';
|
|
import InvitationCodes from '/models/invitationCodes';
|
|
import EmailLocalization from '/server/lib/emailLocalization';
|
|
import { ensureIndex } from '/server/lib/mongoStartup';
|
|
import { Authentication } from '/server/authentication';
|
|
import { sendJsonResult } from '/server/apiMiddleware';
|
|
const { parseCardsLoadingEnv } = require('/models/lib/cardsLoading');
|
|
|
|
const getReactiveCache = () => require('/imports/reactiveCache').ReactiveCache;
|
|
const getTAPi18n = () => require('/imports/i18n').TAPi18n;
|
|
const { SimpleSchema } = require('/imports/simpleSchema');
|
|
const {
|
|
normalizeAuthenticationMethod,
|
|
resolveDefaultAuthenticationMethod,
|
|
} = require('/models/lib/authenticationMethod');
|
|
|
|
const isSandstorm =
|
|
Meteor.settings && Meteor.settings.public && Meteor.settings.public.sandstorm;
|
|
|
|
function getRandomNum(min, max) {
|
|
const range = max - min;
|
|
const rand = Math.random();
|
|
return min + Math.round(rand * range);
|
|
}
|
|
|
|
function getEnvVar(name) {
|
|
const value = process.env[name];
|
|
if (value) {
|
|
return value;
|
|
}
|
|
throw new Meteor.Error([
|
|
'var-not-exist',
|
|
`The environment variable ${name} does not exist`,
|
|
]);
|
|
}
|
|
|
|
function loadOidcConfig(service) {
|
|
check(service, String);
|
|
return ServiceConfiguration.configurations.findOneAsync({ service });
|
|
}
|
|
|
|
async function sendInvitationEmail(_id) {
|
|
const icode = await getReactiveCache().getInvitationCode(_id);
|
|
const author = await getReactiveCache().getCurrentUser();
|
|
try {
|
|
const authorUser = await getReactiveCache().getUser(icode.authorId);
|
|
const fullName = authorUser?.profile?.fullname || '';
|
|
|
|
const params = {
|
|
email: icode.email,
|
|
inviter:
|
|
fullName !== ''
|
|
? `${fullName} (${authorUser.username} )`
|
|
: authorUser.username,
|
|
user: icode.email.split('@')[0],
|
|
icode: icode.code,
|
|
// FlowRouter is client-only; on the server it has no routes and yields a
|
|
// generic link. The sign-up route is the static path '/sign-up'.
|
|
url: Meteor.absoluteUrl('sign-up'),
|
|
};
|
|
const lang = author.getLanguage();
|
|
await EmailLocalization.sendEmail({
|
|
to: icode.email,
|
|
from: Accounts.emailTemplates.from,
|
|
subject: 'email-invite-register-subject',
|
|
text: 'email-invite-register-text',
|
|
params,
|
|
language: lang,
|
|
});
|
|
} catch (e) {
|
|
await InvitationCodes.removeAsync(_id);
|
|
throw new Meteor.Error('email-fail', e.message);
|
|
}
|
|
}
|
|
|
|
async function isNonAdminAllowedToSendMail(currentUser) {
|
|
const currSett = await getReactiveCache().getCurrentSetting();
|
|
let isAllowed = false;
|
|
if (
|
|
currSett &&
|
|
currSett.disableRegistration &&
|
|
currSett.mailDomainName !== undefined &&
|
|
currSett.mailDomainName !== ''
|
|
) {
|
|
for (let i = 0; i < currentUser.emails.length; i++) {
|
|
if (currentUser.emails[i].address.endsWith(currSett.mailDomainName)) {
|
|
isAllowed = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return isAllowed;
|
|
}
|
|
|
|
function isLdapEnabled() {
|
|
return process.env.LDAP_ENABLE === 'true' || process.env.LDAP_ENABLE === true;
|
|
}
|
|
|
|
function isOauth2Enabled() {
|
|
return (
|
|
process.env.OAUTH2_ENABLED === 'true' ||
|
|
process.env.OAUTH2_ENABLED === true
|
|
);
|
|
}
|
|
|
|
function isCasEnabled() {
|
|
return process.env.CAS_ENABLED === 'true' || process.env.CAS_ENABLED === true;
|
|
}
|
|
|
|
function isApiEnabled() {
|
|
return process.env.WITH_API === 'true' || process.env.WITH_API === true;
|
|
}
|
|
|
|
Meteor.startup(async () => {
|
|
await ensureIndex(Settings, { modifiedAt: -1 });
|
|
const setting = await getReactiveCache().getCurrentSetting();
|
|
// #5879: honour the DEFAULT_AUTHENTICATION_METHOD env var. It used to be
|
|
// ignored (settings only ever seeded 'password'), so operators configuring it
|
|
// via Kubernetes/Helm saw no effect. When set it is authoritative, so the
|
|
// default login method can be configured by env without the Admin Panel.
|
|
const envDefaultAuthenticationMethod = normalizeAuthenticationMethod(
|
|
process.env.DEFAULT_AUTHENTICATION_METHOD,
|
|
);
|
|
// CARDS_LOADING env: 'lazy' or 'all' (anything else / unset → undefined = leave
|
|
// to the stored setting, defaulting to 'all'). Like DEFAULT_AUTHENTICATION_METHOD,
|
|
// when the env var is set it is authoritative on every startup.
|
|
const envCardsLoading = parseCardsLoadingEnv(process.env.CARDS_LOADING);
|
|
if (!setting) {
|
|
const now = new Date();
|
|
const domain = process.env.ROOT_URL.match(/\/\/(?:www\.)?(.*)?(?:\/)?/)[1];
|
|
const from = `Boards Support <support@${domain}>`;
|
|
const defaultSetting = {
|
|
disableRegistration: false,
|
|
mailServer: {
|
|
username: '',
|
|
password: '',
|
|
host: '',
|
|
port: '',
|
|
enableTLS: false,
|
|
from,
|
|
},
|
|
createdAt: now,
|
|
modifiedAt: now,
|
|
displayAuthenticationMethod: true,
|
|
defaultAuthenticationMethod:
|
|
resolveDefaultAuthenticationMethod(envDefaultAuthenticationMethod, undefined),
|
|
cardsLoading: envCardsLoading || 'all',
|
|
};
|
|
await Settings.insertAsync(defaultSetting);
|
|
} else if (
|
|
envDefaultAuthenticationMethod &&
|
|
setting.defaultAuthenticationMethod !== envDefaultAuthenticationMethod
|
|
) {
|
|
// Existing install: keep the stored setting in sync with the env var so it
|
|
// wins on every startup (the operator's env is the source of truth).
|
|
await Settings.updateAsync(setting._id, {
|
|
$set: { defaultAuthenticationMethod: envDefaultAuthenticationMethod },
|
|
});
|
|
}
|
|
// Existing install: when CARDS_LOADING is set it wins on startup; otherwise
|
|
// backfill a missing value so the Admin Panel control has something to show.
|
|
if (setting) {
|
|
if (envCardsLoading && setting.cardsLoading !== envCardsLoading) {
|
|
await Settings.updateAsync(setting._id, { $set: { cardsLoading: envCardsLoading } });
|
|
} else if (!setting.cardsLoading) {
|
|
await Settings.updateAsync(setting._id, { $set: { cardsLoading: 'all' } });
|
|
}
|
|
}
|
|
// Mirror the effective mode onto Meteor.settings.public so publications can read
|
|
// it synchronously; keep it in sync whenever an admin changes it in the UI.
|
|
{
|
|
const eff = await getReactiveCache().getCurrentSetting();
|
|
if (!Meteor.settings.public) Meteor.settings.public = {};
|
|
Meteor.settings.public.cardsLoading = (eff && eff.cardsLoading) || 'all';
|
|
}
|
|
if (isSandstorm) {
|
|
const newSetting = await getReactiveCache().getCurrentSetting();
|
|
if (!process.env.MAIL_URL && newSetting.mailUrl()) {
|
|
process.env.MAIL_URL = newSetting.mailUrl();
|
|
}
|
|
Accounts.emailTemplates.from = process.env.MAIL_FROM
|
|
? process.env.MAIL_FROM
|
|
: newSetting.mailServer.from;
|
|
} else {
|
|
Accounts.emailTemplates.from = process.env.MAIL_FROM;
|
|
}
|
|
});
|
|
|
|
// Keep the server-side effective card-loading mode in sync when an admin changes
|
|
// it in Admin Panel / Features (publications read Meteor.settings.public.cardsLoading).
|
|
// A page reload is needed for already-open boards to switch modes.
|
|
Settings.after.update((userId, doc, fieldNames) => {
|
|
if (fieldNames.includes('cardsLoading')) {
|
|
if (!Meteor.settings.public) Meteor.settings.public = {};
|
|
Meteor.settings.public.cardsLoading = doc.cardsLoading || 'all';
|
|
}
|
|
});
|
|
|
|
if (isSandstorm) {
|
|
Settings.after.update((userId, doc, fieldNames) => {
|
|
if (fieldNames.includes('mailServer') && doc.mailServer.host) {
|
|
const protocol = doc.mailServer.enableTLS ? 'smtps://' : 'smtp://';
|
|
if (!doc.mailServer.username && !doc.mailServer.password) {
|
|
process.env.MAIL_URL = `${protocol}${doc.mailServer.host}:${doc.mailServer.port}/`;
|
|
} else {
|
|
process.env.MAIL_URL = `${protocol}${doc.mailServer.username}:${encodeURIComponent(
|
|
doc.mailServer.password,
|
|
)}@${doc.mailServer.host}:${doc.mailServer.port}/`;
|
|
}
|
|
Accounts.emailTemplates.from = doc.mailServer.from;
|
|
}
|
|
});
|
|
}
|
|
|
|
Meteor.methods({
|
|
async sendInvitation(emails, boards) {
|
|
let rc = 0;
|
|
check(emails, [String]);
|
|
check(boards, [String]);
|
|
|
|
const user = await getReactiveCache().getCurrentUser();
|
|
if (!user.isAdmin && !(await isNonAdminAllowedToSendMail(user))) {
|
|
rc = -1;
|
|
throw new Meteor.Error('not-allowed');
|
|
}
|
|
|
|
for (const email of emails) {
|
|
if (email && SimpleSchema.RegEx.Email.test(email)) {
|
|
const userExist = await getReactiveCache().getUser({ email });
|
|
if (userExist) {
|
|
rc = -1;
|
|
throw new Meteor.Error(
|
|
'user-exist',
|
|
`The user with the email ${email} has already an account.`,
|
|
);
|
|
}
|
|
|
|
const invitation = await getReactiveCache().getInvitationCode({ email });
|
|
if (invitation) {
|
|
await InvitationCodes.updateAsync(invitation, {
|
|
$set: { boardsToBeInvited: boards },
|
|
});
|
|
await sendInvitationEmail(invitation._id);
|
|
} else {
|
|
const code = getRandomNum(100000, 999999);
|
|
const _id = await InvitationCodes.insertAsync({
|
|
code,
|
|
email,
|
|
boardsToBeInvited: boards,
|
|
createdAt: new Date(),
|
|
authorId: this.userId,
|
|
});
|
|
if (_id) {
|
|
await sendInvitationEmail(_id);
|
|
} else {
|
|
rc = -1;
|
|
throw new Meteor.Error(
|
|
'invitation-generated-fail',
|
|
'Failed to create invitation code',
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return rc;
|
|
},
|
|
|
|
async sendSMTPTestEmail() {
|
|
if (!this.userId) {
|
|
throw new Meteor.Error('invalid-user');
|
|
}
|
|
const user = await getReactiveCache().getCurrentUser();
|
|
// Sending an SMTP test (and surfacing the server's SMTP error messages) is
|
|
// an admin-only diagnostic, matching the client gating (`unless currentUser.isAdmin`).
|
|
if (!user || !user.isAdmin) {
|
|
throw new Meteor.Error('error-notAuthorized');
|
|
}
|
|
if (!user.emails || !user.emails[0] || !user.emails[0].address) {
|
|
throw new Meteor.Error('email-invalid');
|
|
}
|
|
this.unblock();
|
|
const lang = user.getLanguage();
|
|
try {
|
|
await Email.sendAsync({
|
|
to: user.emails[0].address,
|
|
from: Accounts.emailTemplates.from,
|
|
subject: getTAPi18n().__('email-smtp-test-subject', { lng: lang }),
|
|
text: getTAPi18n().__('email-smtp-test-text', { lng: lang }),
|
|
});
|
|
} catch ({ message }) {
|
|
throw new Meteor.Error(
|
|
'email-fail',
|
|
`${getTAPi18n().__('email-fail-text', { lng: lang })}: ${message}`,
|
|
message,
|
|
);
|
|
}
|
|
return {
|
|
message: 'email-sent',
|
|
email: user.emails[0].address,
|
|
};
|
|
},
|
|
|
|
async getCustomUI() {
|
|
const setting = await getReactiveCache().getCurrentSetting();
|
|
if (!setting.productName) {
|
|
return {
|
|
productName: '',
|
|
};
|
|
}
|
|
return {
|
|
productName: `${setting.productName}`,
|
|
};
|
|
},
|
|
|
|
async isDisableRegistration() {
|
|
const setting = await getReactiveCache().getCurrentSetting();
|
|
return setting.disableRegistration === true;
|
|
},
|
|
|
|
async isDisableForgotPassword() {
|
|
const setting = await getReactiveCache().getCurrentSetting();
|
|
return setting.disableForgotPassword === true;
|
|
},
|
|
|
|
getMatomoConf() {
|
|
return {
|
|
address: getEnvVar('MATOMO_ADDRESS'),
|
|
siteId: getEnvVar('MATOMO_SITE_ID'),
|
|
doNotTrack: process.env.MATOMO_DO_NOT_TRACK || false,
|
|
withUserName: process.env.MATOMO_WITH_USERNAME || false,
|
|
};
|
|
},
|
|
|
|
_isLdapEnabled() {
|
|
return isLdapEnabled();
|
|
},
|
|
|
|
_isOauth2Enabled() {
|
|
return isOauth2Enabled();
|
|
},
|
|
|
|
_isCasEnabled() {
|
|
return isCasEnabled();
|
|
},
|
|
|
|
_isApiEnabled() {
|
|
return isApiEnabled();
|
|
},
|
|
|
|
getAuthenticationsEnabled() {
|
|
return {
|
|
ldap: isLdapEnabled(),
|
|
oauth2: isOauth2Enabled(),
|
|
cas: isCasEnabled(),
|
|
};
|
|
},
|
|
|
|
getOauthServerUrl() {
|
|
return process.env.OAUTH2_SERVER_URL;
|
|
},
|
|
|
|
getOauthDashboardUrl() {
|
|
return process.env.DASHBOARD_URL;
|
|
},
|
|
|
|
// OIDC RP-initiated logout (https://openid.net/specs/openid-connect-rpinitiated-1_0.html).
|
|
// When OAUTH2_LOGOUT_ENDPOINT is set (e.g. Keycloak's
|
|
// /realms/<realm>/protocol/openid-connect/logout), build the end_session URL so
|
|
// logout terminates the identity provider session and returns the user to Wekan
|
|
// via post_logout_redirect_uri, instead of dumping them on the provider's home
|
|
// page (which errors for non-admin users). See issue #6158.
|
|
getOauthLogoutUrl() {
|
|
const endpoint = process.env.OAUTH2_LOGOUT_ENDPOINT;
|
|
if (!endpoint) return '';
|
|
const serverUrl = (process.env.OAUTH2_SERVER_URL || '').replace(/\/$/, '');
|
|
const base = /^https?:\/\//.test(endpoint) ? endpoint : serverUrl + endpoint;
|
|
const params = [
|
|
'post_logout_redirect_uri=' + encodeURIComponent(Meteor.absoluteUrl()),
|
|
];
|
|
if (process.env.OAUTH2_CLIENT_ID) {
|
|
params.push('client_id=' + encodeURIComponent(process.env.OAUTH2_CLIENT_ID));
|
|
}
|
|
return base + (base.includes('?') ? '&' : '?') + params.join('&');
|
|
},
|
|
|
|
getDefaultAuthenticationMethod() {
|
|
return process.env.DEFAULT_AUTHENTICATION_METHOD;
|
|
},
|
|
|
|
isPasswordLoginEnabled() {
|
|
return !(process.env.PASSWORD_LOGIN_ENABLED === 'false');
|
|
},
|
|
|
|
isOidcRedirectionEnabled() {
|
|
return (
|
|
process.env.OIDC_REDIRECTION_ENABLED === 'true' &&
|
|
Object.keys(loadOidcConfig('oidc')).length > 0
|
|
);
|
|
},
|
|
|
|
async getServiceConfiguration(service) {
|
|
const config = await loadOidcConfig(service);
|
|
if (!config) return null;
|
|
// Never expose the client secret to the caller
|
|
const { secret, ...publicConfig } = config;
|
|
return publicConfig;
|
|
},
|
|
});
|
|
|
|
// GlobalAdmin REST API for the Admin Panel global settings.
|
|
//
|
|
// The fields a global admin may read and write over REST. Deliberately excludes
|
|
// `mailServer` (it holds SMTP credentials) so the REST API never exposes or
|
|
// overwrites secrets; SMTP stays admin-panel / env only.
|
|
const REST_SETTINGS_FIELDS = [
|
|
'disableRegistration',
|
|
'disableForgotPassword',
|
|
'productName',
|
|
'displayAuthenticationMethod',
|
|
'defaultAuthenticationMethod',
|
|
'spinnerName',
|
|
'hideLogo',
|
|
'hideCardCounterList',
|
|
'hideBoardMemberList',
|
|
'customLoginLogoImageUrl',
|
|
'customLoginLogoLinkUrl',
|
|
'customHelpLinkUrl',
|
|
'textBelowCustomLoginLogo',
|
|
'automaticLinkedUrlSchemes',
|
|
'customTopLeftCornerLogoImageUrl',
|
|
'customTopLeftCornerLogoLinkUrl',
|
|
'customTopLeftCornerLogoHeight',
|
|
'oidcBtnText',
|
|
'mailDomainName',
|
|
'legalNotice',
|
|
'customHeadEnabled',
|
|
'customHeadMetaTags',
|
|
'customHeadLinkTags',
|
|
'customManifestEnabled',
|
|
'customManifestContent',
|
|
'customAssetLinksEnabled',
|
|
'customAssetLinksContent',
|
|
'accessibilityPageEnabled',
|
|
'accessibilityTitle',
|
|
'accessibilityContent',
|
|
'supportPopupText',
|
|
'supportPageEnabled',
|
|
'supportPagePublic',
|
|
'supportTitle',
|
|
'supportPageText',
|
|
];
|
|
|
|
function pickSettingsFields(doc) {
|
|
const out = { _id: doc && doc._id };
|
|
if (doc) {
|
|
REST_SETTINGS_FIELDS.forEach(field => {
|
|
if (doc[field] !== undefined) {
|
|
out[field] = doc[field];
|
|
}
|
|
});
|
|
}
|
|
return out;
|
|
}
|
|
|
|
/**
|
|
* @operation get_global_settings
|
|
* @tag Settings
|
|
*
|
|
* @summary Get the global Admin Panel settings
|
|
*
|
|
* @description Only the global admin can call this. SMTP/mail-server
|
|
* credentials are never returned.
|
|
*
|
|
* @return_type Settings
|
|
*/
|
|
WebApp.handlers.get('/api/settings', async function(req, res) {
|
|
try {
|
|
await Authentication.checkUserId(req.userId);
|
|
const setting = await Settings.findOneAsync({});
|
|
sendJsonResult(res, { code: 200, data: pickSettingsFields(setting) });
|
|
} catch (error) {
|
|
sendJsonResult(res, { code: 200, data: error });
|
|
}
|
|
});
|
|
|
|
/**
|
|
* @operation update_global_settings
|
|
* @tag Settings
|
|
*
|
|
* @summary Update the global Admin Panel settings
|
|
*
|
|
* @description Only the global admin can call this. The request body is an
|
|
* object whose keys are settings fields to update (see get_global_settings for
|
|
* the list). Unknown keys and `mailServer` are ignored.
|
|
*
|
|
* @param {Object} settings the settings fields to set
|
|
* @return_type Settings
|
|
*/
|
|
WebApp.handlers.put('/api/settings', async function(req, res) {
|
|
try {
|
|
await Authentication.checkUserId(req.userId);
|
|
const setting = await Settings.findOneAsync({});
|
|
if (!setting) {
|
|
sendJsonResult(res, { code: 404, data: { error: 'Settings not found' } });
|
|
return;
|
|
}
|
|
const body = req.body || {};
|
|
const $set = {};
|
|
REST_SETTINGS_FIELDS.forEach(field => {
|
|
if (body[field] !== undefined) {
|
|
$set[field] = body[field];
|
|
}
|
|
});
|
|
if (Object.keys($set).length > 0) {
|
|
await Settings.updateAsync(setting._id, { $set });
|
|
}
|
|
const updated = await Settings.findOneAsync({});
|
|
sendJsonResult(res, { code: 200, data: pickSettingsFields(updated) });
|
|
} catch (error) {
|
|
sendJsonResult(res, { code: 200, data: error });
|
|
}
|
|
});
|