From e8e9a31a6b7849c42edf95ebc9be07b9095d128a Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Mon, 20 Jan 2025 10:11:12 +0100 Subject: [PATCH] Fixed panic if malformed 3rd party url is specified --- commands/instances.go | 18 +++++++++--------- internal/integrationtest/config/config_test.go | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/commands/instances.go b/commands/instances.go index 368f54813ce..d5534494bae 100644 --- a/commands/instances.go +++ b/commands/instances.go @@ -534,9 +534,9 @@ func (s *arduinoCoreServerImpl) UpdateIndex(req *rpc.UpdateIndexRequest, stream return &cmderrors.InvalidInstanceError{} } - report := func(indexURL *url.URL, status rpc.IndexUpdateReport_Status) *rpc.IndexUpdateReport { + report := func(indexURL string, status rpc.IndexUpdateReport_Status) *rpc.IndexUpdateReport { return &rpc.IndexUpdateReport{ - IndexUrl: indexURL.String(), + IndexUrl: indexURL, Status: status, } } @@ -564,7 +564,7 @@ func (s *arduinoCoreServerImpl) UpdateIndex(req *rpc.UpdateIndexRequest, stream downloadCB.Start(u, i18n.Tr("Downloading index: %s", u)) downloadCB.End(false, msg) failed = true - result.UpdatedIndexes = append(result.GetUpdatedIndexes(), report(URL, rpc.IndexUpdateReport_STATUS_FAILED)) + result.UpdatedIndexes = append(result.GetUpdatedIndexes(), report(u, rpc.IndexUpdateReport_STATUS_FAILED)) continue } @@ -582,9 +582,9 @@ func (s *arduinoCoreServerImpl) UpdateIndex(req *rpc.UpdateIndexRequest, stream downloadCB.Start(u, i18n.Tr("Downloading index: %s", filepath.Base(URL.Path))) downloadCB.End(false, msg) failed = true - result.UpdatedIndexes = append(result.GetUpdatedIndexes(), report(URL, rpc.IndexUpdateReport_STATUS_FAILED)) + result.UpdatedIndexes = append(result.GetUpdatedIndexes(), report(u, rpc.IndexUpdateReport_STATUS_FAILED)) } else { - result.UpdatedIndexes = append(result.GetUpdatedIndexes(), report(URL, rpc.IndexUpdateReport_STATUS_SKIPPED)) + result.UpdatedIndexes = append(result.GetUpdatedIndexes(), report(u, rpc.IndexUpdateReport_STATUS_SKIPPED)) } continue } @@ -596,14 +596,14 @@ func (s *arduinoCoreServerImpl) UpdateIndex(req *rpc.UpdateIndexRequest, stream downloadCB.Start(u, i18n.Tr("Downloading index: %s", filepath.Base(URL.Path))) downloadCB.End(false, i18n.Tr("Invalid index URL: %s", err)) failed = true - result.UpdatedIndexes = append(result.GetUpdatedIndexes(), report(URL, rpc.IndexUpdateReport_STATUS_FAILED)) + result.UpdatedIndexes = append(result.GetUpdatedIndexes(), report(u, rpc.IndexUpdateReport_STATUS_FAILED)) continue } indexFile := indexpath.Join(indexFileName) if info, err := indexFile.Stat(); err == nil { ageSecs := int64(time.Since(info.ModTime()).Seconds()) if ageSecs < req.GetUpdateIfOlderThanSecs() { - result.UpdatedIndexes = append(result.GetUpdatedIndexes(), report(URL, rpc.IndexUpdateReport_STATUS_ALREADY_UP_TO_DATE)) + result.UpdatedIndexes = append(result.GetUpdatedIndexes(), report(u, rpc.IndexUpdateReport_STATUS_ALREADY_UP_TO_DATE)) continue } } @@ -622,9 +622,9 @@ func (s *arduinoCoreServerImpl) UpdateIndex(req *rpc.UpdateIndexRequest, stream } if err := indexResource.Download(stream.Context(), indexpath, downloadCB, config); err != nil { failed = true - result.UpdatedIndexes = append(result.GetUpdatedIndexes(), report(URL, rpc.IndexUpdateReport_STATUS_FAILED)) + result.UpdatedIndexes = append(result.GetUpdatedIndexes(), report(u, rpc.IndexUpdateReport_STATUS_FAILED)) } else { - result.UpdatedIndexes = append(result.GetUpdatedIndexes(), report(URL, rpc.IndexUpdateReport_STATUS_UPDATED)) + result.UpdatedIndexes = append(result.GetUpdatedIndexes(), report(u, rpc.IndexUpdateReport_STATUS_UPDATED)) } } syncSend.Send(&rpc.UpdateIndexResponse{ diff --git a/internal/integrationtest/config/config_test.go b/internal/integrationtest/config/config_test.go index bd19d0545c2..d7b4104a095 100644 --- a/internal/integrationtest/config/config_test.go +++ b/internal/integrationtest/config/config_test.go @@ -944,3 +944,18 @@ func TestI18N(t *testing.T) { require.NoError(t, err) require.Contains(t, string(out), "Available Commands") } + +func TestCoreUpdateWithInvalidIndexURL(t *testing.T) { + // https://github.com/arduino/arduino-cli/issues/2786 + env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t) + t.Cleanup(env.CleanUp) + + _, _, err := cli.Run("config", "init") + require.NoError(t, err) + _, _, err = cli.Run("config", "set", "board_manager.additional_urls", "foo=https://espressif.github.io/arduino-esp32/package_esp32_index.json") + require.NoError(t, err) + _, stdErr, err := cli.Run("core", "update-index") + require.Error(t, err) + require.Contains(t, string(stdErr), `Error initializing instance: Some indexes could not be updated.`) + require.Contains(t, string(stdErr), `Invalid additional URL: parse "foo=https://espressif.github.io/arduino-esp32/package_esp32_index.json"`) +}