Skip to content

Commit 367e770

Browse files
Manuelvaasrubenhoenle
authored andcommitted
feat(cli): add validation to endpoint urls
relates to STACKITCLI-340
1 parent cffbc17 commit 367e770

3 files changed

Lines changed: 28 additions & 0 deletions

File tree

internal/pkg/auth/utils.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,21 @@ func parseWellKnownConfiguration(httpClient apiClient, wellKnownConfigURL string
9898
if wellKnownConfig.Issuer == "" {
9999
return nil, fmt.Errorf("found no issuer")
100100
}
101+
if utils.ValidateURLDomain(wellKnownConfig.Issuer) != nil {
102+
return nil, err
103+
}
101104
if wellKnownConfig.AuthorizationEndpoint == "" {
102105
return nil, fmt.Errorf("found no authorization endpoint")
103106
}
107+
if utils.ValidateURLDomain(wellKnownConfig.AuthorizationEndpoint) != nil {
108+
return nil, err
109+
}
104110
if wellKnownConfig.TokenEndpoint == "" {
105111
return nil, fmt.Errorf("found no token endpoint")
106112
}
113+
if utils.ValidateURLDomain(wellKnownConfig.TokenEndpoint) != nil {
114+
return nil, err
115+
}
107116

108117
err = SetAuthField(IDP_TOKEN_ENDPOINT, wellKnownConfig.TokenEndpoint)
109118
if err != nil {

internal/pkg/utils/utils.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/base64"
55
"fmt"
66
"net/url"
7+
"slices"
78
"strings"
89
"time"
910

@@ -82,11 +83,19 @@ func ValidateURLDomain(value string) error {
8283
if err != nil {
8384
return fmt.Errorf("parse url: %w", err)
8485
}
86+
8587
urlHost := urlStruct.Hostname()
8688
if urlHost == "" {
8789
return fmt.Errorf("bad url")
8890
}
8991

92+
allowedSchemes := []string{
93+
"https",
94+
}
95+
if !slices.Contains(allowedSchemes, urlStruct.Scheme) {
96+
return fmt.Errorf("unsupported protocol: %s", urlStruct.Scheme)
97+
}
98+
9099
allowedUrlDomain := viper.GetString(config.AllowedUrlDomainKey)
91100

92101
if !strings.HasSuffix(urlHost, allowedUrlDomain) {

internal/pkg/utils/utils_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,16 @@ func TestValidateURLDomain(t *testing.T) {
9797
input: "",
9898
isValid: false,
9999
},
100+
{
101+
name: "invalid protocol",
102+
input: "http://example.stackit.cloud",
103+
isValid: false,
104+
},
105+
{
106+
name: "no protocol",
107+
input: "example.stackit.cloud",
108+
isValid: false,
109+
},
100110
}
101111

102112
for _, tt := range tests {

0 commit comments

Comments
 (0)