Skip to content

Commit c9910a9

Browse files
Fix sandbox file path duplication and suppress cache warnings
1 parent 23fba77 commit c9910a9

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

app/api/sandbox/[sbxId]/files/content/route.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import path from 'path'
55
export const maxDuration = 60
66
export const runtime = 'nodejs'
77
export 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(

app/api/sandbox/[sbxId]/files/route.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import { FileSystemNode } from '@/components/file-tree'
44
export const maxDuration = 60
55
export const runtime = 'nodejs'
66
export const dynamic = 'force-dynamic'
7+
export const fetchCache = 'force-no-store'
78

89
/**
910
* GET /api/sandbox/[sbxId]/files
1011
* Fetches the file tree from an E2B sandbox
1112
*/
1213
export async function GET(
13-
req: Request,
14+
_req: Request,
1415
{ params }: { params: { sbxId: string } }
1516
) {
1617
try {

0 commit comments

Comments
 (0)