limit search query length

This commit is contained in:
k00b 2025-09-03 12:10:21 -05:00
parent 4d4ce66404
commit 560be165ee
4 changed files with 9 additions and 5 deletions

View File

@ -2,6 +2,7 @@ import { decodeCursor, LIMIT, nextCursorEncoded } from '@/lib/cursor'
import { whenToFrom } from '@/lib/time'
import { getItem, itemQueryWithMeta, SELECT } from './item'
import { parse } from 'tldts'
import { searchSchema, validateSchema } from '@/lib/validate'
function queryParts (q) {
const regex = /"([^"]*)"/gm
@ -173,6 +174,7 @@ export default {
}
},
search: async (parent, { q, cursor, sort, what, when, from: whenFrom, to: whenTo }, { me, models, search }) => {
await validateSchema(searchSchema, { q })
const decodedCursor = decodeCursor(cursor)
let sitems = null

View File

@ -15,6 +15,7 @@ import { useRouter } from 'next/router'
import { whenToFrom } from '@/lib/time'
import { useMe } from './me'
import { useField } from 'formik'
import { searchSchema } from '@/lib/validate'
export default function Search ({ sub }) {
const router = useRouter()
@ -65,6 +66,7 @@ export default function Search ({ sub }) {
<Form
initial={{ q, what, sort, when, from: '', to: '' }}
onSubmit={values => search({ ...values })}
schema={searchSchema}
>
<div className={`${styles.active} mb-3`}>
<SearchInput

View File

@ -22,10 +22,6 @@
form>.active {
display: flex;
pointer-events: auto;
flex-flow: row nowrap;
flex-flow: row;
align-items: center;
}
form>.active :global(.input-group) {
flex-flow: nowrap;
}

View File

@ -116,6 +116,10 @@ async function subHasPostType (name, type, { client, models }) {
return !!(sub?.postTypes?.includes(type))
}
export const searchSchema = object({
q: string().trim().max(100, 'must be at most 100 characters')
})
export function advPostSchemaMembers ({ me, existingBoost = 0, ...args }) {
const boostMin = existingBoost || BOOST_MIN
return {