git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v6 2/8] config: add new way to pass config via `--config-env`

From
Patrick Steinhardt <ps@pks.im>
Date
Jan 7, 2021, 06:36 UTC
Message-ID
<9b8461010e641369316d00e2fc58c16e0e191f42.1610001187.git.ps@pks.im>
In-Reply-To
<cover.1610001187.git.ps@pks.im>

While it's already possible to pass runtime configuration via `git -c <key>=<value>`, it may be undesirable to use when the value contains sensitive information. E.g. if one wants to set `http.extraHeader` to contain an authentication token, doing so via `-c` would trivially leak those credentials via e.g. ps(1), which typically also shows command arguments.

To enable this usecase without leaking credentials, this commit introduces a new switch `--config-env=<key>=<envvar>`. Instead of directly passing a value for the given key, it instead allows the user to specify the name of an environment variable. The value of that variable will then be used as value of the key.

Co-authored-by: Jeff King <peff@peff.net>
Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 Documentation/git.txt | 24 ++++++++++++++++++++++-
 config.c              | 21 ++++++++++++++++++++
 config.h              |  1 +
 git.c                 |  4 +++-
 t/t1300-config.sh     | 45 +++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 93 insertions(+), 2 deletions(-)
diff --git a/Documentation/git.txt b/Documentation/git.txt
index a6d4ad0818..d36e6fd482 100644
--- a/Documentation/git.txt
+++ b/Documentation/git.txt
@@ -13,7 +13,7 @@ SYNOPSIS
     [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
     [-p|--paginate|-P|--no-pager] [--no-replace-objects] [--bare]
     [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
-    [--super-prefix=<path>]
+    [--super-prefix=<path>] [--config-env <name>=<envvar>]
     <command> [<args>]
 
 DESCRIPTION
@@ -80,6 +80,28 @@ config file). Including the equals but with an empty value (like `git -c
 foo.bar= ...`) sets `foo.bar` to the empty string which `git config
 --type=bool` will convert to `false`.
 
+--config-env=<name>=<envvar>::
+	Like `-c <name>=<value>`, give configuration variable
+	'<name>' a value, where <envvar> is the name of an
+	environment variable from which to retrieve the value. Unlike
+	`-c` there is no shortcut for directly setting the value to an
+	empty string, instead the environment variable itself must be
+	set to the empty string.  It is an error if the `<envvar>` does not exist
+	in the environment. `<envvar>` may not contain an equals sign
+	to avoid ambiguity with `<name>`s which contain one.
++
+This is useful for cases where you want to pass transitory
+configuration options to git, but are doing so on OS's where
+other processes might be able to read your cmdline
+(e.g. `/proc/self/cmdline`), but not your environ
+(e.g. `/proc/self/environ`). That behavior is the default on
+Linux, but may not be on your system.
++
+Note that this might add security for variables such as
+`http.extraHeader` where the sensitive information is part of
+the value, but not e.g. `url.<base>.insteadOf` where the
+sensitive information can be part of the key.
+
 --exec-path[=<path>]::
 	Path to wherever your core Git programs are installed.
 	This can also be controlled by setting the GIT_EXEC_PATH
diff --git a/config.c b/config.c
index 1137bd73af..cde3511110 100644
--- a/config.c
+++ b/config.c
@@ -345,6 +345,27 @@ void git_config_push_parameter(const char *text)
 	strbuf_release(&env);
 }
 
+void git_config_push_env(const char *spec)
+{
+	struct strbuf buf = STRBUF_INIT;
+	const char *env_name;
+	const char *env_value;
+
+	env_name = strrchr(spec, '=');
+	if (!env_name)
+		die("invalid config format: %s", spec);
+	env_name++;
+
+	env_value = getenv(env_name);
+	if (!env_value)
+		die("config variable missing for '%s'", env_name);
+
+	strbuf_add(&buf, spec, env_name - spec);
+	strbuf_addstr(&buf, env_value);
+	git_config_push_parameter(buf.buf);
+	strbuf_release(&buf);
+}
+
 static inline int iskeychar(int c)
 {
 	return isalnum(c) || c == '-';
diff --git a/config.h b/config.h
index c1449bb790..19a9adbaa9 100644
--- a/config.h
+++ b/config.h
@@ -138,6 +138,7 @@ int git_config_from_mem(config_fn_t fn,
 int git_config_from_blob_oid(config_fn_t fn, const char *name,
 			     const struct object_id *oid, void *data);
 void git_config_push_parameter(const char *text);
+void git_config_push_env(const char *spec);
 int git_config_from_parameters(config_fn_t fn, void *data);
 void read_early_config(config_fn_t cb, void *data);
 void read_very_early_config(config_fn_t cb, void *data);
diff --git a/git.c b/git.c
index 5a8ff12f87..b5f63d346b 100644
--- a/git.c
+++ b/git.c
@@ -29,7 +29,7 @@ const char git_usage_string[] =
 	   "           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]\n"
 	   "           [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]\n"
 	   "           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]\n"
-	   "           [--super-prefix=<path>]\n"
+	   "           [--super-prefix=<path>] [--config-env=<name>=<envvar>]\n"
 	   "           <command> [<args>]");
 
 const char git_more_info_string[] =
@@ -255,6 +255,8 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
 			git_config_push_parameter((*argv)[1]);
 			(*argv)++;
 			(*argc)--;
+		} else if (skip_prefix(cmd, "--config-env=", &cmd)) {
+			git_config_push_env(cmd);
 		} else if (!strcmp(cmd, "--literal-pathspecs")) {
 			setenv(GIT_LITERAL_PATHSPECS_ENVIRONMENT, "1", 1);
 			if (envchanged)
diff --git a/t/t1300-config.sh b/t/t1300-config.sh
index 97a04c6cc2..46a94814d5 100755
--- a/t/t1300-config.sh
+++ b/t/t1300-config.sh
@@ -1316,6 +1316,51 @@ test_expect_success 'detect bogus GIT_CONFIG_PARAMETERS' '
 		git config --get-regexp "env.*"
 '
 
+test_expect_success 'git --config-env=key=envvar support' '
+	cat >expect <<-\EOF &&
+	value
+	value
+	false
+	EOF
+	{
+		env ENVVAR=value git --config-env=core.name=ENVVAR config core.name &&
+		env ENVVAR=value git --config-env=foo.CamelCase=ENVVAR config foo.camelcase &&
+		env ENVVAR= git --config-env=foo.flag=ENVVAR config --bool foo.flag
+	} >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'git --config-env fails with invalid parameters' '
+	test_must_fail git --config-env=foo.flag config --bool foo.flag 2>error &&
+	test_i18ngrep "invalid config format" error &&
+	test_must_fail git --config-env=foo.flag=NONEXISTENT config --bool foo.flag 2>error &&
+	test_i18ngrep "config variable missing" error
+'
+
+test_expect_success 'git -c and --config-env work together' '
+	cat >expect <<-\EOF &&
+	bar.cmd cmd-value
+	bar.env env-value
+	EOF
+	env ENVVAR=env-value git \
+		-c bar.cmd=cmd-value \
+		--config-env=bar.env=ENVVAR \
+		config --get-regexp "^bar.*" >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'git -c and --config-env override each other' '
+	cat >expect <<-\EOF &&
+	env
+	cmd
+	EOF
+	{
+		env ENVVAR=env git -c bar.bar=cmd --config-env=bar.bar=ENVVAR config bar.bar &&
+		env ENVVAR=env git --config-env=bar.bar=ENVVAR -c bar.bar=cmd config bar.bar
+	} >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'git config --edit works' '
 	git config -f tmp test.value no &&
 	echo test.value=yes >expect &&
-- 
2.30.0
Previous: Patrick SteinhardtNext: Simon Ruderich
Message 77 of 116 in “config: allow specifying config entries via envvar pairs”
  1. 0/2 config: allow specifying config entries via envvar pairsPatrick Steinhardt, Nov 24, 2020
  2. 1/2 config: extract function to parse config pairsPatrick Steinhardt, Nov 24, 2020
  3. 2/2 config: allow specifying config entries via envvar pairsPatrick Steinhardt, Nov 24, 2020
  4. Junio C HamanoNov 25, 2020
  5. Patrick SteinhardtNov 25, 2020
  6. Junio C HamanoNov 25, 2020
  7. Patrick SteinhardtNov 25, 2020
  8. Ævar Arnfjörð BjarmasonNov 25, 2020
  9. Ævar Arnfjörð BjarmasonNov 25, 2020
  10. Junio C HamanoNov 25, 2020
  11. Jeff KingNov 25, 2020
  12. Patrick SteinhardtNov 25, 2020
  13. Jeff KingNov 26, 2020
  14. Junio C HamanoNov 25, 2020
  15. brian m. carlsonNov 25, 2020
  16. Patrick SteinhardtNov 26, 2020
  17. Patrick SteinhardtDec 1, 2020
  18. Jeff KingDec 1, 2020
  19. 0/4 config: allow specifying config entries via envvar pairsPatrick Steinhardt, Dec 1, 2020
  20. 1/4 environment: make `getenv_safe()` non-staticPatrick Steinhardt, Dec 1, 2020
  21. 3/4 config: refactor parsing of GIT_CONFIG_PARAMETERSPatrick Steinhardt, Dec 1, 2020
  22. 2/4 config: extract function to parse config pairsPatrick Steinhardt, Dec 1, 2020
  23. 4/4 config: allow specifying config entries via envvar pairsPatrick Steinhardt, Dec 1, 2020
  24. 0/6 config: allow specifying config entries via envPatrick Steinhardt, Dec 9, 2020
  25. 1/6 git: add `--super-prefix` to usage stringPatrick Steinhardt, Dec 9, 2020
  26. 3/6 environment: make `getenv_safe()` non-staticPatrick Steinhardt, Dec 9, 2020
  27. 2/6 config: add new way to pass config via `--config-env`Patrick Steinhardt, Dec 9, 2020
  28. Ævar Arnfjörð BjarmasonDec 9, 2020
  29. Jeff KingDec 9, 2020
  30. Patrick SteinhardtDec 11, 2020
  31. Jeff KingDec 11, 2020
  32. Patrick SteinhardtDec 11, 2020
  33. Jeff KingDec 11, 2020
  34. Jeff KingDec 9, 2020
  35. 1/3 quote: make sq_dequote_step() a public functionJeff King, Dec 9, 2020
  36. 2/3 config: parse more robust format in GIT_CONFIG_PARAMETERSJeff King, Dec 9, 2020
  37. 3/3 config: store "git -c" variables using more robust formatJeff King, Dec 9, 2020
  38. Jeff KingDec 9, 2020
  39. Ævar Arnfjörð BjarmasonDec 10, 2020
  40. Junio C HamanoDec 10, 2020
  41. Jeff KingDec 11, 2020
  42. Junio C HamanoDec 10, 2020
  43. Jeff KingDec 10, 2020
  44. Junio C HamanoDec 10, 2020
  45. Patrick SteinhardtDec 11, 2020
  46. Jeff KingDec 11, 2020
  47. 4/6 config: extract function to parse config pairsPatrick Steinhardt, Dec 9, 2020
  48. Ævar Arnfjörð BjarmasonDec 9, 2020
  49. 6/6 config: allow specifying config entries via envvar pairsPatrick Steinhardt, Dec 9, 2020
  50. 5/6 config: refactor parsing of GIT_CONFIG_PARAMETERSPatrick Steinhardt, Dec 9, 2020
  51. Ævar Arnfjörð BjarmasonDec 9, 2020
  52. Patrick SteinhardtDec 11, 2020
  53. Jeff KingDec 11, 2020
  54. Jeff KingDec 11, 2020
  55. Patrick SteinhardtDec 11, 2020
  56. Patrick SteinhardtDec 11, 2020
  57. Ævar Arnfjörð BjarmasonDec 11, 2020
  58. Jeff KingDec 11, 2020
  59. 0/8 config: allow specifying config entries via envPatrick Steinhardt, Dec 16, 2020
  60. 1/8 git: add `--super-prefix` to usage stringPatrick Steinhardt, Dec 16, 2020
  61. 2/8 config: add new way to pass config via `--config-env`Patrick Steinhardt, Dec 16, 2020
  62. Junio C HamanoDec 23, 2020
  63. 4/8 config: extract function to parse config pairsPatrick Steinhardt, Dec 16, 2020
  64. 8/8 config: allow specifying config entries via envvar pairsPatrick Steinhardt, Dec 16, 2020
  65. Junio C HamanoDec 23, 2020
  66. Junio C HamanoDec 23, 2020
  67. Patrick SteinhardtJan 6, 2021
  68. Junio C HamanoJan 6, 2021
  69. 7/8 environment: make `getenv_safe()` a public functionPatrick Steinhardt, Dec 16, 2020
  70. 6/8 config: parse more robust format in GIT_CONFIG_PARAMETERSPatrick Steinhardt, Dec 16, 2020
  71. Phillip WoodDec 16, 2020
  72. 3/8 quote: make sq_dequote_step() a public functionPatrick Steinhardt, Dec 16, 2020
  73. 5/8 config: store "git -c" variables using more robust formatPatrick Steinhardt, Dec 16, 2020
  74. 0/8 config: allow specifying config entries via envPatrick Steinhardt, Jan 7, 2021
  75. 4/8 config: extract function to parse config pairsPatrick Steinhardt, Jan 7, 2021
  76. 1/8 git: add `--super-prefix` to usage stringPatrick Steinhardt, Jan 7, 2021
  77. 2/8 config: add new way to pass config via `--config-env`Patrick Steinhardt, Jan 7, 2021
  78. Simon RuderichJan 10, 2021
  79. Junio C HamanoJan 11, 2021
  80. Patrick SteinhardtJan 11, 2021
  81. 3/8 quote: make sq_dequote_step() a public functionPatrick Steinhardt, Jan 7, 2021
  82. 5/8 config: store "git -c" variables using more robust formatPatrick Steinhardt, Jan 7, 2021
  83. 6/8 config: parse more robust format in GIT_CONFIG_PARAMETERSPatrick Steinhardt, Jan 7, 2021
  84. 7/8 environment: make `getenv_safe()` a public functionPatrick Steinhardt, Jan 7, 2021
  85. 8/8 config: allow specifying config entries via envvar pairsPatrick Steinhardt, Jan 7, 2021
  86. 0/8 config: allow specifying config entries via envvar pairsPatrick Steinhardt, Jan 11, 2021
  87. 2/8 config: add new way to pass config via `--config-env`Patrick Steinhardt, Jan 11, 2021
  88. Junio C HamanoJan 11, 2021
  89. 3/8 quote: make sq_dequote_step() a public functionPatrick Steinhardt, Jan 11, 2021
  90. 1/8 git: add `--super-prefix` to usage stringPatrick Steinhardt, Jan 11, 2021
  91. 4/8 config: extract function to parse config pairsPatrick Steinhardt, Jan 11, 2021
  92. 5/8 config: store "git -c" variables using more robust formatPatrick Steinhardt, Jan 11, 2021
  93. 7/8 environment: make `getenv_safe()` a public functionPatrick Steinhardt, Jan 11, 2021
  94. 6/8 config: parse more robust format in GIT_CONFIG_PARAMETERSPatrick Steinhardt, Jan 11, 2021
  95. 8/8 config: allow specifying config entries via envvar pairsPatrick Steinhardt, Jan 11, 2021
  96. 0/8 config: allow specifying config entries via envvar pairsPatrick Steinhardt, Jan 12, 2021
  97. 2/8 config: add new way to pass config via `--config-env`Patrick Steinhardt, Jan 12, 2021
  98. Ævar Arnfjörð BjarmasonApr 16, 2021
  99. Jeff KingApr 17, 2021
  100. Patrick SteinhardtApr 19, 2021
  101. Ævar Arnfjörð BjarmasonApr 20, 2021
  102. Ævar Arnfjörð BjarmasonApr 20, 2021
  103. Jeff KingApr 23, 2021
  104. Ævar Arnfjörð BjarmasonMay 19, 2021
  105. 4/8 config: extract function to parse config pairsPatrick Steinhardt, Jan 12, 2021
  106. 1/8 git: add `--super-prefix` to usage stringPatrick Steinhardt, Jan 12, 2021
  107. 3/8 quote: make sq_dequote_step() a public functionPatrick Steinhardt, Jan 12, 2021
  108. 6/8 config: parse more robust format in GIT_CONFIG_PARAMETERSPatrick Steinhardt, Jan 12, 2021
  109. 8/8 config: allow specifying config entries via envvar pairsPatrick Steinhardt, Jan 12, 2021
  110. 5/8 config: store "git -c" variables using more robust formatPatrick Steinhardt, Jan 12, 2021
  111. Jeff KingJan 15, 2021
  112. Patrick SteinhardtJan 20, 2021
  113. Junio C HamanoJan 20, 2021
  114. Patrick SteinhardtJan 20, 2021
  115. Junio C HamanoJan 20, 2021
  116. 7/8 environment: make `getenv_safe()` a public functionPatrick Steinhardt, Jan 12, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.