Skip to content

Commit 9657ac3

Browse files
committed
add validateURI before paring uri
Signed-off-by: Stephanie <[email protected]>
1 parent c95f71f commit 9657ac3

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/devfile/library
33
go 1.13
44

55
require (
6-
github.com/devfile/api/v2 v2.0.0-20210121164412-49ba915897f4
6+
github.com/devfile/api/v2 v2.0.0-20210202172954-6424f4139ac7
77
github.com/fatih/color v1.7.0
88
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
99
github.com/gobwas/glob v0.2.3

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7Do
4444
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4545
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
4646
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
47-
github.com/devfile/api/v2 v2.0.0-20210121164412-49ba915897f4 h1:jDzWFpF/BoyaemoPjAzw5SmlX172JsSsh+Frujm5Ww4=
48-
github.com/devfile/api/v2 v2.0.0-20210121164412-49ba915897f4/go.mod h1:Cot4snybn3qhIh48oIFi9McocnIx7zY5fFbjfrIpPvg=
47+
github.com/devfile/api/v2 v2.0.0-20210202172954-6424f4139ac7 h1:bQGUVLEGQtVkvS94K4gQbu57Rk/npcZQmgORmCWYNy8=
48+
github.com/devfile/api/v2 v2.0.0-20210202172954-6424f4139ac7/go.mod h1:Cot4snybn3qhIh48oIFi9McocnIx7zY5fFbjfrIpPvg=
4949
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
5050
github.com/docker/docker v0.7.3-0.20190327010347-be7ac8be2ae0/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
5151
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=

pkg/devfile/parser/parse.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"reflect"
1616

1717
v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
18+
"github.com/devfile/api/v2/pkg/validation"
1819
apiOverride "github.com/devfile/api/v2/pkg/utils/overriding"
1920
"github.com/pkg/errors"
2021
)
@@ -180,6 +181,10 @@ func parseParentAndPlugin(d DevfileObj) (err error) {
180181

181182
func parseFromURI(uri string, curDevfile DevfileObj) (DevfileObj, error) {
182183
// validate URI
184+
err := validation.ValidateURI(uri)
185+
if err!= nil {
186+
return DevfileObj{}, err
187+
}
183188

184189
// absolute URL address
185190
if strings.HasPrefix(uri, "http://") || strings.HasPrefix(uri, "https://") {
@@ -188,7 +193,6 @@ func parseFromURI(uri string, curDevfile DevfileObj) (DevfileObj, error) {
188193

189194
// relative path on disk
190195
if curDevfile.Ctx.GetAbsPath() != "" {
191-
192196
return Parse(path.Join(path.Dir(curDevfile.Ctx.GetAbsPath()), uri))
193197
}
194198

pkg/devfile/parser/parse_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -2404,6 +2404,7 @@ func Test_parseFromURI(t *testing.T) {
24042404
const httpPrefix = "http://"
24052405
const localRelativeURI = "testTmp/dir/devfile.yaml"
24062406
const notExistURI = "notexist/devfile.yaml"
2407+
const invalidURL = "http//invalid.com"
24072408
uri2 := path.Join(uri1, localRelativeURI)
24082409

24092410
localDevfile := DevfileObj{
@@ -2643,6 +2644,28 @@ func Test_parseFromURI(t *testing.T) {
26432644
uri: notExistURI,
26442645
wantErr: true,
26452646
},
2647+
{
2648+
name: "case 6: should fail if with invalid URI format",
2649+
curDevfile: DevfileObj{
2650+
Ctx: devfileCtx.NewURLDevfileCtx(OutputDevfileYamlPath),
2651+
Data: &v2.DevfileV2{
2652+
Devfile: v1.Devfile{
2653+
DevWorkspaceTemplateSpec: v1.DevWorkspaceTemplateSpec{
2654+
Parent: &v1.Parent{
2655+
ImportReference: v1.ImportReference{
2656+
ImportReferenceUnion: v1.ImportReferenceUnion{
2657+
Uri: invalidURL,
2658+
},
2659+
},
2660+
},
2661+
DevWorkspaceTemplateSpecContent: v1.DevWorkspaceTemplateSpecContent{},
2662+
},
2663+
},
2664+
},
2665+
},
2666+
uri: invalidURL,
2667+
wantErr: true,
2668+
},
26462669
}
26472670
for _, tt := range tests {
26482671
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)