Skip to content

Commit 843d94d

Browse files
committed
- fix for issue #3
1 parent 20cb541 commit 843d94d

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

client.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ func isSchemeValid(parsed *urllib.URL, config *Config, debugLogFunc func(string)
101101

102102
func isHostValid(parsed *urllib.URL, config *Config, debugLogFunc func(string)) error {
103103
host := parsed.Hostname()
104+
if host == "" {
105+
debugLogFunc("empty host received")
106+
return &InvalidHostError{host: ""}
107+
}
104108

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

261+
type InvalidHostError struct {
262+
host string
263+
}
264+
265+
func (e *InvalidHostError) Error() string {
266+
return fmt.Sprintf("host: %v is not valid", e.host)
267+
}
268+
257269
type AllowedHostError struct {
258270
host string
259271
}

client_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,3 +437,24 @@ func TestInternalIPAreAlwaysBlocked(t *testing.T) {
437437
}
438438

439439
}
440+
441+
func TestInvalidHostValidation(t *testing.T) {
442+
cfg := GetConfigBuilder().Build()
443+
client := Client(cfg)
444+
445+
urls := []string{"http://[]", "http://[]:123", "http://:123"}
446+
447+
for _, url := range urls {
448+
_, err := client.Get(url)
449+
if err == nil {
450+
t.Errorf("invalid host from url => %v was accepted. client didn't not return an error", err)
451+
}
452+
453+
err = unwrap(err)
454+
_, ok := err.(*InvalidHostError)
455+
if !ok {
456+
t.Errorf("client returned incorrect error: %v", err)
457+
}
458+
}
459+
460+
}

0 commit comments

Comments
 (0)