@@ -5,6 +5,7 @@ import path from 'path'
55export const maxDuration = 60
66export const runtime = 'nodejs'
77export const dynamic = 'force-dynamic'
8+ export const fetchCache = 'force-no-store'
89
910/**
1011 * GET /api/sandbox/[sbxId]/files/content?path=/path/to/file
@@ -45,7 +46,11 @@ export async function GET(
4546
4647 // Sanitize path to prevent path traversal attacks
4748 const userDir = '/home/user'
48- const normalizedPath = path . normalize ( path . join ( userDir , filePath ) )
49+
50+ // If path already starts with /home/user, use it as-is; otherwise join with userDir
51+ const normalizedPath = filePath . startsWith ( userDir )
52+ ? path . normalize ( filePath )
53+ : path . normalize ( path . join ( userDir , filePath ) )
4954
5055 // Verify the normalized path is still within the allowed directory
5156 if ( ! normalizedPath . startsWith ( userDir + '/' ) && normalizedPath !== userDir ) {
@@ -56,7 +61,7 @@ export async function GET(
5661 }
5762
5863 // Use E2B SDK's files.read() method for robust file reading
59- const relativePath = normalizedPath . substring ( '/home/user/' . length )
64+ const relativePath = normalizedPath === userDir ? '' : normalizedPath . substring ( userDir . length + 1 )
6065 const content = await sbx . files . read ( relativePath )
6166
6267 return new Response (
@@ -116,7 +121,10 @@ export async function POST(
116121
117122 // Sanitize path to prevent path traversal attacks
118123 const userDir = '/home/user'
119- const normalizedPath = path . normalize ( path . join ( userDir , filePath ) )
124+
125+ const normalizedPath = filePath . startsWith ( userDir )
126+ ? path . normalize ( filePath )
127+ : path . normalize ( path . join ( userDir , filePath ) )
120128
121129 // Verify the normalized path is still within the allowed directory
122130 if ( ! normalizedPath . startsWith ( userDir + '/' ) && normalizedPath !== userDir ) {
@@ -127,7 +135,7 @@ export async function POST(
127135 }
128136
129137 // E2B files.write expects path relative to /home/user
130- const relativePath = normalizedPath . substring ( userDir . length + 1 )
138+ const relativePath = normalizedPath === userDir ? '' : normalizedPath . substring ( userDir . length + 1 )
131139 await sbx . files . write ( relativePath , content )
132140
133141 return new Response (
0 commit comments