@@ -22,6 +22,8 @@ import (
2222 "encoding/hex"
2323 "encoding/json"
2424 "fmt"
25+ "io"
26+ "io/fs"
2527 "os"
2628 "path/filepath"
2729 "sort"
@@ -174,20 +176,44 @@ func deployContentOfSource(ctx context.Context, deployingToMgmtCluster bool, des
174176
175177func readFiles (dir string ) (map [string ]string , error ) {
176178 files := make (map [string ]string )
177- err := filepath .Walk (dir , func (path string , info os.FileInfo , err error ) error {
179+
180+ // 1. Open the directory as a Root handle
181+ root , err := os .OpenRoot (dir )
182+ if err != nil {
183+ return nil , err
184+ }
185+ defer root .Close ()
186+
187+ // 2. WalkDir is faster than Walk as it doesn't Lstat every file unnecessarily
188+ err = filepath .WalkDir (dir , func (path string , d fs.DirEntry , err error ) error {
178189 if err != nil {
179190 return err
180191 }
181192
182- if ! info .IsDir () {
183- content , err := os .ReadFile (path )
193+ if ! d .IsDir () {
194+ // 3. Get the path relative to the root directory
195+ rel , err := filepath .Rel (dir , path )
184196 if err != nil {
185197 return err
186198 }
187- files [filepath .Base (path )] = string (content )
199+
200+ // 4. Open the file via the Root handle to prevent symlink traversal
201+ f , err := root .Open (rel )
202+ if err != nil {
203+ return err
204+ }
205+ defer f .Close ()
206+
207+ content , err := io .ReadAll (f )
208+ if err != nil {
209+ return err
210+ }
211+
212+ files [d .Name ()] = string (content )
188213 }
189214 return nil
190215 })
216+
191217 return files , err
192218}
193219
@@ -1092,12 +1118,12 @@ func getDeployedGroupVersionKinds(clusterSummary *configv1beta1.ClusterSummary,
10921118 gvks := make ([]schema.GroupVersionKind , 0 )
10931119 // For backward compatible we still look at this field.
10941120 // New code set only FeatureDeploymentInfo
1095- fs := getFeatureSummaryForFeatureID (clusterSummary , featureID )
1096- if fs != nil {
1121+ featureSummary := getFeatureSummaryForFeatureID (clusterSummary , featureID )
1122+ if featureSummary != nil {
10971123 //nolint:staticcheck // using for backward compatibility
1098- for j := range fs .DeployedGroupVersionKind {
1124+ for j := range featureSummary .DeployedGroupVersionKind {
10991125 //nolint:staticcheck // using for backward compatibility
1100- gvk , _ := schema .ParseKindArg (fs .DeployedGroupVersionKind [j ])
1126+ gvk , _ := schema .ParseKindArg (featureSummary .DeployedGroupVersionKind [j ])
11011127 gvks = append (gvks , * gvk )
11021128 }
11031129 }
0 commit comments