Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/app/api/transactions/history/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export async function GET(req: NextRequest) {

const supabase = createServerClient()
const { searchParams } = new URL(req.url)
const limit = Math.min(parseInt(searchParams.get('limit') || '50'), 100)
const parsedLimit = parseInt(searchParams.get('limit') || '50')
const limit = isNaN(parsedLimit) || parsedLimit < 1 ? 50 : Math.min(parsedLimit, 100)

// 1. Fetch Sent Transactions (where customer_wallet = walletAddress)
const { data: sentTransactions, error: sentError } = await supabase
Expand Down
3 changes: 2 additions & 1 deletion src/app/api/transactions/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export async function GET(req: NextRequest) {
const supabase = createServerClient()
const { searchParams } = new URL(req.url)
const paymentLinkId = searchParams.get('payment_link_id')
const limit = Math.min(parseInt(searchParams.get('limit') || '50'), 100)
const parsedLimit = parseInt(searchParams.get('limit') || '50')
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flash Review

🚨 Security: While the limit parsing logic is improved to handle NaN and negative values, relying on manual parseInt and isNaN checks for API query parameters can be less robust and consistent than using a dedicated validation library.

Fix: Implement a Zod schema to validate all incoming query parameters for this API route, including payment_link_id and limit. This ensures consistent, declarative validation, better type safety, and prevents unexpected values from reaching the database.

import { z } from 'zod'

const querySchema = z.object({
  payment_link_id: z.string().uuid('Invalid payment link ID format'),
  limit: z.preprocess(
    (val) => parseInt(z.string().parse(val), 10),
    z.number().int().min(1).max(100)
  ).default(50).optional()
})

const { payment_link_id, limit } = querySchema.parse(Object.fromEntries(searchParams))

const limit = isNaN(parsedLimit) || parsedLimit < 1 ? 50 : Math.min(parsedLimit, 100)

// eslint-disable-next-line @typescript-eslint/no-explicit-any
let query = (supabase.from('transactions') as any)
Expand Down
6 changes: 4 additions & 2 deletions src/app/api/v1/payment-links/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,10 @@ export async function GET(req: NextRequest) {
}

const { searchParams } = new URL(req.url)
const limit = Math.min(parseInt(searchParams.get('limit') || '10'), 100)
const offset = parseInt(searchParams.get('offset') || '0')
const parsedLimit = parseInt(searchParams.get('limit') || '10')
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flash Review

🚨 Security: The previous logic for limit could result in NaN if a non-numeric string was provided, potentially leading to unexpected database query behavior or errors. This robust input validation, including isNaN checks and clamping limit to a minimum of 1 and maximum of 100, significantly improves the API's resilience against malformed requests.

Fix: No further fix needed, this change correctly addresses the potential issue.

const limit = isNaN(parsedLimit) || parsedLimit < 1 ? 10 : Math.min(parsedLimit, 100)
const parsedOffset = parseInt(searchParams.get('offset') || '0')
const offset = isNaN(parsedOffset) || parsedOffset < 0 ? 0 : parsedOffset

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const supabase = createServerClient() as any
Expand Down
6 changes: 4 additions & 2 deletions src/app/api/v1/transactions/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ export async function GET(req: NextRequest) {
}

const { searchParams } = new URL(req.url)
const limit = Math.min(parseInt(searchParams.get('limit') || '10'), 100)
const offset = parseInt(searchParams.get('offset') || '0')
const parsedLimit = parseInt(searchParams.get('limit') || '10')
const limit = isNaN(parsedLimit) || parsedLimit < 1 ? 10 : Math.min(parsedLimit, 100)
const parsedOffset = parseInt(searchParams.get('offset') || '0')
const offset = isNaN(parsedOffset) || parsedOffset < 0 ? 0 : parsedOffset
const status = searchParams.get('status')
const paymentLinkId = searchParams.get('payment_link_id')

Expand Down
Loading