Skip to content

Commit dedcff0

Browse files
authored
fix(drizzle): sanitize query value uuid / number id NaN (#8369)
Fixes #8347 (additionally for UUID search as well)
1 parent 338c93a commit dedcff0

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

packages/drizzle/src/queries/parseParams.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { SQL } from 'drizzle-orm'
22
import type { Field, Operator, Where } from 'payload'
33

44
import { and, isNotNull, isNull, ne, notInArray, or, sql } from 'drizzle-orm'
5+
import { PgUUID } from 'drizzle-orm/pg-core'
56
import { QueryError } from 'payload'
67
import { validOperators } from 'payload/shared'
78

@@ -194,6 +195,7 @@ export function parseParams({
194195
adapter,
195196
columns,
196197
field,
198+
isUUID: table?.[columnName] instanceof PgUUID,
197199
operator,
198200
relationOrPath,
199201
val,

packages/drizzle/src/queries/sanitizeQueryValue.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type SanitizeQueryValueArgs = {
1616
rawColumn: SQL<unknown>
1717
}[]
1818
field: Field | TabAsField
19+
isUUID: boolean
1920
operator: string
2021
relationOrPath: string
2122
val: any
@@ -30,6 +31,7 @@ export const sanitizeQueryValue = ({
3031
adapter,
3132
columns,
3233
field,
34+
isUUID,
3335
operator: operatorArg,
3436
relationOrPath,
3537
val,
@@ -90,6 +92,16 @@ export const sanitizeQueryValue = ({
9092

9193
if (field.type === 'number' && typeof formattedValue === 'string') {
9294
formattedValue = Number(val)
95+
96+
if (Number.isNaN(formattedValue)) {
97+
formattedValue = null
98+
}
99+
}
100+
101+
if (isUUID && typeof formattedValue === 'string') {
102+
if (!uuidValidate(val)) {
103+
formattedValue = null
104+
}
93105
}
94106

95107
if (field.type === 'date' && operator !== 'exists') {

test/collections-rest/int.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,22 @@ describe('collections-rest', () => {
934934
expect(result.totalDocs).toEqual(1)
935935
})
936936

937+
it('like - id should not crash', async () => {
938+
const post = await createPost({ title: 'post' })
939+
940+
const response = await restClient.GET(`/${slug}`, {
941+
query: {
942+
where: {
943+
id: {
944+
like: 'words partial',
945+
},
946+
},
947+
},
948+
})
949+
950+
expect(response.status).toEqual(200)
951+
})
952+
937953
it('exists - true', async () => {
938954
const postWithDesc = await createPost({ description: 'exists' })
939955
await createPost({ description: undefined })

0 commit comments

Comments
 (0)