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
19 changes: 18 additions & 1 deletion fetch/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"net"
"net/url"
"strings"
"unicode"
Expand Down Expand Up @@ -183,12 +184,28 @@ func checkMetadataURL(raw string) error {
if u.Scheme != "https" {
return fmt.Errorf("%w: scheme %q", ErrUnsafeURL, u.Scheme)
}
if u.Hostname() == "" {
hostname := u.Hostname()
if hostname == "" {
return fmt.Errorf("%w: empty host", ErrUnsafeURL)
}
if isPrivateHost(hostname) {
return fmt.Errorf("%w: private/loopback host %q", ErrUnsafeURL, hostname)
}
return nil
}

func isPrivateHost(hostname string) bool {
ip := net.ParseIP(hostname)
if ip == nil {
ips, err := net.LookupIP(hostname)
if err != nil || len(ips) == 0 {
return false
}
ip = ips[0]
}
return ip.IsLoopback() || ip.IsPrivate() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast()
}

func filenameFromURL(url string) string {
if idx := strings.LastIndex(url, "/"); idx >= 0 {
return url[idx+1:]
Expand Down
3 changes: 3 additions & 0 deletions fetch/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ func TestCheckMetadataURL(t *testing.T) {
{"https:///path/only", false},
{"//169.254.169.254/latest/meta-data/", false},
{"169.254.169.254/latest/meta-data/", false},
{"https://169.254.169.254/latest/meta-data/", false},
{"https://127.0.0.1/something", false},
{"https://[::1]/something", false},
{"", false},
{"\x00https://evil", false},
}
Expand Down