Files
wekan/tests/syncWebhookHttp.test.cjs
T
Lauri Ojansivu 2cc0b59a51 On Sandstorm, send webhooks through the Powerbox, and accept the REST API with a grain API key.
A grain has no network but the bridge proxy, so an outgoing webhook now
leaves only through an API a board admin approved in the Powerbox for that
webhook's address: each saved webhook offers "Allow this grain to reach this
address". The grant belongs to the destination, not to a person, is found
by the URL being sent to, and is approved as the address's parent path so
the bridge delivers to the exact address. Every sender - immediate, grouped
and stored - goes through webhookFetch, which off Sandstorm, or for an
address no grant covers, is exactly the SSRF-guarded fetchSafe.

The REST API called with a Sandstorm API key acts as the key's user: in a
grain the bridge sets X-Sandstorm-User-Id itself. Only in a grain, only
after a WeKan token, only for an existing enabled account, and a key without
participate reads only.

New strings translated in all languages.

Thanks to xet7 !
2026-10-14 09:49:42 +03:00

72 lines
5.0 KiB
JavaScript

'use strict';
const { test } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const { Readable } = require('node:stream');
const { prepareWebhookPlan, deliveryId } = require('../server/lib/syncWebhookPlan');
function load(relative, dependencies, names) {
const source = fs.readFileSync(path.join(__dirname, '..', relative), 'utf8')
.replace(/^import .*;\n/gm, '').replace(/^export /gm, '');
return new Function(...Object.keys(dependencies), `${source}\nreturn {${names.join(',')}};`)(...Object.values(dependencies));
}
function fixture({ addresses = ['93.184.216.34'], status = 200, body = '{"reply":true}', headers = {} } = {}) {
const connections = [], rows = new Map(); let lookups = 0;
const transport = { request(options, callback) {
connections.push(options);
const response = new Readable({ read() {} });
response.statusCode = status; response.headers = headers;
process.nextTick(() => { callback(response); process.nextTick(() => {
if (!response.destroyed) { response.push(body); response.push(null); }
}); });
return { on() {}, write() {}, end() {}, setTimeout() {} };
} };
const { isIpBlocked } = load('models/lib/attachmentUrlValidation.js', { Meteor: { isServer: true }, require }, ['isIpBlocked']);
const { fetchSafe } = load('server/lib/ssrfGuard.js', { dns: { promises: { lookup: async () => {
lookups++; return addresses.map(address => ({ address, family: address.includes(':') ? 6 : 4 }));
} } }, createOutboundDeadline: require('../server/lib/outboundDeadline').createOutboundDeadline, fs, net: require('node:net'), http: transport, https: transport, URL, isIpBlocked, ...require('../server/lib/outboundProxy'), tls: require('node:tls') }, ['fetchSafe']);
const { sendStoredWebhook } = load('server/notifications/storedWebhookHttp.js', {
// Off Sandstorm, webhookFetch is fetchSafe itself (server/sandstormEgress.js).
webhookFetch: fetchSafe, require: id => require(`..${id}`),
}, ['sendStoredWebhook']);
const responses = { findOne: async ({ _id }) => rows.get(_id), insertOne: async row => rows.set(row._id, row) };
return { sendStoredWebhook, connections, rows, responses, lookups: () => lookups };
}
async function item(url = 'http://public.example/hook', is2way = false) {
const activity = { _id: 'event', boardId: 'board', cardId: 'card', userId: 'actor' };
const plan = await prepareWebhookPlan({ activity,
integrations: [{ _id: 'hook', boardId: 'board', enabled: true, url, type: is2way ? 'bidirectional-webhooks' : 'outgoing-webhooks' }],
prepare: async () => ({ url, headers: { 'Content-Type': 'application/json' }, body: '{}', language: 'fi', is2way }) });
const target = plan.targets[0], id = deliveryId('event', 'hook');
return { activity, target, deliveryId: id, request: { ...target.request, headers: { ...target.request.headers, 'X-Wekan-Delivery-Id': id } } };
}
const guards = { assertCurrent: async () => {}, assertTarget: async () => true };
test('production adapter pins actual guard transport and resumes from saved acceptance without DNS or HTTP', async () => {
const f = fixture(), captured = await item();
const options = { ...guards, responses: f.responses, item: captured,
requestHttp: async () => assert.fail('caller cannot override fetchSafe') };
assert.equal(await f.sendStoredWebhook(options), captured.deliveryId);
assert.equal(await f.sendStoredWebhook(options), captured.deliveryId);
assert.equal(f.lookups(), 1); assert.equal(f.connections.length, 1);
assert.equal(f.connections[0].hostname, '93.184.216.34');
assert.equal(f.connections[0].headers['X-Wekan-Delivery-Id'], captured.deliveryId);
assert.equal(f.rows.size, 1);
});
test('real guard rejects private DNS, literal loopback and redirects without response evidence', async () => {
for (const [options, url, reason] of [[{ addresses: ['127.0.0.1'] }, undefined, /Blocked|blocked/],
[{}, 'http://127.0.0.1/hook', /Blocked|blocked/],
[{ status: 302, headers: { location: 'http://127.0.0.1/hook' } }, undefined, /Redirects are not allowed/]]) {
const f = fixture(options);
await assert.rejects(f.sendStoredWebhook({ ...guards, responses: f.responses, item: await item(url) }), reason);
assert.equal(f.rows.size, 0); assert.ok(f.connections.length <= 1);
}
});
test('request substitution, absent response adapter and revoked access fail before transport', async () => {
const f = fixture(), captured = await item();
captured.request.url = 'http://different.example/hook';
await assert.rejects(f.sendStoredWebhook({ ...guards, responses: f.responses, item: captured }), /http-invalid/);
await assert.rejects(f.sendStoredWebhook({ ...guards, responses: f.responses, item: await item(undefined, true) }), /http-invalid/);
await assert.rejects(f.sendStoredWebhook({ ...guards, responses: f.responses, item: await item(), assertTarget: async () => false }), /target-denied/);
assert.equal(f.connections.length, 0); assert.equal(f.rows.size, 0);
});