@tiledesk/tiledesk-server 3.3.5 → 3.5.1
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 +20 -0
- package/app.js +7 -0
- package/deploynew.sh +7 -1
- package/docs/activities-client.md +27 -0
- package/docs/mcp-oauth2-it.md +279 -0
- package/docs/mcp-oauth2.md +279 -0
- package/docs/numeric-input-validation.md +91 -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/models/request.js +8 -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 +39 -11
- package/pubmodules/activities/test/execution-filter.test.js +133 -0
- package/pubmodules/activities/test/execution-messages.test.js +158 -0
- package/pubmodules/canned/cannedResponseRoute.js +6 -9
- package/pubmodules/events/eventRoute.js +6 -9
- 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/answered.js +7 -3
- package/routes/campaigns.js +7 -3
- package/routes/department.js +14 -3
- package/routes/executions-refire.js +148 -0
- package/routes/faq.js +6 -14
- package/routes/filesp.js +9 -4
- package/routes/kb.js +40 -15
- package/routes/lead.js +23 -28
- package/routes/logs.js +8 -1
- package/routes/mcp.js +11 -8
- package/routes/mcpOAuth.js +133 -0
- package/routes/mcpOAuthCallback.js +75 -0
- package/routes/message.js +3 -1
- package/routes/messagesRoot.js +5 -18
- package/routes/project.js +57 -12
- package/routes/project_user.js +21 -4
- package/routes/property.js +18 -20
- package/routes/request.js +36 -45
- package/routes/roles.js +8 -18
- package/routes/segment.js +8 -18
- package/routes/subscription.js +4 -5
- package/routes/tag.js +6 -9
- package/routes/unanswered.js +7 -3
- package/routes/user-request.js +29 -34
- 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/kbRoute.js +4 -7
- 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/numericInput.js +137 -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/numericInput.js +200 -0
- package/utils/outboundUrlGuard.js +131 -0
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
const { expect } = require('chai');
|
|
2
|
+
const crypto = require('crypto');
|
|
3
|
+
const dns = require('dns');
|
|
4
|
+
|
|
5
|
+
const mcpOAuthCrypto = require('../utils/mcpOAuthCrypto');
|
|
6
|
+
const mcpOAuthPkce = require('../utils/mcpOAuthPkce');
|
|
7
|
+
const outboundUrlGuard = require('../utils/outboundUrlGuard');
|
|
8
|
+
const { redactValue, redactString } = require('../utils/logRedactor');
|
|
9
|
+
const McpOAuthConnection = require('../models/mcpOAuthConnection');
|
|
10
|
+
|
|
11
|
+
function withEnv(name, value, fn) {
|
|
12
|
+
const previous = process.env[name];
|
|
13
|
+
if (value === undefined) {
|
|
14
|
+
delete process.env[name];
|
|
15
|
+
} else {
|
|
16
|
+
process.env[name] = value;
|
|
17
|
+
}
|
|
18
|
+
const restore = () => {
|
|
19
|
+
if (previous === undefined) {
|
|
20
|
+
delete process.env[name];
|
|
21
|
+
} else {
|
|
22
|
+
process.env[name] = previous;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
let result;
|
|
26
|
+
try {
|
|
27
|
+
result = fn();
|
|
28
|
+
} catch (error) {
|
|
29
|
+
restore();
|
|
30
|
+
throw error;
|
|
31
|
+
}
|
|
32
|
+
if (result && typeof result.then === 'function') {
|
|
33
|
+
return result.finally(restore);
|
|
34
|
+
}
|
|
35
|
+
restore();
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function expectRejected(promise, messagePart) {
|
|
40
|
+
let error;
|
|
41
|
+
try {
|
|
42
|
+
await promise;
|
|
43
|
+
} catch (err) {
|
|
44
|
+
error = err;
|
|
45
|
+
}
|
|
46
|
+
expect(error, 'expected a rejection').to.be.an('error');
|
|
47
|
+
expect(error.status).to.equal(400);
|
|
48
|
+
if (messagePart) {
|
|
49
|
+
expect(error.message).to.contain(messagePart);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
describe('MCP OAuth utils', () => {
|
|
54
|
+
|
|
55
|
+
describe('mcpOAuthCrypto', () => {
|
|
56
|
+
const key = crypto.randomBytes(32).toString('base64');
|
|
57
|
+
|
|
58
|
+
it('encrypts and decrypts a secret', () => withEnv('MCP_OAUTH_ENCRYPTION_KEY', key, () => {
|
|
59
|
+
const encrypted = mcpOAuthCrypto.encrypt('refresh-token-value');
|
|
60
|
+
expect(encrypted).to.match(/^v1:[^:]+:[^:]+:[^:]+$/);
|
|
61
|
+
expect(encrypted).to.not.contain('refresh-token-value');
|
|
62
|
+
expect(mcpOAuthCrypto.decrypt(encrypted)).to.equal('refresh-token-value');
|
|
63
|
+
}));
|
|
64
|
+
|
|
65
|
+
it('uses a new IV for every encryption', () => withEnv('MCP_OAUTH_ENCRYPTION_KEY', key, () => {
|
|
66
|
+
expect(mcpOAuthCrypto.encrypt('same')).to.not.equal(mcpOAuthCrypto.encrypt('same'));
|
|
67
|
+
}));
|
|
68
|
+
|
|
69
|
+
it('passes null and undefined through', () => withEnv('MCP_OAUTH_ENCRYPTION_KEY', key, () => {
|
|
70
|
+
expect(mcpOAuthCrypto.encrypt(null)).to.equal(null);
|
|
71
|
+
expect(mcpOAuthCrypto.encrypt(undefined)).to.equal(undefined);
|
|
72
|
+
expect(mcpOAuthCrypto.decrypt(null)).to.equal(null);
|
|
73
|
+
}));
|
|
74
|
+
|
|
75
|
+
it('rejects a tampered ciphertext', () => withEnv('MCP_OAUTH_ENCRYPTION_KEY', key, () => {
|
|
76
|
+
const parts = mcpOAuthCrypto.encrypt('secret-value').split(':');
|
|
77
|
+
const ciphertext = Buffer.from(parts[3], 'base64');
|
|
78
|
+
ciphertext[0] = ciphertext[0] ^ 0xff;
|
|
79
|
+
parts[3] = ciphertext.toString('base64');
|
|
80
|
+
expect(() => mcpOAuthCrypto.decrypt(parts.join(':'))).to.throw();
|
|
81
|
+
}));
|
|
82
|
+
|
|
83
|
+
it('rejects a value encrypted with another key', () => {
|
|
84
|
+
const encrypted = withEnv('MCP_OAUTH_ENCRYPTION_KEY', key, () => mcpOAuthCrypto.encrypt('secret-value'));
|
|
85
|
+
const otherKey = crypto.randomBytes(32).toString('base64');
|
|
86
|
+
withEnv('MCP_OAUTH_ENCRYPTION_KEY', otherKey, () => {
|
|
87
|
+
expect(() => mcpOAuthCrypto.decrypt(encrypted)).to.throw();
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('is not configured without a 32-byte key', () => {
|
|
92
|
+
withEnv('MCP_OAUTH_ENCRYPTION_KEY', undefined, () => {
|
|
93
|
+
expect(mcpOAuthCrypto.isConfigured()).to.equal(false);
|
|
94
|
+
let error;
|
|
95
|
+
try {
|
|
96
|
+
mcpOAuthCrypto.encrypt('x');
|
|
97
|
+
} catch (err) {
|
|
98
|
+
error = err;
|
|
99
|
+
}
|
|
100
|
+
expect(error.status).to.equal(503);
|
|
101
|
+
expect(error.code).to.equal('not_configured');
|
|
102
|
+
});
|
|
103
|
+
withEnv('MCP_OAUTH_ENCRYPTION_KEY', crypto.randomBytes(16).toString('base64'), () => {
|
|
104
|
+
expect(mcpOAuthCrypto.isConfigured()).to.equal(false);
|
|
105
|
+
});
|
|
106
|
+
withEnv('MCP_OAUTH_ENCRYPTION_KEY', key, () => {
|
|
107
|
+
expect(mcpOAuthCrypto.isConfigured()).to.equal(true);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
describe('mcpOAuthPkce', () => {
|
|
113
|
+
|
|
114
|
+
it('computes the RFC 7636 appendix B challenge', () => {
|
|
115
|
+
expect(mcpOAuthPkce.challengeOf('dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk'))
|
|
116
|
+
.to.equal('E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM');
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('creates verifiers and states in the allowed alphabet and length', () => {
|
|
120
|
+
const verifier = mcpOAuthPkce.createVerifier();
|
|
121
|
+
expect(verifier).to.have.lengthOf(64);
|
|
122
|
+
expect(verifier).to.match(/^[A-Za-z0-9_-]+$/);
|
|
123
|
+
const state = mcpOAuthPkce.randomState();
|
|
124
|
+
expect(state).to.have.lengthOf(43);
|
|
125
|
+
expect(state).to.match(/^[A-Za-z0-9_-]+$/);
|
|
126
|
+
expect(mcpOAuthPkce.randomState()).to.not.equal(state);
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
describe('outboundUrlGuard', () => {
|
|
131
|
+
let originalLookup;
|
|
132
|
+
|
|
133
|
+
beforeEach(() => {
|
|
134
|
+
originalLookup = dns.promises.lookup;
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
afterEach(() => {
|
|
138
|
+
dns.promises.lookup = originalLookup;
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('requires https', () => withEnv('MCP_OAUTH_ALLOW_INSECURE_URLS', undefined, () =>
|
|
142
|
+
expectRejected(outboundUrlGuard.assertAllowed('http://mcp.example.com/mcp'), 'https is required')
|
|
143
|
+
));
|
|
144
|
+
|
|
145
|
+
it('rejects invalid URLs', () => withEnv('MCP_OAUTH_ALLOW_INSECURE_URLS', undefined, () =>
|
|
146
|
+
expectRejected(outboundUrlGuard.assertAllowed('not a url'), 'invalid URL')
|
|
147
|
+
));
|
|
148
|
+
|
|
149
|
+
it('rejects credentials in the URL', () => withEnv('MCP_OAUTH_ALLOW_INSECURE_URLS', undefined, () =>
|
|
150
|
+
expectRejected(outboundUrlGuard.assertAllowed('https://user:pass@93.184.216.34/'), 'credentials')
|
|
151
|
+
));
|
|
152
|
+
|
|
153
|
+
[
|
|
154
|
+
'https://127.0.0.1/mcp',
|
|
155
|
+
'https://10.0.0.1/mcp',
|
|
156
|
+
'https://172.16.5.4/mcp',
|
|
157
|
+
'https://192.168.1.1/mcp',
|
|
158
|
+
'https://169.254.169.254/latest/meta-data',
|
|
159
|
+
'https://100.64.0.1/',
|
|
160
|
+
'https://0.0.0.0/',
|
|
161
|
+
'https://[::1]/mcp',
|
|
162
|
+
'https://[fd00::1]/mcp',
|
|
163
|
+
'https://[fe80::1]/mcp',
|
|
164
|
+
'https://[::ffff:10.0.0.1]/mcp'
|
|
165
|
+
].forEach((url) => {
|
|
166
|
+
it('rejects ' + url, () => withEnv('MCP_OAUTH_ALLOW_INSECURE_URLS', undefined, () =>
|
|
167
|
+
expectRejected(outboundUrlGuard.assertAllowed(url), 'private or reserved address')
|
|
168
|
+
));
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('rejects a hostname that resolves to a private address', () => {
|
|
172
|
+
dns.promises.lookup = async () => [{ address: '192.168.1.1', family: 4 }];
|
|
173
|
+
return withEnv('MCP_OAUTH_ALLOW_INSECURE_URLS', undefined, () =>
|
|
174
|
+
expectRejected(outboundUrlGuard.assertAllowed('https://internal.example.com/mcp'), 'private or reserved address')
|
|
175
|
+
);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('rejects a hostname when any resolved address is private', () => {
|
|
179
|
+
dns.promises.lookup = async () => [
|
|
180
|
+
{ address: '93.184.216.34', family: 4 },
|
|
181
|
+
{ address: '10.0.0.7', family: 4 }
|
|
182
|
+
];
|
|
183
|
+
return withEnv('MCP_OAUTH_ALLOW_INSECURE_URLS', undefined, () =>
|
|
184
|
+
expectRejected(outboundUrlGuard.assertAllowed('https://mixed.example.com/mcp'), 'private or reserved address')
|
|
185
|
+
);
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('rejects a hostname that cannot be resolved', () => {
|
|
189
|
+
dns.promises.lookup = async () => {
|
|
190
|
+
throw new Error('ENOTFOUND');
|
|
191
|
+
};
|
|
192
|
+
return withEnv('MCP_OAUTH_ALLOW_INSECURE_URLS', undefined, () =>
|
|
193
|
+
expectRejected(outboundUrlGuard.assertAllowed('https://nowhere.example.com/mcp'), 'cannot be resolved')
|
|
194
|
+
);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it('accepts a public address', async () => {
|
|
198
|
+
dns.promises.lookup = async () => [{ address: '93.184.216.34', family: 4 }];
|
|
199
|
+
await withEnv('MCP_OAUTH_ALLOW_INSECURE_URLS', undefined, async () => {
|
|
200
|
+
const url = await outboundUrlGuard.assertAllowed('https://mcp.example.com/mcp');
|
|
201
|
+
expect(url.hostname).to.equal('mcp.example.com');
|
|
202
|
+
const literal = await outboundUrlGuard.assertAllowed('https://93.184.216.34/mcp');
|
|
203
|
+
expect(literal.hostname).to.equal('93.184.216.34');
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it('allows http and loopback when MCP_OAUTH_ALLOW_INSECURE_URLS=true', () =>
|
|
208
|
+
withEnv('MCP_OAUTH_ALLOW_INSECURE_URLS', 'true', async () => {
|
|
209
|
+
const url = await outboundUrlGuard.assertAllowed('http://localhost:3232/mcp');
|
|
210
|
+
expect(url.port).to.equal('3232');
|
|
211
|
+
await expectRejected(outboundUrlGuard.assertAllowed('ftp://localhost/x'), 'unsupported protocol');
|
|
212
|
+
})
|
|
213
|
+
);
|
|
214
|
+
|
|
215
|
+
it('pins guarded agents unless insecure URLs are allowed', () => {
|
|
216
|
+
withEnv('MCP_OAUTH_ALLOW_INSECURE_URLS', undefined, () => {
|
|
217
|
+
const options = outboundUrlGuard.httpOptions();
|
|
218
|
+
expect(options.maxRedirects).to.equal(0);
|
|
219
|
+
expect(options.timeout).to.equal(10000);
|
|
220
|
+
expect(options.maxContentLength).to.equal(1048576);
|
|
221
|
+
expect(options.httpsAgent).to.exist;
|
|
222
|
+
expect(options.httpAgent).to.exist;
|
|
223
|
+
});
|
|
224
|
+
withEnv('MCP_OAUTH_ALLOW_INSECURE_URLS', 'true', () => {
|
|
225
|
+
const options = outboundUrlGuard.httpOptions();
|
|
226
|
+
expect(options.httpsAgent).to.equal(undefined);
|
|
227
|
+
expect(options.maxRedirects).to.equal(0);
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
describe('guardedLookup', () => {
|
|
232
|
+
let originalDnsLookup;
|
|
233
|
+
|
|
234
|
+
beforeEach(() => {
|
|
235
|
+
originalDnsLookup = dns.lookup;
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
afterEach(() => {
|
|
239
|
+
dns.lookup = originalDnsLookup;
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it('fails the connection when the host now resolves to a private address', (done) => {
|
|
243
|
+
dns.lookup = (host, options, callback) => callback(null, [{ address: '127.0.0.1', family: 4 }]);
|
|
244
|
+
outboundUrlGuard.guardedLookup('rebinding.example.com', {}, (error) => {
|
|
245
|
+
expect(error).to.be.an('error');
|
|
246
|
+
expect(error.message).to.contain('private or reserved address');
|
|
247
|
+
done();
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it('returns the first address for a public host', (done) => {
|
|
252
|
+
dns.lookup = (host, options, callback) => callback(null, [{ address: '93.184.216.34', family: 4 }]);
|
|
253
|
+
outboundUrlGuard.guardedLookup('mcp.example.com', {}, (error, address, family) => {
|
|
254
|
+
expect(error).to.equal(null);
|
|
255
|
+
expect(address).to.equal('93.184.216.34');
|
|
256
|
+
expect(family).to.equal(4);
|
|
257
|
+
done();
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
it('returns every address when asked for all', (done) => {
|
|
262
|
+
const addresses = [{ address: '93.184.216.34', family: 4 }, { address: '2606:2800:220:1::1', family: 6 }];
|
|
263
|
+
dns.lookup = (host, options, callback) => callback(null, addresses);
|
|
264
|
+
outboundUrlGuard.guardedLookup('mcp.example.com', { all: true }, (error, result) => {
|
|
265
|
+
expect(error).to.equal(null);
|
|
266
|
+
expect(result).to.deep.equal(addresses);
|
|
267
|
+
done();
|
|
268
|
+
});
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
describe('logRedactor', () => {
|
|
274
|
+
|
|
275
|
+
it('redacts OAuth token keys', () => {
|
|
276
|
+
const redacted = redactValue({
|
|
277
|
+
access_token: 'at-1',
|
|
278
|
+
refresh_token: 'rt-1',
|
|
279
|
+
code_verifier: 'cv-1',
|
|
280
|
+
access_token_enc: 'v1:a:b:c',
|
|
281
|
+
status: 'active'
|
|
282
|
+
}, new WeakSet(), 0);
|
|
283
|
+
expect(redacted.access_token).to.equal('[REDACTED]');
|
|
284
|
+
expect(redacted.refresh_token).to.equal('[REDACTED]');
|
|
285
|
+
expect(redacted.code_verifier).to.equal('[REDACTED]');
|
|
286
|
+
expect(redacted.access_token_enc).to.equal('[REDACTED]');
|
|
287
|
+
expect(redacted.status).to.equal('active');
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it('keeps error codes readable', () => {
|
|
291
|
+
const redacted = redactValue({ code: 'ECONNREFUSED' }, new WeakSet(), 0);
|
|
292
|
+
expect(redacted.code).to.equal('ECONNREFUSED');
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it('redacts form-encoded token requests and callback query strings', () => {
|
|
296
|
+
const body = 'grant_type=refresh_token&refresh_token=rt-secret&client_id=abc&client_secret=cs-secret';
|
|
297
|
+
const redactedBody = redactString(body);
|
|
298
|
+
expect(redactedBody).to.not.contain('rt-secret');
|
|
299
|
+
expect(redactedBody).to.not.contain('cs-secret');
|
|
300
|
+
expect(redactedBody).to.contain('grant_type=refresh_token');
|
|
301
|
+
expect(redactedBody).to.contain('client_id=abc');
|
|
302
|
+
|
|
303
|
+
const query = redactString('/oauth2/mcp/callback?code=auth-code-1&state=s1');
|
|
304
|
+
expect(query).to.not.contain('auth-code-1');
|
|
305
|
+
expect(query).to.contain('state=s1');
|
|
306
|
+
|
|
307
|
+
const json = redactString('{"access_token":"at-secret","token_type":"Bearer"}');
|
|
308
|
+
expect(json).to.not.contain('at-secret');
|
|
309
|
+
});
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
describe('McpOAuthConnection', () => {
|
|
313
|
+
|
|
314
|
+
it('exposes no secrets in the public shape', () => {
|
|
315
|
+
const connection = new McpOAuthConnection({
|
|
316
|
+
id_project: 'p1',
|
|
317
|
+
mode: 'mcp',
|
|
318
|
+
server_url: 'https://mcp.example.com/mcp',
|
|
319
|
+
name: 'Example',
|
|
320
|
+
status: 'active',
|
|
321
|
+
client_id: 'client-1',
|
|
322
|
+
client_secret_enc: 'v1:a:b:c',
|
|
323
|
+
access_token_enc: 'v1:a:b:c',
|
|
324
|
+
refresh_token_enc: 'v1:a:b:c',
|
|
325
|
+
scopes: ['read']
|
|
326
|
+
});
|
|
327
|
+
const publicConnection = McpOAuthConnection.toPublic(connection);
|
|
328
|
+
expect(publicConnection).to.include({ mode: 'mcp', server_url: 'https://mcp.example.com/mcp', status: 'active' });
|
|
329
|
+
expect(publicConnection._id).to.exist;
|
|
330
|
+
Object.keys(publicConnection).forEach((field) => {
|
|
331
|
+
expect(field).to.not.match(/_enc$/);
|
|
332
|
+
});
|
|
333
|
+
expect(publicConnection).to.not.have.property('client_id');
|
|
334
|
+
expect(publicConnection).to.not.have.property('id_project');
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
it('validates mode and status', () => {
|
|
338
|
+
const error = new McpOAuthConnection({
|
|
339
|
+
id_project: 'p1',
|
|
340
|
+
mode: 'other',
|
|
341
|
+
server_url: 'https://mcp.example.com/mcp',
|
|
342
|
+
status: 'unknown'
|
|
343
|
+
}).validateSync();
|
|
344
|
+
expect(error.errors.mode).to.exist;
|
|
345
|
+
expect(error.errors.status).to.exist;
|
|
346
|
+
});
|
|
347
|
+
});
|
|
348
|
+
});
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
process.env.NODE_ENV = 'test';
|
|
2
|
+
|
|
3
|
+
const crypto = require('crypto');
|
|
4
|
+
const dns = require('dns');
|
|
5
|
+
const nock = require('nock');
|
|
6
|
+
const chai = require('chai');
|
|
7
|
+
const chaiHttp = require('chai-http');
|
|
8
|
+
const expect = chai.expect;
|
|
9
|
+
|
|
10
|
+
const projectService = require('../services/projectService');
|
|
11
|
+
const userService = require('../services/userService');
|
|
12
|
+
const mcpService = require('../services/mcpService');
|
|
13
|
+
const Integration = require('../models/integrations');
|
|
14
|
+
const McpOAuthConnection = require('../models/mcpOAuthConnection');
|
|
15
|
+
const mcpOAuthCrypto = require('../utils/mcpOAuthCrypto');
|
|
16
|
+
const server = require('../app');
|
|
17
|
+
|
|
18
|
+
chai.use(chaiHttp);
|
|
19
|
+
|
|
20
|
+
const MCP_ORIGIN = 'https://mcp.example.com';
|
|
21
|
+
const AUTH_ORIGIN = 'https://auth.example.com';
|
|
22
|
+
const SERVER_URL = MCP_ORIGIN + '/mcp';
|
|
23
|
+
|
|
24
|
+
const ENV = {
|
|
25
|
+
MCP_OAUTH_ENCRYPTION_KEY: crypto.randomBytes(32).toString('base64'),
|
|
26
|
+
MCP_OAUTH_REDIRECT_BASE_URL: 'https://api.tiledesk.test/',
|
|
27
|
+
MCP_OAUTH_ALLOW_INSECURE_URLS: undefined
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const TOOLS = [{ name: 'echo', description: 'Echoes the input' }, { name: 'add', description: 'Adds numbers' }];
|
|
31
|
+
|
|
32
|
+
function isMethod(method) {
|
|
33
|
+
return (body) => body && body.method === method;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function mockInitialize(token, reply = { status: 200 }) {
|
|
37
|
+
return nock(MCP_ORIGIN)
|
|
38
|
+
.matchHeader('authorization', 'Bearer ' + token)
|
|
39
|
+
.post('/mcp', isMethod('initialize'))
|
|
40
|
+
.reply(reply.status, reply.status === 200
|
|
41
|
+
? { jsonrpc: '2.0', id: 1, result: { protocolVersion: '2025-03-26', capabilities: { tools: {} } } }
|
|
42
|
+
: { error: 'invalid_token' });
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function mockToolsList(token) {
|
|
46
|
+
return nock(MCP_ORIGIN)
|
|
47
|
+
.matchHeader('authorization', 'Bearer ' + token)
|
|
48
|
+
.post('/mcp', isMethod('tools/list'))
|
|
49
|
+
.reply(200, { jsonrpc: '2.0', id: 2, result: { tools: TOOLS } });
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function mockTokenRefresh(accessToken) {
|
|
53
|
+
return nock(AUTH_ORIGIN).post('/token').reply(200, { access_token: accessToken, token_type: 'Bearer', expires_in: 3600 });
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function createProjectUser() {
|
|
57
|
+
const email = 'test-mcp-oauth-svc-' + Date.now() + '-' + Math.random().toString(36).slice(2) + '@email.com';
|
|
58
|
+
const pwd = 'pwd';
|
|
59
|
+
const user = await userService.signup(email, pwd, 'Test Firstname', 'Test lastname');
|
|
60
|
+
const project = await projectService.create('test-mcp-oauth-service', user._id);
|
|
61
|
+
return { email, pwd, projectId: project._id.toString() };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async function seedConnection(projectId, overrides = {}) {
|
|
65
|
+
return McpOAuthConnection.create({
|
|
66
|
+
id_project: projectId,
|
|
67
|
+
mode: 'mcp',
|
|
68
|
+
server_url: SERVER_URL,
|
|
69
|
+
name: 'Example',
|
|
70
|
+
status: 'active',
|
|
71
|
+
client_id: 'dcr-client',
|
|
72
|
+
token_endpoint_auth_method: 'none',
|
|
73
|
+
authorization_endpoint: AUTH_ORIGIN + '/authorize',
|
|
74
|
+
token_endpoint: AUTH_ORIGIN + '/token',
|
|
75
|
+
resource: MCP_ORIGIN + '/',
|
|
76
|
+
access_token_enc: mcpOAuthCrypto.encrypt('at-current'),
|
|
77
|
+
refresh_token_enc: mcpOAuthCrypto.encrypt('rt-current'),
|
|
78
|
+
token_type: 'Bearer',
|
|
79
|
+
expires_at: new Date(Date.now() + 3600 * 1000),
|
|
80
|
+
...overrides
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
describe('mcpService with MCP OAuth connections', function () {
|
|
85
|
+
this.timeout(30000);
|
|
86
|
+
|
|
87
|
+
const savedEnv = {};
|
|
88
|
+
const projectIds = [];
|
|
89
|
+
let admin;
|
|
90
|
+
let other;
|
|
91
|
+
let originalLookup;
|
|
92
|
+
|
|
93
|
+
function setEnv(values) {
|
|
94
|
+
Object.keys(values).forEach((key) => {
|
|
95
|
+
if (values[key] === undefined) {
|
|
96
|
+
delete process.env[key];
|
|
97
|
+
} else {
|
|
98
|
+
process.env[key] = values[key];
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
before(async () => {
|
|
104
|
+
Object.keys(ENV).forEach((key) => { savedEnv[key] = process.env[key]; });
|
|
105
|
+
setEnv(ENV);
|
|
106
|
+
admin = await createProjectUser();
|
|
107
|
+
other = await createProjectUser();
|
|
108
|
+
projectIds.push(admin.projectId, other.projectId);
|
|
109
|
+
nock.disableNetConnect();
|
|
110
|
+
nock.enableNetConnect(/(127\.0\.0\.1|localhost)/);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
after(async () => {
|
|
114
|
+
nock.cleanAll();
|
|
115
|
+
nock.enableNetConnect();
|
|
116
|
+
await McpOAuthConnection.deleteMany({ id_project: { $in: projectIds } });
|
|
117
|
+
await Integration.deleteMany({ id_project: { $in: projectIds }, name: 'mcp' });
|
|
118
|
+
setEnv(savedEnv);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
beforeEach(async () => {
|
|
122
|
+
setEnv(ENV);
|
|
123
|
+
originalLookup = dns.promises.lookup;
|
|
124
|
+
dns.promises.lookup = async () => [{ address: '93.184.216.34', family: 4 }];
|
|
125
|
+
mcpService.clearConnections();
|
|
126
|
+
await McpOAuthConnection.deleteMany({ id_project: { $in: projectIds } });
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
afterEach(() => {
|
|
130
|
+
dns.promises.lookup = originalLookup;
|
|
131
|
+
const pending = nock.pendingMocks();
|
|
132
|
+
nock.cleanAll();
|
|
133
|
+
expect(pending, 'unused HTTP mocks').to.deep.equal([]);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
function connect(projectId, body) {
|
|
137
|
+
return chai.request(server)
|
|
138
|
+
.post('/' + projectId + '/mcp/connect')
|
|
139
|
+
.auth(admin.email, admin.pwd)
|
|
140
|
+
.send(body);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
it('sends the bearer token on every MCP request and overrides a custom Authorization header', async () => {
|
|
144
|
+
const connection = await seedConnection(admin.projectId);
|
|
145
|
+
mockInitialize('at-current');
|
|
146
|
+
mockToolsList('at-current');
|
|
147
|
+
|
|
148
|
+
const res = await connect(admin.projectId, {
|
|
149
|
+
url: SERVER_URL,
|
|
150
|
+
customHeaders: [{ key: 'authorization', value: 'Bearer user-supplied' }, { key: 'X-Extra', value: 'kept' }],
|
|
151
|
+
oauth: { connection_id: connection._id.toString() }
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
expect(res.status).to.equal(200);
|
|
155
|
+
expect(res.body.tools.map((tool) => tool.name)).to.deep.equal(['echo', 'add']);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('refreshes the token and retries once when the MCP server answers 401', async () => {
|
|
159
|
+
const connection = await seedConnection(admin.projectId);
|
|
160
|
+
mockInitialize('at-current', { status: 401 });
|
|
161
|
+
mockTokenRefresh('at-refreshed');
|
|
162
|
+
mockInitialize('at-refreshed');
|
|
163
|
+
mockToolsList('at-refreshed');
|
|
164
|
+
|
|
165
|
+
const res = await connect(admin.projectId, { url: SERVER_URL, oauth: { connection_id: connection._id.toString() } });
|
|
166
|
+
|
|
167
|
+
expect(res.status).to.equal(200);
|
|
168
|
+
const saved = await McpOAuthConnection.findById(connection._id);
|
|
169
|
+
expect(mcpOAuthCrypto.decrypt(saved.access_token_enc)).to.equal('at-refreshed');
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('fails without looping when the MCP server still answers 401 after the refresh', async () => {
|
|
173
|
+
const connection = await seedConnection(admin.projectId);
|
|
174
|
+
mockInitialize('at-current', { status: 401 });
|
|
175
|
+
mockTokenRefresh('at-refreshed');
|
|
176
|
+
mockInitialize('at-refreshed', { status: 401 });
|
|
177
|
+
|
|
178
|
+
const res = await connect(admin.projectId, { url: SERVER_URL, oauth: { connection_id: connection._id.toString() } });
|
|
179
|
+
|
|
180
|
+
expect(res.status).to.equal(500);
|
|
181
|
+
expect(res.body.error).to.contain('401');
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('answers 409 when the connection needs a new authorization', async () => {
|
|
185
|
+
const connection = await seedConnection(admin.projectId, { status: 'needs_reauth' });
|
|
186
|
+
|
|
187
|
+
const res = await connect(admin.projectId, { url: SERVER_URL, oauth: { connection_id: connection._id.toString() } });
|
|
188
|
+
|
|
189
|
+
expect(res.status).to.equal(409);
|
|
190
|
+
expect(res.body.success).to.equal(false);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it('does not use a connection of another project', async () => {
|
|
194
|
+
const connection = await seedConnection(admin.projectId);
|
|
195
|
+
|
|
196
|
+
const connectRes = await chai.request(server)
|
|
197
|
+
.post('/' + other.projectId + '/mcp/connect')
|
|
198
|
+
.auth(other.email, other.pwd)
|
|
199
|
+
.send({ url: SERVER_URL, oauth: { connection_id: connection._id.toString() } });
|
|
200
|
+
expect(connectRes.status).to.equal(404);
|
|
201
|
+
|
|
202
|
+
const saveRes = await chai.request(server)
|
|
203
|
+
.post('/' + other.projectId + '/mcp/servers')
|
|
204
|
+
.auth(other.email, other.pwd)
|
|
205
|
+
.send({ type: 'custom', name: 'Stolen', url: SERVER_URL, oauth: { connection_id: connection._id.toString() } });
|
|
206
|
+
expect(saveRes.status).to.equal(404);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it('does not send the token to a server other than the authorized one', async () => {
|
|
210
|
+
const connection = await seedConnection(admin.projectId);
|
|
211
|
+
|
|
212
|
+
const res = await connect(admin.projectId, {
|
|
213
|
+
url: 'https://attacker.example.com/mcp',
|
|
214
|
+
oauth: { connection_id: connection._id.toString() }
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
expect(res.status).to.equal(400);
|
|
218
|
+
expect(res.body.error).to.contain('another MCP server');
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it('saves the server with the connection reference and no token', async () => {
|
|
222
|
+
const connection = await seedConnection(admin.projectId);
|
|
223
|
+
await Integration.deleteMany({ id_project: admin.projectId, name: 'mcp' });
|
|
224
|
+
mockInitialize('at-current');
|
|
225
|
+
mockToolsList('at-current');
|
|
226
|
+
|
|
227
|
+
const res = await chai.request(server)
|
|
228
|
+
.post('/' + admin.projectId + '/mcp/servers')
|
|
229
|
+
.auth(admin.email, admin.pwd)
|
|
230
|
+
.send({ type: 'custom', name: 'Example', url: SERVER_URL, oauth: { connection_id: connection._id.toString(), mode: 'forged' } });
|
|
231
|
+
|
|
232
|
+
expect(res.status).to.equal(200);
|
|
233
|
+
expect(res.body.server.oauth).to.deep.equal({ connection_id: connection._id.toString(), mode: 'mcp' });
|
|
234
|
+
expect(res.body.server.tools.map((tool) => tool.name)).to.deep.equal(['echo', 'add']);
|
|
235
|
+
|
|
236
|
+
const integration = await Integration.findOne({ id_project: admin.projectId, name: 'mcp' });
|
|
237
|
+
const stored = JSON.stringify(integration.value);
|
|
238
|
+
expect(stored).to.not.contain('at-current');
|
|
239
|
+
expect(stored).to.not.contain('rt-current');
|
|
240
|
+
expect(integration.value.servers[0].oauth.connection_id).to.equal(connection._id.toString());
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it('rejects OAuth on native MCP servers', async () => {
|
|
244
|
+
const connection = await seedConnection(admin.projectId);
|
|
245
|
+
|
|
246
|
+
const res = await chai.request(server)
|
|
247
|
+
.post('/' + admin.projectId + '/mcp/servers')
|
|
248
|
+
.auth(admin.email, admin.pwd)
|
|
249
|
+
.send({ type: 'native', nativeId: 'tiledesk-communicator', oauth: { connection_id: connection._id.toString() } });
|
|
250
|
+
|
|
251
|
+
expect(res.status).to.equal(400);
|
|
252
|
+
expect(res.body.error).to.contain('native');
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it('keeps rejecting the legacy oauth object', async () => {
|
|
256
|
+
const res = await connect(admin.projectId, { url: SERVER_URL, oauth: { clientId: 'x', clientSecret: 'y' } });
|
|
257
|
+
|
|
258
|
+
expect(res.status).to.equal(400);
|
|
259
|
+
expect(res.body.error).to.equal('OAuth2 authentication is not supported yet. Use customHeaders instead.');
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
it('keeps MCP sessions of different connections apart', () => {
|
|
263
|
+
expect(mcpService.getCacheKey(SERVER_URL, 'p1')).to.equal('p1_' + SERVER_URL);
|
|
264
|
+
expect(mcpService.getCacheKey(SERVER_URL, 'p1', 'c1')).to.equal('p1_' + SERVER_URL + '_oauth_c1');
|
|
265
|
+
expect(mcpService.getCacheKey(SERVER_URL, 'p1', 'c1')).to.not.equal(mcpService.getCacheKey(SERVER_URL, 'p1', 'c2'));
|
|
266
|
+
});
|
|
267
|
+
});
|