Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

πŸ› fix: fix /api/proxy internal proxy attack #2255

Merged
merged 1 commit into from Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
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
@@ -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 });
};