escape special characters for ILIKE search

This commit is contained in:
x032205
2025-11-14 20:33:50 -05:00
parent 353fe417d0
commit a37d27ece3
3 changed files with 14 additions and 6 deletions

View File

@@ -50,11 +50,14 @@ export const pamAccountDALFactory = (db: TDbClient) => {
}
if (search) {
// escape special characters (`%`, `_`) and the escape character itself (`\`)
const escapedSearch = search.replace(/\\/g, "\\\\").replace(/%/g, "\\%").replace(/_/g, "\\_");
const pattern = `%${escapedSearch}%`;
void query.where((q) => {
void q
.whereILike(`${TableName.PamAccount}.name`, `%${search}%`)
.orWhereILike(`${TableName.PamResource}.name`, `%${search}%`)
.orWhereILike(`${TableName.PamAccount}.description`, `%${search}%`);
.whereRaw(`??.?? ILIKE ? ESCAPE '\\'`, [TableName.PamAccount, "name", pattern])
.orWhereRaw(`??.?? ILIKE ? ESCAPE '\\'`, [TableName.PamResource, "name", pattern])
.orWhereRaw(`??.?? ILIKE ? ESCAPE '\\'`, [TableName.PamAccount, "description", pattern]);
});
}

View File

@@ -43,7 +43,9 @@ export const pamFolderDALFactory = (db: TDbClient) => {
}
if (search) {
void query.whereILike(`${TableName.PamFolder}.name`, `%${search}%`);
// escape special characters (`%`, `_`) and the escape character itself (`\`)
const escapedSearch = search.replace(/\\/g, "\\\\").replace(/%/g, "\\%").replace(/_/g, "\\_");
void query.whereRaw(`??.?? ILIKE ? ESCAPE '\\'`, [TableName.PamFolder, "name", `%${escapedSearch}%`]);
}
const countQuery = query.clone().count("*", { as: "count" }).first();

View File

@@ -47,10 +47,13 @@ export const pamResourceDALFactory = (db: TDbClient) => {
const query = dbInstance(TableName.PamResource).where(`${TableName.PamResource}.projectId`, projectId);
if (search) {
// escape special characters (`%`, `_`) and the escape character itself (`\`)
const escapedSearch = search.replace(/\\/g, "\\\\").replace(/%/g, "\\%").replace(/_/g, "\\_");
const pattern = `%${escapedSearch}%`;
void query.where((q) => {
void q
.whereILike(`${TableName.PamResource}.name`, `%${search}%`)
.orWhereILike(`${TableName.PamResource}.resourceType`, `%${search}%`);
.whereRaw(`??.?? ILIKE ? ESCAPE '\\'`, [TableName.PamResource, "name", pattern])
.orWhereRaw(`??.?? ILIKE ? ESCAPE '\\'`, [TableName.PamResource, "resourceType", pattern]);
});
}