Files
wekan/server/models/integrations.js
T
Lauri Ojansivu ab785acd1e Choose what notifications contain, and group and schedule them, for e-mail, the tray and webhooks.
One delivery model (models/lib/notificationDelivery.js) for every channel -
e-mail, the notification tray and outgoing webhooks - set in the
Notifications popup at Admin Panel, board and Member Settings scope, and in
each one-way webhook's form:

- Content. Webhooks: the text on or off, and which property groups are sent
  (standard, ids, names, links, actor, people, details), with checklist,
  list, swimlane and board names, before/after values, the card description
  and links added as opt-in groups. E-mail and tray: the classic sentence or
  a clear layout - the board as a link first, the actor and card in bold -
  and which parts are shown.
- Grouping: one message per event, per card, per board, or all together.
- Scheduling: immediately, every 5 minutes to 24 hours, or once a day, with
  optional quiet hours, in the time zone of whoever set it.

The most specific explicit setting wins: member, then board, then Admin
Panel for e-mail and tray; the webhook, then its board, then Admin Panel for
webhooks. A member can only leave their own name out of outgoing webhooks.
The built-in defaults keep today's behaviour. Grouped and scheduled webhooks
and e-mail go through durable queues that survive restarts; queued webhooks
use the SSRF guard and log a blocked address in Admin Panel - Problems.
Values of admin-only custom fields now reach only board admins in e-mail and
tray, and never a webhook.

The new strings are in English in every locale, pending Transifex.

Thanks to HaKePlan, Meeques and xet7 !

Fixes #3695,
Fixes #5171.
2026-10-10 15:28:26 +03:00

241 lines
7.6 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { WebApp } from 'meteor/webapp';
import { Authentication } from '/server/authentication';
import { sendJsonResult } from '/server/apiMiddleware';
import { ReactiveCache } from '/imports/reactiveCache';
import Integrations from '/models/integrations';
import { validateAttachmentUrl } from '/models/lib/attachmentUrlValidation';
import { ensureIndex } from '/server/lib/mongoStartup';
const { normalizeDeliverySettings } = require('/models/lib/notificationDelivery');
// ErrorBleed: refusals answer with their real status and a safe message.
const { publicErrorData } = require('/server/lib/apiResponseHelpers');
Meteor.startup(async () => {
await ensureIndex(Integrations, { modifiedAt: -1 });
await ensureIndex(Integrations, { boardId: 1 });
});
WebApp.handlers.get('/api/boards/:boardId/integrations', async function(req, res) {
try {
const paramBoardId = req.params.boardId;
await Authentication.checkBoardAccess(req.userId, paramBoardId);
const data = (await ReactiveCache.getIntegrations(
{ boardId: paramBoardId },
{ fields: { token: 0 } },
)).map(function(doc) {
return doc;
});
sendJsonResult(res, { code: 200, data });
} catch (error) {
sendJsonResult(res, publicErrorData(error));
}
});
WebApp.handlers.get('/api/boards/:boardId/integrations/:intId', async function(req, res) {
try {
const paramBoardId = req.params.boardId;
const paramIntId = req.params.intId;
await Authentication.checkBoardAccess(req.userId, paramBoardId);
sendJsonResult(res, {
code: 200,
data: await ReactiveCache.getIntegration(
{ _id: paramIntId, boardId: paramBoardId },
{ fields: { token: 0 } },
),
});
} catch (error) {
sendJsonResult(res, publicErrorData(error));
}
});
WebApp.handlers.post('/api/boards/:boardId/integrations', async function(req, res) {
try {
const paramBoardId = req.params.boardId;
await Authentication.checkBoardAdmin(req.userId, paramBoardId);
// SSRF protection: block webhook URLs that resolve to private/loopback/
// link-local addresses (e.g. cloud metadata at 169.254.169.254) before the
// URL is ever stored and delivered server-side. See GHSA-hc3x-hq3m-663q.
const validation = await validateAttachmentUrl(req.body.url);
if (!validation.valid) {
sendJsonResult(res, {
code: 400,
data: { error: 'invalid-webhook-url', reason: validation.reason },
});
return;
}
const id = await Integrations.insertAsync({
userId: req.userId,
boardId: paramBoardId,
url: req.body.url,
});
sendJsonResult(res, {
code: 200,
data: {
_id: id,
},
});
} catch (error) {
sendJsonResult(res, publicErrorData(error));
}
});
WebApp.handlers.put('/api/boards/:boardId/integrations/:intId', async function(req, res) {
try {
const paramBoardId = req.params.boardId;
const paramIntId = req.params.intId;
await Authentication.checkBoardAdmin(req.userId, paramBoardId);
// #3695: how this webhook delivers - { webhook: { text, fields, grouping,
// schedule, ... } }, or null to inherit the board / Admin Panel default
// (models/lib/notificationDelivery.js). Validated before ANY field is
// written, so a refused request changes nothing.
let notificationDelivery;
if (req.body.hasOwnProperty('notificationDelivery')) {
try {
notificationDelivery = normalizeDeliverySettings(req.body.notificationDelivery, ['webhook']);
} catch (e) {
sendJsonResult(res, {
code: 400,
data: { error: 'invalid-notification-delivery', reason: e.message },
});
return;
}
}
if (req.body.hasOwnProperty('enabled')) {
await Integrations.direct.updateAsync(
{ _id: paramIntId, boardId: paramBoardId },
{ $set: { enabled: req.body.enabled } },
);
}
if (req.body.hasOwnProperty('title')) {
await Integrations.direct.updateAsync(
{ _id: paramIntId, boardId: paramBoardId },
{ $set: { title: req.body.title } },
);
}
if (req.body.hasOwnProperty('url')) {
// SSRF protection: this update path uses Integrations.direct, which
// bypasses the schema's url validator, so validate explicitly here.
// See GHSA-hc3x-hq3m-663q.
const validation = await validateAttachmentUrl(req.body.url);
if (!validation.valid) {
sendJsonResult(res, {
code: 400,
data: { error: 'invalid-webhook-url', reason: validation.reason },
});
return;
}
await Integrations.direct.updateAsync(
{ _id: paramIntId, boardId: paramBoardId },
{ $set: { url: req.body.url } },
);
}
if (req.body.hasOwnProperty('token')) {
await Integrations.direct.updateAsync(
{ _id: paramIntId, boardId: paramBoardId },
{ $set: { token: req.body.token } },
);
}
if (req.body.hasOwnProperty('notificationDelivery')) {
await Integrations.direct.updateAsync(
{ _id: paramIntId, boardId: paramBoardId },
notificationDelivery ? { $set: { notificationDelivery } } : { $unset: { notificationDelivery: '' } },
);
}
if (req.body.hasOwnProperty('activities')) {
await Integrations.direct.updateAsync(
{ _id: paramIntId, boardId: paramBoardId },
{ $set: { activities: req.body.activities } },
);
}
sendJsonResult(res, {
code: 200,
data: {
_id: paramIntId,
},
});
} catch (error) {
sendJsonResult(res, publicErrorData(error));
}
});
WebApp.handlers.delete(
'/api/boards/:boardId/integrations/:intId/activities',
async function(req, res) {
try {
const paramBoardId = req.params.boardId;
const paramIntId = req.params.intId;
const newActivities = req.body.activities;
await Authentication.checkBoardAdmin(req.userId, paramBoardId);
await Integrations.direct.updateAsync(
{ _id: paramIntId, boardId: paramBoardId },
{ $pullAll: { activities: newActivities } },
);
sendJsonResult(res, {
code: 200,
data: await ReactiveCache.getIntegration(
{ _id: paramIntId, boardId: paramBoardId },
{ fields: { _id: 1, activities: 1 } },
),
});
} catch (error) {
sendJsonResult(res, publicErrorData(error));
}
},
);
WebApp.handlers.post(
'/api/boards/:boardId/integrations/:intId/activities',
async function(req, res) {
try {
const paramBoardId = req.params.boardId;
const paramIntId = req.params.intId;
const newActivities = req.body.activities;
await Authentication.checkBoardAdmin(req.userId, paramBoardId);
await Integrations.direct.updateAsync(
{ _id: paramIntId, boardId: paramBoardId },
{ $addToSet: { activities: { $each: newActivities } } },
);
sendJsonResult(res, {
code: 200,
data: await ReactiveCache.getIntegration(
{ _id: paramIntId, boardId: paramBoardId },
{ fields: { _id: 1, activities: 1 } },
),
});
} catch (error) {
sendJsonResult(res, publicErrorData(error));
}
},
);
WebApp.handlers.delete('/api/boards/:boardId/integrations/:intId', async function(req, res) {
try {
const paramBoardId = req.params.boardId;
const paramIntId = req.params.intId;
await Authentication.checkBoardAdmin(req.userId, paramBoardId);
await Integrations.direct.removeAsync({ _id: paramIntId, boardId: paramBoardId });
sendJsonResult(res, {
code: 200,
data: {
_id: paramIntId,
},
});
} catch (error) {
sendJsonResult(res, publicErrorData(error));
}
});