From 40697e03f3adffe5e40357db675700755c9b84cf Mon Sep 17 00:00:00 2001 From: Willie Date: Fri, 13 Nov 2026 14:50:31 +0800 Subject: [PATCH] fix(oplog): keep source credentials out of the operation log (#881) An install source typed as a URL can carry a token in its userinfo or query, and operations.log stored it as typed, so anyone reading the log or the dashboard's log view could see the credential (#878). Redact in oplog.Write, which every CLI and dashboard writer goes through: the source, oldSource and newSource args lose their userinfo, query and fragment, and the message loses the same parts where an error echoes them. The redaction reuses update's redactRepoURL, moved to install.RedactRepoURL. It now also covers git+https:// and ssh:// passwords, and a source typed without a scheme such as user:token@host/owner/repo, which install clones over HTTPS. logInstallOp logged args[0] as the source, which is a flag when one comes first (install --into team logged "--into"), so the message was never redacted. It now logs the parsed source. Fixes #878 --- cmd/skillshare/install.go | 18 +++++---- cmd/skillshare/search.go | 4 +- cmd/skillshare/search_batch.go | 2 +- cmd/skillshare/update_batch.go | 41 +------------------- cmd/skillshare/update_batch_redact_test.go | 40 ------------------- internal/install/auth.go | 45 ++++++++++++++++++++++ internal/install/auth_test.go | 38 ++++++++++++++++++ internal/oplog/oplog.go | 17 +++++++- internal/oplog/oplog_test.go | 38 ++++++++++++++++++ tests/integration/log_test.go | 24 ++++++++++++ 10 files changed, 175 insertions(+), 92 deletions(-) delete mode 100644 cmd/skillshare/update_batch_redact_test.go diff --git a/cmd/skillshare/install.go b/cmd/skillshare/install.go index 39e022c65..f7081651f 100644 --- a/cmd/skillshare/install.go +++ b/cmd/skillshare/install.go @@ -425,7 +425,7 @@ func cmdInstall(args []string) error { if summary.Mode == "" { summary.Mode = "project" } - logInstallOp(config.ProjectConfigPath(cwd), rest, start, err, summary) + logInstallOp(config.ProjectConfigPath(cwd), parsed.sourceArg, start, err, summary) if parsed.jsonOutput { return jsonWriteResult(summary, err) } @@ -483,7 +483,7 @@ func cmdInstall(args []string) error { // No source argument: install from global config if parsed.sourceArg == "" { summary, err := installFromGlobalConfig(cfg, parsed.opts) - logInstallOp(config.ConfigPath(), rest, start, err, summary) + logInstallOp(config.ConfigPath(), parsed.sourceArg, start, err, summary) if parsed.jsonOutput { return jsonWriteResult(summary, err) } @@ -503,7 +503,7 @@ func cmdInstall(args []string) error { source.Raw = source.Path } if err != nil { - logInstallOp(config.ConfigPath(), rest, start, err, installLogSummary{ + logInstallOp(config.ConfigPath(), parsed.sourceArg, start, err, installLogSummary{ Source: parsed.sourceArg, Mode: "global", }) @@ -541,7 +541,7 @@ func cmdInstall(args []string) error { ui.Warning("Failed to reconcile global skills config: %v", rErr) } } - logInstallOp(config.ConfigPath(), rest, start, err, summary) + logInstallOp(config.ConfigPath(), parsed.sourceArg, start, err, summary) if parsed.jsonOutput { return jsonWriteResult(summary, err) } @@ -563,7 +563,7 @@ func cmdInstall(args []string) error { ui.Warning("Failed to reconcile global skills config: %v", rErr) } } - logInstallOp(config.ConfigPath(), rest, start, err, summary) + logInstallOp(config.ConfigPath(), parsed.sourceArg, start, err, summary) if parsed.jsonOutput { return jsonWriteResult(summary, err) } @@ -590,7 +590,9 @@ func installOutputJSON(summary installLogSummary, start time.Time, installErr er return writeJSONResult(&output, installErr) } -func logInstallOp(cfgPath string, args []string, start time.Time, cmdErr error, summary installLogSummary) { +// logInstallOp logs sourceArg, the source as typed, or summary.Source when +// none was given. +func logInstallOp(cfgPath, sourceArg string, start time.Time, cmdErr error, summary installLogSummary) { status := statusFromErr(cmdErr) if len(summary.InstalledSkills) > 0 && len(summary.FailedSkills) > 0 { status = "partial" @@ -598,8 +600,8 @@ func logInstallOp(cfgPath string, args []string, start time.Time, cmdErr error, e := oplog.NewEntry("install", status, time.Since(start)) fields := map[string]any{} source := summary.Source - if len(args) > 0 { - source = args[0] + if sourceArg != "" { + source = sourceArg } if source != "" { fields["source"] = source diff --git a/cmd/skillshare/search.go b/cmd/skillshare/search.go index e934cf1bd..0e46c8bb8 100644 --- a/cmd/skillshare/search.go +++ b/cmd/skillshare/search.go @@ -403,7 +403,7 @@ func installFromSearchResultProject(result search.SearchResult, cwd string) (err Mode: "project", } defer func() { - logInstallOp(config.ProjectConfigPath(cwd), []string{result.Source}, start, err, logSummary) + logInstallOp(config.ProjectConfigPath(cwd), result.Source, start, err, logSummary) }() if err := ensureProjectConfig(cwd); err != nil { @@ -485,7 +485,7 @@ func installFromSearchResult(result search.SearchResult, cfg *config.Config) (er Mode: "global", } defer func() { - logInstallOp(config.ConfigPath(), []string{result.Source}, start, err, logSummary) + logInstallOp(config.ConfigPath(), result.Source, start, err, logSummary) }() // Parse source diff --git a/cmd/skillshare/search_batch.go b/cmd/skillshare/search_batch.go index 0a8a32ea3..556d0db51 100644 --- a/cmd/skillshare/search_batch.go +++ b/cmd/skillshare/search_batch.go @@ -642,7 +642,7 @@ func logSkillInstallOp(cfgPath string, sr search.SearchResult, r searchInstallRe } else if r.status == "failed" { logSummary.FailedSkills = []string{r.name} } - logInstallOp(cfgPath, []string{sr.Source}, start, opErr, logSummary) + logInstallOp(cfgPath, sr.Source, start, opErr, logSummary) } // classifyFailureDetail parses a batch install error detail and returns: diff --git a/cmd/skillshare/update_batch.go b/cmd/skillshare/update_batch.go index d7b783f67..1746cd89e 100644 --- a/cmd/skillshare/update_batch.go +++ b/cmd/skillshare/update_batch.go @@ -442,45 +442,6 @@ func pruneRegistry(prunedNames []string, uc *updateContext) { // HTTPS repo URL can carry a token as userinfo or in its query, and git's error // text echoes the URL, so both are redacted before they reach the terminal. func fetchFailedEntry(repoURL string, err error) batchBlockedEntry { - name, redact := redactRepoURL(repoURL) + name, redact := install.RedactRepoURL(repoURL) return batchBlockedEntry{name: name, errMsg: redact.Replace(err.Error())} } - -// redactRepoURL returns an http(s) repoURL without its userinfo, query and -// fragment, plus a replacer that removes those parts from text echoing the -// URL. Userinfo is taken up to the last "@" before the first "/", so a "?" or -// "#" inside a password cannot hide it while an "@" in the path stays. Other -// URLs (file, ssh, scp-style, local paths) come back unchanged. -func redactRepoURL(repoURL string) (string, *strings.Replacer) { - scheme, rest, ok := strings.Cut(repoURL, "://") - if !ok || !strings.EqualFold(scheme, "http") && !strings.EqualFold(scheme, "https") { - return repoURL, strings.NewReplacer() - } - var pairs []string - host, path := rest, "" - if i := strings.IndexByte(rest, '/'); i >= 0 { - host, path = rest[:i], rest[i:] - } - if at := strings.LastIndex(host, "@"); at > 0 { - userinfo := host[:at] - host = host[at+1:] - pairs = append(pairs, userinfo+"@", "") - if _, password, ok := strings.Cut(userinfo, ":"); ok && password != "" { - pairs = append(pairs, password, "[REDACTED]") - } - } - rest = host + path - if i := strings.IndexByte(rest, '#'); i >= 0 { - if len(rest)-i > 1 { - pairs = append(pairs, rest[i:], "") - } - rest = rest[:i] - } - if i := strings.IndexByte(rest, '?'); i >= 0 { - if query := rest[i+1:]; query != "" { - pairs = append(pairs, rest[i:], "", query, "[REDACTED]") - } - rest = rest[:i] - } - return scheme + "://" + rest, strings.NewReplacer(pairs...) -} diff --git a/cmd/skillshare/update_batch_redact_test.go b/cmd/skillshare/update_batch_redact_test.go deleted file mode 100644 index fe117c7a5..000000000 --- a/cmd/skillshare/update_batch_redact_test.go +++ /dev/null @@ -1,40 +0,0 @@ -package main - -import ( - "errors" - "strings" - "testing" -) - -func TestFetchFailedEntry_RedactsRepoURL(t *testing.T) { - tests := []struct { - name, repoURL, wantName string - leaks []string - }{ - {"userinfo", "https://someone:s3cret-token@git.example.com/owner/skills.git", "https://git.example.com/owner/skills.git", []string{"s3cret-token", "someone"}}, - {"token as username", "https://s3cret-token@git.example.com/owner/skills.git", "https://git.example.com/owner/skills.git", []string{"s3cret-token"}}, - {"query token", "https://git.example.com/owner/skills?token=s3cret-token.git", "https://git.example.com/owner/skills", []string{"s3cret-token"}}, - {"fragment", "https://git.example.com/owner/skills.git#s3cret-token", "https://git.example.com/owner/skills.git", []string{"s3cret-token"}}, - {"question mark in password", "https://someone:s3cret?token@git.example.com/owner/skills.git", "https://git.example.com/owner/skills.git", []string{"s3cret?token", "someone"}}, - {"at sign in https path", "https://git.example.com/team@archive/skills.git", "https://git.example.com/team@archive/skills.git", nil}, - {"at sign in file path", "file:///tmp/team@archive.git", "file:///tmp/team@archive.git", nil}, - {"scp style", "git@github.com:owner/skills.git", "git@github.com:owner/skills.git", nil}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - err := errors.New("fatal: unable to access '" + tt.repoURL + "/': Failed to connect") - got := fetchFailedEntry(tt.repoURL, err) - if got.name != tt.wantName { - t.Errorf("name = %q, want %q", got.name, tt.wantName) - } - for _, leak := range tt.leaks { - if strings.Contains(got.name, leak) || strings.Contains(got.errMsg, leak) { - t.Errorf("leaked %q: name=%q errMsg=%q", leak, got.name, got.errMsg) - } - } - if !strings.Contains(got.errMsg, "Failed to connect") { - t.Errorf("errMsg lost the git error: %q", got.errMsg) - } - }) - } -} diff --git a/internal/install/auth.go b/internal/install/auth.go index 9c867408f..f3f45334c 100644 --- a/internal/install/auth.go +++ b/internal/install/auth.go @@ -288,6 +288,51 @@ func sanitizeTokens(text string) string { return text } +// RedactRepoURL returns repoURL without its userinfo, query and fragment, plus +// a replacer that removes those parts from text echoing the URL. Userinfo is +// taken up to the last "@" before the first "/", so a "?" or "#" inside a +// password cannot hide it while an "@" in the path stays. A source without a +// scheme, such as "user:token@host/owner/repo", is cloned over HTTPS (see +// expandGitHubShorthand) and redacted as such; scp-style URLs and local paths +// come back unchanged. +func RedactRepoURL(repoURL string) (string, *strings.Replacer) { + scheme, rest, ok := strings.Cut(repoURL, "://") + if !ok { + if isLocalPath(repoURL) || gitSSHPattern.MatchString(repoURL) { + return repoURL, strings.NewReplacer() + } + clean, redact := RedactRepoURL("https://" + repoURL) + return strings.TrimPrefix(clean, "https://"), redact + } + var pairs []string + host, path := rest, "" + if i := strings.IndexByte(rest, '/'); i >= 0 { + host, path = rest[:i], rest[i:] + } + if at := strings.LastIndex(host, "@"); at > 0 { + userinfo := host[:at] + host = host[at+1:] + pairs = append(pairs, userinfo+"@", "") + if _, password, ok := strings.Cut(userinfo, ":"); ok && password != "" { + pairs = append(pairs, password, "[REDACTED]") + } + } + rest = host + path + if i := strings.IndexByte(rest, '#'); i >= 0 { + if len(rest)-i > 1 { + pairs = append(pairs, rest[i:], "") + } + rest = rest[:i] + } + if i := strings.IndexByte(rest, '?'); i >= 0 { + if query := rest[i+1:]; query != "" { + pairs = append(pairs, rest[i:], "", query, "[REDACTED]") + } + rest = rest[:i] + } + return scheme + "://" + rest, strings.NewReplacer(pairs...) +} + // urlUsername extracts the username from an HTTPS URL, if present. // Returns "" for URLs without userinfo (e.g. https://github.com/...). func urlUsername(cloneURL string) string { diff --git a/internal/install/auth_test.go b/internal/install/auth_test.go index 5a66cdb99..2cf2ab7b4 100644 --- a/internal/install/auth_test.go +++ b/internal/install/auth_test.go @@ -585,3 +585,41 @@ func TestProviderCredentialsRejectUntrustedOrigins(t *testing.T) { }) } } + +func TestRedactRepoURL(t *testing.T) { + tests := []struct { + name, repoURL, want string + leaks []string + }{ + {"userinfo", "https://someone:s3cret-token@git.example.com/owner/skills.git", "https://git.example.com/owner/skills.git", []string{"s3cret-token", "someone"}}, + {"token as username", "https://s3cret-token@git.example.com/owner/skills.git", "https://git.example.com/owner/skills.git", []string{"s3cret-token"}}, + {"query token", "https://git.example.com/owner/skills?token=s3cret-token.git", "https://git.example.com/owner/skills", []string{"s3cret-token"}}, + {"fragment", "https://git.example.com/owner/skills.git#s3cret-token", "https://git.example.com/owner/skills.git", []string{"s3cret-token"}}, + {"question mark in password", "https://someone:s3cret?token@git.example.com/owner/skills.git", "https://git.example.com/owner/skills.git", []string{"s3cret?token", "someone"}}, + {"git+https scheme", "git+https://someone:s3cret-token@git.example.com/owner/skills.git", "git+https://git.example.com/owner/skills.git", []string{"s3cret-token", "someone"}}, + {"ssh password", "ssh://someone:s3cret-token@git.example.com/owner/skills.git", "ssh://git.example.com/owner/skills.git", []string{"s3cret-token", "someone"}}, + {"no scheme, cloned over https", "someone:s3cret-token@git.example.com/owner/skills", "git.example.com/owner/skills", []string{"s3cret-token", "someone"}}, + {"at sign in https path", "https://git.example.com/team@archive/skills.git", "https://git.example.com/team@archive/skills.git", nil}, + {"at sign in file path", "file:///tmp/team@archive.git", "file:///tmp/team@archive.git", nil}, + {"scp style", "git@github.com:owner/skills.git", "git@github.com:owner/skills.git", nil}, + {"github shorthand", "anthropics/skills/skills/pdf", "anthropics/skills/skills/pdf", nil}, + {"local path", "~/skills/team@archive", "~/skills/team@archive", nil}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, redact := RedactRepoURL(tt.repoURL) + if got != tt.want { + t.Errorf("RedactRepoURL() = %q, want %q", got, tt.want) + } + echoed := redact.Replace("fatal: unable to access '" + tt.repoURL + "/': Failed to connect") + for _, leak := range tt.leaks { + if strings.Contains(echoed, leak) { + t.Errorf("replacer leaked %q: %q", leak, echoed) + } + } + if !strings.Contains(echoed, "Failed to connect") { + t.Errorf("replacer lost the error text: %q", echoed) + } + }) + } +} diff --git a/internal/oplog/oplog.go b/internal/oplog/oplog.go index 6ba408e46..5aec30089 100644 --- a/internal/oplog/oplog.go +++ b/internal/oplog/oplog.go @@ -5,6 +5,7 @@ package oplog import ( "bufio" "encoding/json" + "maps" "os" "path/filepath" "time" @@ -64,8 +65,22 @@ func IsProjectConfig(configPath string) bool { return ok } -// Write appends a single JSONL entry to the named log file. +// sourceArgs are the args that hold a source as the user typed it. +var sourceArgs = []string{"source", "oldSource", "newSource"} + +// Write appends a single JSONL entry to the named log file. A source keeps no +// credentials: its userinfo and query are dropped, also where the message +// echoes them. func Write(configPath, filename string, e Entry) error { + e.Args = maps.Clone(e.Args) + for _, key := range sourceArgs { + if source, ok := e.Args[key].(string); ok { + clean, redact := install.RedactRepoURL(source) + e.Args[key] = clean + e.Message = redact.Replace(e.Message) + } + } + dir := LogDir(configPath) dirInfo, statErr := os.Stat(dir) logsDirMissing := os.IsNotExist(statErr) diff --git a/internal/oplog/oplog_test.go b/internal/oplog/oplog_test.go index a580f5a6c..92dd91cbc 100644 --- a/internal/oplog/oplog_test.go +++ b/internal/oplog/oplog_test.go @@ -166,6 +166,44 @@ func TestWriteWithArgs(t *testing.T) { } } +// A source typed as a URL can carry credentials in its userinfo or query, and +// git echoes the query in its error text (issue #878). +func TestWrite_RedactsSourceCredentials(t *testing.T) { + cfgPath := tempConfigPath(t) + + e := Entry{ + Command: "install", + Args: map[string]any{"source": "https://someone:s3cret@git.example.com/team/skills.git?token=q-s3cret"}, + Status: "error", + Message: "fatal: unable to access 'https://git.example.com/team/skills.git?token=q-s3cret/': Failed to connect", + } + if err := Write(cfgPath, OpsFile, e); err != nil { + t.Fatalf("Write() error: %v", err) + } + // The dashboard's source edit logs the old and new source. + edit := Entry{ + Command: "skill.source", + Args: map[string]any{"oldSource": "https://old:s3cret@git.example.com/team/skills.git", "newSource": "https://someone:s3cret@git.example.com/team/skills.git"}, + Status: "ok", + } + if err := Write(cfgPath, OpsFile, edit); err != nil { + t.Fatalf("Write() error: %v", err) + } + + raw, err := os.ReadFile(filepath.Join(LogDir(cfgPath), OpsFile)) + if err != nil { + t.Fatalf("read log: %v", err) + } + for _, leak := range []string{"s3cret", "someone"} { + if strings.Contains(string(raw), leak) { + t.Errorf("log leaked %q: %s", leak, raw) + } + } + if !strings.Contains(string(raw), `"source":"https://git.example.com/team/skills.git"`) { + t.Errorf("log lost the source host and path: %s", raw) + } +} + func TestAuditFileIsSeparate(t *testing.T) { cfgPath := tempConfigPath(t) diff --git a/tests/integration/log_test.go b/tests/integration/log_test.go index 79e79f373..2a4206031 100644 --- a/tests/integration/log_test.go +++ b/tests/integration/log_test.go @@ -197,6 +197,30 @@ targets: logResult.AssertOutputContains(t, "installed=log-installed-skill") } +// An install source can carry credentials, also when typed without a scheme or +// after a flag, and git echoes a URL's query in its error (issue #878). +func TestLog_InstallRedactsSourceCredentials(t *testing.T) { + sb := testutil.NewSandbox(t) + defer sb.Cleanup() + + sb.WriteConfig(`source: ` + sb.SourcePath + ` +targets: + claude: + path: ` + sb.CreateTarget("claude") + ` +`) + + sb.RunCLI("install", "--into", "team", "https://someone:s3cret@127.0.0.1:1/team/skills.git?token=q-s3cret") + sb.RunCLI("install", "someone:s3cret@127.0.0.1:1/team/skills") + + logResult := sb.RunCLI("log", "--json", "--cmd", "install") + logResult.AssertSuccess(t) + for _, leak := range []string{"s3cret", "someone"} { + logResult.AssertOutputNotContains(t, leak) + } + logResult.AssertOutputContains(t, `"source":"https://127.0.0.1:1/team/skills.git"`) + logResult.AssertOutputContains(t, `"source":"127.0.0.1:1/team/skills"`) +} + func TestLog_SyncPartialStatus(t *testing.T) { sb := testutil.NewSandbox(t) defer sb.Cleanup()