Skip to content

Commit

Permalink
Fix: validate URI
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Apr 2, 2022
1 parent 2bba5f8 commit 67b160e
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
41 changes: 40 additions & 1 deletion cmd/metadata/resolver/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package resolver

import (
"context"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -61,10 +63,15 @@ func NewHttp(opts ...HttpOption) Http {

// Resolve -
func (s Http) Resolve(ctx context.Context, network, address, link string) ([]byte, error) {
if _, err := url.ParseRequestURI(link); err != nil {
parsed, err := url.ParseRequestURI(link)
if err != nil {
return nil, ErrInvalidURI
}

if err := s.ValidateURL(parsed); err != nil {
return nil, err
}

req, err := http.NewRequestWithContext(ctx, http.MethodGet, link, nil)
if err != nil {
return nil, err
Expand Down Expand Up @@ -92,3 +99,35 @@ func (s Http) Resolve(ctx context.Context, network, address, link string) ([]byt
func (s Http) Is(link string) bool {
return strings.HasPrefix(link, prefixHttp) || strings.HasPrefix(link, prefixHttps)
}

// ValidateURL -
func (s Http) ValidateURL(link *url.URL) error {
if link.Host == "localhost" {
return errors.Wrap(ErrInvalidURI, fmt.Sprintf("invalid host: %s", link.Host))
}

for _, mask := range []string{
"10.0.0.0/8",
"100.64.0.0/10",
"169.254.0.0/16",
"172.16.0.0/12",
"192.0.0.0/24",
"192.0.2.0/24",
"192.168.0.0/16",
"198.18.0.0/15",
"198.51.100.0/24",
"203.0.113.0/24",
"240.0.0.0/4",
} {
_, cidr, err := net.ParseCIDR(mask)
if err != nil {
return err
}

ip := net.ParseIP(link.Host)
if ip != nil && cidr.Contains(ip) {
return errors.Wrap(ErrInvalidURI, fmt.Sprintf("restricted subnet: %s", mask))
}
}
return nil
}
47 changes: 47 additions & 0 deletions cmd/metadata/resolver/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package resolver

import (
"net/url"
"testing"
)

func TestHttp_ValidateURL(t *testing.T) {
tests := []struct {
name string
link *url.URL
wantErr bool
}{
{
name: "localhost",
link: &url.URL{
Host: "localhost",
},
wantErr: true,
}, {
name: "10.0.0.0/8",
link: &url.URL{
Host: "10.0.0.1",
},
wantErr: true,
}, {
name: "valid",
link: &url.URL{
Host: "better-call.dev",
},
}, {
name: "192.0.2.0/24",
link: &url.URL{
Host: "192.0.2.1",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := Http{}
if err := s.ValidateURL(tt.link); (err != nil) != tt.wantErr {
t.Errorf("Http.ValidateURL() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
4 changes: 4 additions & 0 deletions cmd/metadata/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
ErrorTypeKeyTezosNotFond ErrorType = "tezos_key_not_found"
ErrorTypeTezosURIParsing ErrorType = "tezos_uri_parsing"
ErrorTypeInvalidJSON ErrorType = "invalid_json"
ErrorInvalidHTTPURI ErrorType = "invalid_http_uri"
)

// ResolvingError -
Expand Down Expand Up @@ -126,6 +127,9 @@ func (r Receiver) Resolve(ctx context.Context, network, address, link string) (r
}

if err != nil {
if errors.Is(err, ErrInvalidURI) {
return resolved, newResolvingError(0, ErrorInvalidHTTPURI, err)
}
return
}

Expand Down
4 changes: 4 additions & 0 deletions cmd/metadata/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ func (indexer *Indexer) resolveTokenMetadata(ctx context.Context, tm *models.Tok
if e, ok := err.(resolver.ResolvingError); ok {
indexer.incrementErrorCounter(e)
err = e.Err

if e.Type == resolver.ErrorInvalidHTTPURI {
tm.RetryCount = int8(indexer.settings.MaxRetryCountOnError)
}
}

if tm.RetryCount < int8(indexer.settings.MaxRetryCountOnError) {
Expand Down

0 comments on commit 67b160e

Please sign in to comment.