@tiledesk/tiledesk-server 3.5.0 → 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 CHANGED
@@ -5,6 +5,9 @@
5
5
  🚀 IN PRODUCTION 🚀
6
6
  (https://www.npmjs.com/package/@tiledesk/tiledesk-server/v/2.3.77)
7
7
 
8
+ # 3.5.1
9
+ - Fixed insufficient input validation vulnerabilities
10
+
8
11
  # 3.5.0
9
12
  - MCP OAuth2: MCP servers protected by OAuth2 can be connected with the authorization code flow and PKCE. Three modes: `mcp` discovers the authorization server from the MCP server (401 `WWW-Authenticate`, protected resource metadata, authorization server metadata) and registers the client dynamically; `google` uses the Tiledesk Google OAuth client; `manual` takes the endpoints and client from the request. Tokens and client secrets are stored encrypted (AES-256-GCM) in the new `mcp_oauth_connections` collection, one connection per project and server URL
10
13
  - New `POST /:projectid/mcp/oauth/authorize` (admin) `{ mode, server_url, name?, scopes?, return_url?, manual? }` answers `{ authorization_url, connection_id }`. The provider redirects to the new public `GET /oauth2/mcp/callback`, which completes the connection and redirects to `return_url` with `mcp_oauth=success|error&connection_id=`, or shows a page when no `return_url` was given. The `state` is single use and lasts 10 minutes; `return_url` must belong to `MCP_OAUTH_RETURN_ORIGINS`
package/deploynew.sh CHANGED
@@ -4,10 +4,16 @@ set -euo pipefail
4
4
  # Load only what this script needs from .env (full-file export breaks on
5
5
  # values with spaces/special chars, and with set -e it aborts immediately)
6
6
  if [ -z "${NPM_PUBLISH_TOKEN:-}" ] && [ -f .env ]; then
7
- NPM_PUBLISH_TOKEN=$(grep -E '^[[:space:]]*NPM_PUBLISH_TOKEN=' .env | tail -n1 | cut -d= -f2- | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' -e 's/^"//' -e 's/"$//' -e "s/^'//" -e "s/'$//")
7
+ # grep exits 1 when the key is missing. With set -o pipefail that status
8
+ # becomes the assignment's status, and set -e aborts before the login prompt.
9
+ NPM_PUBLISH_TOKEN=$(grep -E '^[[:space:]]*NPM_PUBLISH_TOKEN=' .env | tail -n1 | cut -d= -f2- | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' -e 's/^"//' -e 's/"$//' -e "s/^'//" -e "s/'$//" || true)
8
10
  export NPM_PUBLISH_TOKEN
9
11
  fi
10
12
 
13
+ # Get curent branch name
14
+ current_branch=$(git rev-parse --abbrev-ref HEAD)
15
+ GIT_REMOTE=$(git config --get branch.$current_branch.remote)
16
+
11
17
  REMOTE="${GIT_REMOTE:-tiledesk-server}"
12
18
 
13
19
  # Check if the token is set
@@ -0,0 +1,91 @@
1
+ # Endpoints protected by numeric validation
2
+
3
+ Starting vulnerability: **Insufficient Input Validation**. A client could send a negative number, or any value outside the field's domain, and the API accepted it.
4
+
5
+ An invalid value gets **422** with `{ "success": false, "msg": "..." }` and is neither saved nor used in queries. Negative values that belong to the domain are still accepted and are listed in the Rule column.
6
+
7
+ ## Rating (reported endpoint)
8
+
9
+ | Method | Endpoint | Field | Rule |
10
+ | --- | --- | --- | --- |
11
+ | PATCH | `/:projectid/requests/:requestid/rating` | `rating` | integer from 1 to 5 |
12
+ | PATCH | `/:projectid/requests/:requestid/rating` | `rating_message` | string, if present |
13
+ | PATCH | `/:projectid/requests/:requestid` | `rating` | integer from 1 to 5 |
14
+
15
+ The `Request.rating` model rejects the same values on write too, through Mongoose validators (`min: 1`, `max: 5`, integer).
16
+
17
+ ## Requests and messages
18
+
19
+ | Method | Endpoint | Field | Rule |
20
+ | --- | --- | --- | --- |
21
+ | POST | `/:projectid/requests` | message `status` | one of `-100, 0, 100, 150, 200, 250, 300`. `-100` is a failed send |
22
+ | PATCH | `/:projectid/requests/:requestid` | `status` | one of `50, 100, 150, 200, 300, 1000` |
23
+ | GET | `/:projectid/requests` | `limit`, `page` | `limit` integer ≥ 1; above 100 the default 40 is used. `page` integer ≥ 0 |
24
+ | GET | `/:projectid/requests` | `status` | `all` or a comma-separated list of request codes |
25
+ | GET | `/:projectid/requests` | `direction` | `-1` or `1` |
26
+ | GET | `/:projectid/requests/csv` | `page`, `status`, `direction` | same rules as the list |
27
+ | GET | `/:projectid/requests/me` | `limit`, `page`, `status`, `direction` | `limit` as above; `status` a single request code or `all`; `direction` `-1` or `1` |
28
+ | POST | `/:projectid/requests/:request_id/messages` | `status` | one of `-100, 0, 100, 150, 200, 250, 300` |
29
+ | GET | `/:projectid/messages` | `limit`, `page` | `limit` integer ≥ 1; above 50 the default 10 is used. `page` integer ≥ 0 |
30
+ | POST | `/:projectid/campaigns` | `status` | one of `-100, 0, 100, 150, 200, 250, 300` |
31
+ | POST | `/:projectid/campaigns/direct` | `status` | one of `-100, 0, 100, 150, 200, 250, 300` |
32
+ | POST | `/:projectid/campaigns/directDEPRECATED?` | `status` | one of `-100, 0, 100, 150, 200, 250, 300` |
33
+
34
+ ## Project, departments, leads, properties
35
+
36
+ | Method | Endpoint | Field | Rule |
37
+ | --- | --- | --- | --- |
38
+ | PUT | `/projects/:projectid` | `versions` | integer from 0 to 99999999 |
39
+ | PUT | `/projects/:projectid` | `settings.max_agent_assigned_chat` | `-1` (no limit) or integer from 0 to 100000. Applies both to the dotted field and inside the `settings` object |
40
+ | PUT | `/projects/:projectid` | `settings.reassignment_delay` | integer from 0 to 315360000 |
41
+ | PUT | `/projects/:projectid` | `settings.email.config.port` | integer from 1 to 65535 |
42
+ | PATCH | `/projects/:projectid` | the same four fields | the same rules as PUT |
43
+ | PUT | `/:projectid/project_users` | `max_assigned_chat` | `-1` or integer from 0 to 100000 |
44
+ | PUT | `/:projectid/project_users` | `number_assigned_requests` | integer from 0 to 1000000 |
45
+ | PUT | `/:projectid/project_users/:project_userid` | `max_assigned_chat`, `number_assigned_requests` | the same rules |
46
+ | POST | `/:projectid/departments` | `status` | one of `-1, 0, 1`. `-1` is a hidden department |
47
+ | PUT | `/:projectid/departments/:departmentid` | `status` | one of `-1, 0, 1` |
48
+ | PATCH | `/:projectid/departments/:departmentid` | `status` | one of `-1, 0, 1` |
49
+ | PUT | `/:projectid/leads/:leadid` | `status` | one of `50, 100, 1000` |
50
+ | GET | `/:projectid/leads` | `limit`, `page`, `status`, `direction` | `limit` from 1 to 1000; `page` ≥ 0; `status` a lead code; `direction` `-1` or `1` |
51
+ | GET | `/:projectid/leads/csv` | `page`, `direction` | `page` ≥ 0; `direction` `-1` or `1` |
52
+ | PUT | `/:projectid/properties/:propertyid` | `status` | one of `100, 1000` |
53
+ | GET | `/:projectid/properties` | `limit`, `page`, `status`, `direction` | `limit` from 1 to 1000; `page` ≥ 0; `status` `100` or `1000`; `direction` `-1` or `1` |
54
+
55
+ ## Knowledge base, FAQ, files, logs
56
+
57
+ | Method | Endpoint | Field | Rule |
58
+ | --- | --- | --- | --- |
59
+ | GET | `/:projectid/kb` | `limit`, `page`, `status`, `direction` | `limit` from 1 to 1000; `page` ≥ 0; `status` integer from `-1` to 10000; `direction` `-1` or `1` |
60
+ | GET | `/:projectid/kb/namespace/export/:id` | `status` | integer from `-1` to 10000 |
61
+ | POST | `/:projectid/kb` | `scrape_type` | one of `0, 1, 2, 4` |
62
+ | POST | `/:projectid/kb/multi` | `scrape_type` | one of `0, 1, 2, 4` |
63
+ | POST | `/:projectid/kb/sitemap/import` | `scrape_type` | one of `0, 1, 2, 4` |
64
+ | POST | `/:projectid/kb/namespace/import/:id` | `contents[].scrape_type` | one of `0, 1, 2, 4` |
65
+ | GET | `/:projectid/kb/unanswered/:namespace` | `limit`, `page`, `direction` | `limit` from 1 to 100; `page` ≥ 0; `direction` `-1` or `1` |
66
+ | GET | `/:projectid/kb/answered/:namespace` | `limit`, `page`, `direction` | `limit` from 1 to 100; `page` ≥ 0; `direction` `-1` or `1` |
67
+ | GET | `/:projectid/faq` | `limit`, `page` | `limit` from 1 to 10000; `page` ≥ 0 |
68
+ | GET | `/:projectid/intents` | `limit`, `page` | same route as `/faq` |
69
+ | POST | `/:projectid/files/assets` | `expiration` | seconds, integer from 1 to 315360000. If missing, the file does not expire |
70
+ | POST | `/logs/whatsapp` | `status_code` | integer from 0 to 999, required |
71
+ | POST | `/:projectid/logs/whatsapp` | `status_code` | integer from 0 to 999, required |
72
+
73
+ ## Lists, sorting and activities
74
+
75
+ | Method | Endpoint | Field | Rule |
76
+ | --- | --- | --- | --- |
77
+ | GET | `/:projectid/roles` | `limit`, `page`, `direction` | `limit` from 1 to 1000; `page` ≥ 0; `direction` `-1` or `1` |
78
+ | GET | `/:projectid/segments` | `limit`, `page`, `direction` | `limit` from 1 to 1000; `page` ≥ 0; `direction` `-1` or `1` |
79
+ | GET | `/:projectid/tags` | `page`, `direction` | `page` ≥ 0; `direction` `-1` or `1` |
80
+ | GET | `/:projectid/subscriptions/history` | `page` | integer ≥ 0 |
81
+ | GET | `/:projectid/canned` | `page`, `direction` | `page` ≥ 0; `direction` `-1` or `1` |
82
+ | GET | `/:projectid/events` | `page`, `direction` | `page` ≥ 0; `direction` `-1` or `1` |
83
+ | GET | `/:projectid/activities` | `page`, `direction` | `page` ≥ 0; `direction` `-1` or `1`. The `limit` of this list was already capped |
84
+ | GET | `/:projectid/activities/csv` | `page`, `direction` | `page` ≥ 0; `direction` `-1` or `1` |
85
+
86
+ ## Already safe, not changed
87
+
88
+ These lists already discarded negative `page` and `limit` values, bringing them back into range:
89
+
90
+ - `GET /:projectid/agent-prompts`
91
+ - `GET /:projectid/modules/ai-generations/agents/:id_faq_kb/revisions`
package/models/request.js CHANGED
@@ -215,6 +215,14 @@ var RequestSchema = new Schema({
215
215
  rating: {
216
216
  type: Number,
217
217
  required: false,
218
+ min: 1,
219
+ max: 5,
220
+ validate: {
221
+ validator: function (v) {
222
+ return v == null || Number.isInteger(v);
223
+ },
224
+ message: 'rating must be an integer between 1 and 5'
225
+ }
218
226
  },
219
227
  rating_message: {
220
228
  type: String,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-server",
3
3
  "description": "The Tiledesk server module",
4
- "version": "3.5.0",
4
+ "version": "3.5.1",
5
5
  "scripts": {
6
6
  "start": "node ./bin/www",
7
7
  "quota-restore": "node ./bin/quota-restore",
@@ -5,6 +5,7 @@ const activityMessageUtil = require('../activityMessageUtil');
5
5
  const winston = require('../../../config/winston');
6
6
  const moment = require('moment');
7
7
  const ObjectId = require('mongoose').Types.ObjectId;
8
+ const numericInput = require('../../../utils/numericInput');
8
9
 
9
10
  csv = require('csv-express');
10
11
  csv.separator = ';';
@@ -71,7 +72,9 @@ router.get('/', async (req, res) => {
71
72
  let id_project = req.projectid;
72
73
  const limitConfig = resolveActivityQueryLimit(req.query);
73
74
  let limit = limitConfig.limit;
74
- let page = parseInt(req.query.page) || 0;
75
+ let page = numericInput.takeOptionalBoundedInteger(res, req.query.page, 0, 1000000, 'page');
76
+ if (page === null) return;
77
+ if (page === undefined) page = 0;
75
78
  let skip = page * limit;
76
79
 
77
80
 
@@ -167,11 +170,9 @@ router.get('/', async (req, res) => {
167
170
  // DOWNLOAD ACTIVITIES AS CSV
168
171
  router.get('/csv', function (req, res) {
169
172
  var limit = 100000; // Number of activities per page
170
- var page = 0;
171
-
172
- if (req.query.page) {
173
- page = req.query.page;
174
- }
173
+ var page = numericInput.takeOptionalBoundedInteger(res, req.query.page, 0, 1000000, 'page');
174
+ if (page === null) return;
175
+ if (page === undefined) page = 0;
175
176
 
176
177
  var skip = page * limit;
177
178
  winston.debug('Activity ROUTE - SKIP PAGE ', skip);
@@ -251,10 +252,8 @@ router.get('/csv', function (req, res) {
251
252
  }
252
253
 
253
254
 
254
- var direction = -1; //-1 descending , 1 ascending
255
- if (req.query.direction) {
256
- direction = req.query.direction;
257
- }
255
+ var direction = numericInput.takeSortDirection(res, req.query.direction);
256
+ if (direction === null) return;
258
257
  winston.debug("direction", direction);
259
258
 
260
259
  var sortField = "createdAt";
@@ -4,6 +4,7 @@ var CannedResponse = require("./cannedResponse");
4
4
  var winston = require('../../config/winston');
5
5
  const RoleConstants = require('../../models/roleConstants');
6
6
  const roleConstants = require('../../models/roleConstants');
7
+ var numericInput = require('../../utils/numericInput');
7
8
  // const CannedResponseEvent = require('../event/CannedResponseEvent');
8
9
 
9
10
 
@@ -244,11 +245,9 @@ router.get('/:cannedResponseid', function (req, res) {
244
245
 
245
246
  router.get('/', function (req, res) {
246
247
  var limit = 1000; // Number of CannedResponses per page
247
- var page = 0;
248
-
249
- if (req.query.page) {
250
- page = req.query.page;
251
- }
248
+ var page = numericInput.takeOptionalBoundedInteger(res, req.query.page, 0, 1000000, 'page');
249
+ if (page === null) return;
250
+ if (page === undefined) page = 0;
252
251
 
253
252
  var skip = page * limit;
254
253
  winston.debug('CannedResponse ROUTE - SKIP PAGE ', skip);
@@ -261,10 +260,8 @@ router.get('/', function (req, res) {
261
260
  query.$text = { "$search": req.query.full_text };
262
261
  }
263
262
 
264
- var direction = -1; //-1 descending , 1 ascending
265
- if (req.query.direction) {
266
- direction = req.query.direction;
267
- }
263
+ var direction = numericInput.takeSortDirection(res, req.query.direction);
264
+ if (direction === null) return;
268
265
 
269
266
  var sortField = "createdAt";
270
267
  if (req.query.sort) {
@@ -8,6 +8,7 @@ const { check, validationResult } = require('express-validator');
8
8
  var passport = require('passport');
9
9
  require('../../middleware/passport')(passport);
10
10
  var roleChecker = require('../../middleware/has-role');
11
+ var numericInput = require('../../utils/numericInput');
11
12
 
12
13
  const messageEvent = require('../../event/messageEvent');
13
14
 
@@ -78,11 +79,9 @@ router.get('/', [passport.authenticate(['basic', 'jwt'],
78
79
  roleChecker.hasRole('agent')],
79
80
  function (req, res) {
80
81
  var limit = 40; // Number of leads per page
81
- var page = 0;
82
-
83
- if (req.query.page) {
84
- page = req.query.page;
85
- }
82
+ var page = numericInput.takeOptionalBoundedInteger(res, req.query.page, 0, 1000000, 'page');
83
+ if (page === null) return;
84
+ if (page === undefined) page = 0;
86
85
 
87
86
  var skip = page * limit;
88
87
  winston.debug('Event ROUTE - SKIP PAGE ', skip);
@@ -95,10 +94,8 @@ router.get('/', [passport.authenticate(['basic', 'jwt'],
95
94
  query.project_user = project_user;
96
95
  }
97
96
 
98
- var direction = -1; //-1 descending , 1 ascending
99
- if (req.query.direction) {
100
- direction = req.query.direction;
101
- }
97
+ var direction = numericInput.takeSortDirection(res, req.query.direction);
98
+ if (direction === null) return;
102
99
  //console.log("direction", direction);
103
100
 
104
101
  var sortField = "createdAt";
@@ -4,6 +4,7 @@ const { Namespace, AnsweredQuestion } = require('../models/kb_setting');
4
4
  const 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 unanswerd question
9
10
  router.post('/', async (req, res) => {
@@ -51,10 +52,13 @@ router.get('/:namespace', async (req, res) => {
51
52
  })
52
53
  }
53
54
 
54
- const page = parseInt(req.query.page) || 0;
55
- const limit = parseInt(req.query.limit) || 20;
55
+ const paging = numericInput.takePagination(res, req.query, { defaultLimit: 20, maxLimit: 100, overMax: 'reject' });
56
+ if (!paging) return;
57
+ const page = paging.page;
58
+ const limit = paging.limit;
56
59
  const sortField = req.query.sortField || 'createdAt';
57
- const direction = parseInt(req.query.direction) || -1;
60
+ const direction = numericInput.takeSortDirection(res, req.query.direction);
61
+ if (direction === null) return;
58
62
 
59
63
  const filter = { id_project, namespace };
60
64
 
@@ -11,6 +11,7 @@ var MessageConstants = require("../models/messageConstants");
11
11
  var UIDGenerator = require("../utils/UIDGenerator");
12
12
  var LeadConstants = require("../models/leadConstants");
13
13
  var Segment2MongoConverter = require("../utils/segment2mongoConverter");
14
+ var numericInput = require('../utils/numericInput');
14
15
 
15
16
  var JobManager = require("jobs-worker-queued");
16
17
 
@@ -28,7 +29,8 @@ var jobManager = new JobManager(JOB_RABBITURI,
28
29
  // this endpoint supports support-group- or groups. this create a conversation for the sender (agent console)
29
30
  router.post('/', function (req, res) {
30
31
 
31
- let messageStatus = req.body.status || MessageConstants.CHAT_MESSAGE_STATUS.SENDING;
32
+ let messageStatus = numericInput.takeMessageStatus(res, req.body.status);
33
+ if (messageStatus === null) return;
32
34
 
33
35
  winston.debug(req.body);
34
36
  winston.debug("req.user", req.user);
@@ -68,7 +70,8 @@ Specifica nel campo text il messaggio.
68
70
 
69
71
  router.post('/directDEPRECATED?', async function (req, res) {
70
72
 
71
- let messageStatus = req.body.status || MessageConstants.CHAT_MESSAGE_STATUS.SENDING;
73
+ let messageStatus = numericInput.takeMessageStatus(res, req.body.status);
74
+ if (messageStatus === null) return;
72
75
 
73
76
  winston.debug(req.body);
74
77
  winston.debug("req.user", req.user);
@@ -292,7 +295,8 @@ router.post('/direct', async function (req, res) {
292
295
  var segment_id = req.body.segment_id;
293
296
  winston.info("segment_id"+ segment_id);
294
297
 
295
- let messageStatus = req.body.status || MessageConstants.CHAT_MESSAGE_STATUS.SENDING;
298
+ let messageStatus = numericInput.takeMessageStatus(res, req.body.status);
299
+ if (messageStatus === null) return;
296
300
  // winston.info("messageStatus"+ messageStatus);
297
301
 
298
302
 
@@ -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;
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) {