mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
Use an explicit array index when renaming a dropdown option so schema validation and protected-field checks can preview the update. Match the option ID at that index to avoid renaming a sibling after a concurrent removal or reorder, and return 404 when the update no longer matches. Add handler and browser regressions for persisted names, stable selections, encoded IDs, missing targets, authorization and protected-field restrictions. Thanks to AhmedLukman and xet7 !
344 lines
11 KiB
JavaScript
344 lines
11 KiB
JavaScript
import { Meteor } from 'meteor/meteor';
|
|
import { WebApp } from 'meteor/webapp';
|
|
import { Random } from 'meteor/random';
|
|
import { Authentication } from '/server/authentication';
|
|
import { sendJsonResult } from '/server/apiMiddleware';
|
|
import { ReactiveCache } from '/imports/reactiveCache';
|
|
import Activities from '/models/activities';
|
|
import Cards from '/models/cards';
|
|
import CustomFields from '/models/customFields';
|
|
import { ensureIndex } from '/server/lib/mongoStartup';
|
|
|
|
async function customFieldCreation(userId, doc) {
|
|
await Activities.insertAsync({
|
|
userId,
|
|
activityType: 'createCustomField',
|
|
boardId: doc.boardIds[0],
|
|
customFieldId: doc._id,
|
|
});
|
|
}
|
|
|
|
async function customFieldDeletion(userId, doc) {
|
|
await Activities.insertAsync({
|
|
userId,
|
|
activityType: 'deleteCustomField',
|
|
boardId: doc.boardIds[0],
|
|
customFieldId: doc._id,
|
|
});
|
|
}
|
|
|
|
async function customFieldEdit(userId, doc) {
|
|
// #5390: deleting a custom field from a board ($pull boardIds) calls this; an
|
|
// activity may not exist (e.g. the field never had a value set), so guard the
|
|
// lookup instead of dereferencing `.value` on undefined, which threw and
|
|
// aborted the whole delete.
|
|
const activity = await ReactiveCache.getActivity({ customFieldId: doc._id });
|
|
const customFieldValue = activity ? activity.value : undefined;
|
|
await Activities.insertAsync({
|
|
userId,
|
|
activityType: 'setCustomField',
|
|
boardId: doc.boardIds[0],
|
|
customFieldId: doc._id,
|
|
customFieldValue,
|
|
listId: doc.listId,
|
|
swimlaneId: doc.swimlaneId,
|
|
});
|
|
}
|
|
|
|
Meteor.startup(async () => {
|
|
await ensureIndex(CustomFields, { modifiedAt: -1 });
|
|
await ensureIndex(CustomFields, { boardIds: 1 });
|
|
});
|
|
|
|
CustomFields.after.insert(async (userId, doc) => {
|
|
await customFieldCreation(userId, doc);
|
|
|
|
if (doc.alwaysOnCard) {
|
|
CustomFields.addToAllCards(doc);
|
|
}
|
|
});
|
|
|
|
CustomFields.before.update(async (userId, doc, fieldNames, modifier) => {
|
|
if (fieldNames.includes('boardIds') && modifier.$pull) {
|
|
await Cards.updateAsync(
|
|
{ boardId: modifier.$pull.boardIds, 'customFields._id': doc._id },
|
|
{ $pull: { customFields: { _id: doc._id } } },
|
|
{ multi: true },
|
|
);
|
|
await customFieldEdit(userId, doc);
|
|
await Activities.removeAsync({
|
|
customFieldId: doc._id,
|
|
boardId: modifier.$pull.boardIds,
|
|
listId: doc.listId,
|
|
swimlaneId: doc.swimlaneId,
|
|
});
|
|
} else if (fieldNames.includes('boardIds') && modifier.$push) {
|
|
await Activities.insertAsync({
|
|
userId,
|
|
activityType: 'createCustomField',
|
|
boardId: modifier.$push.boardIds,
|
|
customFieldId: doc._id,
|
|
});
|
|
}
|
|
});
|
|
|
|
CustomFields.after.update((userId, doc) => {
|
|
if (doc.alwaysOnCard) {
|
|
CustomFields.addToAllCards(doc);
|
|
}
|
|
});
|
|
|
|
CustomFields.before.remove(async (userId, doc) => {
|
|
await customFieldDeletion(userId, doc);
|
|
await Activities.removeAsync({
|
|
customFieldId: doc._id,
|
|
});
|
|
|
|
await Cards.updateAsync(
|
|
{ boardId: { $in: doc.boardIds }, 'customFields._id': doc._id },
|
|
{ $pull: { customFields: { _id: doc._id } } },
|
|
{ multi: true },
|
|
);
|
|
});
|
|
|
|
WebApp.handlers.get('/api/boards/:boardId/custom-fields', async function(req, res) {
|
|
const paramBoardId = req.params.boardId;
|
|
await Authentication.checkBoardAccess(req.userId, paramBoardId);
|
|
sendJsonResult(res, {
|
|
code: 200,
|
|
data: (await ReactiveCache.getCustomFields({ boardIds: { $in: [paramBoardId] } })).map(cf => ({
|
|
_id: cf._id,
|
|
name: cf.name,
|
|
type: cf.type,
|
|
})),
|
|
});
|
|
});
|
|
|
|
WebApp.handlers.get('/api/boards/:boardId/custom-fields/:customFieldId', async function(req, res) {
|
|
const paramBoardId = req.params.boardId;
|
|
const paramCustomFieldId = req.params.customFieldId;
|
|
await Authentication.checkBoardAccess(req.userId, paramBoardId);
|
|
const customField = await ReactiveCache.getCustomField({
|
|
_id: paramCustomFieldId,
|
|
boardIds: { $in: [paramBoardId] },
|
|
});
|
|
// 404, not a 200 with an empty body (#6862).
|
|
if (!customField) {
|
|
sendJsonResult(res, { code: 404, data: { error: 'Custom field not found' } });
|
|
return;
|
|
}
|
|
sendJsonResult(res, { code: 200, data: customField });
|
|
});
|
|
|
|
WebApp.handlers.post('/api/boards/:boardId/custom-fields', async function(req, res) {
|
|
const paramBoardId = req.params.boardId;
|
|
await Authentication.checkBoardWriteAccess(req.userId, paramBoardId);
|
|
const board = await ReactiveCache.getBoard(paramBoardId);
|
|
const id = await CustomFields.direct.insertAsync({
|
|
name: req.body.name,
|
|
type: req.body.type,
|
|
settings: req.body.settings,
|
|
showOnCard: req.body.showOnCard,
|
|
automaticallyOnCard: req.body.automaticallyOnCard,
|
|
showLabelOnMiniCard: req.body.showLabelOnMiniCard,
|
|
showSumAtTopOfList: req.body.showSumAtTopOfList,
|
|
boardIds: [board._id],
|
|
});
|
|
|
|
const customField = await ReactiveCache.getCustomField({
|
|
_id: id,
|
|
boardIds: { $in: [paramBoardId] },
|
|
});
|
|
// GHSA-6jr3-42jf-vhm5: the createCustomField activity records the session
|
|
// identity, not an `authorId` the caller chose for itself. A body that still
|
|
// names somebody else is the attempt, and is recorded for Admin Panel /
|
|
// Problems the same way the card paths record it.
|
|
try {
|
|
const claimed = req.body && req.body.authorId;
|
|
if (claimed && String(claimed) !== String(req.userId)) {
|
|
require('/server/lib/securityLog').record({
|
|
key: 'spoofing.author', action: 'ignored', source: 'POST custom-fields',
|
|
userId: req.userId,
|
|
detail: `ignored authorId "${claimed}" from the body; recorded the session instead`,
|
|
});
|
|
}
|
|
} catch (e) { /* logging must never break the guard */ }
|
|
await customFieldCreation(req.userId, customField);
|
|
|
|
sendJsonResult(res, {
|
|
code: 200,
|
|
data: {
|
|
_id: id,
|
|
},
|
|
});
|
|
});
|
|
|
|
WebApp.handlers.put('/api/boards/:boardId/custom-fields/:customFieldId', async function(req, res) {
|
|
const paramBoardId = req.params.boardId;
|
|
const paramFieldId = req.params.customFieldId;
|
|
await Authentication.checkBoardWriteAccess(req.userId, paramBoardId);
|
|
|
|
const boardScopedField = {
|
|
_id: paramFieldId,
|
|
boardIds: { $in: [paramBoardId] },
|
|
};
|
|
// A custom field that is not on this board is 404, whatever the body holds -
|
|
// not a 200 for an update that matched nothing.
|
|
if (!(await ReactiveCache.getCustomField(boardScopedField))) {
|
|
sendJsonResult(res, { code: 404, data: { error: 'Custom field not found' } });
|
|
return;
|
|
}
|
|
|
|
// Validate and save the requested fields together so a rejected value cannot
|
|
// leave earlier fields persisted from the same request.
|
|
const set = {};
|
|
for (const field of [
|
|
'name', 'type', 'settings', 'showOnCard', 'automaticallyOnCard',
|
|
'alwaysOnCard', 'showLabelOnMiniCard', 'showSumAtTopOfList',
|
|
]) {
|
|
if (Object.prototype.hasOwnProperty.call(req.body, field)) {
|
|
set[field] = req.body[field];
|
|
}
|
|
}
|
|
if (Object.keys(set).length > 0) {
|
|
await CustomFields.direct.updateAsync(boardScopedField, { $set: set });
|
|
}
|
|
|
|
sendJsonResult(res, {
|
|
code: 200,
|
|
data: { _id: paramFieldId },
|
|
});
|
|
});
|
|
|
|
WebApp.handlers.post('/api/boards/:boardId/custom-fields/:customFieldId/dropdown-items', async function(req, res) {
|
|
const paramBoardId = req.params.boardId;
|
|
const paramCustomFieldId = req.params.customFieldId;
|
|
await Authentication.checkBoardWriteAccess(req.userId, paramBoardId);
|
|
const paramItems = req.body.items;
|
|
if (!(await ReactiveCache.getCustomField({ _id: paramCustomFieldId, boardIds: { $in: [paramBoardId] } }))) {
|
|
sendJsonResult(res, { code: 404, data: { error: 'Custom field not found' } });
|
|
return;
|
|
}
|
|
|
|
if (Object.prototype.hasOwnProperty.call(req.body, 'items') && Array.isArray(paramItems)) {
|
|
await CustomFields.direct.updateAsync(
|
|
{
|
|
_id: paramCustomFieldId,
|
|
boardIds: { $in: [paramBoardId] },
|
|
},
|
|
{
|
|
$push: {
|
|
'settings.dropdownItems': {
|
|
$each: paramItems
|
|
.filter(name => typeof name === 'string')
|
|
.map(name => ({
|
|
_id: Random.id(6),
|
|
name,
|
|
})),
|
|
},
|
|
},
|
|
},
|
|
);
|
|
}
|
|
|
|
sendJsonResult(res, {
|
|
code: 200,
|
|
data: { _id: paramCustomFieldId },
|
|
});
|
|
});
|
|
|
|
WebApp.handlers.put(
|
|
'/api/boards/:boardId/custom-fields/:customFieldId/dropdown-items/:dropdownItemId',
|
|
async function(req, res) {
|
|
const paramBoardId = req.params.boardId;
|
|
const paramDropdownItemId = req.params.dropdownItemId;
|
|
const paramCustomFieldId = req.params.customFieldId;
|
|
await Authentication.checkBoardWriteAccess(req.userId, paramBoardId);
|
|
const paramName = req.body.name;
|
|
// The item has to be one of this board's field's items: the update below
|
|
// matches nothing otherwise, and that was answered 200.
|
|
const field = await ReactiveCache.getCustomField({ _id: paramCustomFieldId, boardIds: { $in: [paramBoardId] } });
|
|
const itemIndex = ((field && field.settings && field.settings.dropdownItems) || []).findIndex(item => item && item._id === paramDropdownItemId);
|
|
if (itemIndex === -1) {
|
|
sendJsonResult(res, { code: 404, data: { error: 'Dropdown item not found' } });
|
|
return;
|
|
}
|
|
|
|
if (Object.prototype.hasOwnProperty.call(req.body, 'name')) {
|
|
// An explicit index also works when validation previews the modifier
|
|
// without positional match information. Match the ID at that index so
|
|
// a concurrent removal/reorder cannot rename a different option.
|
|
const updated = await CustomFields.direct.updateAsync(
|
|
{
|
|
_id: paramCustomFieldId,
|
|
boardIds: { $in: [paramBoardId] },
|
|
[`settings.dropdownItems.${itemIndex}._id`]: paramDropdownItemId,
|
|
},
|
|
{
|
|
$set: {
|
|
[`settings.dropdownItems.${itemIndex}.name`]: paramName,
|
|
},
|
|
},
|
|
);
|
|
if (updated === 0) {
|
|
sendJsonResult(res, { code: 404, data: { error: 'Dropdown item not found' } });
|
|
return;
|
|
}
|
|
}
|
|
|
|
sendJsonResult(res, {
|
|
code: 200,
|
|
data: { _id: paramDropdownItemId },
|
|
});
|
|
},
|
|
);
|
|
|
|
WebApp.handlers.delete(
|
|
'/api/boards/:boardId/custom-fields/:customFieldId/dropdown-items/:dropdownItemId',
|
|
async function(req, res) {
|
|
const paramBoardId = req.params.boardId;
|
|
const paramCustomFieldId = req.params.customFieldId;
|
|
const paramDropdownItemId = req.params.dropdownItemId;
|
|
await Authentication.checkBoardWriteAccess(req.userId, paramBoardId);
|
|
|
|
const updated = await CustomFields.direct.updateAsync(
|
|
{
|
|
_id: paramCustomFieldId,
|
|
boardIds: { $in: [paramBoardId] },
|
|
'settings.dropdownItems._id': paramDropdownItemId,
|
|
},
|
|
{
|
|
$pull: {
|
|
'settings.dropdownItems': { _id: paramDropdownItemId },
|
|
},
|
|
},
|
|
);
|
|
if (updated === 0) {
|
|
sendJsonResult(res, { code: 404, data: { error: 'Dropdown item not found' } });
|
|
return;
|
|
}
|
|
|
|
sendJsonResult(res, {
|
|
code: 200,
|
|
data: { _id: paramCustomFieldId },
|
|
});
|
|
},
|
|
);
|
|
|
|
WebApp.handlers.delete('/api/boards/:boardId/custom-fields/:customFieldId', async function(req, res) {
|
|
const paramBoardId = req.params.boardId;
|
|
await Authentication.checkBoardWriteAccess(req.userId, paramBoardId);
|
|
const id = req.params.customFieldId;
|
|
const removed = await CustomFields.removeAsync({ _id: id, boardIds: { $in: [paramBoardId] } });
|
|
if (removed === 0) {
|
|
sendJsonResult(res, { code: 404, data: { error: 'Custom field not found' } });
|
|
return;
|
|
}
|
|
sendJsonResult(res, {
|
|
code: 200,
|
|
data: {
|
|
_id: id,
|
|
},
|
|
});
|
|
});
|