mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-18 18:04:17 +00:00
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 <url> logged "--into"), so the message was never redacted. It now logs the parsed source. Fixes #878
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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...)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
+16
-1
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user