-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve security for sandbox video stream by introducing token
- Loading branch information
Showing
7 changed files
with
122 additions
and
72 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 { NextResponse } from 'next/server' | ||
import { createClient } from '@supabase/supabase-js' | ||
import { verifySandbox } from '@/lib/utils' | ||
|
||
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL! | ||
const supabaseServiceKey = process.env.SUPABASE_SERVICE_ROLE_KEY! | ||
|
||
const supabase = createClient(supabaseUrl, supabaseServiceKey) | ||
|
||
export async function GET(request: Request, { params }: { params: { sandboxId: string } }) { | ||
const apiKey = request.headers.get('X-API-Key') | ||
const sandboxId = params.sandboxId | ||
|
||
if (!sandboxId) { | ||
return NextResponse.json({ error: 'Missing sandbox ID' }, { status: 400 }) | ||
} | ||
|
||
if (!apiKey) { | ||
return NextResponse.json({ error: 'Missing E2B API Key' }, { status: 400 }) | ||
} | ||
|
||
if (!(await verifySandbox(apiKey, sandboxId))) { | ||
return NextResponse.json({ error: 'Invalid E2B API Key' }, { status: 401 }) | ||
} | ||
|
||
const { data: stream, error } = await supabase | ||
.from('sandbox_streams') | ||
.select('token') | ||
.eq('sandbox_id', sandboxId) | ||
.single() | ||
|
||
if (error) { | ||
return NextResponse.json({ error: `Failed to retrieve stream - ${error.message}` }, { status: 500 }) | ||
} | ||
|
||
if (!stream) { | ||
return NextResponse.json({ error: `Stream not found for sandbox ${sandboxId}` }, { status: 404 }) | ||
} | ||
|
||
return NextResponse.json({ token: stream.token }, { status: 200 }) | ||
} | ||
|
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 was deleted.
Oops, something went wrong.
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,6 +1,13 @@ | ||
import { Sandbox } from 'e2b' | ||
import { type ClassValue, clsx } from 'clsx' | ||
import { twMerge } from 'tailwind-merge' | ||
|
||
export function cn(...inputs: ClassValue[]) { | ||
return twMerge(clsx(inputs)) | ||
} | ||
|
||
// Verify that the sandbox exists and is associated with the API key | ||
export async function verifySandbox(apiKey: string, sandboxId: string) { | ||
const sandboxes = await Sandbox.list({ apiKey }) | ||
return sandboxes.some((sandbox) => sandbox.sandboxId === sandboxId) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.