Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 04:45 UTC
| 1 | import { NextRequest, NextResponse } from 'next/server'; |
| 2 | |
| 3 | export async function POST(request: NextRequest) { |
| 4 | return handleProxy(request); |
| 5 | } |
| 6 | |
| 7 | export async function GET(request: NextRequest) { |
| 8 | return handleProxy(request); |
| 9 | } |
| 10 | |
| 11 | export async function PUT(request: NextRequest) { |
| 12 | return handleProxy(request); |
| 13 | } |
| 14 | |
| 15 | export async function DELETE(request: NextRequest) { |
| 16 | return handleProxy(request); |
| 17 | } |
| 18 | |
| 19 | export async function PATCH(request: NextRequest) { |
| 20 | return handleProxy(request); |
| 21 | } |
| 22 | |
| 23 | const ALLOWED_HOST = 'backend.composio.dev'; |
| 24 | |
| 25 | async function handleProxy(request: NextRequest) { |
| 26 | try { |
| 27 | const url = new URL(request.url); |
| 28 | const targetUrl = url.searchParams.get('url'); |
| 29 | |
| 30 | if (!targetUrl) { |
| 31 | return NextResponse.json({ error: 'Missing url parameter' }, { status: 400 }); |
| 32 | } |
| 33 | |
| 34 | // Validate the target URL to prevent SSRF |
| 35 | let parsedTarget: URL; |
| 36 | try { |
| 37 | parsedTarget = new URL(targetUrl); |
| 38 | } catch { |
| 39 | return NextResponse.json({ error: 'Invalid URL' }, { status: 400 }); |
| 40 | } |
| 41 | |
| 42 | if (parsedTarget.hostname !== ALLOWED_HOST) { |
| 43 | return NextResponse.json({ error: 'URL not allowed' }, { status: 403 }); |
| 44 | } |
| 45 | |
| 46 | if (parsedTarget.protocol !== 'https:') { |
| 47 | return NextResponse.json({ error: 'Only HTTPS allowed' }, { status: 403 }); |
| 48 | } |
| 49 | |
| 50 | // Get request body for non-GET requests |
| 51 | let body: string | undefined; |
| 52 | if (request.method !== 'GET' && request.method !== 'HEAD') { |
| 53 | body = await request.text(); |
| 54 | } |
| 55 | |
| 56 | // Forward headers (excluding host and other problematic headers) |
| 57 | const headers = new Headers(); |
| 58 | request.headers.forEach((value, key) => { |
| 59 | const lowerKey = key.toLowerCase(); |
| 60 | if (!['host', 'connection', 'content-length'].includes(lowerKey)) { |
| 61 | headers.set(key, value); |
| 62 | } |
| 63 | }); |
| 64 | |
| 65 | const response = await fetch(targetUrl, { |
| 66 | method: request.method, |
| 67 | headers, |
| 68 | body, |
| 69 | }); |
| 70 | |
| 71 | const responseBody = await response.text(); |
| 72 | |
| 73 | return new NextResponse(responseBody, { |
| 74 | status: response.status, |
| 75 | headers: { |
| 76 | 'Content-Type': response.headers.get('Content-Type') || 'application/json', |
| 77 | 'Access-Control-Allow-Origin': '*', |
| 78 | }, |
| 79 | }); |
| 80 | } catch (error) { |
| 81 | console.error('Proxy error:', error); |
| 82 | return NextResponse.json( |
| 83 | { error: 'Proxy request failed' }, |
| 84 | { status: 500 } |
| 85 | ); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | export async function OPTIONS() { |
| 90 | return new NextResponse(null, { |
| 91 | status: 200, |
| 92 | headers: { |
| 93 | 'Access-Control-Allow-Origin': '*', |
| 94 | 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS', |
| 95 | 'Access-Control-Allow-Headers': 'Content-Type, Authorization, x-api-key', |
| 96 | }, |
| 97 | }); |
| 98 | } |