Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,11 @@ Alternately, try running:
Reloading the [codercat URL][] will still return an error message.

You can specify multiple hosts as a comma separated list to either flag, or
prefix a host value with `*.` to allow or deny all sub-domains as well.
prefix a host value with `*.` to allow or deny all sub-domains. You can
also specify a netblock in CIDR notation (`127.0.0.0/8`) -- this is useful for
blocking reserved ranges like `127.0.0.0/8`, `192.168.0.0/16`, etc.

If a host matches both an allowed an a denied host, the request will be denied.
If a host matches both an allowed and denied host, the request will be denied.

### Allowed Content-Type List ###

Expand Down
11 changes: 11 additions & 0 deletions imageproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"io/ioutil"
"log"
"mime"
"net"
"net/http"
"net/url"
"path"
Expand Down Expand Up @@ -324,6 +325,16 @@ func hostMatches(hosts []string, u *url.URL) bool {
if strings.HasPrefix(host, "*.") && strings.HasSuffix(u.Host, host[2:]) {
return true
}
// Checks whether the host in u is an IP
if ip := net.ParseIP(u.Host); ip != nil {
// Checks whether our current host is a CIDR
if _, ipnet, err := net.ParseCIDR(host); err == nil {
// Checks if our host contains the IP in u
if ipnet.Contains(ip) {
return true
}
}
}
}

return false
Expand Down
1 change: 1 addition & 0 deletions imageproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func TestAllowed(t *testing.T) {
{"http://test/image", emptyOptions, nil, []string{"test"}, nil, nil, nil, false},
{"http://test/image", emptyOptions, []string{"test"}, []string{"test"}, nil, nil, nil, false},
{"http://test/image", Options{Signature: "NDx5zZHx7QfE8E-ijowRreq6CJJBZjwiRfOVk_mkfQQ="}, nil, []string{"test"}, nil, key, nil, false},
{"http://127.0.0.1/image", emptyOptions, nil, []string{"127.0.0.0/8"}, nil, nil, nil, false},
}

for _, tt := range tests {
Expand Down