Skip to content

Commit

Permalink
fix: update to use config
Browse files Browse the repository at this point in the history
  • Loading branch information
J0 committed Oct 21, 2024
1 parent f2422a2 commit 3e3661b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
18 changes: 14 additions & 4 deletions internal/conf/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ func loadEnvironment(filename string) error {

// Moving away from the existing HookConfig so we can get a fresh start.
type HookConfiguration struct {
AllowedLocalHostNames []string `json:"allowed_local_names" split_words:"true"`
MFAVerificationAttempt ExtensibilityPointConfiguration `json:"mfa_verification_attempt" split_words:"true"`
PasswordVerificationAttempt ExtensibilityPointConfiguration `json:"password_verification_attempt" split_words:"true"`
CustomAccessToken ExtensibilityPointConfiguration `json:"custom_access_token" split_words:"true"`
Expand Down Expand Up @@ -607,14 +608,23 @@ func (h *HookConfiguration) Validate() error {
h.SendEmail,
}
for _, point := range points {
if err := point.ValidateExtensibilityPoint(); err != nil {
if err := point.ValidateExtensibilityPoint(h.AllowedLocalHostNames); err != nil {
return err
}
}
return nil
}

func (e *ExtensibilityPointConfiguration) ValidateExtensibilityPoint() error {
func isStringInSlice(checkValue string, list []string) bool {
for _, val := range list {
if val == checkValue {
return true
}
}
return false
}

func (e *ExtensibilityPointConfiguration) ValidateExtensibilityPoint(allowedHTTPHostNames []string) error {
if e.URI == "" {
return nil
}
Expand All @@ -627,10 +637,10 @@ func (e *ExtensibilityPointConfiguration) ValidateExtensibilityPoint() error {
return validatePostgresPath(u)
case "http":
hostname := u.Hostname()
if hostname == "localhost" || hostname == "127.0.0.1" || hostname == "::1" || hostname == "host.docker.internal" || hostname == "kong" || hostname == "edge_runtime" {
if isStringInSlice(hostname, allowedHTTPHostNames) {
return validateHTTPHookSecrets(e.HTTPHookSecrets)
}
return fmt.Errorf("only localhost, 127.0.0.1, and ::1 are supported with http")
return fmt.Errorf("Hostname is %q: only %s are supported with http", hostname, strings.Join(allowedHTTPHostNames, ", "))

Check failure on line 643 in internal/conf/configuration.go

View workflow job for this annotation

GitHub Actions / test (1.22.x)

error strings should not be capitalized (ST1005)
case "https":
return validateHTTPHookSecrets(e.HTTPHookSecrets)
default:
Expand Down
13 changes: 11 additions & 2 deletions internal/conf/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ func TestHTTPHookSecretsDecode(t *testing.T) {
}

func TestValidateExtensibilityPointURI(t *testing.T) {
allowedLocalHostNames := []string{
"localhost",
"127.0.0.1",
"::1",
"host.docker.internal",
"kong",
"edge_runtime",
}
cases := []struct {
desc string
uri string
Expand All @@ -180,7 +188,7 @@ func TestValidateExtensibilityPointURI(t *testing.T) {

for _, tc := range cases {
ep := ExtensibilityPointConfiguration{URI: tc.uri}
err := ep.ValidateExtensibilityPoint()
err := ep.ValidateExtensibilityPoint(allowedLocalHostNames)
if tc.expectError {
require.Error(t, err)
} else {
Expand All @@ -206,8 +214,9 @@ func TestValidateExtensibilityPointSecrets(t *testing.T) {
{desc: "Invalid Symmetric Secret", secret: []string{"tommy"}, expectError: true},
}
for _, tc := range cases {
allowedLocalHostNames := []string{}
ep := ExtensibilityPointConfiguration{URI: validHTTPSURI, HTTPHookSecrets: tc.secret}
err := ep.ValidateExtensibilityPoint()
err := ep.ValidateExtensibilityPoint(allowedLocalHostNames)
if tc.expectError {
require.Error(t, err)
} else {
Expand Down

0 comments on commit 3e3661b

Please sign in to comment.