Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion internal/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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{
Expand Down
9 changes: 9 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ""
Expand Down
25 changes: 25 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})

Expand Down