@@ -44,6 +44,15 @@ func isSymlinkInsideRoot(name, link, root string) bool {
4444 return ! strings .HasPrefix (rel , ".." ) && ! strings .HasPrefix (abs , string (os .PathSeparator ))
4545}
4646
47+ func isPathInsideRoot (path , root string ) bool {
48+ cleanPath := filepath .Clean (path )
49+ rel , err := filepath .Rel (root , cleanPath )
50+ if err != nil {
51+ return false
52+ }
53+ return ! strings .HasPrefix (rel , ".." ) && ! strings .HasPrefix (cleanPath , string (os .PathSeparator ))
54+ }
55+
4756// createSteamCMDDirectory creates the SteamCMD directory.
4857func createSteamCMDDirectory (steamCMDDir string ) error {
4958 if err := os .MkdirAll (steamCMDDir , os .ModePerm ); err != nil {
@@ -183,6 +192,9 @@ func untar(dest string, r io.Reader) error {
183192 return fmt .Errorf ("failed to create directory %s: %v" , target , err )
184193 }
185194 case tar .TypeReg :
195+ if ! isPathInsideRoot (target , dest ) {
196+ return fmt .Errorf ("invalid file path attempts to write outside root directory: %s" , target )
197+ }
186198 outFile , err := os .OpenFile (target , os .O_CREATE | os .O_WRONLY , os .FileMode (header .Mode ))
187199 if err != nil {
188200 return fmt .Errorf ("failed to create file %s: %v" , target , err )
@@ -230,6 +242,9 @@ func Unzip(zipReader io.ReaderAt, size int64, dest string) error {
230242 fpath := filepath .Join (dest , f .Name )
231243
232244 // Ensure the file path is within the destination directory
245+ if ! isPathInsideRoot (fpath , dest ) {
246+ return fmt .Errorf ("invalid file path attempts to write outside root directory: %s" , fpath )
247+ }
233248 relPath , err := filepath .Rel (dest , fpath )
234249 if err != nil || strings .HasPrefix (relPath , ".." ) || strings .HasPrefix (relPath , string (os .PathSeparator )) {
235250 return fmt .Errorf ("invalid file path: %s" , fpath )
0 commit comments