Files
wekan/models/cardComments.js
T
Lauri Ojansivu 8dff96d7b3 Preserve copied comment history
Keep each copied comment's original author and timestamps, re-home it without mutating the cached source, and avoid generating false new-comment activity. Add positive, negative, wiring, and REST browser regressions.

Thanks to ocdtrekkie, 2martens, frmwrk123 and xet7 !

Fixes #1213,
2026-08-25 17:57:13 +03:00

318 lines
11 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { ReactiveCache } from '/imports/reactiveCache';
import escapeForRegex from 'escape-string-regexp';
import Boards from '/models/boards';
import CardCommentReactions from '/models/cardCommentReactions';
const { SimpleSchema } = require('/imports/simpleSchema');
// Server-side text sanitization function
function sanitizeText(text) {
if (typeof text !== 'string') return text;
// Strip HTML tags and return only text content.
// Repeat replacement until stable to avoid incomplete multi-character sanitization.
let sanitized = text;
let previous;
do {
previous = sanitized;
sanitized = sanitized.replace(/<[^>]*>/g, '');
} while (sanitized !== previous);
// A comment losing a `<b>` is an everyday thing and says nothing. A comment
// losing a SCRIPT TAG, an event handler or a javascript: URI is a stored-XSS
// attempt that this line defused, and an admin should be told who wrote it
// (docs/Security/Remediation/WeKan.md §12.6). The comment is stored sanitized
// either way; the author is told nothing.
if (Meteor.isServer && sanitized !== text) {
try {
const { removedActiveMarkup, CANARY_IDS } = require('/models/lib/injectionDetect');
if (removedActiveMarkup(text, sanitized)) {
require('/server/lib/canary').tripCanary(CANARY_IDS.text, {
detail: 'active markup removed from a comment',
});
}
} catch (e) { /* a canary must never break a comment */ }
}
return sanitized;
}
const CardComments = new Mongo.Collection('card_comments');
/**
* Pure permission decision for editing/deleting a comment.
*
* Kept as a standalone exported function so it can be unit-tested in isolation
* and reused by both the collection hooks (server) and the UI (client).
*
* @param {Object} opts
* @param {boolean} opts.isAuthor is the acting user the comment author?
* @param {boolean} opts.isBoardAdmin is the acting user a board admin?
* @param {boolean} opts.restrictCommentEditing board setting: when true, board
* admins may NOT edit/delete comments
* authored by others.
* @returns {boolean} whether the acting user may edit/delete the comment.
*/
export function canEditComment({ isAuthor, isBoardAdmin, restrictCommentEditing }) {
// The author may always edit/delete their own comment.
if (isAuthor) {
return true;
}
// When editing is restricted, admins lose the ability to touch others' comments.
if (restrictCommentEditing) {
return false;
}
// Default (unrestricted) behaviour: board admins may edit/delete others' comments.
return !!isBoardAdmin;
}
/**
* A comment on a card
*/
CardComments.attachSchema(
new SimpleSchema({
boardId: {
/**
* the board ID
*/
type: String,
},
cardId: {
/**
* the card ID
*/
type: String,
},
// XXX Rename in `content`? `text` is a bit vague...
text: {
/**
* the text of the comment
*/
type: String,
},
parentId: {
/**
* the _id of the comment this comment is a reply to (threaded replies).
* Optional: top-level comments have no parentId.
*/
type: String,
optional: true,
defaultValue: '',
},
createdAt: {
/**
* when was the comment created
*/
type: Date,
// eslint-disable-next-line consistent-return
autoValue() {
if (this.isInsert) {
return new Date();
} else if (this.isUpsert) {
return { $setOnInsert: new Date() };
} else {
this.unset();
}
},
},
modifiedAt: {
type: Date,
// eslint-disable-next-line consistent-return
autoValue() {
if (this.isInsert || this.isUpsert || this.isUpdate) {
return new Date();
} else {
this.unset();
}
},
},
// XXX Should probably be called `authorId`
userId: {
/**
* the author ID of the comment
*/
type: String,
// eslint-disable-next-line consistent-return
autoValue() {
if (this.isInsert && !this.isSet) {
return this.userId;
}
},
},
}),
);
CardComments.helpers({
copy(newCardId, newBoardId) {
const { buildCopiedComment } = require('./lib/copiedComment');
const copy = buildCopiedComment(this, newCardId, newBoardId || this.boardId);
if (!copy) return null;
// #1213: this is existing conversation history, not a newly posted
// comment. Preserve its author and dates, avoid mutating the cached source,
// and bypass the add-comment activity hook. Disabling auto-values prevents
// the schema from replacing createdAt with the time of the card copy.
return CardComments.direct.insertAsync(copy, { getAutoValues: false });
},
user() {
return ReactiveCache.getUser(this.userId);
},
// The comment this one replies to, or undefined for top-level comments.
parentComment() {
if (!this.parentId) {
return undefined;
}
return ReactiveCache.getCardComment(this.parentId);
},
reactions() {
const cardCommentReactions = ReactiveCache.getCardCommentReaction({cardCommentId: this._id});
return !!cardCommentReactions ? cardCommentReactions.reactions : [];
},
toggleReaction(reactionCodepoint) {
if (reactionCodepoint !== sanitizeText(reactionCodepoint)) {
return false;
} else {
const cardCommentReactions = ReactiveCache.getCardCommentReaction({cardCommentId: this._id});
const reactions = !!cardCommentReactions ? cardCommentReactions.reactions : [];
const userId = Meteor.userId();
const reaction = reactions.find(r => r.reactionCodepoint === reactionCodepoint);
// If no reaction is set for the codepoint, add this
if (!reaction) {
reactions.push({ reactionCodepoint, userIds: [userId] });
} else {
// toggle user reaction upon previous reaction state
const userHasReacted = reaction.userIds.includes(userId);
if (userHasReacted) {
reaction.userIds.splice(reaction.userIds.indexOf(userId), 1);
if (reaction.userIds.length === 0) {
reactions.splice(reactions.indexOf(reaction), 1);
}
} else {
reaction.userIds.push(userId);
}
}
// If no reaction doc exists yet create otherwise update reaction set
if (!!cardCommentReactions) {
return CardCommentReactions.updateAsync({ _id: cardCommentReactions._id }, { $set: { reactions } });
} else {
return CardCommentReactions.insertAsync({
boardId: this.boardId,
cardCommentId: this._id,
cardId: this.cardId,
reactions
});
}
}
}
});
CardComments.hookOptions.after.update = { fetchPrevious: false };
// Server-side enforcement of comment edit/delete permissions (issue #5906).
//
// The DDP `allow` rule in server/permissions/cardComments.js is the first gate,
// but the per-board `restrictCommentEditing` setting is enforced here so the
// rule cannot be bypassed and the decision lives next to the data. Exported so
// the REST handlers can enforce the SAME rule (GHSA-pqr4-rxgp-hv2m) rather than
// relying on collection hooks, which cannot see an HTTP caller's identity.
export async function assertCanMutateComment(userId, doc) {
// Server-internal operations (board copy, cleanup, migrations, etc.) run
// without an authenticated user; do not block those here. User-initiated
// DDP calls always carry a userId and are still gated by the allow rule.
//
// GHSA-pqr4-rxgp-hv2m: an HTTP handler is NOT a server-internal operation,
// but it reached the collection with no Meteor userId in the invocation
// context, so it landed on this trusted path — and every board member could
// delete anyone's comment through the REST API, restrictCommentEditing or
// not. The REST handlers now call this function themselves, with req.userId,
// BEFORE touching the collection (server/models/cardComments.js). The trusted
// path stays for the genuine internal callers it was written for.
if (!userId) {
return;
}
const isAuthor = userId === doc.userId;
if (isAuthor) {
return; // Authors may always edit/delete their own comments.
}
const board = await Boards.findOneAsync(doc.boardId);
const isBoardAdmin = !!board && !!userId && board.hasAdmin(userId);
const restrictCommentEditing = !!board && !!board.restrictCommentEditing;
if (!canEditComment({ isAuthor, isBoardAdmin, restrictCommentEditing })) {
const error = new Meteor.Error(
'error-comment-edit-not-allowed',
"You are not allowed to edit or delete another user's comment on this board.",
);
// So the REST layer answers 403 rather than 500 (httpStatusForError).
error.statusCode = 403;
throw error;
}
}
if (Meteor.isServer) {
CardComments.before.update(async (userId, doc) => {
await assertCanMutateComment(userId, doc);
});
CardComments.before.remove(async (userId, doc) => {
await assertCanMutateComment(userId, doc);
});
}
async function commentCreation(userId, doc) {
const card = await ReactiveCache.getCard(doc.cardId);
if (!card) {
console.warn('[commentCreation] Card not found for cardId:', doc.cardId, '— skipping activity insert.');
return;
}
await Activities.insertAsync({
userId,
activityType: 'addComment',
boardId: doc.boardId,
cardId: doc.cardId,
commentId: doc._id,
listId: card.listId,
swimlaneId: card.swimlaneId,
});
}
CardComments.textSearch = async (userId, textArray) => {
const selector = {
boardId: { $in: await Boards.userBoardIds(userId) },
$and: [],
};
for (const text of textArray) {
selector.$and.push({ text: new RegExp(escapeForRegex(text), 'i') });
}
// eslint-disable-next-line no-console
// console.log('cardComments selector:', selector);
const comments = await ReactiveCache.getCardComments(selector);
// eslint-disable-next-line no-console
// console.log('count:', comments.count());
// eslint-disable-next-line no-console
// console.log('cards with comments:', comments.map(com => { return com.cardId }));
return comments;
};
if (Meteor.isServer) {
// A card's comments, newest first - one selector, one sort, every time a card
// is opened.
const { ensureIndex } = require('/server/lib/mongoStartup');
Meteor.startup(async () => {
await ensureIndex(CardComments, { cardId: 1, createdAt: -1 });
await ensureIndex(CardComments, { boardId: 1 });
});
}
export default CardComments;