@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.
Files changed (75) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/app.js +7 -0
  3. package/deploynew.sh +7 -1
  4. package/docs/activities-client.md +27 -0
  5. package/docs/mcp-oauth2-it.md +279 -0
  6. package/docs/mcp-oauth2.md +279 -0
  7. package/docs/numeric-input-validation.md +91 -0
  8. package/docs/superpowers/plans/2026-11-10-mcp-oauth2-server-it.md +155 -0
  9. package/docs/superpowers/plans/2026-11-10-mcp-oauth2-server.md +155 -0
  10. package/docs/superpowers/plans/2026-11-10-mcp-oauth2-tybot-it.md +63 -0
  11. package/docs/superpowers/plans/2026-11-10-mcp-oauth2-tybot.md +63 -0
  12. package/docs/superpowers/specs/2026-11-10-mcp-oauth2-design-it.md +207 -0
  13. package/docs/superpowers/specs/2026-11-10-mcp-oauth2-design.md +207 -0
  14. package/jobs.js +15 -0
  15. package/models/mcpOAuthConnection.js +131 -0
  16. package/models/request.js +8 -0
  17. package/package.json +1 -1
  18. package/pubmodules/activities/activityMessageUtil.js +137 -0
  19. package/pubmodules/activities/models/activity.js +2 -0
  20. package/pubmodules/activities/routes/activity.js +39 -11
  21. package/pubmodules/activities/test/execution-filter.test.js +133 -0
  22. package/pubmodules/activities/test/execution-messages.test.js +158 -0
  23. package/pubmodules/canned/cannedResponseRoute.js +6 -9
  24. package/pubmodules/events/eventRoute.js +6 -9
  25. package/pubmodules/executions-publisher/index.js +457 -0
  26. package/pubmodules/executions-publisher/test/audit-consumer.test.js +384 -0
  27. package/pubmodules/executions-publisher/test/executions-publisher.test.js +204 -0
  28. package/pubmodules/pubModulesManager.js +12 -0
  29. package/routes/answered.js +7 -3
  30. package/routes/campaigns.js +7 -3
  31. package/routes/department.js +14 -3
  32. package/routes/executions-refire.js +148 -0
  33. package/routes/faq.js +6 -14
  34. package/routes/filesp.js +9 -4
  35. package/routes/kb.js +40 -15
  36. package/routes/lead.js +23 -28
  37. package/routes/logs.js +8 -1
  38. package/routes/mcp.js +11 -8
  39. package/routes/mcpOAuth.js +133 -0
  40. package/routes/mcpOAuthCallback.js +75 -0
  41. package/routes/message.js +3 -1
  42. package/routes/messagesRoot.js +5 -18
  43. package/routes/project.js +57 -12
  44. package/routes/project_user.js +21 -4
  45. package/routes/property.js +18 -20
  46. package/routes/request.js +36 -45
  47. package/routes/roles.js +8 -18
  48. package/routes/segment.js +8 -18
  49. package/routes/subscription.js +4 -5
  50. package/routes/tag.js +6 -9
  51. package/routes/unanswered.js +7 -3
  52. package/routes/user-request.js +29 -34
  53. package/routes/webhook.js +120 -54
  54. package/services/mcpOAuthDiscovery.js +248 -0
  55. package/services/mcpOAuthGoogleScopes.js +55 -0
  56. package/services/mcpOAuthService.js +732 -0
  57. package/services/mcpService.js +111 -15
  58. package/services/requestService.js +4 -2
  59. package/services/webhookService.js +4 -2
  60. package/test/executions-refire.test.js +362 -0
  61. package/test/kbRoute.js +4 -7
  62. package/test/mcpOAuthDiscovery.test.js +331 -0
  63. package/test/mcpOAuthGoogleScopes.test.js +25 -0
  64. package/test/mcpOAuthRoute.test.js +455 -0
  65. package/test/mcpOAuthService.test.js +693 -0
  66. package/test/mcpOAuthUtils.test.js +348 -0
  67. package/test/mcpServiceOAuth.test.js +267 -0
  68. package/test/numericInput.js +137 -0
  69. package/test/requestRoute.js +57 -0
  70. package/test/webhookConversationRoute.js +51 -0
  71. package/utils/logRedactor.js +4 -2
  72. package/utils/mcpOAuthCrypto.js +69 -0
  73. package/utils/mcpOAuthPkce.js +20 -0
  74. package/utils/numericInput.js +200 -0
  75. package/utils/outboundUrlGuard.js +131 -0
package/routes/request.js CHANGED
@@ -37,6 +37,7 @@ const { Scheduler } = require('../services/Scheduler');
37
37
  const faq_kb = require('../models/faq_kb');
38
38
  const participantInviteService = require('../services/participantInviteService');
39
39
  const participantRemoveService = require('../services/participantRemoveService');
40
+ const numericInput = require('../utils/numericInput');
40
41
 
41
42
  const datesUtil = require('../utils/datesUtil');
42
43
  //const JobManager = require('../utils/jobs-worker-queue-manager-v2/JobManagerV2');
@@ -111,7 +112,8 @@ router.post('/',
111
112
  var fullname = req.body.senderFullname || req.user.fullName;
112
113
  var email = req.body.email || req.user.email;
113
114
 
114
- let messageStatus = req.body.status || MessageConstants.CHAT_MESSAGE_STATUS.SENDING;
115
+ let messageStatus = numericInput.takeMessageStatus(res, req.body.status);
116
+ if (messageStatus === null) return;
115
117
  winston.debug('messageStatus: ' + messageStatus);
116
118
 
117
119
  var request_id = req.body.request_id || 'support-group-' + req.projectid + "-" + UIDGenerator.generate();
@@ -233,8 +235,10 @@ router.patch('/:requestid', function (req, res) {
233
235
  }
234
236
 
235
237
  // TODO test it. does it work?
236
- if (req.body.status) {
237
- update.status = req.body.status;
238
+ if (req.body.status !== undefined && req.body.status !== null && req.body.status !== '') {
239
+ var status = numericInput.takeOptionalAllowedInteger(res, req.body.status, numericInput.REQUEST_STATUSES, 'status');
240
+ if (status === null) return;
241
+ update.status = status;
238
242
  }
239
243
 
240
244
  if (req.body.tags) {
@@ -245,8 +249,12 @@ router.patch('/:requestid', function (req, res) {
245
249
  // update.notes = req.body.notes;
246
250
  // }
247
251
 
248
- if (req.body.rating) {
249
- update.rating = req.body.rating;
252
+ if (req.body.rating !== undefined && req.body.rating !== null && req.body.rating !== '') {
253
+ var rating = numericInput.parseBoundedInteger(req.body.rating, 1, 5);
254
+ if (rating === null) {
255
+ return numericInput.sendInvalidNumber(res, 'rating must be an integer between 1 and 5');
256
+ }
257
+ update.rating = rating;
250
258
  }
251
259
 
252
260
  if (req.body.rating_message) {
@@ -294,7 +302,7 @@ router.patch('/:requestid', function (req, res) {
294
302
  winston.verbose("Request patch update", update);
295
303
 
296
304
  //cacheinvalidation
297
- return Request.findOneAndUpdate({ "request_id": req.params.requestid, "id_project": req.projectid }, { $set: update }, { new: true, upsert: false })
305
+ return Request.findOneAndUpdate({ "request_id": req.params.requestid, "id_project": req.projectid }, { $set: update }, { new: true, upsert: false, runValidators: true })
298
306
  .populate('lead')
299
307
  .populate('department')
300
308
  .populate('participatingBots')
@@ -574,6 +582,11 @@ router.put('/:requestid/replace', async (req, res) => {
574
582
  winston.verbose("participants to be set: ", participants);
575
583
 
576
584
  const assignmentOptions = assignmentContextUtil.buildSetParticipantsOptions(req, participants);
585
+ // Subagent calls/returns reuse this route: the reason tells the executions publisher
586
+ // that the bot change is not a replacement of the conversation's bot.
587
+ if (req.body.reason === 'subagent_call' || req.body.reason === 'subagent_return') {
588
+ assignmentOptions.reason = req.body.reason;
589
+ }
577
590
  requestService.setParticipantsByRequestId(req.params.requestid, req.projectid, participants, assignmentOptions).then((updatedRequest) => {
578
591
  winston.debug("SetParticipant response: ", updatedRequest);
579
592
  // Additive: include the resolved canonical (root) bot id for analytics
@@ -1269,29 +1282,15 @@ router.get('/', function (req, res, next) {
1269
1282
  winston.debug("req.query.sort", req.query.sort);
1270
1283
  winston.debug('REQUEST ROUTE - QUERY ', req.query)
1271
1284
 
1272
- const DEFAULT_LIMIT = 40;
1273
-
1274
- var page = 0;
1275
- var limit = DEFAULT_LIMIT; // Number of request per page
1276
- var page = 0;
1277
- var skip = 0;
1285
+ const paging = numericInput.takePagination(res, req.query, { defaultLimit: 40, maxLimit: 100, overMax: 'default' });
1286
+ if (!paging) return;
1287
+ var limit = paging.limit;
1288
+ var page = paging.page;
1289
+ var skip = paging.skip;
1278
1290
  let statusArray = [];
1279
1291
  var projectuser = req.projectuser;
1280
1292
  let filterRangeField = req.query.filterRangeField || 'createdAt';
1281
1293
 
1282
- if (req.query.limit) {
1283
- limit = parseInt(req.query.limit);
1284
- }
1285
- if (limit > 100) {
1286
- limit = DEFAULT_LIMIT;
1287
- }
1288
-
1289
- if (req.query.page) {
1290
- page = req.query.page;
1291
- }
1292
-
1293
- skip = page * limit;
1294
-
1295
1294
  // Default query
1296
1295
  var query = { "id_project": req.projectid, "status": { $lt: 1000, $nin: [50, 150] }, preflight: false };
1297
1296
 
@@ -1342,8 +1341,8 @@ router.get('/', function (req, res, next) {
1342
1341
  if (req.query.status === 'all') {
1343
1342
  delete query.status;
1344
1343
  } else {
1345
- let statusArray = req.query.status.split(',').map(Number);
1346
- statusArray = statusArray.map(status => { return isNaN(status) ? null : status }).filter(status => status !== null)
1344
+ statusArray = numericInput.takeStatusList(res, req.query.status, numericInput.REQUEST_STATUSES);
1345
+ if (!statusArray) return;
1347
1346
  if (statusArray.length > 0) {
1348
1347
  query.status = {
1349
1348
  $in: statusArray
@@ -1487,10 +1486,8 @@ router.get('/', function (req, res, next) {
1487
1486
  }
1488
1487
 
1489
1488
 
1490
- var direction = -1; //-1 descending , 1 ascending
1491
- if (req.query.direction) {
1492
- direction = req.query.direction;
1493
- }
1489
+ var direction = numericInput.takeSortDirection(res, req.query.direction);
1490
+ if (direction === null) return;
1494
1491
 
1495
1492
  var sortField = "createdAt";
1496
1493
  if (req.query.sort) {
@@ -1998,17 +1995,13 @@ router.get('/csv', function (req, res, next) {
1998
1995
  winston.debug('REQUEST ROUTE - QUERY ', req.query)
1999
1996
 
2000
1997
  var limit = 100000; // Number of request per page
2001
- var page = 0;
2002
- var skip = 0;
1998
+ var page = numericInput.takeOptionalBoundedInteger(res, req.query.page, 0, 1000000, 'page');
1999
+ if (page === null) return;
2000
+ if (page === undefined) page = 0;
2001
+ var skip = page * limit;
2003
2002
  let statusArray = [];
2004
2003
  var projectuser = req.projectuser;
2005
2004
  let filterRangeField = req.query.filterRangeField || 'createdAt';
2006
-
2007
- if (req.query.page) {
2008
- page = req.query.page;
2009
- }
2010
-
2011
- skip = page * limit;
2012
2005
  winston.debug('REQUEST ROUTE - SKIP PAGE ', skip);
2013
2006
 
2014
2007
  // Default query (same as GET /)
@@ -2052,8 +2045,8 @@ router.get('/csv', function (req, res, next) {
2052
2045
  if (req.query.status === 'all') {
2053
2046
  delete query.status;
2054
2047
  } else {
2055
- statusArray = req.query.status.split(',').map(Number);
2056
- statusArray = statusArray.map(status => { return isNaN(status) ? null : status }).filter(status => status !== null);
2048
+ statusArray = numericInput.takeStatusList(res, req.query.status, numericInput.REQUEST_STATUSES);
2049
+ if (!statusArray) return;
2057
2050
  if (statusArray.length > 0) {
2058
2051
  query.status = { $in: statusArray };
2059
2052
  } else {
@@ -2168,10 +2161,8 @@ router.get('/csv', function (req, res, next) {
2168
2161
  query.priority = req.query.priority;
2169
2162
  }
2170
2163
 
2171
- var direction = -1; // same default as GET /
2172
- if (req.query.direction) {
2173
- direction = req.query.direction;
2174
- }
2164
+ var direction = numericInput.takeSortDirection(res, req.query.direction);
2165
+ if (direction === null) return;
2175
2166
  winston.debug("direction", direction);
2176
2167
 
2177
2168
  var sortField = "createdAt";
package/routes/roles.js CHANGED
@@ -2,6 +2,7 @@ var express = require('express');
2
2
  var router = express.Router();
3
3
  var Role = require("../models/role");
4
4
  var winston = require('../config/winston');
5
+ var numericInput = require('../utils/numericInput');
5
6
  const roleEvent = require('../event/roleEvent');
6
7
 
7
8
  router.post('/', function (req, res) {
@@ -92,20 +93,11 @@ router.get('/:roleid', function (req, res) {
92
93
 
93
94
  router.get('/', async(req, res) => {
94
95
 
95
- var limit = 40; // Number of request per page
96
-
97
- if (req.query.limit) {
98
- limit = parseInt(req.query.limit);
99
- winston.debug('LEAD ROUTE - limit: '+limit);
100
- }
101
-
102
- var page = 0;
103
-
104
- if (req.query.page) {
105
- page = req.query.page;
106
- }
107
-
108
- var skip = page * limit;
96
+ const paging = numericInput.takePagination(res, req.query, { defaultLimit: 40, maxLimit: 1000, overMax: 'reject' });
97
+ if (!paging) return;
98
+ var limit = paging.limit;
99
+ var skip = paging.skip;
100
+ winston.debug('LEAD ROUTE - limit: '+limit);
109
101
  winston.debug('ROLE ROUTE - SKIP PAGE ', skip);
110
102
 
111
103
 
@@ -113,10 +105,8 @@ router.get('/', async(req, res) => {
113
105
 
114
106
 
115
107
 
116
- var direction = -1; //-1 descending , 1 ascending
117
- if (req.query.direction) {
118
- direction = req.query.direction;
119
- }
108
+ var direction = numericInput.takeSortDirection(res, req.query.direction);
109
+ if (direction === null) return;
120
110
 
121
111
  var sortField = "createdAt";
122
112
  if (req.query.sort) {
package/routes/segment.js CHANGED
@@ -2,6 +2,7 @@ var express = require('express');
2
2
  var router = express.Router();
3
3
  var winston = require('../config/winston');
4
4
  var Segment = require("../models/segment");
5
+ var numericInput = require('../utils/numericInput');
5
6
 
6
7
 
7
8
  router.post('/', function (req, res) {
@@ -91,29 +92,18 @@ router.get('/:segmentid', function (req, res) {
91
92
 
92
93
  router.get('/', function (req, res) {
93
94
 
94
- var limit = 40; // Number of request per page
95
-
96
- if (req.query.limit) {
97
- limit = parseInt(req.query.limit);
98
- winston.debug('LEAD ROUTE - limit: '+limit);
99
- }
100
-
101
- var page = 0;
102
-
103
- if (req.query.page) {
104
- page = req.query.page;
105
- }
106
-
107
- var skip = page * limit;
95
+ const paging = numericInput.takePagination(res, req.query, { defaultLimit: 40, maxLimit: 1000, overMax: 'reject' });
96
+ if (!paging) return;
97
+ var limit = paging.limit;
98
+ var skip = paging.skip;
99
+ winston.debug('LEAD ROUTE - limit: '+limit);
108
100
  winston.debug('LEAD ROUTE - SKIP PAGE ', skip);
109
101
 
110
102
 
111
103
  var query = { "id_project": req.projectid, "status": 100};
112
104
 
113
- var direction = -1; //-1 descending , 1 ascending
114
- if (req.query.direction) {
115
- direction = req.query.direction;
116
- }
105
+ var direction = numericInput.takeSortDirection(res, req.query.direction);
106
+ if (direction === null) return;
117
107
 
118
108
  var sortField = "createdAt";
119
109
  if (req.query.sort) {
@@ -4,6 +4,7 @@ var Subscription = require("../models/subscription");
4
4
  var SubscriptionLog = require("../models/subscriptionLog");
5
5
  var subscriptionEvent = require("../event/subscriptionEvent");
6
6
  var winston = require('../config/winston');
7
+ var numericInput = require('../utils/numericInput');
7
8
 
8
9
  //space
9
10
 
@@ -96,11 +97,9 @@ router.get('/', function (req, res) {
96
97
  router.get('/history', function (req, res) {
97
98
 
98
99
  var limit = 40; // Number of leads per page
99
- var page = 0;
100
-
101
- if (req.query.page) {
102
- page = req.query.page;
103
- }
100
+ var page = numericInput.takeOptionalBoundedInteger(res, req.query.page, 0, 1000000, 'page');
101
+ if (page === null) return;
102
+ if (page === undefined) page = 0;
104
103
 
105
104
  var skip = page * limit;
106
105
  winston.debug('Subscription ROUTE - SKIP PAGE ', skip);
package/routes/tag.js CHANGED
@@ -2,6 +2,7 @@ var express = require('express');
2
2
  var router = express.Router();
3
3
  var TagLibrary = require("../models/tagLibrary");
4
4
  var winston = require('../config/winston');
5
+ var numericInput = require('../utils/numericInput');
5
6
 
6
7
  router.post('/', function (req, res) {
7
8
 
@@ -86,11 +87,9 @@ router.get('/:tagid', function (req, res) {
86
87
 
87
88
  router.get('/', function (req, res) {
88
89
  var limit = 40; // Number of Tags per page
89
- var page = 0;
90
-
91
- if (req.query.page) {
92
- page = req.query.page;
93
- }
90
+ var page = numericInput.takeOptionalBoundedInteger(res, req.query.page, 0, 1000000, 'page');
91
+ if (page === null) return;
92
+ if (page === undefined) page = 0;
94
93
 
95
94
  var skip = page * limit;
96
95
  winston.debug('Tag ROUTE - SKIP PAGE ', skip);
@@ -104,10 +103,8 @@ router.get('/', function (req, res) {
104
103
  // }
105
104
 
106
105
 
107
- var direction = -1; //-1 descending , 1 ascending
108
- if (req.query.direction) {
109
- direction = req.query.direction;
110
- }
106
+ var direction = numericInput.takeSortDirection(res, req.query.direction);
107
+ if (direction === null) return;
111
108
 
112
109
  var sortField = "createdAt";
113
110
  if (req.query.sort) {
@@ -4,6 +4,7 @@ const { Namespace, UnansweredQuestion } = require('../models/kb_setting');
4
4
  var winston = require('../config/winston');
5
5
  var fastCsv = require('fast-csv');
6
6
  const kbQuestionService = require('../services/kbQuestionService');
7
+ const numericInput = require('../utils/numericInput');
7
8
 
8
9
  // Add a new unanswered question
9
10
  router.post('/', async (req, res) => {
@@ -49,10 +50,13 @@ router.get('/:namespace', async (req, res) => {
49
50
  });
50
51
  }
51
52
 
52
- const page = parseInt(req.query.page) || 0;
53
- const limit = parseInt(req.query.limit) || 20;
53
+ const paging = numericInput.takePagination(res, req.query, { defaultLimit: 20, maxLimit: 100, overMax: 'reject' });
54
+ if (!paging) return;
55
+ const page = paging.page;
56
+ const limit = paging.limit;
54
57
  const sortField = req.query.sortField || 'createdAt';
55
- const direction = parseInt(req.query.direction) || -1;
58
+ const direction = numericInput.takeSortDirection(res, req.query.direction);
59
+ if (direction === null) return;
56
60
 
57
61
  const filter = { id_project, namespace };
58
62
 
@@ -4,20 +4,28 @@ var Request = require("../models/request");
4
4
  var winston = require('../config/winston');
5
5
  var moment = require('moment');
6
6
  const requestEvent = require('../event/requestEvent');
7
- const { check, validationResult } = require('express-validator');
8
7
  var requestService = require('../services/requestService');
9
8
  var mongoose = require('mongoose');
9
+ var numericInput = require('../utils/numericInput');
10
10
 
11
11
 
12
12
  router.patch('/:requestid/rating', function (req, res) {
13
13
  winston.debug(req.body);
14
14
  const update = {};
15
15
 
16
- if (req.body.rating) {
17
- update.rating = req.body.rating;
16
+ // Satisfaction is an integer 1-5. Negative, zero, and out-of-range values are rejected.
17
+ if (req.body.rating !== undefined && req.body.rating !== null && req.body.rating !== '') {
18
+ var rating = numericInput.parseBoundedInteger(req.body.rating, 1, 5);
19
+ if (rating === null) {
20
+ return numericInput.sendInvalidNumber(res, 'rating must be an integer between 1 and 5');
21
+ }
22
+ update.rating = rating;
18
23
  }
19
24
 
20
25
  if (req.body.rating_message) {
26
+ if (typeof req.body.rating_message !== 'string') {
27
+ return numericInput.sendInvalidNumber(res, 'rating_message must be a string');
28
+ }
21
29
  update.rating_message = req.body.rating_message;
22
30
  }
23
31
 
@@ -32,7 +40,7 @@ router.patch('/:requestid/rating', function (req, res) {
32
40
  }
33
41
 
34
42
  //cacheinvalidation
35
- return Request.findOneAndUpdate(query, { $set: update }, { new: true, upsert: false })
43
+ return Request.findOneAndUpdate(query, { $set: update }, { new: true, upsert: false, runValidators: true })
36
44
  .populate('lead')
37
45
  .populate('department')
38
46
  .populate('participatingBots')
@@ -87,25 +95,11 @@ router.get('/me', function (req, res, next) {
87
95
  winston.debug("req.query.sort", req.query.sort);
88
96
  winston.debug('REQUEST ROUTE - QUERY ', req.query)
89
97
 
90
- const DEFAULT_LIMIT = 40;
91
-
92
- var limit = DEFAULT_LIMIT; // Number of request per page
93
-
94
- if (req.query.limit) {
95
- limit = parseInt(req.query.limit);
96
- }
97
- if (limit > 100) {
98
- limit = DEFAULT_LIMIT;
99
- }
100
-
101
-
102
- var page = 0;
103
-
104
- if (req.query.page) {
105
- page = req.query.page;
106
- }
107
-
108
- var skip = page * limit;
98
+ const paging = numericInput.takePagination(res, req.query, { defaultLimit: 40, maxLimit: 100, overMax: 'default' });
99
+ if (!paging) return;
100
+ var limit = paging.limit;
101
+ var page = paging.page;
102
+ var skip = paging.skip;
109
103
  winston.debug('REQUEST ROUTE - SKIP PAGE ', skip);
110
104
 
111
105
 
@@ -149,14 +143,17 @@ router.get('/me', function (req, res, next) {
149
143
 
150
144
  if (req.query.status) {
151
145
  winston.debug('req.query.status', req.query.status);
152
- query.status = req.query.status;
153
-
154
- if (req.query.status == 1000 || req.query.status == "1000") {
146
+ if (req.query.status === "all") {
155
147
  history_search = true;
156
- }
157
- if (req.query.status==="all") {
158
- history_search = true;
159
- delete query.status;
148
+ } else {
149
+ var status = numericInput.takeOptionalAllowedInteger(res, req.query.status, numericInput.REQUEST_STATUSES, 'status');
150
+ if (status === null) return;
151
+ if (status !== undefined) {
152
+ query.status = status;
153
+ if (status === 1000) {
154
+ history_search = true;
155
+ }
156
+ }
160
157
  }
161
158
  }
162
159
 
@@ -312,10 +309,8 @@ router.get('/me', function (req, res, next) {
312
309
  }
313
310
 
314
311
 
315
- var direction = -1; //-1 descending , 1 ascending
316
- if (req.query.direction) {
317
- direction = req.query.direction;
318
- }
312
+ var direction = numericInput.takeSortDirection(res, req.query.direction);
313
+ if (direction === null) return;
319
314
  winston.debug("direction", direction);
320
315
 
321
316
  var sortField = "createdAt";