@@ -44,15 +44,18 @@ describe("managed binary archive utilities", () => {
4444
4545 it ( "kills a process that exceeds its timeout" , async ( ) => {
4646 vi . useFakeTimers ( )
47- const child = createChild ( )
48- mockSpawn . mockReturnValue ( child as unknown as ReturnType < typeof spawn > )
49- const processResult = runProcess ( "tool" , [ ] , 100 )
50- const assertion = expect ( processResult ) . rejects . toThrow ( "tool timed out" )
51-
52- await vi . advanceTimersByTimeAsync ( 100 )
53- await assertion
54- expect ( child . kill ) . toHaveBeenCalledWith ( "SIGKILL" )
55- vi . useRealTimers ( )
47+ try {
48+ const child = createChild ( )
49+ mockSpawn . mockReturnValue ( child as unknown as ReturnType < typeof spawn > )
50+ const processResult = runProcess ( "tool" , [ ] , 100 )
51+ const assertion = expect ( processResult ) . rejects . toThrow ( "tool timed out" )
52+
53+ await vi . advanceTimersByTimeAsync ( 100 )
54+ await assertion
55+ expect ( child . kill ) . toHaveBeenCalledWith ( "SIGKILL" )
56+ } finally {
57+ vi . useRealTimers ( )
58+ }
5659 } )
5760
5861 it ( "extracts tar.gz archives with hardened flags" , async ( ) => {
@@ -112,7 +115,7 @@ describe("managed binary archive utilities", () => {
112115 mockSpawn . mockReturnValueOnce ( listing as unknown as ReturnType < typeof spawn > )
113116 mockSpawn . mockReturnValueOnce ( extraction as unknown as ReturnType < typeof spawn > )
114117 const result = extractSingleFileTarXzArchive ( "/tmp/archive.tar.xz" , "/tmp/output" , "binary" , "Tool" )
115- listing . stdout . write ( "./binary\n" )
118+ listing . stdout . write ( "-rwxr-xr-x user/group 1 2026-01-01 00:00 ./binary\n" )
116119 listing . emit ( "close" , 0 )
117120 await new Promise < void > ( ( resolve ) => setImmediate ( resolve ) )
118121 extraction . emit ( "close" , 0 )
@@ -121,22 +124,58 @@ describe("managed binary archive utilities", () => {
121124 expect ( mockSpawn ) . toHaveBeenNthCalledWith (
122125 2 ,
123126 "tar" ,
124- [ "-xJf" , "/tmp/archive.tar.xz" , "-C" , "/tmp/output" , "./binary" ] ,
127+ [
128+ "-xJf" ,
129+ "/tmp/archive.tar.xz" ,
130+ "-C" ,
131+ "/tmp/output" ,
132+ "--no-same-owner" ,
133+ ...( process . platform === "linux" ? [ "--no-overwrite-dir" ] : [ ] ) ,
134+ "./binary" ,
135+ ] ,
125136 expect . any ( Object ) ,
126137 )
127138 } )
128139
140+ it . each ( [
141+ [ "-rwxr-xr-x user/group 1 2026-01-01 00:00 ./other\n" , "an unexpected filename" ] ,
142+ [
143+ "-rwxr-xr-x user/group 1 2026-01-01 00:00 ./binary\n-rwxr-xr-x user/group 1 2026-01-01 00:00 ./other\n" ,
144+ "multiple entries" ,
145+ ] ,
146+ [ "lrwxrwxrwx user/group 0 2026-01-01 00:00 ./binary\n" , "a non-regular entry" ] ,
147+ ] ) ( "rejects a tar.xz archive with %s" , async ( listingOutput ) => {
148+ const listing = createChild ( )
149+ mockSpawn . mockReturnValue ( listing as unknown as ReturnType < typeof spawn > )
150+ const result = extractSingleFileTarXzArchive ( "/tmp/archive.tar.xz" , "/tmp/output" , "binary" , "Tool" )
151+ listing . stdout . write ( listingOutput )
152+ listing . emit ( "close" , 0 )
153+
154+ await expect ( result ) . rejects . toThrow ( "Tool archive has an unexpected layout" )
155+ } )
156+
129157 it ( "builds a single-entry-validated PowerShell ZIP extraction" , async ( ) => {
130158 const child = createChild ( )
131159 mockSpawn . mockReturnValue ( child as unknown as ReturnType < typeof spawn > )
132- const extraction = extractSingleFileZipArchive ( "C:\\archive.zip" , "C:\\output" , "binary.exe" , "Tool" )
133- child . emit ( "close" , 0 )
134- await extraction
135-
136- const args = mockSpawn . mock . calls [ 0 ] [ 1 ]
137- const script = args [ 3 ]
138- expect ( script ) . toContain ( "$entries.Count -ne 1" )
139- expect ( script ) . not . toContain ( "C:\\archive.zip" )
140- expect ( args . slice ( 4 ) ) . toEqual ( [ "C:\\archive.zip" , path . join ( "C:\\output" , "binary.exe" ) , "binary.exe" , "Tool" ] )
160+ const originalPlatform = Object . getOwnPropertyDescriptor ( process , "platform" )
161+ Object . defineProperty ( process , "platform" , { value : "win32" , configurable : true } )
162+ try {
163+ const extraction = extractSingleFileZipArchive ( "C:\\archive.zip" , "C:\\output" , "binary.exe" , "Tool" )
164+ child . emit ( "close" , 0 )
165+ await extraction
166+
167+ const args = mockSpawn . mock . calls [ 0 ] [ 1 ]
168+ const script = args [ 3 ]
169+ expect ( script ) . toContain ( "$entries.Count -ne 1" )
170+ expect ( script ) . not . toContain ( "C:\\archive.zip" )
171+ expect ( args . slice ( 4 ) ) . toEqual ( [
172+ "C:\\archive.zip" ,
173+ path . join ( "C:\\output" , "binary.exe" ) ,
174+ "binary.exe" ,
175+ "Tool" ,
176+ ] )
177+ } finally {
178+ if ( originalPlatform ) Object . defineProperty ( process , "platform" , originalPlatform )
179+ }
141180 } )
142181} )
0 commit comments