@@ -20,11 +20,11 @@ import (
2020 "context"
2121 "fmt"
2222 "os"
23- "path"
2423 "path/filepath"
2524 "strings"
2625
2726 "github.com/Masterminds/semver/v3"
27+ securejoin "github.com/cyphar/filepath-securejoin"
2828 "golang.org/x/sync/errgroup"
2929 helmchart "helm.sh/helm/v3/pkg/chart"
3030 "helm.sh/helm/v3/pkg/chart/loader"
@@ -39,32 +39,31 @@ type DependencyWithRepository struct {
3939
4040// DependencyManager manages dependencies for helm charts
4141type DependencyManager struct {
42- Chart * helmchart. Chart
42+ BaseDir string
4343 ChartPath string
44+ Chart * helmchart.Chart
4445 Dependencies []* DependencyWithRepository
4546}
4647
4748// Build compiles and builds the chart dependencies
4849func (dm * DependencyManager ) Build () error {
49- if dm .Dependencies == nil {
50+ if len ( dm .Dependencies ) == 0 {
5051 return nil
5152 }
5253
5354 ctx := context .Background ()
5455 errs , ctx := errgroup .WithContext (ctx )
5556
5657 for _ , item := range dm .Dependencies {
57- dep := item .Dependency
58- chartRepo := item .Repo
5958 errs .Go (func () error {
6059 var (
6160 ch * helmchart.Chart
6261 err error
6362 )
64- if strings .HasPrefix (dep .Repository , "file://" ) {
65- ch , err = chartForLocalDependency (dep , dm .ChartPath )
63+ if strings .HasPrefix (item . Dependency .Repository , "file://" ) {
64+ ch , err = chartForLocalDependency (item . Dependency , dm . BaseDir , dm .ChartPath )
6665 } else {
67- ch , err = chartForRemoteDependency (dep , chartRepo )
66+ ch , err = chartForRemoteDependency (item . Dependency , item . Repo )
6867 }
6968 if err != nil {
7069 return err
@@ -77,8 +76,9 @@ func (dm *DependencyManager) Build() error {
7776 return errs .Wait ()
7877}
7978
80- func chartForLocalDependency (dep * helmchart.Dependency , cp string ) (* helmchart.Chart , error ) {
81- origPath , err := filepath .Abs (path .Join (cp , strings .TrimPrefix (dep .Repository , "file://" )))
79+ func chartForLocalDependency (dep * helmchart.Dependency , baseDir , chartPath string ) (* helmchart.Chart , error ) {
80+ origPath , err := securejoin .SecureJoin (baseDir ,
81+ filepath .Join (strings .TrimPrefix (chartPath , baseDir ), strings .TrimPrefix (dep .Repository , "file://" )))
8282 if err != nil {
8383 return nil , err
8484 }
@@ -114,20 +114,19 @@ func chartForLocalDependency(dep *helmchart.Dependency, cp string) (*helmchart.C
114114 return ch , nil
115115}
116116
117- func chartForRemoteDependency (dep * helmchart.Dependency , chartrepo * ChartRepository ) (* helmchart.Chart , error ) {
118- if chartrepo == nil {
119- err := fmt .Errorf ("chartrepo should not be nil" )
120- return nil , err
117+ func chartForRemoteDependency (dep * helmchart.Dependency , chartRepo * ChartRepository ) (* helmchart.Chart , error ) {
118+ if chartRepo == nil {
119+ return nil , fmt .Errorf ("chartrepo should not be nil" )
121120 }
122121
123122 // Lookup the chart version in the chart repository index
124- chartVer , err := chartrepo .Get (dep .Name , dep .Version )
123+ chartVer , err := chartRepo .Get (dep .Name , dep .Version )
125124 if err != nil {
126125 return nil , err
127126 }
128127
129128 // Download chart
130- res , err := chartrepo .DownloadChart (chartVer )
129+ res , err := chartRepo .DownloadChart (chartVer )
131130 if err != nil {
132131 return nil , err
133132 }
0 commit comments