11import  path  from  'path' ; 
22import  {  promisify  }  from  'util' ; 
3- import  {  exec  as  execCallback  }  from  'node:child_process' ; 
3+ import  {  execFile  as  execFileCallback  }  from  'node:child_process' ; 
44import  {  LastSaveDTO ,  StorageUsedDTO  }  from  '~/types' ; 
5+ import  repositoryNameCheck  from  '~/helpers/functions/repositoryNameCheck' ; 
56
6- const  exec  =  promisify ( execCallback ) ; 
7+ const  execFile  =  promisify ( execFileCallback ) ; 
78const  shellsDirectory  =  path . join ( process . cwd ( ) ,  '/helpers/shells' ) ; 
89
910// This is to prevent the cronjob from being executed multiple times 
1011let  isLastSaveListRunning  =  false ; 
1112let  isStorageUsedRunning  =  false ; 
1213
14+ function  isValidSshKey ( key : string ) : boolean  { 
15+   return  / ^ s s h - ( r s a | e d 2 5 5 1 9 | e d 2 5 5 1 9 - s k )   [ A - Z a - z 0 - 9 + / = ] + ( \s .+ ) ? $ / . test ( key . trim ( ) ) ; 
16+ } 
17+ 
1318export  const  ShellService  =  { 
1419  getLastSaveList : async  ( ) : Promise < LastSaveDTO [ ] >  =>  { 
1520    if  ( isLastSaveListRunning )  { 
@@ -19,7 +24,7 @@ export const ShellService = {
1924    } 
2025
2126    try  { 
22-       const  {  stdout }  =  await  exec ( `${ shellsDirectory }  ) ; 
27+       const  {  stdout }  =  await  execFile ( `${ shellsDirectory }  ) ; 
2328      return  JSON . parse ( stdout  ||  '[]' ) ; 
2429    }  finally  { 
2530      isLastSaveListRunning  =  false ; 
@@ -33,15 +38,15 @@ export const ShellService = {
3338      isStorageUsedRunning  =  true ; 
3439    } 
3540    try  { 
36-       const  {  stdout }  =  await  exec ( `${ shellsDirectory }  ) ; 
41+       const  {  stdout }  =  await  execFile ( `${ shellsDirectory }  ) ; 
3742      return  JSON . parse ( stdout  ||  '[]' ) ; 
3843    }  finally  { 
3944      isStorageUsedRunning  =  false ; 
4045    } 
4146  } , 
4247
4348  deleteRepo : async  ( repositoryName : string )  =>  { 
44-     const  {  stdout,  stderr }  =  await  exec ( `${ shellsDirectory }   ${ repositoryName } `  ) ; 
49+     const  {  stdout,  stderr }  =  await  execFile ( `${ shellsDirectory } `  ,   [ repositoryName ] ) ; 
4550    return  {  stdout,  stderr } ; 
4651  } , 
4752
@@ -51,9 +56,19 @@ export const ShellService = {
5156    storageSize : number , 
5257    appendOnlyMode : boolean 
5358  )  =>  { 
54-     const  {  stdout,  stderr }  =  await  exec ( 
55-       `${ shellsDirectory } ${ repositoryName } ${ sshPublicKey } ${ storageSize } ${ appendOnlyMode }  
56-     ) ; 
59+     if  ( ! isValidSshKey ( sshPublicKey ) )  { 
60+       throw  new  Error ( 'Invalid SSH key format' ) ; 
61+     } 
62+     if  ( ! repositoryNameCheck ( repositoryName ) )  { 
63+       throw  new  Error ( 'Invalid repository name format' ) ; 
64+     } 
65+ 
66+     const  {  stdout,  stderr }  =  await  execFile ( `${ shellsDirectory }  ,  [ 
67+       repositoryName , 
68+       sshPublicKey , 
69+       storageSize . toString ( ) , 
70+       appendOnlyMode . toString ( ) , 
71+     ] ) ; 
5772    return  {  stdout,  stderr } ; 
5873  } , 
5974
@@ -62,9 +77,15 @@ export const ShellService = {
6277    storageSize : number , 
6378    appendOnlyMode : boolean 
6479  ) : Promise < {  stdout ?: string ;  stderr ?: string  } >  =>  { 
65-     const  {  stdout,  stderr }  =  await  exec ( 
66-       `${ shellsDirectory } ${ sshPublicKey } ${ storageSize } ${ appendOnlyMode }  
67-     ) ; 
80+     if  ( ! isValidSshKey ( sshPublicKey ) )  { 
81+       throw  new  Error ( 'Invalid SSH key format' ) ; 
82+     } 
83+ 
84+     const  {  stdout,  stderr }  =  await  execFile ( `${ shellsDirectory }  ,  [ 
85+       sshPublicKey , 
86+       storageSize . toString ( ) , 
87+       appendOnlyMode . toString ( ) , 
88+     ] ) ; 
6889    return  {  stdout,  stderr } ; 
6990  } , 
7091} ; 
0 commit comments