Files
wekan/tests/webhookPreparation.test.cjs
T
Lauri Ojansivu b01c233269 Moving a card with an outgoing webhook no longer counts as HookBleed or disables the mover's account.
The activity hook delivered webhooks through Meteor.call('outgoingWebhooks'),
and that method trusted a call without this.connection as the server's own.
Meteor gives a server-side Meteor.call the userId AND connection of the method
it runs in (ddp-server applyAsync), and a card moved from a browser runs in
that browser's method. So the server's own act-moveCard delivery arrived as a
client forging one: it was recorded as ssrf.webhook-forge (high) with the
mover's userId, and blockOnSecurityEvent set loginDisabled.

- deliverActivityWebhook() in server/notifications/outgoing.js is the server's
  delivery: a function, not a method, so no client reaches it and no inherited
  context changes it. It reads the integration again (enabled) and names the
  activity's actor. server/models/activities.js calls it through safeDeliver.
- The outgoingWebhooks method treats every call as a client's: only the
  card-opened notification, rebuilt from the card, as before for clients.
- Server deliveries no longer need the actor to be a member of the hook's
  board, which also skipped a global webhook ('_global' is no board) and an
  activity with no method behind it (REST, timers).
- securityLog.record() now passes the catalog key and blocksAccount to the
  block decision. The stored row carries neither, so every 'high' blocked row
  disabled whoever it named: a hook refused for its own internal address
  disabled the mover too, and ssrf.redirect/ssrf.attachment's
  blocksAccount: false never applied. ssrf.webhook is now blocksAccount:
  false - the person whose move sent it did not write the hook's URL.

tests/hookBleedServerDelivery.test.cjs: the move delivered with nothing
logged, in an inherited context too; a forged call still refused, logged and
blocking; the card-opened notification; failing and internal hooks blocking
nobody, through record() itself; and a whole-tree scan that no server code
calls a this.connection-gated method from inside a client's method without
clearing the invocation. tests/playwright/specs/hook-bleed.e2e.js moves a
card through the Move dialog with a webhook enabled.

Thanks to do4me and xet7 !

Fixes #6782.
2026-10-18 14:28:19 +03:00

121 lines
7.5 KiB
JavaScript

'use strict';
const { test } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const vm = require('node:vm');
function fixture({ load = async () => {}, translate = (key, params, language) => `${language}:${params.card}`, member = true, user = true } = {}) {
let language = 'fi', method, stored = null;
const requests = [], writes = [], translations = [];
const context = { structuredClone, process: { env: {} },
Meteor: { methods: methods => { method = methods.outgoingWebhooks; },
setTimeout: () => { throw new Error('preparation must not set echo locks'); } },
check() {}, Integrations: { Const: { TWOWAY: 'two-way' } },
CardComments: { direct: { updateAsync: async (...args) => writes.push(args) } },
ReactiveCache: { getUser: async () => user ? { getLanguage: () => language } : null,
// HookBleed: delivery builds the request from the STORED integration.
getIntegration: async () => stored, getBoard: async () => ({ hasMember: () => member }),
getCurrentSetting: async () => ({}),
getCard: async id => (id === 'card' ? { _id: 'card', boardId: 'board', listId: 'list' } : null) },
TAPi18n: { ensureLanguageLoaded: load, __: (...args) => { translations.push(args); return translate(...args); } },
// The senders call webhookFetch: fetchSafe off Sandstorm (server/sandstormEgress.js).
webhookFetch: async (url, request) => { requests.push({ url, ...request }); return { status: 200 }; },
// #3695: the payload builder outgoing.js imports (stripped below).
...require('../models/lib/webhookPayload'), ...require('../models/lib/notificationDelivery'),
// GHSA-r4gm-mjj9-g3x8: the card-opened notification asks the card rule.
...require('../models/lib/boardCardScope'),
};
const source = fs.readFileSync(require.resolve('../server/notifications/outgoing.js'), 'utf8')
.replace(/^import .*;\n/gm, '').replace(/^export /gm, '');
vm.runInNewContext(source, context);
return { prepare: context.prepareOutgoingWebhook,
// Ordinary use: the stored integration is the one the caller names.
// #6782: the server's own delivery is deliverActivityWebhook(), not the
// method; a call of the method is a client's, connection or not.
send: (input, { storedIntegration = structuredClone(input.integration), connection = null } = {}) => {
stored = storedIntegration;
if (connection === null) return context.deliverActivityWebhook(input.integration, input.description, input.params, 'actor');
return method.call({ userId: 'actor', connection, unblock() {} }, input.integration, input.description, input.params);
},
client: (input, { storedIntegration = structuredClone(input.integration), connection = null } = {}) => {
stored = storedIntegration;
return method.call({ userId: 'actor', connection, unblock() {} }, input.integration, input.description, input.params);
}, requests, writes, translations,
language: value => { language = value; } };
}
const input = type => ({ actorId: 'actor', description: 'act-test',
integration: { boardId: 'board', url: 'https://example.test/hook', token: 'old-token', type },
params: { user: 'Person', card: 'Original', url: 'https://example.test/card', customFieldValue: { nested: [0, false, null] }, commentId: 'comment', comment: 'Before' } });
test('preparation freezes nested parameters, endpoint, token and language across asynchronous loading', async () => {
let entered, release;
const waiting = new Promise(resolve => { entered = resolve; });
const gate = new Promise(resolve => { release = resolve; });
const loaded = [];
const f = fixture({ load: async language => { loaded.push(language); entered(); await gate; } });
const original = input('outgoing');
const pending = f.prepare(original);
await waiting;
original.params.card = 'Changed'; original.params.customFieldValue.nested[0] = 42;
original.integration.url = 'https://changed.test'; original.integration.token = 'new-token';
f.language('de'); release();
const prepared = await pending;
assert.equal(prepared.url, 'https://example.test/hook');
assert.equal(prepared.headers['X-Wekan-Token'], 'old-token');
assert.equal(prepared.language, 'fi'); assert.deepEqual(loaded, ['fi']);
const body = JSON.parse(prepared.body);
assert.equal(body.text, 'Person fi:"Original"\nhttps://example.test/card');
assert.deepEqual(body.customFieldValue, { nested: [0, false, null] });
assert.equal(f.requests.length, 0); assert.equal(f.writes.length, 0);
});
test('ordinary delivery sends precisely the prepared wire payload; two-way preparation has no lock or writes', async () => {
for (const type of ['outgoing', 'two-way']) {
const f = fixture(), data = input(type);
if (type === 'two-way') delete data.params.commentId;
const prepared = await f.prepare(data);
assert.equal(f.requests.length, 0); assert.equal(f.writes.length, 0);
await f.send(data);
assert.equal(f.requests.length, 1);
assert.equal(f.requests[0].body, prepared.body);
assert.equal(f.requests[0].url, prepared.url);
assert.deepEqual(structuredClone(f.requests[0].headers), structuredClone(prepared.headers));
}
const f = fixture();
const prepared = await f.prepare(input('two-way'));
assert.equal(JSON.parse(prepared.body).commentId, 'comment');
assert.equal(f.requests.length, 0); assert.equal(f.writes.length, 0);
});
test('suppression, missing user, denied membership and preparation failure never send', async () => {
for (const options of [{ translate: () => '-' }, { user: false }]) {
const f = fixture(options);
assert.equal(await f.prepare(input('outgoing')), null);
await f.send(input('outgoing')); assert.equal(f.requests.length, 0);
}
// A client that is not a member of the hook's board sends nothing, not even
// the card-opened notification.
const denied = fixture({ member: false });
await denied.client({ ...input('outgoing'), description: 'CardSelected', params: { cardId: 'card' } }, { connection: {} });
assert.equal(denied.requests.length, 0);
const broken = fixture({ load: async () => { throw new Error('load failed'); } });
await assert.rejects(broken.prepare(input('outgoing')), /load failed/);
await assert.rejects(broken.send(input('outgoing')), /load failed/);
assert.equal(broken.requests.length, 0);
});
// HookBleed (2026-10-02): the request came from the CALLER's integration
// object, description and params, so any member could post arbitrary text to
// the board's chat webhook as WeKan, or turn a one-way hook two-way.
test('a client cannot forge what a board webhook sends', async () => {
const f = fixture();
const forged = input('two-way');
forged.description = 'act-anything';
// A stored ONE-way hook; the caller claims two-way and a message of its own.
await f.send(forged, { storedIntegration: { boardId: 'board', url: 'https://example.test/hook', token: 'stored', type: 'outgoing' }, connection: {} });
assert.equal(f.requests.length, 0, 'only the card-opened notification may come from a client');
// The card-opened notification is sent - built from the stored hook and the
// card, not from what the client sent.
const opened = { ...input('two-way'), description: 'CardSelected', params: { cardId: 'card', user: 'Somebody else', text: 'forged text' } };
await f.send(opened, { storedIntegration: { boardId: 'board', url: 'https://example.test/hook', token: 'stored', type: 'outgoing' }, connection: {} });
assert.equal(f.requests.length, 1);
assert.equal(f.requests[0].headers['X-Wekan-Token'], 'stored');
assert.doesNotMatch(f.requests[0].body, /forged text|Somebody else/);
});