1- import { afterEach , describe , expect , it , type TestContext } from "vitest" ;
1+ import { afterEach , describe , expect , it , vi , type TestContext } from "vitest" ;
22import { Hono } from "hono" ;
33import {
44 existsSync ,
@@ -17,6 +17,7 @@ import type { StudioApiAdapter } from "../types";
1717
1818const tempDirs : string [ ] = [ ] ;
1919afterEach ( ( ) => {
20+ vi . restoreAllMocks ( ) ;
2021 for ( const dir of tempDirs . splice ( 0 ) ) rmSync ( dir , { recursive : true , force : true } ) ;
2122} ) ;
2223
@@ -201,3 +202,61 @@ describe("file route containment", () => {
201202 ) ;
202203 } ) ;
203204} ) ;
205+
206+ describe ( "upload collision races" , ( ) => {
207+ function raceDuringRead ( filename : string , collide : ( ) => void ) {
208+ const file = new File ( [ "new upload" ] , filename ) ;
209+ const read = file . arrayBuffer . bind ( file ) ;
210+ vi . spyOn ( file , "arrayBuffer" ) . mockImplementation ( async ( ) => {
211+ collide ( ) ;
212+ return read ( ) ;
213+ } ) ;
214+ const form = new FormData ( ) ;
215+ form . append ( "files" , file ) ;
216+ vi . spyOn ( Request . prototype , "formData" ) . mockResolvedValue ( form ) ;
217+ }
218+
219+ it ( "preserves an upload that appears while the body is read" , async ( ) => {
220+ const { app, project } = fixture ( ) ;
221+ raceDuringRead ( "upload.txt" , ( ) => writeFileSync ( join ( project , "upload.txt" ) , "other upload" ) ) ;
222+ const response = await upload ( app ) ;
223+ expect ( response . status ) . toBe ( 201 ) ;
224+ expect ( await response . json ( ) ) . toMatchObject ( { files : [ "upload (2).txt" ] } ) ;
225+ expect ( readFileSync ( join ( project , "upload.txt" ) , "utf8" ) ) . toBe ( "other upload" ) ;
226+ expect ( readFileSync ( join ( project , "upload (2).txt" ) , "utf8" ) ) . toBe ( "new upload" ) ;
227+ } ) ;
228+
229+ it . each ( [ ".gitignore" , "archive.tar.txt" ] ) (
230+ "retries suffix races without changing extension rules: %s" ,
231+ async ( name ) => {
232+ const { app, project } = fixture ( ) ;
233+ const dot = name . indexOf ( "." , name . startsWith ( "." ) ? 1 : 0 ) ;
234+ const base = dot > 0 ? name . slice ( 0 , dot ) : name ;
235+ const ext = dot > 0 ? name . slice ( dot ) : "" ;
236+ writeFileSync ( join ( project , name ) , "original" ) ;
237+ raceDuringRead ( name , ( ) => {
238+ writeFileSync ( join ( project , `${ base } (2)${ ext } ` ) , "second" ) ;
239+ writeFileSync ( join ( project , `${ base } (3)${ ext } ` ) , "third" ) ;
240+ } ) ;
241+ const response = await upload ( app , "" , name ) ;
242+ expect ( response . status ) . toBe ( 201 ) ;
243+ expect ( await response . json ( ) ) . toMatchObject ( { files : [ `${ base } (4)${ ext } ` ] } ) ;
244+ expect ( readFileSync ( join ( project , `${ base } (2)${ ext } ` ) , "utf8" ) ) . toBe ( "second" ) ;
245+ expect ( readFileSync ( join ( project , `${ base } (3)${ ext } ` ) , "utf8" ) ) . toBe ( "third" ) ;
246+ } ,
247+ ) ;
248+
249+ it ( "does not write through a symlink planted during the upload read" , async ( context ) => {
250+ const { app, project, outside } = fixture ( ) ;
251+ // Establish symlink support before entering the mocked asynchronous read.
252+ const probe = join ( project , "probe-link" ) ;
253+ linkOrSkip ( context , join ( outside , "secret.txt" ) , probe , "file" ) ;
254+ rmSync ( probe ) ;
255+ raceDuringRead ( "upload.txt" , ( ) =>
256+ symlinkSync ( join ( outside , "secret.txt" ) , join ( project , "upload.txt" ) ) ,
257+ ) ;
258+ const response = await upload ( app ) ;
259+ expect ( await response . json ( ) ) . toMatchObject ( { files : [ ] } ) ;
260+ expect ( readFileSync ( join ( outside , "secret.txt" ) , "utf8" ) ) . toBe ( "outside secret" ) ;
261+ } ) ;
262+ } ) ;
0 commit comments