Skip to content
Open
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
18 changes: 18 additions & 0 deletions app/api/upload-binary/route.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { NextResponse } from 'next/server';

// Disallow internal/reserved IP ranges to prevent SSRF abuse of the proxy.
const INTERNAL_HOST_RE = /^(127\.|10\.|172\.(1[6-9]|2[0-9]|3[01])\.|192\.168\.|169\.254\.|0\.|::1|localhost)/i;

function isUnsafeTarget(url) {
try {
const parsed = new URL(url);
if (parsed.protocol !== 'https:') return true;
if (INTERNAL_HOST_RE.test(parsed.hostname)) return true;
return false;
} catch {
return true;
}
}

export async function POST(request) {
try {
const formData = await request.formData();
Expand All @@ -11,6 +25,10 @@ export async function POST(request) {
return NextResponse.json({ error: 'Missing proxy target URL' }, { status: 400 });
}

if (isUnsafeTarget(targetUrl)) {
return NextResponse.json({ error: 'Invalid or unsafe proxy target URL' }, { status: 400 });
}

// Reconstruct the FormData for S3 (excluding our internal proxy marker)
const s3FormData = new FormData();

Expand Down
18 changes: 18 additions & 0 deletions app/api/v1/upload-binary/route.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { NextResponse } from 'next/server';

// Disallow internal/reserved IP ranges to prevent SSRF abuse of the proxy.
const INTERNAL_HOST_RE = /^(127\.|10\.|172\.(1[6-9]|2[0-9]|3[01])\.|192\.168\.|169\.254\.|0\.|::1|localhost)/i;

function isUnsafeTarget(url) {
try {
const parsed = new URL(url);
if (parsed.protocol !== 'https:') return true;
if (INTERNAL_HOST_RE.test(parsed.hostname)) return true;
return false;
} catch {
return true;
}
}

export async function POST(request) {
try {
const formData = await request.formData();
Expand All @@ -11,6 +25,10 @@ export async function POST(request) {
return NextResponse.json({ error: 'Missing proxy target URL' }, { status: 400 });
}

if (isUnsafeTarget(targetUrl)) {
return NextResponse.json({ error: 'Invalid or unsafe proxy target URL' }, { status: 400 });
}

const s3FormData = new FormData();
for (const [key, value] of formData.entries()) {
if (key !== 'x-proxy-target-url') {
Expand Down