0
Fork 0
mirror of https://github.com/willnorris/imageproxy.git synced 2024-12-30 22:34:18 -05:00

rename several validFoo method to fooMatches

this more accurately describes what the methods are actually doing:
verifying if the value matches, without making any judgement amount
validity.
This commit is contained in:
Will Norris 2019-03-22 03:11:39 +00:00
parent 7264d177a1
commit 2612fa4111
2 changed files with 17 additions and 17 deletions

View file

@ -183,7 +183,7 @@ func (p *Proxy) serveImage(w http.ResponseWriter, r *http.Request) {
} }
contentType, _, _ := mime.ParseMediaType(resp.Header.Get("Content-Type")) contentType, _, _ := mime.ParseMediaType(resp.Header.Get("Content-Type"))
if resp.ContentLength != 0 && !validContentType(p.ContentTypes, contentType) { if resp.ContentLength != 0 && !contentTypeMatches(p.ContentTypes, contentType) {
msg := fmt.Sprintf("forbidden content-type: %q", contentType) msg := fmt.Sprintf("forbidden content-type: %q", contentType)
log.Print(msg) log.Print(msg)
http.Error(w, msg, http.StatusForbidden) http.Error(w, msg, http.StatusForbidden)
@ -225,11 +225,11 @@ func (p *Proxy) allowed(r *Request) error {
// backwards compatible with old naming of the field // backwards compatible with old naming of the field
p.AllowHosts = p.Whitelist p.AllowHosts = p.Whitelist
} }
if len(p.Referrers) > 0 && !validReferrer(p.Referrers, r.Original) { if len(p.Referrers) > 0 && !referrerMatches(p.Referrers, r.Original) {
return fmt.Errorf("request does not contain an allowed referrer: %v", r) return fmt.Errorf("request does not contain an allowed referrer: %v", r)
} }
if validHost(p.DenyHosts, r.URL) { if hostMatches(p.DenyHosts, r.URL) {
return fmt.Errorf("request contains a denied host %v", r) return fmt.Errorf("request contains a denied host %v", r)
} }
@ -237,7 +237,7 @@ func (p *Proxy) allowed(r *Request) error {
return nil // no allowed hosts or signature key, all requests accepted return nil // no allowed hosts or signature key, all requests accepted
} }
if len(p.AllowHosts) > 0 && validHost(p.AllowHosts, r.URL) { if len(p.AllowHosts) > 0 && hostMatches(p.AllowHosts, r.URL) {
return nil return nil
} }
@ -248,8 +248,8 @@ func (p *Proxy) allowed(r *Request) error {
return fmt.Errorf("request does not contain an allowed host or valid signature: %v", r) return fmt.Errorf("request does not contain an allowed host or valid signature: %v", r)
} }
// validContentType returns whether contentType matches one of the allowed patterns. // contentTypeMatches returns whether contentType matches one of the allowed patterns.
func validContentType(patterns []string, contentType string) bool { func contentTypeMatches(patterns []string, contentType string) bool {
if len(patterns) == 0 { if len(patterns) == 0 {
return true return true
} }
@ -263,8 +263,8 @@ func validContentType(patterns []string, contentType string) bool {
return false return false
} }
// validHost returns whether the host in u matches one of hosts. // hostMatches returns whether the host in u matches one of hosts.
func validHost(hosts []string, u *url.URL) bool { func hostMatches(hosts []string, u *url.URL) bool {
for _, host := range hosts { for _, host := range hosts {
if u.Host == host { if u.Host == host {
return true return true
@ -278,13 +278,13 @@ func validHost(hosts []string, u *url.URL) bool {
} }
// returns whether the referrer from the request is in the host list. // returns whether the referrer from the request is in the host list.
func validReferrer(hosts []string, r *http.Request) bool { func referrerMatches(hosts []string, r *http.Request) bool {
u, err := url.Parse(r.Header.Get("Referer")) u, err := url.Parse(r.Header.Get("Referer"))
if err != nil { // malformed or blank header, just deny if err != nil { // malformed or blank header, just deny
return false return false
} }
return validHost(hosts, u) return hostMatches(hosts, u)
} }
// validSignature returns whether the request signature is valid. // validSignature returns whether the request signature is valid.

View file

@ -158,8 +158,8 @@ func TestAllowed(t *testing.T) {
} }
} }
func TestValidHost(t *testing.T) { func TestHostMatches(t *testing.T) {
allowHosts := []string{"a.test", "*.b.test", "*c.test"} hosts := []string{"a.test", "*.b.test", "*c.test"}
tests := []struct { tests := []struct {
url string url string
@ -182,8 +182,8 @@ func TestValidHost(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("error parsing url %q: %v", tt.url, err) t.Errorf("error parsing url %q: %v", tt.url, err)
} }
if got, want := validHost(allowHosts, u), tt.valid; got != want { if got, want := hostMatches(hosts, u), tt.valid; got != want {
t.Errorf("validHost(%v, %q) returned %v, want %v", allowHosts, u, got, want) t.Errorf("hostMatches(%v, %q) returned %v, want %v", hosts, u, got, want)
} }
} }
} }
@ -413,7 +413,7 @@ func TestTransformingTransport(t *testing.T) {
} }
} }
func TestValidContentType(t *testing.T) { func TestContentTypeMatches(t *testing.T) {
tests := []struct { tests := []struct {
patterns []string patterns []string
contentType string contentType string
@ -454,9 +454,9 @@ func TestValidContentType(t *testing.T) {
{[]string{"text/*", "image/*"}, "image/jpeg", true}, {[]string{"text/*", "image/*"}, "image/jpeg", true},
} }
for _, tt := range tests { for _, tt := range tests {
got := validContentType(tt.patterns, tt.contentType) got := contentTypeMatches(tt.patterns, tt.contentType)
if want := tt.valid; got != want { if want := tt.valid; got != want {
t.Errorf("validContentType(%q, %q) returned %v, want %v", tt.patterns, tt.contentType, got, want) t.Errorf("contentTypeMatches(%q, %q) returned %v, want %v", tt.patterns, tt.contentType, got, want)
} }
} }
} }