Skip to content

Validate the handles in constraint #651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/common/challenge-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ class ChallengeHelper {

// check groups authorization
await helper.ensureAccessibleByGroupsAccess(currentUser, challenge);

if (challenge.constraints) {
await this.validateChallengeConstraints(data.constraints);
}
}

async validateChallengeUpdateRequest(currentUser, challenge, data) {
Expand Down Expand Up @@ -196,6 +200,32 @@ class ChallengeHelper {
`Cannot set winners for challenge with non-completed ${challenge.status} status`
);
}

if (data.constraints) {
await this.validateChallengeConstraints(data.constraints);
}
}

async validateChallengeConstraints(constraints) {
if (!_.isEmpty(constraints.allowedRegistrants)) {
await this.validateAllowedRegistrants(constraints.allowedRegistrants);
}
}

async validateAllowedRegistrants(allowedRegistrants) {
const members = await helper.getMembersByHandles(allowedRegistrants);
const incorrectHandles = _.difference(
allowedRegistrants,
_.map(members, (m) => _.lowerCase(m.handle))
);
if (incorrectHandles.length > 0) {
throw new errors.BadRequestError(
`Cannot create challenge with invalid handle in constraints. [${_.join(
incorrectHandles,
","
)}]`
);
}
}

sanitizeRepeatedFieldsInUpdateRequest(data) {
Expand Down
17 changes: 17 additions & 0 deletions src/common/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,22 @@ async function getMemberByHandle(handle) {
return res.data || {};
}

/**
* Get members by handles
* @param {Array<String>} handles the user handle
* @returns {Object}
*/
async function getMembersByHandles(handles) {
const token = await m2mHelper.getM2MToken();
const res = await axios.get(
`${config.MEMBERS_API_URL}/?fields=handle&handlesLower=["${_.join(handles, '","')}"]`,
{
headers: { Authorization: `Bearer ${token}` },
}
);
return res.data;
}

/**
* Send self service notification
* @param {String} type the notification type
Expand Down Expand Up @@ -1199,6 +1215,7 @@ module.exports = {
cancelPayment,
sendSelfServiceNotification,
getMemberByHandle,
getMembersByHandles,
submitZendeskRequest,
updateSelfServiceProjectInfo,
getFromInternalCache,
Expand Down
4 changes: 2 additions & 2 deletions src/services/ChallengeService.js
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,7 @@ createChallenge.schema = {
legacyId: Joi.number().integer().positive(),
constraints: Joi.object()
.keys({
allowedRegistrants: Joi.array().items(Joi.string()).optional(),
allowedRegistrants: Joi.array().items(Joi.string().trim().lowercase()).optional(),
})
.optional(),
startDate: Joi.date().iso(),
Expand Down Expand Up @@ -1998,7 +1998,7 @@ updateChallenge.schema = {
legacyId: Joi.number().integer().positive(),
constraints: Joi.object()
.keys({
allowedRegistrants: Joi.array().items(Joi.string()).optional(),
allowedRegistrants: Joi.array().items(Joi.string().trim().lowercase()).optional(),
})
.optional(),
status: Joi.string().valid(_.values(constants.challengeStatuses)),
Expand Down