@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
@@ -12,17 +12,20 @@ var winston = require('../config/winston');
12
12
  var cacheUtil = require('../utils/cacheUtil');
13
13
 
14
14
  var departmentEvent = require("../event/departmentEvent");
15
+ var numericInput = require('../utils/numericInput');
15
16
 
16
17
 
17
18
  router.post('/', [passport.authenticate(['basic', 'jwt'], { session: false }), validtoken, roleChecker.hasRole('admin')], function (req, res) {
18
19
 
19
20
  winston.debug("DEPT REQ BODY ", req.body);
21
+ var departmentStatus = numericInput.takeOptionalAllowedInteger(res, req.body.status, numericInput.DEPARTMENT_STATUSES, 'status');
22
+ if (departmentStatus === null) return;
20
23
  var newDepartment = new Department({
21
24
  routing: req.body.routing,
22
25
  name: req.body.name,
23
26
  description: req.body.description,
24
27
  default: req.body.default,
25
- status: req.body.status,
28
+ status: departmentStatus,
26
29
  id_group: req.body.id_group,
27
30
  groups: req.body.groups,
28
31
  id_project: req.projectid,
@@ -78,7 +81,11 @@ router.put('/:departmentid', [passport.authenticate(['basic', 'jwt'], { session:
78
81
  update.online_msg = req.body.online_msg;
79
82
  }
80
83
  if (req.body.status!=undefined) {
81
- update.status = req.body.status;
84
+ var departmentStatus = numericInput.takeOptionalAllowedInteger(res, req.body.status, numericInput.DEPARTMENT_STATUSES, 'status');
85
+ if (departmentStatus === null) return;
86
+ if (departmentStatus !== undefined) {
87
+ update.status = departmentStatus;
88
+ }
82
89
  }
83
90
  if (req.body.groups!=undefined) {
84
91
  update.groups = req.body.groups;
@@ -104,7 +111,11 @@ router.put('/:departmentid', [passport.authenticate(['basic', 'jwt'], { session:
104
111
 
105
112
 
106
113
  if (req.body.status!=undefined) {
107
- update.status = req.body.status;
114
+ var departmentStatus = numericInput.takeOptionalAllowedInteger(res, req.body.status, numericInput.DEPARTMENT_STATUSES, 'status');
115
+ if (departmentStatus === null) return;
116
+ if (departmentStatus !== undefined) {
117
+ update.status = departmentStatus;
118
+ }
108
119
  }
109
120
  if (req.body.id_bot!=undefined) {
110
121
  update.id_bot = req.body.id_bot;
@@ -0,0 +1,148 @@
1
+ /**
2
+ * POST /:projectid/executions/refire — "start new" of an agent execution (traceability Phase 3).
3
+ *
4
+ * Called by tiledesk-executions with the user's own Authorization header after it audited the start new:
5
+ * { chatbot_id, trigger: { type, source_id, start_point_id }, payload, retry_of }
6
+ * It starts a new run of the same unattended trigger with the user-confirmed `payload` (never re-read
7
+ * from the stored mapping), carrying `retry_of` so tybot links the new execution to the old one.
8
+ * Replies `{ success: true, request_id }`.
9
+ *
10
+ * Mounted only when EXECUTIONS_AMQP_URL is set (mountIfEnabled). Needs `executions_control`: owner/admin
11
+ * by role hierarchy, a custom role only with that permission; agent/supervisor get 403.
12
+ * Bodies up to the global JSON_BODY_LIMIT (default 500KB) are accepted, enough for the ~300 KB refire.
13
+ */
14
+ const express = require('express');
15
+ const passport = require('passport');
16
+ const winston = require('../config/winston');
17
+ const validtoken = require('../middleware/valid-token');
18
+ const roleChecker = require('../middleware/has-role');
19
+ const { Webhook } = require('../models/webhook');
20
+ const webhookService = require('../services/webhookService');
21
+ const webhookSettingsValidator = require('../services/webhookSettingsValidator');
22
+ const webhookEvent = require('../event/webhookEvent');
23
+ const webhookRoute = require('./webhook');
24
+ const ObjectId = require('mongoose').Types.ObjectId;
25
+
26
+ const PERMISSION = 'executions_control';
27
+ // Keys the server and tybot add to an automation payload on its way to /block (the stored payload carries them):
28
+ // never taken from the user's confirmed input. webhook_http_method / webhook_query_params describe the call: kept.
29
+ const SERVER_KEYS = ['request_id', 'preloaded_request_id', 'token', 'async', 'draft'];
30
+ const TRIGGER_TYPES = ['scheduled', 'webhook', 'automation'];
31
+
32
+ const router = express.Router({ mergeParams: true });
33
+
34
+ function isObject(v) {
35
+ return v !== null && typeof v === 'object' && !Array.isArray(v);
36
+ }
37
+
38
+ async function findProjectWebhook(id_project, webhook_id) {
39
+ if (!webhook_id) {
40
+ return null;
41
+ }
42
+ return Webhook.findOne({ id_project: String(id_project), webhook_id: String(webhook_id) });
43
+ }
44
+
45
+ async function refireAutomation(req, res, webhook, payload, retry_of) {
46
+ if (!(await webhookRoute.checkEnabledAndRateLimit(req, res, webhook))) {
47
+ return;
48
+ }
49
+ // Same body the public automation route builds; tybot's /block lifts the top-level retry_of out of it.
50
+ const body = Object.assign({}, payload, { retry_of: retry_of });
51
+ SERVER_KEYS.forEach(function (k) { delete body[k]; });
52
+ body.request_id = "automation-request-" + webhook.id_project + "-" + new ObjectId() + "-" + webhook.webhook_id;
53
+ try {
54
+ // forceAsync: the reply comes once tybot took the run, never after the flow (a sync webhook could outlast the
55
+ // caller's timeout, and the user's retry would start a second run)
56
+ await webhookService.run(webhook, body, undefined, undefined, { forceAsync: true });
57
+ } catch (err) {
58
+ winston.error("(executions-refire) automation run failed for webhook " + webhook.webhook_id, err);
59
+ const status = (err && err.status) || 500;
60
+ return res.status(status).send({ success: false, error: "Error running the automation" });
61
+ }
62
+ webhookEvent.emit("webhook.triggered", { webhook: webhook, payload: body });
63
+ return res.status(202).send({ success: true, request_id: body.preloaded_request_id });
64
+ }
65
+
66
+ router.post('/refire', async (req, res) => {
67
+ const id_project = req.params.projectid;
68
+ const b = req.body || {};
69
+ const trigger = isObject(b.trigger) ? b.trigger : {};
70
+ const type = trigger.type;
71
+ const retry_of = b.retry_of;
72
+ const payload = isObject(b.payload) ? b.payload : {};
73
+
74
+ if (!b.chatbot_id || typeof b.chatbot_id !== 'string') {
75
+ return res.status(400).send({ success: false, error: 'chatbot_id is required' });
76
+ }
77
+ if (TRIGGER_TYPES.indexOf(type) < 0) {
78
+ return res.status(400).send({ success: false, error: 'unknown_trigger_type' });
79
+ }
80
+ if (retry_of !== undefined && retry_of !== null && typeof retry_of !== 'string') {
81
+ return res.status(400).send({ success: false, error: 'retry_of must be a string' });
82
+ }
83
+
84
+ let chatbot;
85
+ try {
86
+ chatbot = await webhookSettingsValidator.findChatbot(String(id_project), b.chatbot_id);
87
+ } catch (err) {
88
+ winston.error("(executions-refire) error finding chatbot " + b.chatbot_id, err);
89
+ return res.status(500).send({ success: false, error: 'Error finding chatbot' });
90
+ }
91
+ if (!chatbot) {
92
+ return res.status(404).send({ success: false, error: 'chatbot_not_found' });
93
+ }
94
+
95
+ const extraAttributes = retry_of ? { retry_of: retry_of } : {};
96
+
97
+ if (type === 'scheduled') {
98
+ return webhookRoute.startScheduledRun(req, res, {
99
+ id_project: String(id_project),
100
+ chatbot_id: b.chatbot_id,
101
+ payload: payload,
102
+ extraAttributes: extraAttributes
103
+ });
104
+ }
105
+
106
+ let webhook;
107
+ try {
108
+ webhook = await findProjectWebhook(id_project, trigger.source_id);
109
+ } catch (err) {
110
+ winston.error("(executions-refire) error finding webhook " + trigger.source_id, err);
111
+ return res.status(500).send({ success: false, error: 'Error finding webhook' });
112
+ }
113
+ if (!webhook || String(webhook.chatbot_id) !== String(b.chatbot_id)) {
114
+ return res.status(404).send({ success: false, error: 'webhook_not_found' });
115
+ }
116
+
117
+ // The run kind follows the webhook's current mode; a refire of the other kind would start the wrong path.
118
+ const conversationMode = webhook.mode === webhookSettingsValidator.CONVERSATION_MODE;
119
+ if ((type === 'webhook') !== conversationMode) {
120
+ return res.status(409).send({ success: false, error: 'trigger_mismatch' });
121
+ }
122
+
123
+ if (type === 'webhook') {
124
+ if (!(await webhookRoute.checkEnabledAndRateLimit(req, res, webhook))) {
125
+ return;
126
+ }
127
+ return webhookRoute.startWebhookConversation(res, webhook, payload, { dev: false, extraAttributes: extraAttributes });
128
+ }
129
+
130
+ return refireAutomation(req, res, webhook, payload, retry_of);
131
+ });
132
+
133
+ // The off switch: the route exists only when EXECUTIONS_AMQP_URL is set (read when mounting).
134
+ function mountIfEnabled(app) {
135
+ if (!process.env.EXECUTIONS_AMQP_URL) {
136
+ winston.info("executions refire route disabled: EXECUTIONS_AMQP_URL not set");
137
+ return false;
138
+ }
139
+ app.use('/:projectid/executions', [
140
+ passport.authenticate(['basic', 'jwt'], { session: false }),
141
+ validtoken,
142
+ roleChecker.hasRoleOrTypes('admin', undefined, PERMISSION)
143
+ ], router);
144
+ return true;
145
+ }
146
+
147
+ module.exports = router;
148
+ module.exports.mountIfEnabled = mountIfEnabled;
package/routes/faq.js CHANGED
@@ -17,6 +17,7 @@ var configGlobal = require('../config/global');
17
17
  const roleConstants = require('../models/roleConstants');
18
18
  const roleChecker = require('../middleware/has-role');
19
19
  const { ChatbotService } = require('../services/chatbotService');
20
+ var numericInput = require('../utils/numericInput');
20
21
 
21
22
  const apiUrl = process.env.API_URL || configGlobal.apiUrl;
22
23
 
@@ -605,20 +606,11 @@ router.get('/', roleChecker.hasRoleOrTypes('agent', ['bot', 'subscription']), fu
605
606
  query.id_faq_kb = req.query.id_faq_kb;
606
607
  }
607
608
 
608
- var limit = 3000; // Number of request per page
609
-
610
- if (req.query.limit) {
611
- limit = parseInt(req.query.limit);
612
- winston.debug('faq ROUTE - limit: ' + limit);
613
- }
614
-
615
- var page = 0;
616
-
617
- if (req.query.page) {
618
- page = req.query.page;
619
- }
620
-
621
- var skip = page * limit;
609
+ const paging = numericInput.takePagination(res, req.query, { defaultLimit: 3000, maxLimit: 10000, overMax: 'reject' });
610
+ if (!paging) return;
611
+ var limit = paging.limit;
612
+ var skip = paging.skip;
613
+ winston.debug('faq ROUTE - limit: ' + limit);
622
614
  winston.debug('faq ROUTE - SKIP PAGE ', skip);
623
615
 
624
616
 
package/routes/filesp.js CHANGED
@@ -13,6 +13,7 @@ require('../middleware/passport.js')(passport);
13
13
  const validtoken = require('../middleware/valid-token.js')
14
14
  const roleChecker = require('../middleware/has-role.js');
15
15
  const winston = require('../config/winston.js');
16
+ const numericInput = require('../utils/numericInput');
16
17
  const FileGridFsService = require('../services/fileGridFsService.js');
17
18
  const roleConstants = require('../models/roleConstants.js');
18
19
  const faq_kb = require('../models/faq_kb');
@@ -217,10 +218,14 @@ router.post('/assets', [
217
218
  validtoken,
218
219
  roleChecker.hasRoleOrTypes('admin', ['bot','subscription'])
219
220
  ], async (req, res) => {
220
- // Assets have no retention by default, but can be set via query parameter
221
- let customExpiration = parseInt(req.query?.expiration, 10);
222
- if (customExpiration && !isNaN(customExpiration) && customExpiration > 0) {
223
- req.expireAt = new Date(Date.now() + customExpiration * 1000);
221
+ // Assets have no retention by default, but can be set via query parameter.
222
+ // expiration is a positive number of seconds.
223
+ if (req.query && req.query.expiration !== undefined && req.query.expiration !== '') {
224
+ let customExpiration = numericInput.takeOptionalBoundedInteger(res, req.query.expiration, 1, 315360000, 'expiration');
225
+ if (customExpiration === null) return;
226
+ if (customExpiration !== undefined) {
227
+ req.expireAt = new Date(Date.now() + customExpiration * 1000);
228
+ }
224
229
  }
225
230
 
226
231
 
package/routes/kb.js CHANGED
@@ -10,6 +10,7 @@ var mongoose = require('mongoose');
10
10
  var parsecsv = require("fast-csv");
11
11
 
12
12
  var { Namespace, KB } = require('../models/kb_setting');
13
+ var numericInput = require('../utils/numericInput');
13
14
  const faq = require('../models/faq');
14
15
  const faq_kb = require('../models/faq_kb');
15
16
 
@@ -1183,7 +1184,11 @@ router.get('/namespace/export/:id', async (req, res) => {
1183
1184
  query.namespace = namespace_id;
1184
1185
 
1185
1186
  if (req.query.status) {
1186
- query.status = parseInt(req.query.status)
1187
+ var exportStatus = numericInput.takeOptionalBoundedInteger(res, req.query.status, -1, 10000, 'status');
1188
+ if (exportStatus === null) return;
1189
+ if (exportStatus !== undefined) {
1190
+ query.status = exportStatus;
1191
+ }
1187
1192
  }
1188
1193
 
1189
1194
  query.type = { $in: [kbTypes.URL, kbTypes.TEXT, kbTypes.FAQ] };
@@ -1349,6 +1354,17 @@ router.post('/namespace/import/:id', freezePineconeWrites, upload.single('upload
1349
1354
  return res.status(403).send({ success: false, error: "Cannot exceed the number of resources in the current plan", plan_limit: kbs_limit })
1350
1355
  }
1351
1356
 
1357
+ if (Array.isArray(contents)) {
1358
+ for (var contentIndex = 0; contentIndex < contents.length; contentIndex++) {
1359
+ var contentItem = contents[contentIndex];
1360
+ if (contentItem && contentItem.scrape_type !== undefined && contentItem.scrape_type !== null && contentItem.scrape_type !== '') {
1361
+ if (numericInput.parseAllowedInteger(contentItem.scrape_type, numericInput.SCRAPE_TYPES) === null) {
1362
+ return numericInput.sendInvalidNumber(res, 'scrape_type must be an integer and one of: ' + numericInput.SCRAPE_TYPES.join(', '));
1363
+ }
1364
+ }
1365
+ }
1366
+ }
1367
+
1352
1368
  let addingContents = [];
1353
1369
  contents.forEach(async (e) => {
1354
1370
  let content = {
@@ -1653,8 +1669,11 @@ router.get('/', async (req, res) => {
1653
1669
  query["namespace"] = namespace;
1654
1670
 
1655
1671
  if (req.query.status) {
1656
- status = parseInt(req.query.status);
1657
- query["status"] = status;
1672
+ status = numericInput.takeOptionalBoundedInteger(res, req.query.status, -1, 10000, 'status');
1673
+ if (status === null) return;
1674
+ if (status !== undefined) {
1675
+ query["status"] = status;
1676
+ }
1658
1677
  winston.debug("Get kb status: " + status)
1659
1678
  }
1660
1679
 
@@ -1664,21 +1683,18 @@ router.get('/', async (req, res) => {
1664
1683
  winston.debug("Get kb type: " + type);
1665
1684
  }
1666
1685
 
1667
- if (req.query.limit) {
1668
- limit = parseInt(req.query.limit);
1669
- winston.debug("Get kb limit: " + limit)
1670
- }
1671
-
1672
- if (req.query.page) {
1673
- page = parseInt(req.query.page);
1674
- winston.debug("Get kb page: " + page)
1675
- }
1676
-
1677
- let skip = page * limit;
1686
+ const kbPaging = numericInput.takePagination(res, req.query, { defaultLimit: limit, maxLimit: 1000, overMax: 'reject' });
1687
+ if (!kbPaging) return;
1688
+ limit = kbPaging.limit;
1689
+ page = kbPaging.page;
1690
+ let skip = kbPaging.skip;
1691
+ winston.debug("Get kb limit: " + limit)
1692
+ winston.debug("Get kb page: " + page)
1678
1693
  winston.debug("Get kb skip page: " + skip);
1679
1694
 
1680
1695
  if (req.query.direction) {
1681
- direction = parseInt(req.query.direction)
1696
+ direction = numericInput.takeSortDirection(res, req.query.direction);
1697
+ if (direction === null) return;
1682
1698
  winston.debug("Get kb direction: " + direction)
1683
1699
  }
1684
1700
 
@@ -1773,6 +1789,9 @@ router.post('/', freezePineconeWrites, async (req, res) => {
1773
1789
  const project = req.project
1774
1790
 
1775
1791
  const { name, type, source, content, refresh_rate, scrape_type, scrape_options, tags, situated_context, chunk_regex } = req.body;
1792
+ if (scrape_type !== undefined && scrape_type !== null && scrape_type !== '' && numericInput.parseAllowedInteger(scrape_type, numericInput.SCRAPE_TYPES) === null) {
1793
+ return numericInput.sendInvalidNumber(res, 'scrape_type must be an integer and one of: ' + numericInput.SCRAPE_TYPES.join(', '));
1794
+ }
1776
1795
  const namespace_id = req.body?.namespace;
1777
1796
 
1778
1797
  if (!namespace_id) {
@@ -1907,6 +1926,9 @@ router.post('/multi', freezePineconeWrites, upload.single('uploadFile'), async (
1907
1926
  const id_project = req.projectid;
1908
1927
  const project = req.project;
1909
1928
  let { refresh_rate = 'never', scrape_type = 2, scrape_options, situated_context } = req.body;
1929
+ if (numericInput.parseAllowedInteger(scrape_type, numericInput.SCRAPE_TYPES) === null) {
1930
+ return numericInput.sendInvalidNumber(res, 'scrape_type must be an integer and one of: ' + numericInput.SCRAPE_TYPES.join(', '));
1931
+ }
1910
1932
  let tags = parseStringArrayField(req.body.tags);
1911
1933
 
1912
1934
 
@@ -2121,6 +2143,9 @@ router.post('/sitemap/import', freezePineconeWrites, async (req, res) => {
2121
2143
  const namespace_id = req.query.namespace;
2122
2144
 
2123
2145
  let { type, source, refresh_rate = 'never', scrape_type = 2, scrape_options, tags, situated_context } = req.body;
2146
+ if (numericInput.parseAllowedInteger(scrape_type, numericInput.SCRAPE_TYPES) === null) {
2147
+ return numericInput.sendInvalidNumber(res, 'scrape_type must be an integer and one of: ' + numericInput.SCRAPE_TYPES.join(', '));
2148
+ }
2124
2149
  if (scrape_type === 2 && !scrape_options) {
2125
2150
  scrape_options = aiManager.setDefaultScrapeOptions();
2126
2151
  }
package/routes/lead.js CHANGED
@@ -9,6 +9,7 @@ csv.separator = ';';
9
9
  const leadEvent = require('../event/leadEvent');
10
10
  var Segment = require("../models/segment");
11
11
  var Segment2MongoConverter = require("../utils/segment2mongoConverter");
12
+ var numericInput = require('../utils/numericInput');
12
13
 
13
14
 
14
15
  router.post('/', function (req, res) {
@@ -37,7 +38,11 @@ router.put('/:leadid', function (req, res) {
37
38
  update.attributes = req.body.attributes;
38
39
  }
39
40
  if (req.body.status!=undefined) {
40
- update.status = req.body.status;
41
+ var leadStatus = numericInput.takeOptionalAllowedInteger(res, req.body.status, numericInput.LEAD_STATUSES, 'status');
42
+ if (leadStatus === null) return;
43
+ if (leadStatus !== undefined) {
44
+ update.status = leadStatus;
45
+ }
41
46
  }
42
47
 
43
48
 
@@ -390,10 +395,9 @@ router.delete('/:leadid/physical', function (req, res) {
390
395
  // DOWNLOAD leads AS CSV
391
396
  router.get('/csv', function (req, res, next) {
392
397
  var limit = 100000; // Number of leads per page
393
- var page = 0;
394
- if (req.query.page) {
395
- page = req.query.page;
396
- }
398
+ var page = numericInput.takeOptionalBoundedInteger(res, req.query.page, 0, 1000000, 'page');
399
+ if (page === null) return;
400
+ if (page === undefined) page = 0;
397
401
  var skip = page * limit;
398
402
  winston.debug('LEAD ROUTE - SKIP PAGE ', skip);
399
403
 
@@ -409,10 +413,8 @@ router.get('/csv', function (req, res, next) {
409
413
  query.email = req.query.email;
410
414
  }
411
415
 
412
- var direction = -1; //-1 descending , 1 ascending
413
- if (req.query.direction) {
414
- direction = req.query.direction;
415
- }
416
+ var direction = numericInput.takeSortDirection(res, req.query.direction);
417
+ if (direction === null) return;
416
418
  winston.debug("direction", direction);
417
419
 
418
420
  var sortField = "createdAt";
@@ -463,20 +465,11 @@ router.get('/:leadid', function (req, res) {
463
465
 
464
466
  router.get('/', async(req, res) => {
465
467
 
466
- var limit = 40; // Number of request per page
467
-
468
- if (req.query.limit) {
469
- limit = parseInt(req.query.limit);
470
- winston.debug('LEAD ROUTE - limit: '+limit);
471
- }
472
-
473
- var page = 0;
474
-
475
- if (req.query.page) {
476
- page = req.query.page;
477
- }
478
-
479
- var skip = page * limit;
468
+ const paging = numericInput.takePagination(res, req.query, { defaultLimit: 40, maxLimit: 1000, overMax: 'reject' });
469
+ if (!paging) return;
470
+ var limit = paging.limit;
471
+ var skip = paging.skip;
472
+ winston.debug('LEAD ROUTE - limit: '+limit);
480
473
  winston.debug('LEAD ROUTE - SKIP PAGE ', skip);
481
474
 
482
475
 
@@ -525,16 +518,18 @@ router.get('/', async(req, res) => {
525
518
  query["status"] = LeadConstants.NORMAL;
526
519
 
527
520
  if (req.query.status) {
528
- query.status = req.query.status;
521
+ var leadStatus = numericInput.takeOptionalAllowedInteger(res, req.query.status, numericInput.LEAD_STATUSES, 'status');
522
+ if (leadStatus === null) return;
523
+ if (leadStatus !== undefined) {
524
+ query.status = leadStatus;
525
+ }
529
526
  }
530
527
 
531
528
  winston.debug("query", query);
532
529
 
533
530
 
534
- var direction = -1; //-1 descending , 1 ascending
535
- if (req.query.direction) {
536
- direction = req.query.direction;
537
- }
531
+ var direction = numericInput.takeSortDirection(res, req.query.direction);
532
+ if (direction === null) return;
538
533
 
539
534
  var sortField = "createdAt";
540
535
  if (req.query.sort) {
package/routes/logs.js CHANGED
@@ -8,6 +8,7 @@ const { v4: uuidv4 } = require('uuid');
8
8
  const jwt = require("jsonwebtoken")
9
9
 
10
10
  const jwtSecret = process.env.CHAT21_JWT_SECRET || "tokenKey";
11
+ var numericInput = require('../utils/numericInput');
11
12
 
12
13
  router.get('/', function (req, res, next) {
13
14
  winston.info("logs", req.body);
@@ -87,13 +88,19 @@ router.post('/whatsapp', async (req, res) => {
87
88
 
88
89
  winston.info("save following log: ", req.body);
89
90
 
91
+ var statusCode = numericInput.takeOptionalBoundedInteger(res, req.body.status_code, 0, 999, 'status_code');
92
+ if (statusCode === null) return;
93
+ if (statusCode === undefined) {
94
+ return numericInput.sendInvalidNumber(res, 'status_code must be an integer between 0 and 999');
95
+ }
96
+
90
97
  let log = new MessageLog({
91
98
  id_project: req.body.id_project,
92
99
  json_message: req.body.json_message,
93
100
  transaction_id: req.body.transaction_id,
94
101
  message_id: req.body.message_id,
95
102
  status: req.body.status,
96
- status_code: req.body.status_code,
103
+ status_code: statusCode,
97
104
  error: req.body.error
98
105
  });
99
106
 
package/routes/mcp.js CHANGED
@@ -2,6 +2,9 @@ let express = require('express');
2
2
  let router = express.Router();
3
3
  let winston = require('../config/winston');
4
4
  const mcpService = require('../services/mcpService');
5
+ const mcpOAuth = require('./mcpOAuth');
6
+
7
+ router.use('/oauth', mcpOAuth);
5
8
 
6
9
 
7
10
  /**
@@ -39,7 +42,7 @@ router.post('/native/:nativeId/connect', async (req, res) => {
39
42
  });
40
43
  } catch (error) {
41
44
  winston.error('Error connecting to native MCP server:', error);
42
- res.status(error.statusCode || 500).send({
45
+ res.status(error.statusCode || error.status || 500).send({
43
46
  success: false,
44
47
  error: error.message || 'Error connecting to native MCP server'
45
48
  });
@@ -61,7 +64,7 @@ router.post('/native/:nativeId/tools', async (req, res) => {
61
64
  res.status(200).send(tools);
62
65
  } catch (error) {
63
66
  winston.error('Error getting tools from native MCP server:', error);
64
- res.status(error.statusCode || 500).send({
67
+ res.status(error.statusCode || error.status || 500).send({
65
68
  success: false,
66
69
  error: error.message || 'Error getting tools from native MCP server'
67
70
  });
@@ -72,7 +75,7 @@ router.post('/native/:nativeId/tools', async (req, res) => {
72
75
  * POST /mcp/servers
73
76
  * Adds or updates an MCP server in the project mcp integration
74
77
  * Body (native): { type: 'native', nativeId, tools?, selectedTools? }
75
- * Body (custom): { type: 'custom', name, url, transport?, customHeaders?, tools?, selectedTools?, auth? }
78
+ * Body (custom): { type: 'custom', name, url, transport?, customHeaders?, tools?, selectedTools?, auth?, oauth?: { connection_id } }
76
79
  */
77
80
  router.post('/servers', async (req, res) => {
78
81
  try {
@@ -86,7 +89,7 @@ router.post('/servers', async (req, res) => {
86
89
  });
87
90
  } catch (error) {
88
91
  winston.error('Error saving MCP server in integration:', error);
89
- res.status(error.statusCode || 500).send({
92
+ res.status(error.statusCode || error.status || 500).send({
90
93
  success: false,
91
94
  error: error.message || 'Error saving MCP server in integration'
92
95
  });
@@ -99,7 +102,7 @@ router.post('/servers', async (req, res) => {
99
102
  * Body: {
100
103
  * url: string,
101
104
  * customHeaders?: [{ key: string, value: string }],
102
- * oauth?: object (not supported yet),
105
+ * oauth?: { connection_id: string } (MCP OAuth connection of the project, see /mcp/oauth),
103
106
  * auth?: object (legacy, optional)
104
107
  * }
105
108
  */
@@ -124,7 +127,7 @@ router.post('/connect', async (req, res) => {
124
127
  });
125
128
  } catch (error) {
126
129
  winston.error(`Error connecting to MCP server:`, error);
127
- res.status(500).send({ success: false, error: error.message || 'Error connecting to MCP server' });
130
+ res.status(error.statusCode || error.status || 500).send({ success: false, error: error.message || 'Error connecting to MCP server' });
128
131
  }
129
132
  });
130
133
 
@@ -134,7 +137,7 @@ router.post('/connect', async (req, res) => {
134
137
  * Body: {
135
138
  * url: string,
136
139
  * customHeaders?: [{ key: string, value: string }],
137
- * oauth?: object (not supported yet),
140
+ * oauth?: { connection_id: string } (MCP OAuth connection of the project, see /mcp/oauth),
138
141
  * auth?: object (legacy, optional)
139
142
  * }
140
143
  */
@@ -157,7 +160,7 @@ router.post('/tools', async (req, res) => {
157
160
  res.status(200).send(tools);
158
161
  } catch (error) {
159
162
  winston.error(`Error getting tools from MCP server:`, error);
160
- res.status(500).send({ success: false, error: error.message || 'Error getting tools from MCP server' });
163
+ res.status(error.statusCode || error.status || 500).send({ success: false, error: error.message || 'Error getting tools from MCP server' });
161
164
  }
162
165
  });
163
166