fix: use safe LIKE prefix matching instead of non-character range

This commit is contained in:
asepharyana
2026-07-07 06:04:17 +07:00
parent d1c8cc9d23
commit bc806313b1
+3 -1
View File
@@ -55,6 +55,8 @@ const mapDbRowToS3Record = (row: Record<string, unknown>): S3FileRecord => {
}; };
}; };
const escapeLike = (s: string): string => s.replace(/[%_\\]/g, '\\$&');
export const listObjectsByPrefix = async ( export const listObjectsByPrefix = async (
bucketId: string, bucketId: string,
prefix: string, prefix: string,
@@ -63,7 +65,7 @@ export const listObjectsByPrefix = async (
startAfter: string | null, startAfter: string | null,
): Promise<{ objects: S3FileRecord[]; prefixes: string[] }> => { ): Promise<{ objects: S3FileRecord[]; prefixes: string[] }> => {
let query = prefix let query = prefix
? sql`SELECT * FROM files WHERE bucket_id = ${bucketId}::uuid AND is_deleted = false AND s3_key >= ${prefix} AND s3_key < ${`${prefix}￿`}` ? sql`SELECT * FROM files WHERE bucket_id = ${bucketId}::uuid AND is_deleted = false AND s3_key LIKE ${`${escapeLike(prefix)}%`}`
: sql`SELECT * FROM files WHERE bucket_id = ${bucketId}::uuid AND is_deleted = false`; : sql`SELECT * FROM files WHERE bucket_id = ${bucketId}::uuid AND is_deleted = false`;
if (startAfter) { if (startAfter) {