@@ -15,6 +15,11 @@ export const POST = withAuth(async ({ request, locals }) => {
1515 return NextResponse . json ( { error : 'Missing conversationId' } , { status : 400 } ) ;
1616 }
1717
18+ const normalizedLimit = normalizeMessageLimit ( limit ) ;
19+ if ( normalizedLimit === null ) {
20+ return NextResponse . json ( { error : 'limit must be an integer between 1 and 100' } , { status : 400 } ) ;
21+ }
22+
1823 const { supabase, user : authUser } = locals ;
1924
2025 // Get internal user ID from auth user ID
@@ -35,7 +40,7 @@ export const POST = withAuth(async ({ request, locals }) => {
3540 conversationId,
3641 authUserId : authUser . id ,
3742 internalUserId : userId ,
38- limit,
43+ limit : normalizedLimit ,
3944 before
4045 } ) ;
4146
@@ -64,7 +69,7 @@ export const POST = withAuth(async ({ request, locals }) => {
6469 . eq ( 'message_recipients.recipient_user_id' , userId )
6570 . is ( 'deleted_at' , null )
6671 . order ( 'created_at' , { ascending : true } )
67- . limit ( limit ) ;
72+ . limit ( normalizedLimit ) ;
6873
6974 if ( before ) {
7075 query = query . lt ( 'created_at' , before ) ;
@@ -132,10 +137,14 @@ export const POST = withAuth(async ({ request, locals }) => {
132137 return NextResponse . json ( {
133138 success : true ,
134139 messages : processedMessages ,
135- hasMore : processedMessages . length === limit
140+ hasMore : processedMessages . length === normalizedLimit
136141 } ) ;
137142 } catch ( error ) {
138143 console . error ( '📨 [SSE-LOAD] Exception:' , error ) ;
139144 return NextResponse . json ( { error : 'Internal server error' } , { status : 500 } ) ;
140145 }
141- } ) ;
146+ } ) ;
147+
148+ function normalizeMessageLimit ( limit ) {
149+ return Number . isInteger ( limit ) && limit >= 1 && limit <= 100 ? limit : null ;
150+ }
0 commit comments