File tree Expand file tree Collapse file tree
apps/frontend/src/app/(app)/api/uploads/[[...path]] Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import { NextRequest , NextResponse } from 'next/server' ;
22import { createReadStream , statSync } from 'fs' ;
3+ import { resolve , sep } from 'path' ;
34// @ts -ignore
45import mime from 'mime' ;
56async function * nodeStreamToIterator ( stream : any ) {
@@ -28,8 +29,13 @@ export const GET = async (
2829 }
2930) => {
3031 const { path } = await context . params ;
31- const filePath =
32- process . env . UPLOAD_DIRECTORY + '/' + ( path ?? [ ] ) . join ( '/' ) ;
32+ const base = resolve ( process . env . UPLOAD_DIRECTORY ! ) ;
33+ const filePath = resolve ( base , ( path ?? [ ] ) . join ( '/' ) ) ;
34+ // Confine reads to UPLOAD_DIRECTORY. resolve() collapses any `..` segments
35+ // (including URL-decoded ones), so this blocks every path-traversal variant.
36+ if ( filePath !== base && ! filePath . startsWith ( base + sep ) ) {
37+ return new NextResponse ( 'Not found' , { status : 404 } ) ;
38+ }
3339 const response = createReadStream ( filePath ) ;
3440 const fileStats = statSync ( filePath ) ;
3541 const contentType = mime . getType ( filePath ) || 'application/octet-stream' ;
You can’t perform that action at this time.
0 commit comments