Files
wekan/server/accountWebhook.js
T
Lauri Ojansivu d2b40f64df Turn off all webhooks: the server sends none and refuses to add or change one.
Admin Panel / Settings / Features gets one switch, stored as
webhooksDisabled on the Settings document and saved by the site-admin
method saveWebhooksDisabled (audited). While it is on:

- nothing is sent: board and global webhooks, two-way webhooks,
  Mattermost direct messages, the grouped and scheduled queue, durable
  Sync webhooks, the login and registration webhook and bot dispatches
  all ask webhooksOff() first (models/lib/webhooksSwitch.js SENDERS);
- adding or changing a webhook is refused: the Integrations deny rules,
  the REST POST/PUT and activities routes (403 webhooks-disabled), a
  webhook's own delivery settings and saveAccountWebhook. Each refusal
  is recorded in Admin Panel / Problems as "Webhooks turned off"
  (authz.webhooks-disabled, blocksAccount: false); a silent non-send is
  never recorded;
- nothing is deleted, so turning it off brings every webhook back.
  Removing a webhook stays allowed.

Also the site-admin method webhooksReportPage for the coming Admin Panel
/ Problems report: every integration with its board and owner, the
token reduced to whether one is set, secrets masked in the URL.

Tests: tests/webhooksSwitch.test.cjs (the queue sends nothing and keeps
what waits; every webhookFetch caller is a sender that asks or is listed
as not a webhook; every write path refuses and records; report rows
carry no token), a server mocha test, and the existing vm-driven suites
supplied with the switch, with a negative case that nothing is sent.

Thanks to xet7 !
2026-11-22 20:34:47 +02:00

77 lines
3.3 KiB
JavaScript

// #2290: the login and registration webhook (models/lib/accountWebhook.js).
// Read and saved through site-admin methods, never published: the address
// and its token are the administrator's.
import { Meteor } from 'meteor/meteor';
import { Accounts } from 'meteor/accounts-base';
import { check } from 'meteor/check';
import Settings from '/models/settings';
import Users from '/models/users';
const { cleanAccountWebhook, sends, accountWebhookPayload } = require('/models/lib/accountWebhook');
async function requireSiteAdmin(userId) {
const user = userId && await Meteor.users.findOneAsync(userId, { fields: { isAdmin: 1 } });
if (!user || user.isAdmin !== true) throw new Meteor.Error('not-authorized');
}
// Every login asks, so the setting is cached briefly.
let cached = { at: 0, config: null };
async function accountWebhookConfig() {
if (Date.now() - cached.at < 10000) return cached.config;
const setting = await Settings.findOneAsync({}, { fields: { accountWebhook: 1 } });
cached = { at: Date.now(), config: cleanAccountWebhook((setting && setting.accountWebhook) || {}).value };
return cached.config;
}
// Fire and forget: a login must never wait for, or fail because of, the
// address it is reported to.
function send(event, user, ip) {
Meteor.defer(async () => {
try {
// Admin Panel / Settings / Features / Turn off all webhooks.
if (await require('/server/webhooksSwitch').webhooksOff()) return;
const config = await accountWebhookConfig();
if (!sends(config, event)) return;
const headers = { 'Content-Type': 'application/json' };
if (config.token) headers['X-Wekan-Token'] = config.token;
const { webhookFetch } = require('/server/sandstormEgress');
await webhookFetch(config.url, { method: 'POST', headers,
body: JSON.stringify(accountWebhookPayload(event, user, { config, ip })), timeoutMs: 10000 });
} catch (error) {
console.warn(`Login and registration webhook: ${event} not delivered: ${error && error.message}`);
}
});
}
if (Meteor.isServer) {
Accounts.onLogin((info) => {
if (!info || !info.user) return;
send('login', info.user, info.connection && info.connection.clientAddress);
});
Users.after.insert((userId, doc) => {
if (doc) send('register', doc);
});
}
Meteor.methods({
async getAccountWebhook() {
await requireSiteAdmin(this.userId);
const setting = await Settings.findOneAsync({}, { fields: { accountWebhook: 1 } });
return cleanAccountWebhook((setting && setting.accountWebhook) || {}).value;
},
async saveAccountWebhook(input) {
check(input, Object);
await requireSiteAdmin(this.userId);
// Its pane is hidden while webhooks are turned off; the stored one is kept.
await require('/server/webhooksSwitch').refuseWebhookWriteWhenOff({ userId: this.userId,
source: 'saveAccountWebhook', detail: 'Changed the login and registration webhook while webhooks are turned off.' });
const { value, error } = cleanAccountWebhook(input);
if (error) throw new Meteor.Error(error);
const setting = await Settings.findOneAsync({});
if (!setting) throw new Meteor.Error('settings-not-found');
await Settings.updateAsync(setting._id, { $set: { accountWebhook: value } });
cached = { at: 0, config: null };
return value;
},
});