mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
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 !
97 lines
4.2 KiB
JavaScript
97 lines
4.2 KiB
JavaScript
// Admin Panel / Settings / Features / Turn off all webhooks
|
|
// (models/lib/webhooksSwitch.js): the switch on the server, the refusal of a
|
|
// webhook write while it is on, and Admin Panel / Problems / Webhooks.
|
|
import { Meteor } from 'meteor/meteor';
|
|
import { check, Match } from 'meteor/check';
|
|
import Settings from '/models/settings';
|
|
import Integrations from '/models/integrations';
|
|
|
|
const switchLib = require('/models/lib/webhooksSwitch');
|
|
|
|
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 activity asks, so the answer is cached for two seconds. Saving clears
|
|
// it in this process at once; another server process of the same WeKan sees
|
|
// the change within the two seconds.
|
|
const CACHE_MS = 2000;
|
|
let cached = { at: 0, off: false };
|
|
|
|
export async function webhooksOff() {
|
|
if (Date.now() - cached.at < CACHE_MS) return cached.off;
|
|
let off = false;
|
|
try {
|
|
const setting = await Settings.findOneAsync({}, { fields: { [switchLib.SETTING_FIELD]: 1 } });
|
|
off = switchLib.webhooksDisabled(setting);
|
|
} catch (e) {
|
|
// A database that cannot be read keeps the last known answer.
|
|
return cached.off;
|
|
}
|
|
cached = { at: Date.now(), off };
|
|
return off;
|
|
}
|
|
|
|
export function clearWebhooksSwitchCache() {
|
|
cached = { at: 0, off: false };
|
|
}
|
|
|
|
// A webhook write while the switch is on. No screen offers one then, so it is
|
|
// an attempt and is recorded in Admin Panel / Problems; the refusal stands even
|
|
// if the record cannot be written.
|
|
export function recordWebhookWriteRefused({ userId, source, detail }) {
|
|
try {
|
|
require('/server/lib/securityLog').record({
|
|
key: 'authz.webhooks-disabled', action: 'blocked', source, userId,
|
|
detail: String(detail || 'webhook write while webhooks are turned off').slice(0, 200),
|
|
});
|
|
} catch (e) { /* logging must never break the guard */ }
|
|
}
|
|
|
|
// For a Meteor method: throws 'webhooks-disabled' when the switch is on.
|
|
export async function refuseWebhookWriteWhenOff({ userId, source, detail }) {
|
|
if (!(await webhooksOff())) return;
|
|
recordWebhookWriteRefused({ userId, source, detail });
|
|
throw new Meteor.Error(switchLib.WEBHOOKS_DISABLED.error, switchLib.WEBHOOKS_DISABLED.reason);
|
|
}
|
|
|
|
// The report: every integration, newest first, filtered and searched on the
|
|
// server. Board titles and owners are read for the page's rows only.
|
|
async function webhooksReportPage(input) {
|
|
const Boards = require('/models/boards').default;
|
|
const { filter, search, limit, skip } = switchLib.cleanReportQuery(input);
|
|
const docs = await Integrations.find(switchLib.reportSelector(filter), {
|
|
fields: switchLib.REPORT_FIELDS, sort: { createdAt: -1, _id: 1 }, transform: null,
|
|
}).fetchAsync();
|
|
const boardIds = [...new Set(docs.map(d => d.boardId).filter(id => id && id !== '_global'))];
|
|
const userIds = [...new Set(docs.map(d => d.userId).filter(Boolean))];
|
|
const [boards, users] = await Promise.all([
|
|
boardIds.length ? Boards.find({ _id: { $in: boardIds } }, { fields: { title: 1, slug: 1 }, transform: null }).fetchAsync() : [],
|
|
userIds.length ? Meteor.users.find({ _id: { $in: userIds } }, { fields: { username: 1, 'profile.fullname': 1 } }).fetchAsync() : [],
|
|
]);
|
|
const boardOf = new Map(boards.map(b => [b._id, b]));
|
|
const userOf = new Map(users.map(u => [u._id, u]));
|
|
const rows = docs
|
|
.map(doc => switchLib.webhookReportRow(doc, { board: boardOf.get(doc.boardId), owner: userOf.get(doc.userId) }))
|
|
.filter(row => switchLib.matchesSearch(row, search));
|
|
return { rows: rows.slice(skip, skip + limit), total: rows.length, webhooksOff: await webhooksOff() };
|
|
}
|
|
|
|
Meteor.methods({
|
|
async saveWebhooksDisabled(value) {
|
|
check(value, Boolean);
|
|
await requireSiteAdmin(this.userId);
|
|
const setting = await Settings.findOneAsync({});
|
|
if (!setting) throw new Meteor.Error('settings-not-found');
|
|
await Settings.updateAsync(setting._id, { $set: { [switchLib.SETTING_FIELD]: value } });
|
|
clearWebhooksSwitchCache();
|
|
return value;
|
|
},
|
|
async webhooksReportPage(input) {
|
|
check(input, Match.Maybe(Object));
|
|
await requireSiteAdmin(this.userId);
|
|
return webhooksReportPage(input);
|
|
},
|
|
});
|