Skip to content

Commit

Permalink
- fix for issue #3
Browse files Browse the repository at this point in the history
  • Loading branch information
v1ktor0t committed Feb 1, 2023
1 parent 20cb541 commit 843d94d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
12 changes: 12 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ func isSchemeValid(parsed *urllib.URL, config *Config, debugLogFunc func(string)

func isHostValid(parsed *urllib.URL, config *Config, debugLogFunc func(string)) error {
host := parsed.Hostname()
if host == "" {
debugLogFunc("empty host received")
return &InvalidHostError{host: ""}
}

if config.AllowedHosts != nil && !isAllowedHost(host, config.AllowedHosts) {
debugLogFunc(fmt.Sprintf("disallowed host: %s", host))
Expand Down Expand Up @@ -254,6 +258,14 @@ func (e *AllowedSchemeError) Error() string {
return fmt.Sprintf("scheme: %v not found in allowlist", e.scheme)
}

type InvalidHostError struct {
host string
}

func (e *InvalidHostError) Error() string {
return fmt.Sprintf("host: %v is not valid", e.host)
}

type AllowedHostError struct {
host string
}
Expand Down
21 changes: 21 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,3 +437,24 @@ func TestInternalIPAreAlwaysBlocked(t *testing.T) {
}

}

func TestInvalidHostValidation(t *testing.T) {
cfg := GetConfigBuilder().Build()
client := Client(cfg)

urls := []string{"http://[]", "http://[]:123", "http://:123"}

for _, url := range urls {
_, err := client.Get(url)
if err == nil {
t.Errorf("invalid host from url => %v was accepted. client didn't not return an error", err)
}

err = unwrap(err)
_, ok := err.(*InvalidHostError)
if !ok {
t.Errorf("client returned incorrect error: %v", err)
}
}

}

0 comments on commit 843d94d

Please sign in to comment.