From 89d0c75c33694babcf18aeac4fc0d41553af0eac Mon Sep 17 00:00:00 2001 From: phatlc Date: Fri, 13 Nov 2026 11:15:42 +0700 Subject: [PATCH 1/2] fix(http): send the vfox User-Agent with vfox's own requests The requests vfox makes for itself went out as Go-http-client, which some mirrors refuse. The client from RuntimeEnvContext.HttpClient now sends vfox/ with every request that carries no User-Agent, and the download of an SDK archive sends the agent of the plugin's own requests unless PreInstall returned one in its headers. --- internal/env/context.go | 24 +++---- internal/env/context_test.go | 27 ++++++++ internal/plugin/luai/context.go | 31 +-------- internal/sdk/download_test.go | 55 ++++++++++++++++ internal/sdk/sdk.go | 5 ++ internal/shared/util/useragent.go | 73 +++++++++++++++++++++ internal/shared/util/useragent_test.go | 87 ++++++++++++++++++++++++++ 7 files changed, 261 insertions(+), 41 deletions(-) create mode 100644 internal/shared/util/useragent.go create mode 100644 internal/shared/util/useragent_test.go diff --git a/internal/env/context.go b/internal/env/context.go index 7224a02f..4aa91ba0 100644 --- a/internal/env/context.go +++ b/internal/env/context.go @@ -27,6 +27,7 @@ import ( "github.com/version-fox/vfox/internal/config" "github.com/version-fox/vfox/internal/pathmeta" + "github.com/version-fox/vfox/internal/shared/util" ) // RuntimeEnvContext represents the runtime environment context. @@ -74,22 +75,21 @@ func (m *RuntimeEnvContext) LoadVfoxTomlChainByScopes(scopes ...UseScope) (VfoxT } // HttpClient creates an HTTP client based on the proxy settings in the user configuration. +// Its requests carry the vfox User-Agent unless they set one themselves. func (m *RuntimeEnvContext) HttpClient() *http.Client { - var client *http.Client + var transport http.RoundTripper = http.DefaultTransport if m.UserConfig.Proxy.Enable { - if uri, err := url.Parse(m.UserConfig.Proxy.Url); err == nil { - transPort := &http.Transport{ - Proxy: http.ProxyURL(uri), - } - client = &http.Client{ - Transport: transPort, - } + uri, err := url.Parse(m.UserConfig.Proxy.Url) + if err != nil { + return nil + } + transport = &http.Transport{ + Proxy: http.ProxyURL(uri), } - } else { - client = http.DefaultClient } - - return client + return &http.Client{ + Transport: util.WithDefaultUserAgent(transport, util.UserAgent(m.RuntimeVersion, "", "")), + } } // GetLinkDirPathByScope returns the symlink directory path for the given scope. diff --git a/internal/env/context_test.go b/internal/env/context_test.go index b8c337a0..22d616d3 100644 --- a/internal/env/context_test.go +++ b/internal/env/context_test.go @@ -17,6 +17,9 @@ package env import ( + "io" + "net/http" + "net/http/httptest" "os" "runtime" "strings" @@ -229,3 +232,27 @@ func TestCleanSystemPaths_BackwardCompatibility(t *testing.T) { t.Errorf("CleanSystemPaths() = %v, want %v", got, expected) } } + +func TestHttpClient_SendsVfoxUserAgent(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _, _ = w.Write([]byte(r.UserAgent())) + })) + defer srv.Close() + + ctx := &RuntimeEnvContext{ + UserConfig: &config.Config{Proxy: config.EmptyProxy}, + RuntimeVersion: "1.0.13", + } + resp, err := ctx.HttpClient().Get(srv.URL) + if err != nil { + t.Fatalf("Get() error = %v", err) + } + defer resp.Body.Close() + got, err := io.ReadAll(resp.Body) + if err != nil { + t.Fatalf("read body: %v", err) + } + if string(got) != "vfox/1.0.13" { + t.Fatalf("server saw User-Agent %q, want vfox/1.0.13", got) + } +} diff --git a/internal/plugin/luai/context.go b/internal/plugin/luai/context.go index 210ab996..2d44a320 100644 --- a/internal/plugin/luai/context.go +++ b/internal/plugin/luai/context.go @@ -17,8 +17,7 @@ package luai import ( - "fmt" - "strings" + "github.com/version-fox/vfox/internal/shared/util" ) // ComputeUserAgent constructs a user agent string for the vfox runtime and plugin. @@ -33,31 +32,5 @@ import ( // A user agent string in the format "vfox/ vfox-/", // omitting version information if not provided, and trimming extra spaces. func ComputeUserAgent(runtimeVersion, pluginName, pluginVersion string) string { - components := make([]string, 0, 2) - if runtimeVersion != "" { - components = append(components, fmt.Sprintf("vfox/%s", runtimeVersion)) - } else { - components = append(components, "vfox") - } - - name := ensurePrefix(pluginName) - if name != "" { - if pluginVersion != "" { - components = append(components, fmt.Sprintf("%s/%s", name, pluginVersion)) - } else { - components = append(components, name) - } - } - - return strings.TrimSpace(strings.Join(components, " ")) -} - -func ensurePrefix(name string) string { - if name == "" { - return "" - } - if strings.HasPrefix(name, "vfox-") { - return name - } - return "vfox-" + name + return util.UserAgent(runtimeVersion, pluginName, pluginVersion) } diff --git a/internal/sdk/download_test.go b/internal/sdk/download_test.go index e7142bf5..2d086e77 100644 --- a/internal/sdk/download_test.go +++ b/internal/sdk/download_test.go @@ -230,3 +230,58 @@ func TestSdk_MoveLocalFileRedactsURL(t *testing.T) { t.Fatalf("moveLocalFile() printed %q, want the password and the query token redacted", printed) } } + +func TestSdk_Download_UserAgent(t *testing.T) { + tests := []struct { + name string + plugin *plugin.Wrapper + headers map[string]string + want string + }{ + { + name: "the agent of the plugin", + plugin: &plugin.Wrapper{Metadata: &plugin.Metadata{Name: "gradle", Version: "0.0.3"}}, + want: "vfox/1.0.13 vfox-gradle/0.0.3", + }, + { + name: "an agent from PreInstall wins", + plugin: &plugin.Wrapper{Metadata: &plugin.Metadata{Name: "gradle", Version: "0.0.3"}}, + headers: map[string]string{"User-Agent": "custom"}, + want: "custom", + }, + { + name: "an agent from PreInstall in lower case wins", + plugin: &plugin.Wrapper{Metadata: &plugin.Metadata{Name: "gradle", Version: "0.0.3"}}, + headers: map[string]string{"user-agent": "custom"}, + want: "custom", + }, + { + name: "no plugin", + want: "vfox/1.0.13", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var got []string + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + got = r.Header.Values("User-Agent") + _, _ = w.Write([]byte("the archive bytes")) + })) + defer srv.Close() + + sdk := newDownloadTestSDK(t) + sdk.envContext.RuntimeVersion = "1.0.13" + sdk.plugin = tt.plugin + u, err := url.Parse(srv.URL + "/repro.zip") + if err != nil { + t.Fatalf("parse url: %v", err) + } + if _, err = sdk.Download(u, tt.headers); err != nil { + t.Fatalf("Download() error = %v", err) + } + if len(got) != 1 || got[0] != tt.want { + t.Fatalf("server saw User-Agent %q, want [%q]", got, tt.want) + } + }) + } +} diff --git a/internal/sdk/sdk.go b/internal/sdk/sdk.go index 218aa045..ab8b1d6c 100644 --- a/internal/sdk/sdk.go +++ b/internal/sdk/sdk.go @@ -1089,6 +1089,11 @@ func (b *impl) Download(u *url.URL, headers map[string]string) (string, error) { for key, value := range headers { req.Header.Add(key, value) } + // The archive is asked for with the agent of the plugin's own requests, + // unless PreInstall returned one in its headers. + if req.Header.Get("User-Agent") == "" && b.plugin != nil && b.plugin.Metadata != nil { + req.Header.Set("User-Agent", util.UserAgent(b.envContext.RuntimeVersion, b.plugin.Name, b.plugin.Version)) + } resp, err := b.envContext.HttpClient().Do(req) if err != nil { var urlErr *url.Error diff --git a/internal/shared/util/useragent.go b/internal/shared/util/useragent.go new file mode 100644 index 00000000..b2ccf0e2 --- /dev/null +++ b/internal/shared/util/useragent.go @@ -0,0 +1,73 @@ +/* + * Copyright 2026 Han Li and contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package util + +import ( + "fmt" + "net/http" + "strings" +) + +// UserAgent returns the User-Agent of a request made by vfox: +// "vfox/", followed by "vfox-/" +// when the request is made for a plugin. A version that is empty is left out, +// and so is its slash. +func UserAgent(runtimeVersion, pluginName, pluginVersion string) string { + components := make([]string, 0, 2) + if runtimeVersion != "" { + components = append(components, fmt.Sprintf("vfox/%s", runtimeVersion)) + } else { + components = append(components, "vfox") + } + + if pluginName != "" { + if !strings.HasPrefix(pluginName, "vfox-") { + pluginName = "vfox-" + pluginName + } + if pluginVersion != "" { + components = append(components, fmt.Sprintf("%s/%s", pluginName, pluginVersion)) + } else { + components = append(components, pluginName) + } + } + + return strings.TrimSpace(strings.Join(components, " ")) +} + +// WithDefaultUserAgent returns a transport that sends userAgent with every +// request that carries no User-Agent of its own. Without it such a request +// goes out as "Go-http-client", which some mirrors refuse. +func WithDefaultUserAgent(base http.RoundTripper, userAgent string) http.RoundTripper { + if base == nil { + base = http.DefaultTransport + } + return &userAgentTransport{base: base, userAgent: userAgent} +} + +type userAgentTransport struct { + base http.RoundTripper + userAgent string +} + +func (t *userAgentTransport) RoundTrip(req *http.Request) (*http.Response, error) { + if req.Header.Get("User-Agent") == "" { + // A RoundTripper must not modify the request it is given. + req = req.Clone(req.Context()) + req.Header.Set("User-Agent", t.userAgent) + } + return t.base.RoundTrip(req) +} diff --git a/internal/shared/util/useragent_test.go b/internal/shared/util/useragent_test.go new file mode 100644 index 00000000..81a10957 --- /dev/null +++ b/internal/shared/util/useragent_test.go @@ -0,0 +1,87 @@ +/* + * Copyright 2026 Han Li and contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package util + +import ( + "net/http" + "net/http/httptest" + "testing" +) + +func TestUserAgent(t *testing.T) { + tests := []struct { + name string + runtimeVersion, pluginName, pluginVersion string + want string + }{ + {"vfox only", "1.0.13", "", "", "vfox/1.0.13"}, + {"plugin", "1.0.13", "gradle", "0.0.3", "vfox/1.0.13 vfox-gradle/0.0.3"}, + {"plugin name already prefixed", "1.0.13", "vfox-gradle", "0.0.3", "vfox/1.0.13 vfox-gradle/0.0.3"}, + {"no plugin version", "1.0.13", "gradle", "", "vfox/1.0.13 vfox-gradle"}, + {"no runtime version", "", "gradle", "0.0.3", "vfox vfox-gradle/0.0.3"}, + {"nothing", "", "", "", "vfox"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := UserAgent(tt.runtimeVersion, tt.pluginName, tt.pluginVersion); got != tt.want { + t.Fatalf("UserAgent() = %q, want %q", got, tt.want) + } + }) + } +} + +func TestWithDefaultUserAgent(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _, _ = w.Write([]byte(r.UserAgent())) + })) + defer srv.Close() + client := &http.Client{Transport: WithDefaultUserAgent(nil, "vfox/1.0.13")} + + tests := []struct { + name string + header map[string]string + want string + }{ + {"no agent of its own", nil, "vfox/1.0.13"}, + {"own agent", map[string]string{"User-Agent": "custom"}, "custom"}, + {"own agent in lower case", map[string]string{"user-agent": "custom"}, "custom"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + req, err := http.NewRequest(http.MethodGet, srv.URL, nil) + if err != nil { + t.Fatalf("new request: %v", err) + } + for key, value := range tt.header { + req.Header.Add(key, value) + } + resp, err := client.Do(req) + if err != nil { + t.Fatalf("Do() error = %v", err) + } + defer resp.Body.Close() + got := make([]byte, 64) + n, _ := resp.Body.Read(got) + if string(got[:n]) != tt.want { + t.Fatalf("server saw User-Agent %q, want %q", got[:n], tt.want) + } + if tt.header == nil && req.Header.Get("User-Agent") != "" { + t.Fatalf("the caller's request was modified: %v", req.Header) + } + }) + } +} From 802984db06004bac8912af44cf338b6e7da4af48 Mon Sep 17 00:00:00 2001 From: phatlc Date: Fri, 13 Nov 2026 18:26:46 +0700 Subject: [PATCH 2/2] fix(http): return an error for an invalid proxy URL HttpClient returned nil when the proxy URL did not parse, and every caller used the result directly. It now returns an error, with the URL redacted, and the callers pass it on. Also gofmt context_test.go, read the whole body in TestWithDefaultUserAgent, and test the proxy branch of HttpClient. --- cmd/commands/upgrade.go | 5 +- internal/env/context.go | 7 +- internal/env/context_test.go | 104 ++++++++++++++++++------- internal/manager.go | 17 +++- internal/sdk/download_test.go | 12 +++ internal/sdk/sdk.go | 6 +- internal/shared/util/useragent_test.go | 11 ++- 7 files changed, 123 insertions(+), 39 deletions(-) diff --git a/cmd/commands/upgrade.go b/cmd/commands/upgrade.go index d1ae0c30..ae205489 100644 --- a/cmd/commands/upgrade.go +++ b/cmd/commands/upgrade.go @@ -136,7 +136,10 @@ func upgradeCmd(ctx context.Context, cmd *cli.Command) error { return err } defer manager.Close() - httpClient := manager.RuntimeEnvContext.HttpClient() + httpClient, err := manager.RuntimeEnvContext.HttpClient() + if err != nil { + return cli.Exit("Failed to create the HTTP client: "+err.Error(), 1) + } currVersion := fmt.Sprintf("v%s", internal.RuntimeVersion) latestVersion, err := fetchLatestVersion(httpClient) diff --git a/internal/env/context.go b/internal/env/context.go index 4aa91ba0..ac4b7620 100644 --- a/internal/env/context.go +++ b/internal/env/context.go @@ -76,12 +76,13 @@ func (m *RuntimeEnvContext) LoadVfoxTomlChainByScopes(scopes ...UseScope) (VfoxT // HttpClient creates an HTTP client based on the proxy settings in the user configuration. // Its requests carry the vfox User-Agent unless they set one themselves. -func (m *RuntimeEnvContext) HttpClient() *http.Client { +// It returns an error when the proxy is enabled and its URL does not parse. +func (m *RuntimeEnvContext) HttpClient() (*http.Client, error) { var transport http.RoundTripper = http.DefaultTransport if m.UserConfig.Proxy.Enable { uri, err := url.Parse(m.UserConfig.Proxy.Url) if err != nil { - return nil + return nil, fmt.Errorf("invalid proxy url: %w", util.RedactError(err)) } transport = &http.Transport{ Proxy: http.ProxyURL(uri), @@ -89,7 +90,7 @@ func (m *RuntimeEnvContext) HttpClient() *http.Client { } return &http.Client{ Transport: util.WithDefaultUserAgent(transport, util.UserAgent(m.RuntimeVersion, "", "")), - } + }, nil } // GetLinkDirPathByScope returns the symlink directory path for the given scope. diff --git a/internal/env/context_test.go b/internal/env/context_test.go index 22d616d3..4262a615 100644 --- a/internal/env/context_test.go +++ b/internal/env/context_test.go @@ -36,19 +36,19 @@ func TestSplitSystemPaths(t *testing.T) { } tests := []struct { - name string - pathEnv string - wantPrefix []string - wantClean []string + name string + pathEnv string + wantPrefix []string + wantClean []string }{ { name: "virtualenv before vfox paths", pathEnv: strings.Join([]string{ - "/home/user/venv/bin", // virtualenv - should be in prefix - "/home/user/.version-fox/sdks/python/bin", // vfox path - should be removed - "/home/user/.version-fox/sdks/nodejs/bin", // vfox path - should be removed - "/usr/local/bin", // system path - should be in clean - "/usr/bin", // system path - should be in clean + "/home/user/venv/bin", // virtualenv - should be in prefix + "/home/user/.version-fox/sdks/python/bin", // vfox path - should be removed + "/home/user/.version-fox/sdks/nodejs/bin", // vfox path - should be removed + "/usr/local/bin", // system path - should be in clean + "/usr/bin", // system path - should be in clean }, ":"), wantPrefix: []string{"/home/user/venv/bin"}, wantClean: []string{"/usr/local/bin", "/usr/bin"}, @@ -56,10 +56,10 @@ func TestSplitSystemPaths(t *testing.T) { { name: "multiple paths before vfox", pathEnv: strings.Join([]string{ - "/home/user/venv/bin", // user path - prefix - "/home/user/.local/bin", // user path - prefix - "/home/user/.version-fox/sdks/python/bin", // vfox path - removed - "/usr/bin", // system path - clean + "/home/user/venv/bin", // user path - prefix + "/home/user/.local/bin", // user path - prefix + "/home/user/.version-fox/sdks/python/bin", // vfox path - removed + "/usr/bin", // system path - clean }, ":"), wantPrefix: []string{"/home/user/venv/bin", "/home/user/.local/bin"}, wantClean: []string{"/usr/bin"}, @@ -67,9 +67,9 @@ func TestSplitSystemPaths(t *testing.T) { { name: "no paths before vfox", pathEnv: strings.Join([]string{ - "/home/user/.version-fox/sdks/python/bin", // vfox path - removed - "/usr/local/bin", // system path - clean - "/usr/bin", // system path - clean + "/home/user/.version-fox/sdks/python/bin", // vfox path - removed + "/usr/local/bin", // system path - clean + "/usr/bin", // system path - clean }, ":"), wantPrefix: []string{}, wantClean: []string{"/usr/local/bin", "/usr/bin"}, @@ -87,16 +87,16 @@ func TestSplitSystemPaths(t *testing.T) { { name: "project vfox path (.vfox directory)", pathEnv: strings.Join([]string{ - "/home/user/venv/bin", // virtualenv - prefix - "/home/user/project/.vfox/sdks/python/bin", // project vfox path - removed - "/usr/bin", // system path - clean + "/home/user/venv/bin", // virtualenv - prefix + "/home/user/project/.vfox/sdks/python/bin", // project vfox path - removed + "/usr/bin", // system path - clean }, ":"), wantPrefix: []string{"/home/user/venv/bin"}, wantClean: []string{"/usr/bin"}, }, { - name: "empty PATH", - pathEnv: "", + name: "empty PATH", + pathEnv: "", wantPrefix: []string{}, wantClean: []string{}, }, @@ -163,11 +163,11 @@ func TestSplitSystemPaths_VirtualenvPriority(t *testing.T) { // Simulate: user activated virtualenv, then vfox hook runs // The virtualenv path appears before vfox paths testPath := strings.Join([]string{ - "/root/venv/bin", // virtualenv activated by user - "/root/.version-fox/sdks/python/bin", // vfox global python - "/root/.version-fox/sdks/protobuf/bin", // vfox global protobuf - "/root/.local/bin", // user local bin - "/usr/local/bin", // system paths + "/root/venv/bin", // virtualenv activated by user + "/root/.version-fox/sdks/python/bin", // vfox global python + "/root/.version-fox/sdks/protobuf/bin", // vfox global protobuf + "/root/.local/bin", // user local bin + "/usr/local/bin", // system paths "/usr/bin", }, ":") os.Setenv("PATH", testPath) @@ -243,7 +243,11 @@ func TestHttpClient_SendsVfoxUserAgent(t *testing.T) { UserConfig: &config.Config{Proxy: config.EmptyProxy}, RuntimeVersion: "1.0.13", } - resp, err := ctx.HttpClient().Get(srv.URL) + client, err := ctx.HttpClient() + if err != nil { + t.Fatalf("HttpClient() error = %v", err) + } + resp, err := client.Get(srv.URL) if err != nil { t.Fatalf("Get() error = %v", err) } @@ -256,3 +260,49 @@ func TestHttpClient_SendsVfoxUserAgent(t *testing.T) { t.Fatalf("server saw User-Agent %q, want vfox/1.0.13", got) } } + +func TestHttpClient_SendsVfoxUserAgentThroughProxy(t *testing.T) { + var gotURI, gotAgent string + proxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + gotURI = r.RequestURI + gotAgent = r.UserAgent() + })) + defer proxy.Close() + + ctx := &RuntimeEnvContext{ + UserConfig: &config.Config{Proxy: &config.Proxy{Enable: true, Url: proxy.URL}}, + RuntimeVersion: "1.0.13", + } + client, err := ctx.HttpClient() + if err != nil { + t.Fatalf("HttpClient() error = %v", err) + } + // The host is never resolved: the request stops at the proxy. + resp, err := client.Get("http://vfox.invalid/index.json") + if err != nil { + t.Fatalf("Get() error = %v", err) + } + defer resp.Body.Close() + if gotURI != "http://vfox.invalid/index.json" { + t.Fatalf("proxy saw request URI %q, want http://vfox.invalid/index.json", gotURI) + } + if gotAgent != "vfox/1.0.13" { + t.Fatalf("proxy saw User-Agent %q, want vfox/1.0.13", gotAgent) + } +} + +func TestHttpClient_InvalidProxyURL(t *testing.T) { + ctx := &RuntimeEnvContext{ + UserConfig: &config.Config{Proxy: &config.Proxy{Enable: true, Url: "http://user:secret@proxy.example.com:80%80"}}, + } + client, err := ctx.HttpClient() + if err == nil { + t.Fatalf("HttpClient() = %v, want an error", client) + } + if client != nil { + t.Fatalf("HttpClient() returned a client together with error %v", err) + } + if strings.Contains(err.Error(), "secret") { + t.Fatalf("error contains the proxy password: %v", err) + } +} diff --git a/internal/manager.go b/internal/manager.go index e664559b..cbe82abc 100644 --- a/internal/manager.go +++ b/internal/manager.go @@ -386,7 +386,11 @@ func (m *Manager) Update(pluginName string) error { func (m *Manager) fetchPluginManifest(url string) (*RegistryPluginManifest, error) { logger.Debugf("Fetching plugin manifest from: %s\n", util.RedactSourceURL(url)) - resp, err := m.RuntimeEnvContext.HttpClient().Get(url) + client, err := m.RuntimeEnvContext.HttpClient() + if err != nil { + return nil, fmt.Errorf("fetch manifest error: %w", err) + } + resp, err := client.Get(url) if err != nil { err = util.RedactError(err) logger.Debugf("Failed to fetch manifest: %v\n", err) @@ -448,7 +452,11 @@ func (m *Manager) downloadPlugin(downloadUrl string) (string, error) { logger.Debugf("Failed to create HTTP request: %v\n", err) return "", err } - resp, err := m.RuntimeEnvContext.HttpClient().Do(req) + client, err := m.RuntimeEnvContext.HttpClient() + if err != nil { + return "", err + } + resp, err := client.Do(req) if err != nil { var urlErr *url.Error if errors.As(err, &urlErr) { @@ -667,7 +675,10 @@ func (m *Manager) installPluginToTemp(path string) (*plugin.Wrapper, error) { } func (m *Manager) Available() (RegistryIndex, error) { - client := m.RuntimeEnvContext.HttpClient() + client, err := m.RuntimeEnvContext.HttpClient() + if err != nil { + return nil, fmt.Errorf("get plugin index error: %w", err) + } resp, err := client.Get(m.GetRegistryAddress("index.json")) if err != nil { return nil, fmt.Errorf("get plugin index error: %w", util.RedactError(err)) diff --git a/internal/sdk/download_test.go b/internal/sdk/download_test.go index 2d086e77..7c6a8060 100644 --- a/internal/sdk/download_test.go +++ b/internal/sdk/download_test.go @@ -285,3 +285,15 @@ func TestSdk_Download_UserAgent(t *testing.T) { }) } } + +func TestSdk_Download_InvalidProxyURL(t *testing.T) { + sdk := newDownloadTestSDK(t) + sdk.envContext.UserConfig.Proxy = &config.Proxy{Enable: true, Url: "http://user:secret@proxy.example.com:80%80"} + u, err := url.Parse("http://vfox.invalid/repro.zip") + if err != nil { + t.Fatalf("parse url: %v", err) + } + if _, err = sdk.Download(u, nil); err == nil { + t.Fatal("Download() error = nil, want an error for the proxy URL") + } +} diff --git a/internal/sdk/sdk.go b/internal/sdk/sdk.go index ab8b1d6c..f6cb8d01 100644 --- a/internal/sdk/sdk.go +++ b/internal/sdk/sdk.go @@ -1094,7 +1094,11 @@ func (b *impl) Download(u *url.URL, headers map[string]string) (string, error) { if req.Header.Get("User-Agent") == "" && b.plugin != nil && b.plugin.Metadata != nil { req.Header.Set("User-Agent", util.UserAgent(b.envContext.RuntimeVersion, b.plugin.Name, b.plugin.Version)) } - resp, err := b.envContext.HttpClient().Do(req) + client, err := b.envContext.HttpClient() + if err != nil { + return "", err + } + resp, err := client.Do(req) if err != nil { var urlErr *url.Error if errors.As(err, &urlErr) { diff --git a/internal/shared/util/useragent_test.go b/internal/shared/util/useragent_test.go index 81a10957..991089cf 100644 --- a/internal/shared/util/useragent_test.go +++ b/internal/shared/util/useragent_test.go @@ -17,6 +17,7 @@ package util import ( + "io" "net/http" "net/http/httptest" "testing" @@ -74,10 +75,12 @@ func TestWithDefaultUserAgent(t *testing.T) { t.Fatalf("Do() error = %v", err) } defer resp.Body.Close() - got := make([]byte, 64) - n, _ := resp.Body.Read(got) - if string(got[:n]) != tt.want { - t.Fatalf("server saw User-Agent %q, want %q", got[:n], tt.want) + got, err := io.ReadAll(resp.Body) + if err != nil { + t.Fatalf("read body: %v", err) + } + if string(got) != tt.want { + t.Fatalf("server saw User-Agent %q, want %q", got, tt.want) } if tt.header == nil && req.Header.Get("User-Agent") != "" { t.Fatalf("the caller's request was modified: %v", req.Header)