Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.8k
improvement(mothership): restructured stream, tool structures, code typing, file write/patch/append tools, timing issues#4090
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
2d2448f1fc84b8e2de4d2b6e1df4acc00dfcac100a0d09d110b3f3ed0a41b8bd85775eb74cf2887ff68c4ee6fa833433b1ca2afaa8dfbe8ac29941e2da0cbec52d6330dd1ee0d25632c5c47c1febc030bc31ae46a6fbb511a18ebb674695e75d5d13ed2dad02b799f35b22f1f5be55d289f88423893afdce1f00cfd4fa1cd22f367b49d67ef2fcfe7817833c2ba422881ac66f2abf6aca738a6d69d69eed25c2436f04c48b1caeb0c77f204e610df6fe5baf7f0d381906386049da574a649ee9c3ef87e52d2f782485dce7f509e33e321e998f3c8e47835df424abd879272b153252736c61cbb033d1342c026ce75b94db62156f49c74c4a9ca361a3949601c91301df734a4d1e2b4eb3386d0aa3690dc2da28f8aac84c6209f06cbd11d3df22cd5e40f6c16d6fe2be87c08cc0227d73d5f7b98d72e3c69cce740dd55d18ace6db00833f1328c9903f977061c90c8947a3ac741b2b10e2df15ebd054c4153e218991dc9e89b087abad273a4176355867e8e77bebf3ece4345c6cf6309c6File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Diff view
Diff view
Uh oh!
There was an error while loading. Please reload this page.
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| import { db } from '@sim/db' | ||
| import { user } from '@sim/db/schema' | ||
| import { eq } from 'drizzle-orm' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { getSession } from '@/lib/auth' | ||
| import { env } from '@/lib/core/config/env' | ||
| const ENV_URLS: Record<string, string | undefined> = { | ||
| dev: env.MOTHERSHIP_DEV_URL, | ||
| staging: env.MOTHERSHIP_STAGING_URL, | ||
| prod: env.MOTHERSHIP_PROD_URL, | ||
| } | ||
| function getMothershipUrl(environment: string): string | null { | ||
| return ENV_URLS[environment] ?? null | ||
| } | ||
| async function isAdminRequestAuthorized() { | ||
| const session = await getSession() | ||
| if (!session?.user?.id) return false | ||
| const [currentUser] = await db | ||
| .select({ role: user.role }) | ||
| .from(user) | ||
| .where(eq(user.id, session.user.id)) | ||
| .limit(1) | ||
| return currentUser?.role === 'admin' | ||
| } | ||
| /** | ||
| * Proxy to the mothership admin API. | ||
| * | ||
| * Query params: | ||
| * env - "dev" | "staging" | "prod" | ||
| * endpoint - the admin endpoint path, e.g. "requests", "licenses", "traces" | ||
| * | ||
| * The request body (for POST) is forwarded as-is. Additional query params | ||
| * (e.g. requestId for GET /traces) are forwarded. | ||
| */ | ||
| export async function POST(req: NextRequest) { | ||
| if (!(await isAdminRequestAuthorized())) { | ||
| return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) | ||
| } | ||
| const adminKey = env.MOTHERSHIP_API_ADMIN_KEY | ||
| if (!adminKey) { | ||
| return NextResponse.json({ error: 'MOTHERSHIP_API_ADMIN_KEY not configured' }, { status: 500 }) | ||
| } | ||
| const { searchParams } = new URL(req.url) | ||
| const environment = searchParams.get('env') || 'dev' | ||
| const endpoint = searchParams.get('endpoint') | ||
| if (!endpoint) { | ||
| return NextResponse.json({ error: 'endpoint query param required' }, { status: 400 }) | ||
| } | ||
| const baseUrl = getMothershipUrl(environment) | ||
| if (!baseUrl) { | ||
| return NextResponse.json( | ||
| { error: `No URL configured for environment: ${environment}` }, | ||
| { status: 400 } | ||
| ) | ||
| } | ||
| const targetUrl = `${baseUrl}/api/admin/${endpoint}` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Endpoint parameter allows path traversal in proxy URLMedium Severity The Additional Locations (1)Reviewed by Cursor Bugbot for commit 949601c. Configure here. | ||
| try { | ||
| const body = await req.text() | ||
| const upstream = await fetch(targetUrl, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| 'x-api-key': adminKey, | ||
| }, | ||
| ...(body ? { body } : {}), | ||
| }) | ||
| const data = await upstream.json() | ||
| return NextResponse.json(data, { status: upstream.status }) | ||
| } catch (error) { | ||
| return NextResponse.json( | ||
| { | ||
| error: `Failed to reach mothership (${environment}): ${error instanceof Error ? error.message : 'Unknown error'}`, | ||
| }, | ||
| { status: 502 } | ||
| ) | ||
| } | ||
| } | ||
| export async function GET(req: NextRequest) { | ||
| if (!(await isAdminRequestAuthorized())) { | ||
| return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) | ||
| } | ||
| const adminKey = env.MOTHERSHIP_API_ADMIN_KEY | ||
| if (!adminKey) { | ||
| return NextResponse.json({ error: 'MOTHERSHIP_API_ADMIN_KEY not configured' }, { status: 500 }) | ||
| } | ||
| const { searchParams } = new URL(req.url) | ||
| const environment = searchParams.get('env') || 'dev' | ||
| const endpoint = searchParams.get('endpoint') | ||
| if (!endpoint) { | ||
| return NextResponse.json({ error: 'endpoint query param required' }, { status: 400 }) | ||
| } | ||
| const baseUrl = getMothershipUrl(environment) | ||
| if (!baseUrl) { | ||
| return NextResponse.json( | ||
| { error: `No URL configured for environment: ${environment}` }, | ||
| { status: 400 } | ||
| ) | ||
| } | ||
| const forwardParams = new URLSearchParams() | ||
| searchParams.forEach((value, key) => { | ||
| if (key !== 'env' && key !== 'endpoint') { | ||
| forwardParams.set(key, value) | ||
| } | ||
| }) | ||
| const qs = forwardParams.toString() | ||
| const targetUrl = `${baseUrl}/api/admin/${endpoint}${qs ? `?${qs}` : ''}` | ||
| try { | ||
| const upstream = await fetch(targetUrl, { | ||
| method: 'GET', | ||
| headers: { 'x-api-key': adminKey }, | ||
| }) | ||
| const data = await upstream.json() | ||
| return NextResponse.json(data, { status: upstream.status }) | ||
| } catch (error) { | ||
| return NextResponse.json( | ||
| { | ||
| error: `Failed to reach mothership (${environment}): ${error instanceof Error ? error.message : 'Unknown error'}`, | ||
| }, | ||
| { status: 502 } | ||
| ) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.


Uh oh!
There was an error while loading. Please reload this page.