Skip to content

Commit e24ea3a

Browse files
committed
feat: don't allow getting user details fields for invites
1 parent 9291dc0 commit e24ea3a

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

src/routes/projectMemberInvites/create.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { PROJECT_MEMBER_ROLE, PROJECT_MEMBER_MANAGER_ROLES,
1212
MAX_PARALLEL_REQUEST_QTY, CONNECT_NOTIFICATION_EVENT } from '../../constants';
1313
import { createEvent } from '../../services/busApi';
1414

15+
const ALLOWED_FIELDS = _.keys(models.ProjectMemberInvite.rawAttributes).concat(['handle']);
16+
1517
/**
1618
* API to create member invite to project.
1719
*
@@ -24,6 +26,9 @@ const addMemberValidations = {
2426
emails: Joi.array().items(Joi.string().email()).optional().min(1),
2527
role: Joi.any().valid(_.values(PROJECT_MEMBER_ROLE)).required(),
2628
}).required(),
29+
query: {
30+
fields: Joi.string().optional(),
31+
},
2732
};
2833

2934
/**
@@ -234,6 +239,14 @@ module.exports = [
234239
// let us request user fields during creating, probably this should be move to GET by ID endpoint instead
235240
const fields = req.query.fields ? req.query.fields.split(',') : null;
236241

242+
try {
243+
util.validateFields(fields, ALLOWED_FIELDS);
244+
} catch (validationError) {
245+
const err = new Error(`"fields" is not valid: ${validationError.message}`);
246+
err.status = 400;
247+
return next(err);
248+
}
249+
237250
if (!invite.userIds && !invite.emails) {
238251
const err = new Error('Either userIds or emails are required');
239252
err.status = 400;

src/routes/projectMemberInvites/get.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { middleware as tcMiddleware } from 'tc-core-library-js';
77
import models from '../../models';
88
import util from '../../util';
99

10+
const ALLOWED_FIELDS = _.keys(models.ProjectMemberInvite.rawAttributes).concat(['handle']);
11+
1012
/**
1113
* API to update invite member to project.
1214
*
@@ -28,7 +30,15 @@ module.exports = [
2830
const email = req.authUser.email;
2931
const fields = req.query.fields ? req.query.fields.split(',') : null;
3032

31-
util.fetchByIdFromES('invites', {
33+
try {
34+
util.validateFields(fields, ALLOWED_FIELDS);
35+
} catch (validationError) {
36+
const err = new Error(`"fields" is not valid: ${validationError.message}`);
37+
err.status = 400;
38+
return next(err);
39+
}
40+
41+
return util.fetchByIdFromES('invites', {
3242
query: {
3343
nested: {
3444
path: 'invites',

src/routes/projectMemberInvites/list.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { middleware as tcMiddleware } from 'tc-core-library-js';
77
import models from '../../models';
88
import util from '../../util';
99

10+
const ALLOWED_FIELDS = _.keys(models.ProjectMemberInvite.rawAttributes).concat(['handle']);
11+
1012
/**
1113
* API to update invite member to project.
1214
*
@@ -26,7 +28,15 @@ module.exports = [
2628
const projectId = _.parseInt(req.params.projectId);
2729
const fields = req.query.fields ? req.query.fields.split(',') : null;
2830

29-
util.fetchByIdFromES('invites', {
31+
try {
32+
util.validateFields(fields, ALLOWED_FIELDS);
33+
} catch (validationError) {
34+
const err = new Error(`"fields" is not valid: ${validationError.message}`);
35+
err.status = 400;
36+
return next(err);
37+
}
38+
39+
return util.fetchByIdFromES('invites', {
3040
query: {
3141
nested: {
3242
path: 'invites',

src/util.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,29 @@ _.assignIn(util, {
12001200

12011201
return markupKey ? _.includes(_.values(ESTIMATION_TYPE), markupKey) : false;
12021202
},
1203+
1204+
/**
1205+
* Validate if `fields` list has only allowed values from `allowedFields` or throws error.
1206+
*
1207+
* @param {Array} fields fields to validate
1208+
* @param {Array} allowedFields allowed fields
1209+
*
1210+
* @throws {Error}
1211+
* @returns {void}
1212+
*/
1213+
validateFields: (fields, allowedFields) => {
1214+
if (!allowedFields) {
1215+
throw new Error('List of "allowedFields" has to be provided.');
1216+
}
1217+
1218+
const disallowedFields = _.difference(fields, allowedFields);
1219+
1220+
if (disallowedFields.length > 0) {
1221+
const disallowedFieldsString = disallowedFields.map(field => `"${field}"`).join(', ');
1222+
1223+
throw new Error(`values ${disallowedFieldsString} are not allowed`);
1224+
}
1225+
},
12031226
});
12041227

12051228
export default util;

0 commit comments

Comments
 (0)