diff --git a/internal/cli/cli_test.go b/internal/cli/cli_test.go index 5e697e1..e88dd74 100644 --- a/internal/cli/cli_test.go +++ b/internal/cli/cli_test.go @@ -24,6 +24,7 @@ func TestConfigCommandsWriteAndShowMaskedEffectiveConfig(t *testing.T) { runCLI(t, paths, "global", "set-url", "https://global.example") runCLI(t, paths, "global", "set-token", "perm:global-secret") runCLI(t, paths, "set-url", "https://local.example") + runCLI(t, paths, "set-token", "perm:local-secret") runCLI(t, paths, "set-project-id", "0-1") out := runCLI(t, paths, "show") @@ -37,11 +38,41 @@ func TestConfigCommandsWriteAndShowMaskedEffectiveConfig(t *testing.T) { if !strings.Contains(out, "project_id: 0-1") { t.Fatalf("show output = %q, want local project_id", out) } - if strings.Contains(out, "global-secret") { + if strings.Contains(out, "global-secret") || strings.Contains(out, "local-secret") { t.Fatalf("show output leaked full token: %q", out) } } +func TestLocalURLDoesNotReceiveGlobalToken(t *testing.T) { + temp := t.TempDir() + paths := config.Paths{ + Global: filepath.Join(temp, "global", "config.json"), + Local: filepath.Join(temp, "project", ".ytrack", "config.json"), + } + requests := 0 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + requests++ + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte(`[]`)) + })) + defer server.Close() + + runCLI(t, paths, "global", "set-url", "https://global.example") + runCLI(t, paths, "global", "set-token", "perm:global-secret") + runCLI(t, paths, "set-url", server.URL) + runCLI(t, paths, "set-project-id", "0-1") + + cmd := NewRootCommand(Options{Paths: paths, In: strings.NewReader(""), Out: &bytes.Buffer{}, Err: &bytes.Buffer{}}) + cmd.SetArgs([]string{"issue", "list"}) + err := cmd.Execute() + if err == nil || !strings.Contains(err.Error(), "missing configured token") { + t.Fatalf("issue list error = %v, want missing configured token", err) + } + if requests != 0 { + t.Fatalf("local server received %d requests, want none", requests) + } +} + func TestInitPromptsAndWritesLocalProjectConfig(t *testing.T) { temp := t.TempDir() paths := config.Paths{ diff --git a/internal/config/config.go b/internal/config/config.go index 0d9e921..241a1a7 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -92,6 +92,11 @@ func Merge(global, local Config) Config { } if local.URL != "" { merged.URL = local.URL + // The global token belongs to the global URL. Never send it to a + // different server configured by a (possibly untrusted) local file. + if !sameURL(local.URL, global.URL) { + merged.Token = "" + } } if local.Token != "" { merged.Token = local.Token @@ -100,6 +105,10 @@ func Merge(global, local Config) Config { return merged } +func sameURL(a, b string) bool { + return strings.EqualFold(strings.TrimRight(a, "/"), strings.TrimRight(b, "/")) +} + func MaskToken(token string) string { if token == "" { return "" diff --git a/internal/config/config_test.go b/internal/config/config_test.go index f83fecc..dcbe97d 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -31,6 +31,31 @@ func TestMergePrefersLocalValuesAndKeepsProjectLocalOnly(t *testing.T) { } } +func TestMergeDoesNotSendGlobalTokenToLocalURL(t *testing.T) { + global := Config{URL: "https://global.example", Token: "global-token"} + local := Config{URL: "https://attacker.example", ProjectID: "LOCAL"} + + got := Merge(global, local) + + if got.URL != "https://attacker.example" { + t.Fatalf("URL = %q, want local override", got.URL) + } + if got.Token != "" { + t.Fatalf("Token = %q, want empty so the global token is not sent to a different URL", got.Token) + } +} + +func TestMergeKeepsGlobalTokenWhenLocalURLMatchesGlobal(t *testing.T) { + global := Config{URL: "https://youtrack.example", Token: "global-token"} + local := Config{URL: "https://YouTrack.example/", ProjectID: "LOCAL"} + + got := Merge(global, local) + + if got.Token != "global-token" { + t.Fatalf("Token = %q, want global token for the same URL", got.Token) + } +} + func TestMergeDoesNotUseGlobalProjectID(t *testing.T) { got := Merge(Config{ProjectID: "GLOBAL"}, Config{})