@tiledesk/tiledesk-server 3.3.5 → 3.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/app.js +7 -0
- package/docs/activities-client.md +27 -0
- package/docs/mcp-oauth2-it.md +279 -0
- package/docs/mcp-oauth2.md +279 -0
- package/docs/superpowers/plans/2026-11-10-mcp-oauth2-server-it.md +155 -0
- package/docs/superpowers/plans/2026-11-10-mcp-oauth2-server.md +155 -0
- package/docs/superpowers/plans/2026-11-10-mcp-oauth2-tybot-it.md +63 -0
- package/docs/superpowers/plans/2026-11-10-mcp-oauth2-tybot.md +63 -0
- package/docs/superpowers/specs/2026-11-10-mcp-oauth2-design-it.md +207 -0
- package/docs/superpowers/specs/2026-11-10-mcp-oauth2-design.md +207 -0
- package/jobs.js +15 -0
- package/models/mcpOAuthConnection.js +131 -0
- package/package.json +1 -1
- package/pubmodules/activities/activityMessageUtil.js +137 -0
- package/pubmodules/activities/models/activity.js +2 -0
- package/pubmodules/activities/routes/activity.js +30 -1
- package/pubmodules/activities/test/execution-filter.test.js +133 -0
- package/pubmodules/activities/test/execution-messages.test.js +158 -0
- package/pubmodules/executions-publisher/index.js +457 -0
- package/pubmodules/executions-publisher/test/audit-consumer.test.js +384 -0
- package/pubmodules/executions-publisher/test/executions-publisher.test.js +204 -0
- package/pubmodules/pubModulesManager.js +12 -0
- package/routes/executions-refire.js +148 -0
- package/routes/mcp.js +11 -8
- package/routes/mcpOAuth.js +133 -0
- package/routes/mcpOAuthCallback.js +75 -0
- package/routes/request.js +5 -0
- package/routes/webhook.js +120 -54
- package/services/mcpOAuthDiscovery.js +248 -0
- package/services/mcpOAuthGoogleScopes.js +55 -0
- package/services/mcpOAuthService.js +732 -0
- package/services/mcpService.js +111 -15
- package/services/requestService.js +4 -2
- package/services/webhookService.js +4 -2
- package/test/executions-refire.test.js +362 -0
- package/test/mcpOAuthDiscovery.test.js +331 -0
- package/test/mcpOAuthGoogleScopes.test.js +25 -0
- package/test/mcpOAuthRoute.test.js +455 -0
- package/test/mcpOAuthService.test.js +693 -0
- package/test/mcpOAuthUtils.test.js +348 -0
- package/test/mcpServiceOAuth.test.js +267 -0
- package/test/requestRoute.js +57 -0
- package/test/webhookConversationRoute.js +51 -0
- package/utils/logRedactor.js +4 -2
- package/utils/mcpOAuthCrypto.js +69 -0
- package/utils/mcpOAuthPkce.js +20 -0
- package/utils/outboundUrlGuard.js +131 -0
|
@@ -345,3 +345,54 @@ describe('WebhookConversationRoute - run', function () {
|
|
|
345
345
|
expect(statuses.filter((s) => s !== 429 && s !== 202)).to.deep.equal([]);
|
|
346
346
|
});
|
|
347
347
|
});
|
|
348
|
+
|
|
349
|
+
// Executions lineage (contract 8): retry_of is set only by the server's own refire, never by a public caller.
|
|
350
|
+
describe('WebhookConversationRoute - retry_of from public callers', function () {
|
|
351
|
+
|
|
352
|
+
this.timeout(20000);
|
|
353
|
+
const webhookService = require('../services/webhookService');
|
|
354
|
+
|
|
355
|
+
it('drops a body retry_of on the public conversation route', async () => {
|
|
356
|
+
const ctx = await setupProjectWithBot(server);
|
|
357
|
+
const webhook = await createConversationWebhook(ctx);
|
|
358
|
+
const res = await chai.request(server).post('/webhook/' + webhook.webhook_id).send({ alert: 'a', retry_of: 'exec-spoofed' });
|
|
359
|
+
expect(res).to.have.status(202);
|
|
360
|
+
const request = await Request.findOne({ request_id: res.body.request_id }).lean().exec();
|
|
361
|
+
expect(request.attributes.payload.alert).to.equal('a');
|
|
362
|
+
expect(request.attributes.payload).to.not.have.property('retry_of');
|
|
363
|
+
expect(request.attributes).to.not.have.property('retry_of');
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
it('drops a body retry_of on the dev conversation route', async () => {
|
|
367
|
+
const ctx = await setupProjectWithBot(server);
|
|
368
|
+
const webhook = await createConversationWebhook(ctx);
|
|
369
|
+
const preload = await chai.request(server).post('/' + ctx.project._id + '/webhooks/preload/' + webhook.webhook_id).auth(ctx.email, ctx.pwd);
|
|
370
|
+
expect(preload).to.have.status(200);
|
|
371
|
+
await new Promise((resolve) => setTimeout(resolve, 200)); // preload writes to Redis without awaiting
|
|
372
|
+
const res = await chai.request(server).post('/webhook/' + webhook.webhook_id + '/dev').send({ alert: 'd', retry_of: 'exec-spoofed' });
|
|
373
|
+
expect(res).to.have.status(202);
|
|
374
|
+
const request = await Request.findOne({ request_id: res.body.request_id }).lean().exec();
|
|
375
|
+
expect(request.attributes.payload).to.not.have.property('retry_of');
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
it('drops a body retry_of on the public and dev automation routes', async () => {
|
|
379
|
+
const ctx = await setupProjectWithBot(server);
|
|
380
|
+
const webhook = await createWebhook(ctx, { block_id: 'legacy-block' });
|
|
381
|
+
const originalRun = webhookService.run;
|
|
382
|
+
const calls = [];
|
|
383
|
+
webhookService.run = function (...args) {
|
|
384
|
+
calls.push(args[1]);
|
|
385
|
+
return originalRun.apply(webhookService, args);
|
|
386
|
+
};
|
|
387
|
+
try {
|
|
388
|
+
const res = await chai.request(server).post('/webhook/' + webhook.webhook_id).send({ alert: 'x', retry_of: 'exec-spoofed' });
|
|
389
|
+
expect(res).to.have.status(200);
|
|
390
|
+
await chai.request(server).post('/webhook/' + webhook.webhook_id + '/dev').send({ alert: 'y', retry_of: 'exec-spoofed' });
|
|
391
|
+
} finally {
|
|
392
|
+
webhookService.run = originalRun;
|
|
393
|
+
}
|
|
394
|
+
expect(calls).to.have.length(2);
|
|
395
|
+
expect(calls[0].alert).to.equal('x');
|
|
396
|
+
for (const p of calls) expect(p).to.not.have.property('retry_of');
|
|
397
|
+
});
|
|
398
|
+
});
|
package/utils/logRedactor.js
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
// Object keys whose value is a secret and must never be logged.
|
|
18
|
-
const SENSITIVE_KEY_RE = /^(api_?key|gptkey|llm_key|apikey|embedding_key|token|password|passwd|secret|client_secret|authorization|x-auth-token|kb_webhook_token)$/i;
|
|
18
|
+
const SENSITIVE_KEY_RE = /^(api_?key|gptkey|llm_key|apikey|embedding_key|token|password|passwd|secret|client_secret|authorization|x-auth-token|kb_webhook_token|access_token|refresh_token|code_verifier|access_token_enc|refresh_token_enc|client_secret_enc)$/i;
|
|
19
19
|
|
|
20
20
|
const REDACTED = '[REDACTED]';
|
|
21
21
|
const MAX_DEPTH = 8;
|
|
@@ -30,7 +30,9 @@ function redactString(str) {
|
|
|
30
30
|
.replace(/pcsk_[A-Za-z0-9_-]{12,}/g, 'pcsk_[REDACTED]')
|
|
31
31
|
.replace(/AIza[A-Za-z0-9_-]{20,}/g, 'AIza[REDACTED]')
|
|
32
32
|
// JSON-encoded secret fields inside a serialized body: "api_key":"..."
|
|
33
|
-
.replace(/("(?:api_?key|gptkey|llm_key|apikey|embedding_key|token|password|secret|client_secret)"\s*:\s*)"[^"]*"/gi, '$1"[REDACTED]"')
|
|
33
|
+
.replace(/("(?:api_?key|gptkey|llm_key|apikey|embedding_key|token|password|secret|client_secret|access_token|refresh_token|code_verifier)"\s*:\s*)"[^"]*"/gi, '$1"[REDACTED]"')
|
|
34
|
+
// form-encoded OAuth bodies and callback query strings: refresh_token=...&code=...
|
|
35
|
+
.replace(/(^|[?&])(access_token|refresh_token|code_verifier|client_secret|code)=[^&\s"]*/gi, '$1$2=[REDACTED]')
|
|
34
36
|
// bearer tokens
|
|
35
37
|
.replace(/Bearer\s+[A-Za-z0-9._-]+/gi, 'Bearer [REDACTED]');
|
|
36
38
|
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const crypto = require('crypto');
|
|
4
|
+
|
|
5
|
+
const VERSION = 'v1';
|
|
6
|
+
const ALGORITHM = 'aes-256-gcm';
|
|
7
|
+
const IV_BYTES = 12;
|
|
8
|
+
const KEY_BYTES = 32;
|
|
9
|
+
|
|
10
|
+
function getKey() {
|
|
11
|
+
const raw = process.env.MCP_OAUTH_ENCRYPTION_KEY;
|
|
12
|
+
if (!raw) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
const key = Buffer.from(raw, 'base64');
|
|
16
|
+
return key.length === KEY_BYTES ? key : null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function isConfigured() {
|
|
20
|
+
return getKey() !== null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function requireKey() {
|
|
24
|
+
const key = getKey();
|
|
25
|
+
if (!key) {
|
|
26
|
+
const error = new Error('MCP OAuth is not available on this installation');
|
|
27
|
+
error.status = 503;
|
|
28
|
+
error.code = 'not_configured';
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
return key;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Encrypts a secret as `v1:<iv>:<tag>:<ciphertext>` (base64 parts).
|
|
36
|
+
* null and undefined are returned unchanged.
|
|
37
|
+
*/
|
|
38
|
+
function encrypt(plain) {
|
|
39
|
+
if (plain === null || plain === undefined) {
|
|
40
|
+
return plain;
|
|
41
|
+
}
|
|
42
|
+
const key = requireKey();
|
|
43
|
+
const iv = crypto.randomBytes(IV_BYTES);
|
|
44
|
+
const cipher = crypto.createCipheriv(ALGORITHM, key, iv);
|
|
45
|
+
const ciphertext = Buffer.concat([cipher.update(String(plain), 'utf8'), cipher.final()]);
|
|
46
|
+
const tag = cipher.getAuthTag();
|
|
47
|
+
return [VERSION, iv.toString('base64'), tag.toString('base64'), ciphertext.toString('base64')].join(':');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Decrypts a value produced by encrypt. Throws when the value was tampered with
|
|
52
|
+
* or was encrypted with another key.
|
|
53
|
+
*/
|
|
54
|
+
function decrypt(encrypted) {
|
|
55
|
+
if (encrypted === null || encrypted === undefined) {
|
|
56
|
+
return encrypted;
|
|
57
|
+
}
|
|
58
|
+
const key = requireKey();
|
|
59
|
+
const parts = String(encrypted).split(':');
|
|
60
|
+
if (parts.length !== 4 || parts[0] !== VERSION) {
|
|
61
|
+
throw new Error('Unsupported encrypted value');
|
|
62
|
+
}
|
|
63
|
+
const [, iv, tag, ciphertext] = parts;
|
|
64
|
+
const decipher = crypto.createDecipheriv(ALGORITHM, key, Buffer.from(iv, 'base64'));
|
|
65
|
+
decipher.setAuthTag(Buffer.from(tag, 'base64'));
|
|
66
|
+
return Buffer.concat([decipher.update(Buffer.from(ciphertext, 'base64')), decipher.final()]).toString('utf8');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
module.exports = { isConfigured, encrypt, decrypt };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const crypto = require('crypto');
|
|
4
|
+
|
|
5
|
+
/** Random OAuth `state`: 32 bytes, base64url (43 chars). */
|
|
6
|
+
function randomState() {
|
|
7
|
+
return crypto.randomBytes(32).toString('base64url');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** PKCE code_verifier: 48 random bytes, base64url (64 chars, RFC 7636 allows 43..128). */
|
|
11
|
+
function createVerifier() {
|
|
12
|
+
return crypto.randomBytes(48).toString('base64url');
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** PKCE S256 code_challenge of a verifier. */
|
|
16
|
+
function challengeOf(verifier) {
|
|
17
|
+
return crypto.createHash('sha256').update(verifier, 'ascii').digest('base64url');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
module.exports = { randomState, createVerifier, challengeOf };
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const dns = require('dns');
|
|
4
|
+
const http = require('http');
|
|
5
|
+
const https = require('https');
|
|
6
|
+
const net = require('net');
|
|
7
|
+
|
|
8
|
+
const TIMEOUT_MS = 10000;
|
|
9
|
+
const MAX_CONTENT_LENGTH = 1024 * 1024;
|
|
10
|
+
|
|
11
|
+
const blockedAddresses = new net.BlockList();
|
|
12
|
+
[
|
|
13
|
+
['0.0.0.0', 8],
|
|
14
|
+
['10.0.0.0', 8],
|
|
15
|
+
['100.64.0.0', 10],
|
|
16
|
+
['127.0.0.0', 8],
|
|
17
|
+
['169.254.0.0', 16],
|
|
18
|
+
['172.16.0.0', 12],
|
|
19
|
+
['192.168.0.0', 16],
|
|
20
|
+
['224.0.0.0', 4],
|
|
21
|
+
['240.0.0.0', 4]
|
|
22
|
+
].forEach(([address, prefix]) => blockedAddresses.addSubnet(address, prefix, 'ipv4'));
|
|
23
|
+
[
|
|
24
|
+
['::', 128],
|
|
25
|
+
['::1', 128],
|
|
26
|
+
['fc00::', 7],
|
|
27
|
+
['fe80::', 10],
|
|
28
|
+
['ff00::', 8]
|
|
29
|
+
].forEach(([address, prefix]) => blockedAddresses.addSubnet(address, prefix, 'ipv6'));
|
|
30
|
+
|
|
31
|
+
function insecureAllowed() {
|
|
32
|
+
return process.env.MCP_OAUTH_ALLOW_INSECURE_URLS === 'true';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function notAllowed(reason) {
|
|
36
|
+
const error = new Error('URL not allowed: ' + reason);
|
|
37
|
+
error.status = 400;
|
|
38
|
+
error.code = 'bad_request';
|
|
39
|
+
return error;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function isBlockedAddress(address) {
|
|
43
|
+
const version = net.isIP(address);
|
|
44
|
+
if (version === 0) {
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
return blockedAddresses.check(address, version === 6 ? 'ipv6' : 'ipv4');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async function resolveHost(host) {
|
|
51
|
+
try {
|
|
52
|
+
const results = await dns.promises.lookup(host, { all: true });
|
|
53
|
+
return results.map((result) => result.address);
|
|
54
|
+
} catch (error) {
|
|
55
|
+
throw notAllowed('host cannot be resolved');
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Checks a user-influenced URL before the server calls it: https only, and the
|
|
61
|
+
* host must not resolve to a loopback, private, link-local or reserved address.
|
|
62
|
+
* MCP_OAUTH_ALLOW_INSECURE_URLS=true (dev/test only) allows http and any host.
|
|
63
|
+
*/
|
|
64
|
+
async function assertAllowed(rawUrl) {
|
|
65
|
+
let url;
|
|
66
|
+
try {
|
|
67
|
+
url = new URL(rawUrl);
|
|
68
|
+
} catch (error) {
|
|
69
|
+
throw notAllowed('invalid URL');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (insecureAllowed()) {
|
|
73
|
+
if (url.protocol !== 'https:' && url.protocol !== 'http:') {
|
|
74
|
+
throw notAllowed('unsupported protocol');
|
|
75
|
+
}
|
|
76
|
+
return url;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (url.protocol !== 'https:') {
|
|
80
|
+
throw notAllowed('https is required');
|
|
81
|
+
}
|
|
82
|
+
if (url.username || url.password) {
|
|
83
|
+
throw notAllowed('credentials in URL');
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const host = url.hostname.replace(/^\[|\]$/g, '');
|
|
87
|
+
const addresses = net.isIP(host) ? [host] : await resolveHost(host);
|
|
88
|
+
if (addresses.length === 0 || addresses.some(isBlockedAddress)) {
|
|
89
|
+
throw notAllowed('private or reserved address');
|
|
90
|
+
}
|
|
91
|
+
return url;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Checked again at connect time: the DNS answer can change after assertAllowed.
|
|
95
|
+
function guardedLookup(hostname, options, callback) {
|
|
96
|
+
if (typeof options === 'function') {
|
|
97
|
+
callback = options;
|
|
98
|
+
options = {};
|
|
99
|
+
}
|
|
100
|
+
dns.lookup(hostname, { ...options, all: true }, (error, addresses) => {
|
|
101
|
+
if (error) {
|
|
102
|
+
return callback(error);
|
|
103
|
+
}
|
|
104
|
+
if (addresses.length === 0 || addresses.some((entry) => isBlockedAddress(entry.address))) {
|
|
105
|
+
return callback(notAllowed('private or reserved address'));
|
|
106
|
+
}
|
|
107
|
+
if (options.all) {
|
|
108
|
+
return callback(null, addresses);
|
|
109
|
+
}
|
|
110
|
+
callback(null, addresses[0].address, addresses[0].family);
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const guardedHttpAgent = new http.Agent({ lookup: guardedLookup });
|
|
115
|
+
const guardedHttpsAgent = new https.Agent({ lookup: guardedLookup });
|
|
116
|
+
|
|
117
|
+
/** axios options for requests to user-influenced URLs. */
|
|
118
|
+
function httpOptions() {
|
|
119
|
+
const options = {
|
|
120
|
+
maxRedirects: 0,
|
|
121
|
+
timeout: TIMEOUT_MS,
|
|
122
|
+
maxContentLength: MAX_CONTENT_LENGTH
|
|
123
|
+
};
|
|
124
|
+
if (!insecureAllowed()) {
|
|
125
|
+
options.httpAgent = guardedHttpAgent;
|
|
126
|
+
options.httpsAgent = guardedHttpsAgent;
|
|
127
|
+
}
|
|
128
|
+
return options;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
module.exports = { assertAllowed, httpOptions, isBlockedAddress, guardedLookup };
|