From 14181acfeb2d6b21d86e67e30327e4d679b225ef Mon Sep 17 00:00:00 2001 From: aniket866 Date: Sun, 1 Nov 2026 03:33:53 +0530 Subject: [PATCH 1/2] fix: reject malformed local configuration Signed-off-by: aniket866 --- pkg/config/localconfig.go | 7 +++ pkg/config/localconfig_malformed_test.go | 59 ++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 pkg/config/localconfig_malformed_test.go diff --git a/pkg/config/localconfig.go b/pkg/config/localconfig.go index ac7dd870..29eee9df 100644 --- a/pkg/config/localconfig.go +++ b/pkg/config/localconfig.go @@ -22,6 +22,7 @@ import ( "path/filepath" "slices" + clierrors "github.com/microcks/microcks-cli/pkg/errors" configUtil "github.com/microcks/microcks-cli/pkg/util" ) @@ -104,6 +105,9 @@ func ReadLocalConfig(path string) (*LocalConfig, error) { if os.IsNotExist(err) { return nil, nil } + if err != nil { + return nil, clierrors.Wrap(clierrors.KindEnvironment, fmt.Errorf("reading local config: %w", err)) + } err = ValidateLocalConfig(config) if err != nil { @@ -417,6 +421,9 @@ func ReadLocalWatchConfig(path string) (*WatchConfig, error) { if os.IsNotExist(err) { return nil, nil } + if err != nil { + return nil, clierrors.Wrap(clierrors.KindEnvironment, fmt.Errorf("reading local watch config: %w", err)) + } return &config, nil } diff --git a/pkg/config/localconfig_malformed_test.go b/pkg/config/localconfig_malformed_test.go new file mode 100644 index 00000000..fb62eb4d --- /dev/null +++ b/pkg/config/localconfig_malformed_test.go @@ -0,0 +1,59 @@ +/* + * Copyright The Microcks Authors. + * + * 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 config + +import ( + "os" + "path/filepath" + "testing" + + clierrors "github.com/microcks/microcks-cli/pkg/errors" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestReadLocalConfigMalformedFile(t *testing.T) { + path := filepath.Join(t.TempDir(), "config") + want := []byte("current-context: [broken\n") + require.NoError(t, os.WriteFile(path, want, 0o600)) + + config, err := ReadLocalConfig(path) + + assert.Nil(t, config) + require.Error(t, err) + assert.Equal(t, clierrors.KindEnvironment, clierrors.KindOf(err)) + assert.Contains(t, err.Error(), "reading local config") + got, readErr := os.ReadFile(path) + require.NoError(t, readErr) + assert.Equal(t, want, got) +} + +func TestReadLocalWatchConfigMalformedFile(t *testing.T) { + path := filepath.Join(t.TempDir(), "watch") + want := []byte("entries: [broken\n") + require.NoError(t, os.WriteFile(path, want, 0o600)) + + config, err := ReadLocalWatchConfig(path) + + assert.Nil(t, config) + require.Error(t, err) + assert.Equal(t, clierrors.KindEnvironment, clierrors.KindOf(err)) + assert.Contains(t, err.Error(), "reading local watch config") + got, readErr := os.ReadFile(path) + require.NoError(t, readErr) + assert.Equal(t, want, got) +} From c8f59c66a705e2f3cd0bb6c91f8701c81d4a71f5 Mon Sep 17 00:00:00 2001 From: aniket866 Date: Sun, 1 Nov 2026 03:38:30 +0530 Subject: [PATCH 2/2] test: handle login response write errors Signed-off-by: aniket866 --- cmd/login_test.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/cmd/login_test.go b/cmd/login_test.go index 141ad415..73e83902 100644 --- a/cmd/login_test.go +++ b/cmd/login_test.go @@ -111,7 +111,9 @@ func TestLoginHonorsConfigFlag(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/api/keycloak/config" { w.Header().Set("Content-Type", "application/json") - _, _ = w.Write([]byte(`{"enabled": false}`)) + if _, err := w.Write([]byte(`{"enabled": false}`)); err != nil { + t.Errorf("writing Keycloak config response: %v", err) + } return } http.NotFound(w, r) @@ -147,7 +149,9 @@ func TestLoginHonorsConfigFlagWithCustomContextName(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/api/keycloak/config" { w.Header().Set("Content-Type", "application/json") - _, _ = w.Write([]byte(`{"enabled": false}`)) + if _, err := w.Write([]byte(`{"enabled": false}`)); err != nil { + t.Errorf("writing Keycloak config response: %v", err) + } return } http.NotFound(w, r) @@ -174,7 +178,9 @@ func TestLoginPreservesExistingContextsInCustomConfig(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/api/keycloak/config" { w.Header().Set("Content-Type", "application/json") - _, _ = w.Write([]byte(`{"enabled": false}`)) + if _, err := w.Write([]byte(`{"enabled": false}`)); err != nil { + t.Errorf("writing Keycloak config response: %v", err) + } return } http.NotFound(w, r)