All files / lib search-sql.ts

100% Statements 8/8
100% Branches 4/4
100% Functions 2/2
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27            15x 2x   13x 13x 4x   9x 9x         26x              
// LIKE pattern for searching an identifier inside a JSON-array column.
// Accepts any alphanumeric-with-underscore/hyphen identifier.
// Used for taxonomy slugs and other non-UUID lookups inside JSON arrays.
export function buildJsonArrayLikePatternSafe(
	value: string | undefined,
): string | null {
	if (!value) {
		return null;
	}
	const safeIdRegex = /^[a-zA-Z0-9_-]+$/;
	if (!safeIdRegex.test(value)) {
		return null;
	}
	const escaped = value.replace(/%/g, "\\%").replace(/_/g, "\\_");
	return `%"${escaped}"%`;
}
 
// Trim, escape LIKE meta-chars, and cap length to prevent malicious patterns.
export function sanitizeSearchInput(search: string): string {
	return search
		.trim()
		.replace(/\\/g, "\\\\")
		.replace(/%/g, "\\%")
		.replace(/_/g, "\\_")
		.substring(0, 200);
}