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
18 changes: 13 additions & 5 deletions blob/azureblob/azureblob.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,29 +290,37 @@ func isAlphanumeric(s string) bool {
}

func verifyLocalhost(domain string) error {
// Ensure the domain is a bare host[:port].
u, err := url.Parse("http://" + domain)
if err != nil {
return fmt.Errorf("failed to parse local emulator domain %q as a URL: %w", domain, err)
}
if u.Host != domain || u.User != nil || u.Path != "" || u.RawQuery != "" || u.Fragment != "" {
return fmt.Errorf("local emulator domain %q must be a bare host[:port]", domain)
}
host := domain

// SplitHostPort fails if there is no port, so only overwrite host if it succeeds
// SplitHostPort fails if there is no port, so only overwrite host if it succeeds.
h, port, err := net.SplitHostPort(domain)
if err == nil {
host = h
if !isNumeric(port) {
return fmt.Errorf("local emulator domain %q port %q must be numeric", domain, port)
}
} else {
// Handle bracketed IPv6 without a port, e.g., "[::1]"
// Handle bracketed IPv6 without a port, e.g., "[::1]".
host = strings.Trim(host, "[]")
}

// Normalize case and trailing FQDN dot (e.g., "LOCALHOST.")
// Normalize case and trailing FQDN dot (e.g., "LOCALHOST.").
host = strings.ToLower(strings.TrimSuffix(host, "."))

// Check standard localhost domain names (RFC 6761)
// Check standard localhost domain names (RFC 6761).
if host == "localhost" || strings.HasSuffix(host, ".localhost") {
return nil
}

// Check if the host is a loopback IP (127.0.0.0/8 for IPv4, ::1 for IPv6)
// Check if the host is a loopback IP (127.0.0.0/8 for IPv4, ::1 for IPv6).
ip, err := netip.ParseAddr(host)
if err != nil {
return fmt.Errorf("failed to netip.ParseAddr for local emulator domain %q host %q: %w", domain, host, err)
Expand Down
38 changes: 37 additions & 1 deletion blob/azureblob/azureblob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,42 @@ func TestNewServiceURL(t *testing.T) {
},
want: "http://127.0.0.1:8080/myaccount",
},
{
// Local emulator, non-bare URL with encoded "/".
opts: ServiceURLOptions{
AccountName: "myaccount",
StorageDomain: "attacker.example%2F.localhost",
},
query: url.Values{
"localemu": {"true"},
"protocol": {"http"},
},
wantErrURL: true,
},
{
// Local emulator, non-bare URL with encoded "?".
opts: ServiceURLOptions{
AccountName: "myaccount",
StorageDomain: "attacker.example%3F.localhost",
},
query: url.Values{
"localemu": {"true"},
"protocol": {"http"},
},
wantErrURL: true,
},
{
// Local emulator, non-bare URL with encoded "#".
opts: ServiceURLOptions{
AccountName: "myaccount",
StorageDomain: "attacker.example%23.localhost",
},
query: url.Values{
"localemu": {"true"},
"protocol": {"http"},
},
wantErrURL: true,
},
}

for _, test := range tests {
Expand All @@ -725,7 +761,7 @@ func TestNewServiceURL(t *testing.T) {
}
got, err := NewServiceURL(opts)
if (err != nil) != test.wantErrURL {
t.Fatalf("(%v, %v): NewServiceURL got err %v want error %v", test.opts, test.query, err, test.wantErrURL)
t.Fatalf("(%v, %v): NewServiceURL got err %v result URL %q, want error %v", test.opts, test.query, err, got, test.wantErrURL)
}
if got != test.want {
t.Errorf("(%v, %v): got %q want %q", test.opts, test.query, got, test.want)
Expand Down
Loading