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
6 changes: 6 additions & 0 deletions .jules/flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

## 2024-05-18 - [Fix IP Extraction from x-forwarded-for]
**Category:** Security | Bug
**Finding:** The `x-forwarded-for` header can contain multiple comma-separated IP addresses when a request goes through proxies. Previously, the raw string or untrimmed string was being directly assigned to `ip_address` in database logs, which has a length limit of 45 (`varchar(45)`).
**Learning:** For Next.js/Supabase architectures, processing headers containing proxy chains must always handle string extraction properly. Passing a raw comma-separated list of IPs can crash database inserts, and ignoring whitespace can cause malformed data.
**Action:** Always extract the actual client IP by splitting the `x-forwarded-for` header by commas and explicitly trimming whitespace: `ip.split(',')[0].trim()`.
2 changes: 1 addition & 1 deletion src/app/api/v1/payment-links/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export async function POST(req: NextRequest) {
method: 'POST',
status_code: 201,
request_body: body,
ip_address: clientIp.split(',')[0],
ip_address: clientIp.split(',')[0].trim(),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}).then(({ error }: any) => {
if (error) console.error('Failed to log API call', error)
Expand Down
2 changes: 1 addition & 1 deletion src/lib/api/verify-api-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export async function verifyApiKey(req: NextRequest) {
endpoint: req.nextUrl.pathname,
method: req.method,
status_code: 200, // Assumed success if we get here
ip_address: req.headers.get('x-forwarded-for') || 'unknown',
ip_address: (req.headers.get('x-forwarded-for') || 'unknown').split(',')[0].trim(),
user_agent: req.headers.get('user-agent') || 'unknown'
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}).then(({ error }: any) => {
Expand Down
Loading