@@ -16,6 +16,8 @@ import (
16
16
"testing"
17
17
"time"
18
18
19
+ log "github.com/sirupsen/logrus"
20
+
19
21
"github.com/ghodss/yaml"
20
22
"github.com/stretchr/testify/assert"
21
23
"github.com/stretchr/testify/mock"
@@ -149,6 +151,76 @@ func TestGenerateYamlManifestInDir(t *testing.T) {
149
151
assert .Equal (t , 3 , len (res2 .Manifests ))
150
152
}
151
153
154
+ func Test_GenerateManifests_NoOutOfBoundsAccess (t * testing.T ) {
155
+ testCases := []struct {
156
+ name string
157
+ outOfBoundsFilename string
158
+ outOfBoundsFileContents string
159
+ mustNotContain string // Optional string that must not appear in error or manifest output. If empty, use outOfBoundsFileContents.
160
+ }{
161
+ {
162
+ name : "out of bounds JSON file should not appear in error output" ,
163
+ outOfBoundsFilename : "test.json" ,
164
+ outOfBoundsFileContents : `{"some": "json"}` ,
165
+ },
166
+ {
167
+ name : "malformed JSON file contents should not appear in error output" ,
168
+ outOfBoundsFilename : "test.json" ,
169
+ outOfBoundsFileContents : "$" ,
170
+ },
171
+ {
172
+ name : "out of bounds JSON manifest should not appear in manifest output" ,
173
+ outOfBoundsFilename : "test.json" ,
174
+ // JSON marshalling is deterministic. So if there's a leak, exactly this should appear in the manifests.
175
+ outOfBoundsFileContents : `{"apiVersion":"v1","kind":"Secret","metadata":{"name":"test","namespace":"default"},"type":"Opaque"}` ,
176
+ },
177
+ {
178
+ name : "out of bounds YAML manifest should not appear in manifest output" ,
179
+ outOfBoundsFilename : "test.yaml" ,
180
+ outOfBoundsFileContents : "apiVersion: v1\n kind: Secret\n metadata:\n name: test\n namespace: default\n type: Opaque" ,
181
+ mustNotContain : `{"apiVersion":"v1","kind":"Secret","metadata":{"name":"test","namespace":"default"},"type":"Opaque"}` ,
182
+ },
183
+ }
184
+
185
+ for _ , testCase := range testCases {
186
+ testCaseCopy := testCase
187
+ t .Run (testCaseCopy .name , func (t * testing.T ) {
188
+ t .Parallel ()
189
+
190
+ outOfBoundsDir := t .TempDir ()
191
+ outOfBoundsFile := path .Join (outOfBoundsDir , testCaseCopy .outOfBoundsFilename )
192
+ err := os .WriteFile (outOfBoundsFile , []byte (testCaseCopy .outOfBoundsFileContents ), os .FileMode (0444 ))
193
+ require .NoError (t , err )
194
+
195
+ repoDir := t .TempDir ()
196
+ err = os .Symlink (outOfBoundsFile , path .Join (repoDir , testCaseCopy .outOfBoundsFilename ))
197
+ require .NoError (t , err )
198
+
199
+ var mustNotContain = testCaseCopy .outOfBoundsFileContents
200
+ if testCaseCopy .mustNotContain != "" {
201
+ mustNotContain = testCaseCopy .mustNotContain
202
+ }
203
+
204
+ q := apiclient.ManifestRequest {Repo : & argoappv1.Repository {}, ApplicationSource : & argoappv1.ApplicationSource {}}
205
+ res , err := GenerateManifests (context .Background (), repoDir , "" , "" , & q , false , & git.NoopCredsStore {})
206
+ require .Error (t , err )
207
+ assert .NotContains (t , err .Error (), mustNotContain )
208
+ assert .Contains (t , err .Error (), "illegal filepath" )
209
+ assert .Nil (t , res )
210
+ })
211
+ }
212
+ }
213
+
214
+ func TestGenerateManifests_MissingSymlinkDestination (t * testing.T ) {
215
+ repoDir := t .TempDir ()
216
+ err := os .Symlink ("/obviously/does/not/exist" , path .Join (repoDir , "test.yaml" ))
217
+ require .NoError (t , err )
218
+
219
+ q := apiclient.ManifestRequest {Repo : & argoappv1.Repository {}, ApplicationSource : & argoappv1.ApplicationSource {}}
220
+ _ , err = GenerateManifests (context .Background (), repoDir , "" , "" , & q , false , & git.NoopCredsStore {})
221
+ require .NoError (t , err )
222
+ }
223
+
152
224
func TestGenerateManifests_K8SAPIResetCache (t * testing.T ) {
153
225
service := newService ("../.." )
154
226
@@ -1641,7 +1713,7 @@ func TestFindResources(t *testing.T) {
1641
1713
for i := range testCases {
1642
1714
tc := testCases [i ]
1643
1715
t .Run (tc .name , func (t * testing.T ) {
1644
- objs , err := findManifests ("testdata/app-include-exclude" , "." , nil , argoappv1.ApplicationSourceDirectory {
1716
+ objs , err := findManifests (& log. Entry {}, "testdata/app-include-exclude" , "." , nil , argoappv1.ApplicationSourceDirectory {
1645
1717
Recurse : true ,
1646
1718
Include : tc .include ,
1647
1719
Exclude : tc .exclude ,
@@ -1659,7 +1731,7 @@ func TestFindResources(t *testing.T) {
1659
1731
}
1660
1732
1661
1733
func TestFindManifests_Exclude (t * testing.T ) {
1662
- objs , err := findManifests ("testdata/app-include-exclude" , "." , nil , argoappv1.ApplicationSourceDirectory {
1734
+ objs , err := findManifests (& log. Entry {}, "testdata/app-include-exclude" , "." , nil , argoappv1.ApplicationSourceDirectory {
1663
1735
Recurse : true ,
1664
1736
Exclude : "subdir/deploymentSub.yaml" ,
1665
1737
}, map [string ]bool {})
@@ -1672,7 +1744,7 @@ func TestFindManifests_Exclude(t *testing.T) {
1672
1744
}
1673
1745
1674
1746
func TestFindManifests_Exclude_NothingMatches (t * testing.T ) {
1675
- objs , err := findManifests ("testdata/app-include-exclude" , "." , nil , argoappv1.ApplicationSourceDirectory {
1747
+ objs , err := findManifests (& log. Entry {}, "testdata/app-include-exclude" , "." , nil , argoappv1.ApplicationSourceDirectory {
1676
1748
Recurse : true ,
1677
1749
Exclude : "nothing.yaml" ,
1678
1750
}, map [string ]bool {})
0 commit comments