@@ -79,8 +79,15 @@ func importSyncProviderWebDAV(c *gin.Context) {
7979 return
8080 }
8181
82- tmp := filepath .Join (importDir , f .Filename )
83- if err = os .WriteFile (tmp , data , 0644 ); err != nil {
82+ writePath := filepath .Join (importDir , f .Filename )
83+ if ! util .IsSubPath (importDir , writePath ) {
84+ logging .LogErrorf ("import path [%s] is not sub path of import dir [%s]" , writePath , importDir )
85+ ret .Code = - 1
86+ ret .Msg = "import path is not sub path of import dir"
87+ return
88+ }
89+
90+ if err = os .WriteFile (writePath , data , 0644 ); err != nil {
8491 logging .LogErrorf ("import WebDAV provider failed: %s" , err )
8592 ret .Code = - 1
8693 ret .Msg = err .Error ()
@@ -89,15 +96,15 @@ func importSyncProviderWebDAV(c *gin.Context) {
8996
9097 tmpDir := filepath .Join (importDir , "webdav" )
9198 os .RemoveAll (tmpDir )
92- if strings .HasSuffix (strings .ToLower (tmp ), ".zip" ) {
93- if err = gulu .Zip .Unzip (tmp , tmpDir ); err != nil {
99+ if strings .HasSuffix (strings .ToLower (writePath ), ".zip" ) {
100+ if err = gulu .Zip .Unzip (writePath , tmpDir ); err != nil {
94101 logging .LogErrorf ("import WebDAV provider failed: %s" , err )
95102 ret .Code = - 1
96103 ret .Msg = err .Error ()
97104 return
98105 }
99- } else if strings .HasSuffix (strings .ToLower (tmp ), ".json" ) {
100- if err = gulu .File .CopyFile (tmp , filepath .Join (tmpDir , f .Filename )); err != nil {
106+ } else if strings .HasSuffix (strings .ToLower (writePath ), ".json" ) {
107+ if err = gulu .File .CopyFile (writePath , filepath .Join (tmpDir , f .Filename )); err != nil {
101108 logging .LogErrorf ("import WebDAV provider failed: %s" , err )
102109 ret .Code = - 1
103110 ret .Msg = err .Error ()
@@ -124,8 +131,8 @@ func importSyncProviderWebDAV(c *gin.Context) {
124131 return
125132 }
126133
127- tmp = filepath .Join (tmpDir , entries [0 ].Name ())
128- data , err = os .ReadFile (tmp )
134+ writePath = filepath .Join (tmpDir , entries [0 ].Name ())
135+ data , err = os .ReadFile (writePath )
129136 if err != nil {
130137 logging .LogErrorf ("import WebDAV provider failed: %s" , err )
131138 ret .Code = - 1
@@ -265,8 +272,15 @@ func importSyncProviderS3(c *gin.Context) {
265272 return
266273 }
267274
268- tmp := filepath .Join (importDir , f .Filename )
269- if err = os .WriteFile (tmp , data , 0644 ); err != nil {
275+ writePath := filepath .Join (importDir , f .Filename )
276+ if ! util .IsSubPath (importDir , writePath ) {
277+ logging .LogErrorf ("import path [%s] is not sub path of import dir [%s]" , writePath , importDir )
278+ ret .Code = - 1
279+ ret .Msg = "import path is not sub path of import dir"
280+ return
281+ }
282+
283+ if err = os .WriteFile (writePath , data , 0644 ); err != nil {
270284 logging .LogErrorf ("import S3 provider failed: %s" , err )
271285 ret .Code = - 1
272286 ret .Msg = err .Error ()
@@ -275,15 +289,15 @@ func importSyncProviderS3(c *gin.Context) {
275289
276290 tmpDir := filepath .Join (importDir , "s3" )
277291 os .RemoveAll (tmpDir )
278- if strings .HasSuffix (strings .ToLower (tmp ), ".zip" ) {
279- if err = gulu .Zip .Unzip (tmp , tmpDir ); err != nil {
292+ if strings .HasSuffix (strings .ToLower (writePath ), ".zip" ) {
293+ if err = gulu .Zip .Unzip (writePath , tmpDir ); err != nil {
280294 logging .LogErrorf ("import S3 provider failed: %s" , err )
281295 ret .Code = - 1
282296 ret .Msg = err .Error ()
283297 return
284298 }
285- } else if strings .HasSuffix (strings .ToLower (tmp ), ".json" ) {
286- if err = gulu .File .CopyFile (tmp , filepath .Join (tmpDir , f .Filename )); err != nil {
299+ } else if strings .HasSuffix (strings .ToLower (writePath ), ".json" ) {
300+ if err = gulu .File .CopyFile (writePath , filepath .Join (tmpDir , f .Filename )); err != nil {
287301 logging .LogErrorf ("import S3 provider failed: %s" , err )
288302 ret .Code = - 1
289303 ret .Msg = err .Error ()
@@ -310,8 +324,8 @@ func importSyncProviderS3(c *gin.Context) {
310324 return
311325 }
312326
313- tmp = filepath .Join (tmpDir , entries [0 ].Name ())
314- data , err = os .ReadFile (tmp )
327+ writePath = filepath .Join (tmpDir , entries [0 ].Name ())
328+ data , err = os .ReadFile (writePath )
315329 if err != nil {
316330 logging .LogErrorf ("import S3 provider failed: %s" , err )
317331 ret .Code = - 1
0 commit comments