Files
wekan/models/lib/cardSearch.js
T
Lauri Ojansivu dad16609c9 Board search: match the typed text, not a regular expression.
The sidebar card search, the link-card popup and the list, swimlane and
board pickers (Board.searchCards / searchLists / searchSwimlanes /
searchBoards, models/lib/cardSearch.js) and the import member search
(UserSearchIndex) compiled the typed term AS a regular expression:

- "C++", "(2026)", "[x" or a case number like "PL0900-2026(1)" threw
  "Invalid regular expression" and the search showed nothing;
- "." matched any character and "*" or "?" changed what was searched for.

Every server-side search (global search, publications, boardTextSearch)
already escapes the term and matches it literally. These now do the same,
still case-insensitive, through literalSearchRegex() in cardSearch.js,
which the user search reuses rather than carrying its own escape. The
Advanced Filter's /regex/ syntax is a separate path and is unchanged.

tests/noUnescapedDynamicRegExp.test.cjs no longer needs its reviewed
exceptions for models/boards.js, models/lib/cardSearch.js and
models/users.js; tests/searchLiteralTerm.test.cjs drives the matching and
fails on any model, client or imports file that compiles a raw term into
a RegExp. From the reporter's patch.

Thanks to Raymond-Voogt and xet7 !

Fixes #6822.
2026-10-27 21:15:51 +02:00

85 lines
4.1 KiB
JavaScript

'use strict';
import { parseCardKey } from './boardKey.js';
// Pure helpers for board card search. Extracted from models/boards.js
// (Board.searchCards) so the query it builds is unit-testable without Meteor.
//
// #5680 (can't search numbers in custom fields): "number" and "currency" custom
// fields store their value as a JS Number (the inputs save `parseInt(...)` /
// `Number(...)`), but the search only matched custom fields with
// `{ value: <regex> }`. A MongoDB / Minimongo regex only matches STRING values,
// so it silently skipped every numeric custom field — searching a card's
// transaction number (e.g. 2025001) or currency amount (e.g. 123) never found
// it. We add an exact numeric-equality clause when the term parses to a plain
// finite number, so numeric custom fields match too. (Card search runs against
// Minimongo on the client, which — like MongoDB — cannot regex a numeric field,
// so equality is the correct cross-environment match rather than `$toString`.)
// Parse a search term into a Number the way the number/currency custom-field
// inputs store their value (a comma is accepted as a decimal separator, matching
// the currency input's `value.replace(',', '.')`). Returns null when the term is
// not a plain number, so we never widen a text search into an accidental match.
function parseNumericSearchTerm(term) {
if (typeof term !== 'string') return null;
const trimmed = term.trim();
if (trimmed === '') return null;
const normalized = trimmed.replace(',', '.');
// Require a plain decimal number. This rejects things Number() would otherwise
// coerce (Number('') === 0, Number('0x1f') === 31, Number('1e3') === 1000) so
// only genuine numeric terms add the numeric clause.
if (!/^[+-]?(\d+(\.\d+)?|\.\d+)$/.test(normalized)) return null;
const n = Number(normalized);
return Number.isFinite(n) ? n : null;
}
// #6822: the typed term, matched literally and case-insensitively. Board search
// used the term AS a regular expression: "C++", "(2026)" or "[x" threw "Invalid
// regular expression" and the sidebar search and the link-card popup showed
// nothing, and "." or "*" matched far more than was typed. Every server-side
// search already escapes the same way (server/lib/boardTextSearch.js).
const escapeRegExp = text => String(text).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
function literalSearchRegex(term) {
return new RegExp(escapeRegExp(term), 'i');
}
// Build the `$or` clause array for a board card search on `term`. `term` is
// expected to be a trimmed, non-empty string (Board.searchCards guards that).
function buildCardSearchOr(term, { boardKey = null } = {}) {
const regex = literalSearchRegex(term);
const or = [
{ title: regex },
{ description: regex },
{ customFields: { $elemMatch: { value: regex } } },
];
const numeric = parseNumericSearchTerm(term);
if (numeric !== null) {
// #5680: also match numeric custom fields (number / currency) by value.
or.push({ customFields: { $elemMatch: { value: numeric } } });
}
// #2449/#2450: the card's own KEY-7, on its board.
const byKey = parseCardKey(term);
if (byKey && boardKey && byKey.key === boardKey) or.push({ cardNumber: byKey.number });
return or;
}
// #3841: board search must also find cards whose match is only in a COMMENT,
// not just title/description/custom fields. `comments` is a plain array of
// `{ cardId, text }` (as pulled from the CardComments collection for the
// current board); returns the de-duplicated list of `cardId`s whose comment
// text matches `term`, using the SAME case-insensitive regex the rest of the
// card search uses (reuses `buildCardSearchOr`'s matching rule rather than a
// second implementation of "does this term match this text").
function matchingCommentCardIds(comments, term) {
const regex = literalSearchRegex(term);
const ids = new Set();
(comments || []).forEach(comment => {
if (comment && typeof comment.text === 'string' && regex.test(comment.text)) {
ids.add(comment.cardId);
}
});
return Array.from(ids);
}
export { parseNumericSearchTerm, buildCardSearchOr, matchingCommentCardIds, literalSearchRegex };