-
Notifications
You must be signed in to change notification settings - Fork 3.5k
fix(table): escape LIKE wildcards in $contains filter values #3949
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -322,10 +322,15 @@ function buildComparisonClause( | |
| return sql`(${sql.raw(`${tableName}.data->>'${escapedField}'`)})::numeric ${sql.raw(operator)} ${value}` | ||
| } | ||
|
|
||
| /** Escapes LIKE/ILIKE wildcard characters so they match literally */ | ||
| function escapeLikePattern(value: string): string { | ||
| return value.replace(/[\\%_]/g, '\\$&') | ||
| } | ||
|
|
||
| /** Builds case-insensitive pattern match: `data->>'field' ILIKE '%value%'` */ | ||
| function buildContainsClause(tableName: string, field: string, value: string): SQL { | ||
| const escapedField = field.replace(/'/g, "''") | ||
| return sql`${sql.raw(`${tableName}.data->>'${escapedField}'`)} ILIKE ${`%${value}%`}` | ||
| return sql`${sql.raw(`${tableName}.data->>'${escapedField}'`)} ILIKE ${`%${escapeLikePattern(value)}%`}` | ||
|
Comment on lines
+325
to
+333
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The The PR description lists three concrete scenarios to verify ( describe('escapeLikePattern', () => {
it('escapes percent signs', () => {
expect(escapeLikePattern('100%')).toBe('100\\%')
})
it('escapes underscores', () => {
expect(escapeLikePattern('a_b')).toBe('a\\_b')
})
it('escapes backslashes', () => {
expect(escapeLikePattern('a\\b')).toBe('a\\\\b')
})
it('leaves plain strings unchanged', () => {
expect(escapeLikePattern('john')).toBe('john')
})
}) |
||
| } | ||
|
Comment on lines
330
to
334
|
||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicated
escapeLikePatternutility across two modulesLow Severity
The new
escapeLikePatternfunction insql.tsis a semantic duplicate of the existingescapeLikePatterninapps/sim/lib/knowledge/documents/service.ts. Both escape the same three characters (\,%,_) for LIKE patterns, just with slightly different implementations (single regex vs. three chained.replacecalls). Having two copies risks divergent bug fixes if one is updated without the other. This could be extracted to a shared utility.Reviewed by Cursor Bugbot for commit b1790f3. Configure here.