Files
wekan/models/cardComments.js
T
Lauri Ojansivu ab1f60d806 Cap comment replies at one level of threading, grouped under their parent.
A comment's Reply link (added earlier for #5907) let a reply be posted
against ANY comment, including another reply, so clicking Reply enough
times nested comments arbitrarily deep. For #3011, a reply now attaches
to a reply's own parent instead of the reply itself - resolveParentId()
(models/cardComments.js) makes that decision once, is unit tested, and
is applied both when the client opens the composer (so the "In reply
to ..." banner already names the right comment) and again in a
CardComments.before.insert hook server-side, so the cap holds no
matter how a comment is inserted.

Replies also now render directly under their top-level parent instead
of interleaved by date with unrelated comments: groupCommentsByThread()
(imports/lib/commentThreading.js) reorders the already-sorted flat list
the comments template already used, with no schema or query change.

The "Replying to <author>" indicator on the composer reuses the
existing, already fully translated comment-in-reply-to string plus the
parent comment's author name, rather than adding a new i18n key that
would need translating across every locale file for a small wording
difference.

Added tests/commentReplyFlattening.test.cjs (the one-level cap, both
the client and server wiring) and tests/commentThreadGrouping.test.cjs
(grouping, including the regression cases: a flat list of only
top-level comments is unchanged, and a comment whose parent was
deleted still renders as top-level rather than being dropped).

Thanks to FuXXz and xet7 !

Closes #3011,
2026-09-10 20:28:46 +03:00

359 lines
12 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.
*/
/**
* Resolve the parentId a NEW comment should actually be stored with, capping
* threading at one level (issue #3011). A reply to a top-level comment keeps
* its parentId as-is; a reply to a REPLY is flattened onto that reply's own
* parent (the original top-level comment), so nesting never goes deeper than
* one level no matter how many times "Reply" is clicked down the chain.
*
* Kept standalone and pure (no collection access) so it can be unit-tested
* without a database, and reused by both the client (choosing what to send)
* and the server (defense in depth against a caller that sends its own
* parentId, e.g. a future API).
*
* @param {string} parentId the _id of the comment being replied to, or ''.
* @param {function(string): ({parentId: string}|undefined)} getComment
* looks up a comment by _id; returns the doc (or at least its
* parentId) if found, undefined otherwise.
* @returns {string} the parentId to store, '' for a top-level comment.
*/
export function resolveParentId(parentId, getComment) {
if (!parentId) {
return '';
}
const parent = getComment(parentId);
if (parent && parent.parentId) {
return parent.parentId;
}
return parentId;
}
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) {
// Issue #3011: cap threading at one level regardless of caller. The client
// already sends a flattened parentId (see the reply-click handler in
// client/components/activities/comments.js), but this re-derives it
// server-side so a reply-to-a-reply can never be stored deeper than one
// level, however the insert was made.
CardComments.before.insert(async (userId, doc) => {
if (doc.parentId) {
const parent = await ReactiveCache.getCardComment(doc.parentId);
doc.parentId = resolveParentId(doc.parentId, () => parent);
}
});
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;