@@ -129,31 +129,43 @@ func (svc *Service) UploadBatchSpecWorkspaceFiles(ctx context.Context, workingDi
129129}
130130
131131func getFilePaths (workingDir , filePath string ) ([]string , error ) {
132+ root , err := os .OpenRoot (workingDir )
133+ if err != nil {
134+ return nil , errors .Wrap (err , "opening working directory" )
135+ }
136+ defer root .Close ()
137+ return getFilePathsInRoot (root , filePath )
138+ }
139+
140+ func getFilePathsInRoot (root * os.Root , filePath string ) ([]string , error ) {
141+ // Clean the path to resolve any ".." segments before accessing the root.
142+ filePath = filepath .Clean (filePath )
143+
132144 var filePaths []string
133- actualFilePath := filepath .Join (workingDir , filePath )
134- info , err := os .Stat (actualFilePath )
145+ info , err := root .Stat (filePath )
135146 if err != nil {
136147 return nil , err
137148 }
138149
139150 if info .IsDir () {
140- dir , err := os .ReadDir (actualFilePath )
151+ dir , err := root .Open (filePath )
152+ if err != nil {
153+ return nil , err
154+ }
155+ entries , err := dir .ReadDir (- 1 )
156+ dir .Close ()
141157 if err != nil {
142158 return nil , err
143159 }
144- for _ , dirEntry := range dir {
145- paths , err := getFilePaths ( workingDir , filepath .Join (filePath , dirEntry .Name ()))
160+ for _ , dirEntry := range entries {
161+ paths , err := getFilePathsInRoot ( root , filepath .Join (filePath , dirEntry .Name ()))
146162 if err != nil {
147163 return nil , err
148164 }
149165 filePaths = append (filePaths , paths ... )
150166 }
151167 } else {
152- relPath , err := filepath .Rel (workingDir , actualFilePath )
153- if err != nil {
154- return nil , err
155- }
156- filePaths = append (filePaths , relPath )
168+ filePaths = append (filePaths , filePath )
157169 }
158170 return filePaths , nil
159171}
@@ -201,7 +213,13 @@ func (svc *Service) uploadFile(ctx context.Context, workingDir, filePath, batchS
201213const maxFileSize = 10 << 20 // 10MB
202214
203215func createFormFile (w * multipart.Writer , workingDir string , mountPath string ) error {
204- f , err := os .Open (filepath .Join (workingDir , mountPath ))
216+ root , err := os .OpenRoot (workingDir )
217+ if err != nil {
218+ return errors .Wrap (err , "opening working directory" )
219+ }
220+ defer root .Close ()
221+
222+ f , err := root .Open (mountPath )
205223 if err != nil {
206224 return err
207225 }
0 commit comments