Skip to content

Commit

Permalink
host might come in header in connect request (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
btoews authored Oct 3, 2023
1 parent 3a24357 commit 92c315b
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions request_validator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tokenizer

import (
"errors"
"fmt"
"net/http"
"regexp"
Expand All @@ -25,8 +26,15 @@ func AllowHosts(hosts ...string) RequestValidator {
}

func (v allowedHosts) Validate(r *http.Request) error {
if _, allowed := v[r.URL.Host]; !allowed {
return fmt.Errorf("%w: secret not valid for %s", ErrBadRequest, r.URL.Host)
host := r.URL.Host
if host == "" {
host = r.Host
}
if host == "" {
return errors.New("coun't find host in request")
}
if _, allowed := v[host]; !allowed {
return fmt.Errorf("%w: secret not valid for %s", ErrBadRequest, host)
}
return nil
}
Expand All @@ -44,8 +52,15 @@ func AllowHostPattern(pattern *regexp.Regexp) RequestValidator {
}

func (v *allowedHostPattern) Validate(r *http.Request) error {
if match := (*regexp.Regexp)(v).MatchString(r.URL.Host); !match {
return fmt.Errorf("%w: secret not valid for %s", ErrBadRequest, r.URL.Host)
host := r.URL.Host
if host == "" {
host = r.Host
}
if host == "" {
return errors.New("coun't find host in request")
}
if match := (*regexp.Regexp)(v).MatchString(host); !match {
return fmt.Errorf("%w: secret not valid for %s", ErrBadRequest, host)
}
return nil
}

0 comments on commit 92c315b

Please sign in to comment.