Skip to content

Commit

Permalink
馃悰 fix: fix /api/proxy internal proxy attack (#2255)
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Apr 28, 2024
1 parent 6805752 commit 465665a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
"i18next-resources-to-backend": "^1.2.1",
"idb-keyval": "^6.2.1",
"immer": "^10.0.4",
"ip": "^2.0.1",
"jose": "^5.2.4",
"langfuse": "^3.7.0",
"langfuse-core": "^3.7.0",
Expand Down Expand Up @@ -183,6 +184,7 @@
"@types/chroma-js": "^2.4.4",
"@types/debug": "^4.1.12",
"@types/diff": "^5.2.0",
"@types/ip": "^1.1.3",
"@types/json-schema": "^7.0.15",
"@types/lodash": "^4.17.0",
"@types/lodash-es": "^4.17.12",
Expand Down
28 changes: 25 additions & 3 deletions src/app/api/proxy/route.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
export const runtime = 'edge';
import { isPrivate } from 'ip';
import { NextResponse } from 'next/server';
import dns from 'node:dns';
import { promisify } from 'node:util';

const lookupAsync = promisify(dns.lookup);

export const runtime = 'nodejs';

/**
* just for a proxy
*/
export const POST = async (req: Request) => {
const url = await req.text();
const url = new URL(await req.text());
let address;

try {
const lookupResult = await lookupAsync(url.hostname);
address = lookupResult.address;
} catch (err) {
console.error(`${url.hostname} DNS parser error:`, err);

return NextResponse.json({ error: 'DNS parser error' }, { status: 504 });
}

const isInternalHost = isPrivate(address);

if (isInternalHost)
return NextResponse.json({ error: 'Not support internal host proxy' }, { status: 400 });

const res = await fetch(url);
const res = await fetch(url.toString());

return new Response(res.body, { headers: res.headers });
};

0 comments on commit 465665a

Please sign in to comment.