From 3bf8236c8df90c37e90f417f50d34d83063f8982 Mon Sep 17 00:00:00 2001 From: Robert van Gent Date: Sun, 22 Nov 2026 09:11:42 -0800 Subject: [PATCH] blob/azureblob: block non-bare domains --- blob/azureblob/azureblob.go | 18 ++++++++++----- blob/azureblob/azureblob_test.go | 38 +++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/blob/azureblob/azureblob.go b/blob/azureblob/azureblob.go index c3293de07f..41ccb41b10 100644 --- a/blob/azureblob/azureblob.go +++ b/blob/azureblob/azureblob.go @@ -290,9 +290,17 @@ 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 @@ -300,19 +308,19 @@ func verifyLocalhost(domain string) error { 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) diff --git a/blob/azureblob/azureblob_test.go b/blob/azureblob/azureblob_test.go index fdffe5ab45..ad84f32f7a 100644 --- a/blob/azureblob/azureblob_test.go +++ b/blob/azureblob/azureblob_test.go @@ -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 { @@ -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)