Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(comments): allow square brackets in comment mention display names (#15092)

Harsh Kashyap17bd9fdde23f
diff --git a/web/src/features/comments/lib/mentionParser.clienttest.ts b/web/src/features/comments/lib/mentionParser.clienttest.ts
index 1f6e540709..5fc521d872 100644
--- a/web/src/features/comments/lib/mentionParser.clienttest.ts
+++ b/web/src/features/comments/lib/mentionParser.clienttest.ts
@@ -103,6 +103,21 @@ describe("mentionParser", () => {
expect(result).toEqual(["AlIcE123"]);
});
+
+ it("should extract mentions when display name contains brackets", () => {
+ const content =
+ "@[John Doe[ Platform Team ]](user:alice123) and @[Lead[Platform]](user:bob456)";
+ const result = extractUniqueMentionedUserIds(content);
+
+ expect(result).toEqual(["alice123", "bob456"]);
+ });
+
+ it("should extract mention when display name contains a newline", () => {
+ const content = "@[Line1\nLine2](user:alice123)";
+ const result = extractUniqueMentionedUserIds(content);
+
+ expect(result).toEqual(["alice123"]);
+ });
});
describe("invalid patterns", () => {
@@ -134,13 +149,6 @@ describe("mentionParser", () => {
expect(result).toEqual([]);
});
- it("should not match mentions with nested brackets", () => {
- const content = "@[Alice [Admin]](user:alice123)";
- const result = extractUniqueMentionedUserIds(content);
-
- expect(result).toEqual([]);
- });
-
it("should not match mentions with user ID containing invalid characters", () => {
const content = "@[Alice](user:alice@123)";
const result = extractUniqueMentionedUserIds(content);
@@ -261,6 +269,8 @@ describe("mentionParser", () => {
});
it("should handle many repeated brackets without hanging", () => {
+ // Display names may contain brackets now; the regex must still
+ // terminate quickly via the literal `](user:` anchor.
const content = "@[[[[[[[Alice](user:alice123)";
const startTime = Date.now();
@@ -268,19 +278,7 @@ describe("mentionParser", () => {
const duration = Date.now() - startTime;
expect(duration).toBeLessThan(100);
- expect(result).toEqual([]);
- });
-
- it("should handle pathological regex patterns efficiently", () => {
- // Pattern that could cause catastrophic backtracking in poorly designed regex
- const content =
- "@[" + "A".repeat(50) + "[" + "B".repeat(50) + "](user:test)";
-
- const startTime = Date.now();
- extractUniqueMentionedUserIds(content);
- const duration = Date.now() - startTime;
-
- expect(duration).toBeLessThan(100);
+ expect(result).toEqual(["alice123"]);
});
it("should handle very large input efficiently", () => {
@@ -344,6 +342,23 @@ describe("mentionParser", () => {
expect(result.sanitizedContent).toBe("@[User](user:minimal111) review");
});
+
+ it("should preserve square brackets when normalising from the DB (#14836)", () => {
+ const bracketMember: ProjectMember[] = [
+ {
+ id: "alice123",
+ name: "John Doe[ Platform Team ]",
+ email: "[email protected]",
+ },
+ ];
+ const content = "Hey @[Anything](user:alice123), check this";
+ const result = sanitizeMentions(content, bracketMember);
+
+ expect(result.sanitizedContent).toBe(
+ "Hey @[John Doe[ Platform Team ]](user:alice123), check this",
+ );
+ expect(result.validMentionedUserIds).toEqual(["alice123"]);
+ });
});
describe("invalid user handling", () => {
diff --git a/web/src/features/comments/lib/mentionParser.ts b/web/src/features/comments/lib/mentionParser.ts
index d16a3539ef..c47a4a051e 100644
--- a/web/src/features/comments/lib/mentionParser.ts
+++ b/web/src/features/comments/lib/mentionParser.ts
@@ -11,12 +11,19 @@ export const MENTION_USER_PREFIX = "user:";
/**
* Mention format: @[Display Name](user:userId)
- * Regex pattern with bounded quantifiers to prevent ReDoS attacks
- * - Display name: 1-100 characters, excluding brackets
- * - User ID: 1-30 characters (CUID is 25 chars, custom IDs may include hyphens/underscores)
+ * The display name is allowed to contain `[` or `]` (e.g. a user's display
+ * name like `John Doe[ Platform Team ]`). The non-greedy match anchored on
+ * the literal `](user:` terminator guarantees the closest `]` is the
+ * closing bracket, so internal brackets in the display name are tolerated.
+ * The capture group uses `[\s\S]` (not bare `.`) so a display name with a
+ * literal newline matches, matching the pre-PR behavior of the negated
+ * class `[^[\]]{1,100}` which also matched line terminators. The hard
+ * 1-100 cap on the capture keeps the engine bounded.
+ * User ID: 1-30 characters (CUID is 25 chars, custom IDs may include
+ * hyphens/underscores).
*/
const MENTION_REGEX = new RegExp(
- `@\\[([^[\\]]{1,100})\\]\\(${MENTION_USER_PREFIX}([a-z0-9_-]{1,30})\\)`,
+ `@\\[([\\s\\S]{1,100}?)\\]\\(${MENTION_USER_PREFIX}([a-z0-9_-]{1,30})\\)`,
"gi",
);
diff --git a/worker/src/features/notifications/commentMentionHandler.ts b/worker/src/features/notifications/commentMentionHandler.ts
index df0d8eff11..da46d5f271 100644
--- a/worker/src/features/notifications/commentMentionHandler.ts
+++ b/worker/src/features/notifications/commentMentionHandler.ts
@@ -133,8 +133,14 @@ export async function handleCommentMentionNotification(
comment.content.length > 500
? comment.content.substring(0, 497) + "..."
: comment.content;
- // Convert @[DisplayName](user:userId) to @DisplayName
- return truncated.replace(/@\[([^\]]+)\]\(user:[^)]+\)/g, "@$1");
+ // Convert @[DisplayName](user:userId) to @DisplayName. Mirrors
+ // MENTION_REGEX in web/src/features/comments/lib/mentionParser.ts so
+ // display names containing square brackets or line breaks survive
+ // both the parser and this preview stripper.
+ return truncated.replace(
+ /@\[([\s\S]{1,100}?)\]\(user:[a-z0-9_-]{1,30}\)/gi,
+ "@$1",
+ );
})();
// Process each mentioned user