From d8afbd1766d008d0ffcfcb0549fe9435d18531e8 Mon Sep 17 00:00:00 2001
From: neoLsH <43921685+neoLsH@users.noreply.github.com>
Date: Tue, 17 Nov 2026 17:12:35 +0800
Subject: [PATCH] Redact git config values that can carry credentials
--git-config, --git-config-add and the startup 'git config' dump print
http.extraHeader values verbatim, and that header is the documented way
to pass a token. Redact extraHeader values and password-bearing URLs in
url..insteadOf keys in all three places, including the error path
when a config fails to apply.
---
main.go | 118 ++++++++++++++++++++++++++++++++++++---
main_test.go | 152 +++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 262 insertions(+), 8 deletions(-)
diff --git a/main.go b/main.go
index 7b55be5cb..b9e1554f9 100644
--- a/main.go
+++ b/main.go
@@ -702,7 +702,7 @@ func main() {
"uid", os.Getuid(),
"gid", os.Getgid(),
"home", os.Getenv("HOME"),
- "flags", logSafeFlags(*flVerbose))
+ "flags", logSafeFlags(pflag.CommandLine, *flVerbose))
if _, err := exec.LookPath(*flGitCmd); err != nil {
log.Error(err, "FATAL: git executable not found", "git", *flGitCmd)
@@ -839,14 +839,14 @@ func main() {
// This needs to be after all other git-related config flags.
if *flGitConfigString != "" {
if err := git.SetupExtraGitConfigs(ctx, *flGitConfigString, "--git-config"); err != nil {
- log.Error(err, "can't set additional git configs", "configs", *flGitConfigString)
+ log.Error(err, "can't set additional git configs", "configs", redactGitConfigsFlag(*flGitConfigString))
os.Exit(1)
}
}
if len(*flGitConfigList) > 0 {
str := strings.Join(*flGitConfigList, ",")
if err := git.SetupExtraGitConfigs(ctx, str, "--git-config-add"); err != nil {
- log.Error(err, "can't set additional git configs", "configs", str)
+ log.Error(err, "can't set additional git configs", "configs", redactGitConfigsFlag(str))
os.Exit(1)
}
}
@@ -861,7 +861,7 @@ func main() {
log.V(0).Info("unexpected stderr reading git config", "stdout", stdout, "stderr", stderr)
os.Exit(1)
} else {
- log.V(0).Info("git config", "configs", parseGitConfigList(stdout))
+ log.V(0).Info("git config", "configs", redactGitConfigList(parseGitConfigList(stdout)))
}
// The scope of the initialization context ends here, so we call cancel to release resources associated with it.
@@ -1228,12 +1228,99 @@ func redactURL(urlstr string) string {
return u.String()
}
+// redactGitConfigKey redacts credentials embedded in a git config key. A key
+// of the form url..insteadOf holds as a URL which may carry a
+// password.
+func redactGitConfigKey(key string) string {
+ if len(key) <= len("url.") || !strings.EqualFold(key[:len("url.")], "url.") {
+ return key
+ }
+ for _, suffix := range []string{".pushInsteadOf", ".insteadOf"} {
+ end := len(key) - len(suffix)
+ if end > len("url.") && strings.EqualFold(key[end:], suffix) {
+ return "url." + redactURL(key[len("url."):end]) + key[end:]
+ }
+ }
+ return key
+}
+
+// redactGitConfigValue redacts values which may carry credentials. git config
+// can hold arbitrary secrets, but extraHeader is the documented way to pass a
+// token, and insteadOf values are URLs.
+func redactGitConfigValue(key, val string) string {
+ k := strings.ToLower(key)
+ if strings.HasSuffix(k, ".extraheader") {
+ return redactedString
+ }
+ if strings.HasSuffix(k, ".insteadof") || strings.HasSuffix(k, ".pushinsteadof") {
+ return redactURL(val)
+ }
+ return val
+}
+
+// redactGitConfigs returns a copy of kvs with sensitive keys and values
+// redacted, for logging.
+func redactGitConfigs(kvs []keyVal) []keyVal {
+ ret := make([]keyVal, 0, len(kvs))
+ for _, kv := range kvs {
+ ret = append(ret, keyVal{
+ key: redactGitConfigKey(kv.key),
+ val: redactGitConfigValue(kv.key, kv.val),
+ })
+ }
+ return ret
+}
+
+// redactGitConfigList returns a copy of kvs with sensitive keys and values
+// redacted, for logging.
+func redactGitConfigList(kvs funcr.PseudoStruct) funcr.PseudoStruct {
+ ret := make(funcr.PseudoStruct, 0, len(kvs))
+ for i := 0; i+1 < len(kvs); i += 2 {
+ key, _ := kvs[i].(string) //nolint:forcetypeassert
+ ret = append(ret, redactGitConfigKey(key))
+ if val, ok := kvs[i+1].(string); ok {
+ ret = append(ret, redactGitConfigValue(key, val))
+ } else {
+ ret = append(ret, kvs[i+1])
+ }
+ }
+ return ret
+}
+
+// formatGitConfigs renders key/value pairs the way --git-config accepts them,
+// for logging.
+func formatGitConfigs(kvs []keyVal) string {
+ parts := make([]string, 0, len(kvs))
+ for _, kv := range kvs {
+ key, val := kv.key, kv.val
+ if strings.ContainsAny(key, ":,") {
+ key = strconv.Quote(key)
+ }
+ if strings.ContainsAny(val, ":,") {
+ val = strconv.Quote(val)
+ }
+ parts = append(parts, key+":"+val)
+ }
+ return strings.Join(parts, ",")
+}
+
+// redactGitConfigsFlag redacts a --git-config or --git-config-add value. If
+// the value cannot be parsed, we cannot tell which parts are sensitive, so we
+// redact all of it.
+func redactGitConfigsFlag(val string) string {
+ kvs, err := parseGitConfigs(val)
+ if err != nil {
+ return redactedString
+ }
+ return formatGitConfigs(redactGitConfigs(kvs))
+}
+
// logSafeFlags makes sure any sensitive args (e.g. passwords) are redacted
// before logging. This returns a slice rather than a map so it is always
// sorted.
-func logSafeFlags(v int) []string {
+func logSafeFlags(fs *pflag.FlagSet, v int) []string {
ret := []string{}
- pflag.VisitAll(func(fl *pflag.Flag) {
+ fs.VisitAll(func(fl *pflag.Flag) {
// Don't log hidden flags
if fl.Hidden {
return
@@ -1273,6 +1360,21 @@ func logSafeFlags(v int) []string {
tmp.value = sl
val = tmp.String()
}
+ // Handle --git-config and --git-config-add
+ if arg == "git-config" {
+ val = redactGitConfigsFlag(val)
+ }
+ if arg == "git-config-add" {
+ if vals, err := fs.GetStringArray(arg); err != nil {
+ val = redactedString
+ } else {
+ redacted := make([]string, 0, len(vals))
+ for _, s := range vals {
+ redacted = append(redacted, redactGitConfigsFlag(s))
+ }
+ val = fmt.Sprintf("%v", redacted)
+ }
+ }
ret = append(ret, "--"+arg+"="+val)
})
@@ -2323,10 +2425,10 @@ func (git *repoSync) SetupExtraGitConfigs(ctx context.Context, configsFlag strin
if err != nil {
return fmt.Errorf("can't parse %s flag: %w", flagName, err)
}
- git.log.V(1).Info("setting additional git configs", "configs", configs)
+ git.log.V(1).Info("setting additional git configs", "configs", redactGitConfigs(configs))
for _, kv := range configs {
if _, _, err := git.Run(ctx, "", "config", "--global", kv.key, kv.val); err != nil {
- return fmt.Errorf("error configuring additional git configs %q %q: %w", kv.key, kv.val, err)
+ return fmt.Errorf("error configuring additional git configs %q %q: %w", redactGitConfigKey(kv.key), redactGitConfigValue(kv.key, kv.val), err)
}
}
diff --git a/main_test.go b/main_test.go
index b1d331139..1e694294a 100644
--- a/main_test.go
+++ b/main_test.go
@@ -357,6 +357,158 @@ func TestParseGitConfigList(t *testing.T) {
}
}
+func TestRedactGitConfigKey(t *testing.T) {
+ cases := []struct {
+ name string
+ key string
+ expect string
+ }{{
+ name: "plain",
+ key: "core.bare",
+ expect: "core.bare",
+ }, {
+ name: "url-with-password",
+ key: "url.https://user:TOKEN@example.com/.insteadOf",
+ expect: "url.https://user:REDACTED@example.com/.insteadOf",
+ }, {
+ name: "push-instead-of",
+ key: "url.https://user:TOKEN@example.com/.pushInsteadOf",
+ expect: "url.https://user:REDACTED@example.com/.pushInsteadOf",
+ }, {
+ name: "url-without-password",
+ key: "url.https://example.com/.insteadOf",
+ expect: "url.https://example.com/.insteadOf",
+ }, {
+ name: "not-a-url-key",
+ key: "http.extraHeader",
+ expect: "http.extraHeader",
+ }}
+
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ if got := redactGitConfigKey(tc.key); got != tc.expect {
+ t.Errorf("bad result:\n\texpected: %q\n\t got: %q", tc.expect, got)
+ }
+ })
+ }
+}
+
+func TestRedactGitConfigValue(t *testing.T) {
+ cases := []struct {
+ name string
+ key string
+ val string
+ expect string
+ }{{
+ name: "plain",
+ key: "core.bare",
+ val: "false",
+ expect: "false",
+ }, {
+ name: "extra-header",
+ key: "http.extraHeader",
+ val: "Authorization: Basic TOKEN",
+ expect: redactedString,
+ }, {
+ name: "per-url-extra-header",
+ key: "http.https://example.com/.extraHeader",
+ val: "PRIVATE-TOKEN: TOKEN",
+ expect: redactedString,
+ }, {
+ name: "instead-of",
+ key: "url.https://example.com/.insteadOf",
+ val: "https://user:TOKEN@example.com/",
+ expect: "https://user:REDACTED@example.com/",
+ }, {
+ name: "empty-extra-header",
+ key: "http.extraHeader",
+ val: "",
+ expect: redactedString,
+ }}
+
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ if got := redactGitConfigValue(tc.key, tc.val); got != tc.expect {
+ t.Errorf("bad result:\n\texpected: %q\n\t got: %q", tc.expect, got)
+ }
+ })
+ }
+}
+
+func TestRedactGitConfigList(t *testing.T) {
+ in := funcr.PseudoStruct{
+ "http.extraHeader", "Authorization: Basic TOKEN",
+ "core.bare", "false",
+ "demo.bare", nil,
+ "url.https://user:TOKEN@example.com/.insteadOf", "https://example.com/",
+ }
+ expect := funcr.PseudoStruct{
+ "http.extraHeader", redactedString,
+ "core.bare", "false",
+ "demo.bare", nil,
+ "url.https://user:REDACTED@example.com/.insteadOf", "https://example.com/",
+ }
+ if got := redactGitConfigList(in); !reflect.DeepEqual(got, expect) {
+ t.Errorf("bad result:\n\texpected: %#v\n\t got: %#v", expect, got)
+ }
+}
+
+func TestRedactGitConfigsFlag(t *testing.T) {
+ cases := []struct {
+ name string
+ in string
+ expect string
+ }{{
+ name: "unparsable",
+ in: "key-without-value",
+ expect: redactedString,
+ }, {
+ name: "keep-plain",
+ in: `core.askPass:""`,
+ expect: "core.askPass:",
+ }, {
+ name: "redact-extra-header",
+ in: `http.extraHeader:Authorization: Basic TOKEN,core.bare:false`,
+ expect: "http.extraHeader:REDACTED,core.bare:false",
+ }, {
+ name: "redact-url-key",
+ in: `"url.https://user:TOKEN@example.com/.insteadOf":"https://example.com/"`,
+ expect: `"url.https://user:REDACTED@example.com/.insteadOf":"https://example.com/"`,
+ }}
+
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ if got := redactGitConfigsFlag(tc.in); got != tc.expect {
+ t.Errorf("bad result:\n\texpected: %q\n\t got: %q", tc.expect, got)
+ }
+ })
+ }
+}
+
+func TestLogSafeFlagsRedactsGitConfig(t *testing.T) {
+ fs := pflag.NewFlagSet("git-sync", pflag.ContinueOnError)
+ fs.String("git-config", "", "additional git config")
+ fs.StringArray("git-config-add", nil, "one additional git config")
+ fs.String("repo", "", "the repo")
+
+ err := fs.Parse([]string{
+ "--git-config=http.extraHeader:Authorization: Basic TOKEN",
+ "--git-config-add=core.bare:false",
+ `--git-config-add="url.https://user:TOKEN@example.com/.insteadOf":"https://example.com/"`,
+ })
+ if err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+
+ expect := []string{
+ "--git-config=http.extraHeader:" + redactedString,
+ `--git-config-add=[core.bare:false "url.https://user:REDACTED@example.com/.insteadOf":"https://example.com/"]`,
+ }
+ if got := logSafeFlags(fs, 5); !reflect.DeepEqual(got, expect) {
+ t.Errorf("bad result:\n\texpected: %#v\n\t got: %#v", expect, got)
+ }
+}
+
func TestDirIsEmpty(t *testing.T) {
root := absPath(t.TempDir())