-
Notifications
You must be signed in to change notification settings - Fork 472
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0b42cce
commit 514acd1
Showing
3 changed files
with
47 additions
and
43 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { ipSafe } from 'apps/web/src/middleware/ipSafe'; | ||
import dns from 'dns/promises'; | ||
import ipaddr from 'ipaddr.js'; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import { URL } from 'url'; | ||
|
||
export function withIPCheck(handler: (req: NextRequest) => Promise<Response>) { | ||
return async function (req: NextRequest) { | ||
const searchParams = req.nextUrl.searchParams; | ||
const url = searchParams.get('url'); | ||
|
||
if (url) { | ||
try { | ||
const parsedUrl = new URL(url); | ||
const hostname = parsedUrl.hostname; | ||
const resolvedAddresses = await dns.resolve(hostname); | ||
|
||
let allSafe = true; | ||
|
||
for (const address of resolvedAddresses) { | ||
if (ipaddr.isValid(address)) { | ||
if (!ipSafe(address)) { | ||
allSafe = false; | ||
} | ||
} else { | ||
return NextResponse.json({ message: 'Invalid IP address resolution' }, { status: 400 }); | ||
} | ||
} | ||
|
||
if (!allSafe) { | ||
return NextResponse.json({ message: 'Forbidden' }, { status: 403 }); | ||
} | ||
|
||
return await handler(req); | ||
} catch (error) { | ||
return NextResponse.json({ message: 'Invalid URL format' }, { status: 400 }); | ||
} | ||
} | ||
|
||
return handler(req); | ||
}; | ||
} |
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,46 +1,5 @@ | ||
import { GET as getHandler, POST as postHandler } from '@frames.js/render/next'; | ||
import { ipSafe } from 'apps/web/src/middleware/ipSafe'; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import ipaddr from 'ipaddr.js'; | ||
import { URL } from 'url'; | ||
import dns from 'dns/promises'; | ||
|
||
function withIPCheck(handler: (req: NextRequest) => Promise<Response>) { | ||
return async function (req: NextRequest) { | ||
const searchParams = req.nextUrl.searchParams; | ||
const url = searchParams.get('url'); | ||
|
||
if (url) { | ||
try { | ||
const parsedUrl = new URL(url); | ||
const hostname = parsedUrl.hostname; | ||
const resolvedAddresses = await dns.resolve(hostname); | ||
|
||
let allSafe = true; | ||
|
||
for (const address of resolvedAddresses) { | ||
if (ipaddr.isValid(address)) { | ||
if (!ipSafe(address)) { | ||
allSafe = false; | ||
} | ||
} else { | ||
return NextResponse.json({ message: 'Invalid IP address resolution' }, { status: 400 }); | ||
} | ||
} | ||
|
||
if (!allSafe) { | ||
return NextResponse.json({ message: 'Forbidden' }, { status: 403 }); | ||
} | ||
|
||
return await handler(req); | ||
} catch (error) { | ||
return NextResponse.json({ message: 'Invalid URL format' }, { status: 400 }); | ||
} | ||
} | ||
|
||
return handler(req); | ||
}; | ||
} | ||
import { withIPCheck } from 'apps/web/app/frames/proxy-ip-check'; | ||
|
||
export const GET = withIPCheck(getHandler); | ||
export const POST = withIPCheck(postHandler); |