@@ -41,7 +41,6 @@ import fs from 'fs';
4141import { promises as fsp , Stats } from 'fs' ;
4242
4343import * as sinon from 'sinon' ;
44- import { FileExceptionMessages , RequestError } from '../src/file.js' ;
4544
4645describe ( 'Transfer Manager' , ( ) => {
4746 const BUCKET_NAME = 'test-bucket' ;
@@ -219,10 +218,7 @@ describe('Transfer Manager', () => {
219218 it ( 'sets the destination correctly when provided a prefix' , async ( ) => {
220219 const prefix = 'test-prefix' ;
221220 const filename = 'first.txt' ;
222- const expectedDestination = path . resolve (
223- process . cwd ( ) ,
224- path . join ( prefix , filename )
225- ) ;
221+ const expectedDestination = path . normalize ( `${ prefix } /${ filename } ` ) ;
226222
227223 const file = new File ( bucket , filename ) ;
228224 sandbox . stub ( file , 'download' ) . callsFake ( options => {
@@ -237,7 +233,7 @@ describe('Transfer Manager', () => {
237233 it ( 'sets the destination correctly when provided a strip prefix' , async ( ) => {
238234 const stripPrefix = 'should-be-removed/' ;
239235 const filename = 'should-be-removed/first.txt' ;
240- const expectedDestination = path . resolve ( process . cwd ( ) , 'first.txt' ) ;
236+ const expectedDestination = 'first.txt' ;
241237
242238 const file = new File ( bucket , filename ) ;
243239 sandbox . stub ( file , 'download' ) . callsFake ( options => {
@@ -267,10 +263,8 @@ describe('Transfer Manager', () => {
267263 destination : 'test-destination' ,
268264 } ;
269265 const filename = 'first.txt' ;
270- const expectedDestination = path . resolve (
271- process . cwd ( ) ,
272- passthroughOptions . destination ,
273- filename
266+ const expectedDestination = path . normalize (
267+ `${ passthroughOptions . destination } /${ filename } `
274268 ) ;
275269 const download = ( optionsOrCb ?: DownloadOptions | DownloadCallback ) => {
276270 if ( typeof optionsOrCb === 'function' ) {
@@ -286,57 +280,6 @@ describe('Transfer Manager', () => {
286280 await transferManager . downloadManyFiles ( [ file ] , { passthroughOptions} ) ;
287281 } ) ;
288282
289- it ( 'should throws an error for absolute file names' , async ( ) => {
290- const expectedErr = new RequestError (
291- FileExceptionMessages . ABSOLUTE_FILE_NAME
292- ) ;
293- const maliciousFilename = '/etc/passwd' ;
294- const file = new File ( bucket , maliciousFilename ) ;
295-
296- await assert . rejects (
297- transferManager . downloadManyFiles ( [ file ] ) ,
298- expectedErr
299- ) ;
300- } ) ;
301-
302- it ( 'should throw an error for path traversal in destination' , async ( ) => {
303- const expectedErr = new RequestError (
304- FileExceptionMessages . TRAVERSAL_OUTSIDE_BASE_DESTINATION
305- ) ;
306- const passthroughOptions = {
307- destination : '../traversal-destination' ,
308- } ;
309- const file = new File ( bucket , 'first.txt' ) ;
310- await assert . rejects (
311- transferManager . downloadManyFiles ( [ file ] , { passthroughOptions} ) ,
312- expectedErr
313- ) ;
314- } ) ;
315-
316- it ( 'should throw an error for path traversal in file name' , async ( ) => {
317- const expectedErr = new RequestError (
318- FileExceptionMessages . TRAVERSAL_OUTSIDE_BASE
319- ) ;
320- const file = new File ( bucket , '../traversal-filename.txt' ) ;
321- await assert . rejects (
322- transferManager . downloadManyFiles ( [ file ] ) ,
323- expectedErr
324- ) ;
325- } ) ;
326-
327- it ( 'should throw an error for path traversal using prefix' , async ( ) => {
328- const expectedErr = new RequestError (
329- FileExceptionMessages . TRAVERSAL_OUTSIDE_BASE
330- ) ;
331- const file = new File ( bucket , 'first.txt' ) ;
332- await assert . rejects (
333- transferManager . downloadManyFiles ( [ file ] , {
334- prefix : '../traversal-prefix' ,
335- } ) ,
336- expectedErr
337- ) ;
338- } ) ;
339-
340283 it ( 'does not download files that already exist locally when skipIfExists is true' , async ( ) => {
341284 const firstFile = new File ( bucket , 'first.txt' ) ;
342285 sandbox . stub ( firstFile , 'download' ) . callsFake ( options => {
@@ -358,16 +301,14 @@ describe('Transfer Manager', () => {
358301 await transferManager . downloadManyFiles ( files , options ) ;
359302 } ) ;
360303
361- it ( 'sets the destination to CWD when prefix, strip prefix and passthroughOptions.destination are not provided' , async ( ) => {
304+ it ( 'does not set the destination when prefix, strip prefix and passthroughOptions.destination are not provided' , async ( ) => {
362305 const options = { } ;
363306 const filename = 'first.txt' ;
364- const expectedDestination = path . resolve ( process . cwd ( ) , filename ) ;
365-
366307 const download = ( optionsOrCb ?: DownloadOptions | DownloadCallback ) => {
367308 if ( typeof optionsOrCb === 'function' ) {
368309 optionsOrCb ( null , Buffer . alloc ( 0 ) ) ;
369310 } else if ( optionsOrCb ) {
370- assert . strictEqual ( optionsOrCb . destination , expectedDestination ) ;
311+ assert . strictEqual ( optionsOrCb . destination , undefined ) ;
371312 }
372313 return Promise . resolve ( [ Buffer . alloc ( 0 ) ] ) as Promise < DownloadResponse > ;
373314 } ;
@@ -380,16 +321,10 @@ describe('Transfer Manager', () => {
380321 it ( 'should recursively create directory and write file contents if destination path is nested' , async ( ) => {
381322 const prefix = 'text-prefix' ;
382323 const folder = 'nestedFolder/' ;
383- const filename = 'first.txt' ;
384- const filesOrFolder = [ folder , path . join ( folder , filename ) ] ;
385- const dirNameWithPrefix = path . join ( prefix , folder ) ;
386- const normalizedDir = path . resolve ( process . cwd ( ) , dirNameWithPrefix ) ;
387- const expectedDir = normalizedDir + path . sep ;
388- const expectedFilePath = path . resolve (
389- process . cwd ( ) ,
390- path . join ( prefix , folder , filename )
391- ) ;
392-
324+ const file = 'first.txt' ;
325+ const filesOrFolder = [ folder , path . join ( folder , file ) ] ;
326+ const expectedFilePath = path . join ( prefix , folder , file ) ;
327+ const expectedDir = path . join ( prefix , folder ) ;
393328 const mkdirSpy = sandbox . spy ( fsp , 'mkdir' ) ;
394329 const download = ( optionsOrCb ?: DownloadOptions | DownloadCallback ) => {
395330 if ( typeof optionsOrCb === 'function' ) {
@@ -400,21 +335,16 @@ describe('Transfer Manager', () => {
400335 return Promise . resolve ( [ Buffer . alloc ( 0 ) ] ) as Promise < DownloadResponse > ;
401336 } ;
402337
403- sandbox . stub ( bucket , 'file' ) . callsFake ( objectName => {
404- const file = new File ( bucket , objectName ) ;
405- if ( objectName === path . join ( folder , filename ) ) {
406- file . download = download ;
407- } else {
408- file . download = ( ) =>
409- Promise . resolve ( [ Buffer . alloc ( 0 ) ] ) as Promise < DownloadResponse > ;
410- }
338+ sandbox . stub ( bucket , 'file' ) . callsFake ( filename => {
339+ const file = new File ( bucket , filename ) ;
340+ file . download = download ;
411341 return file ;
412342 } ) ;
413343 await transferManager . downloadManyFiles ( filesOrFolder , {
414344 prefix : prefix ,
415345 } ) ;
416346 assert . strictEqual (
417- mkdirSpy . calledWith ( expectedDir , {
347+ mkdirSpy . calledOnceWith ( expectedDir , {
418348 recursive : true ,
419349 } ) ,
420350 true
0 commit comments