-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
馃悰 fix: fix
/api/proxy
internal proxy attack (#2255)
- Loading branch information
Showing
2 changed files
with
27 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
}; |