From patchwork Fri Nov 13 12:16:18 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 11903211 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9E958C4742C for ; Fri, 13 Nov 2020 12:16:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0605F22240 for ; Fri, 13 Nov 2020 12:16:22 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="Z9aqs2gD"; dkim=temperror (0-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="LgdKYy+8" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726561AbgKMMQW (ORCPT ); Fri, 13 Nov 2020 07:16:22 -0500 Received: from wout5-smtp.messagingengine.com ([64.147.123.21]:39519 "EHLO wout5-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726494AbgKMMQV (ORCPT ); Fri, 13 Nov 2020 07:16:21 -0500 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.west.internal (Postfix) with ESMTP id 5ADCDBDC for ; Fri, 13 Nov 2020 07:16:20 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute1.internal (MEProxy); Fri, 13 Nov 2020 07:16:20 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=date :from:to:subject:message-id:references:mime-version:content-type :in-reply-to; s=fm2; bh=ruH50YMW6iPk9Ubh0rCgYbZphJKUcFDssaHXu+IY BJA=; b=Z9aqs2gD0p/gOvOM1h/ZdZ36DfZkP3qkHPWss8Pt8QoUulhcPiShOJAr 6lRKMH2eFWXuQ8eWSLU2GAS93tw4qKwaku392K4A6kr+y4i5cmiNF8o2MNWyAW+1 bMQLSCPr1fg8/dMSfud7yQooEHBEN2zRN4mWtNXX0o0S8JdX7Z6D7pN5l+MmZYIn mg9l4ijRkeiZOed7CuzC/qGcytmaeehUPaeEqfOIDXIemZ+mKDxqag3mFG9n2vKC fK1vu3nfPaC3pimCkb0Tkxka5RLIhIJtHKKukYkEF7QeTLKBOtpqLriAO/6AFe4V 6ZbPGi8dQ5z/4vHhn1Aqy0mrlkMDVA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=ruH50Y MW6iPk9Ubh0rCgYbZphJKUcFDssaHXu+IYBJA=; b=LgdKYy+869REsfntxDnAiy 3uCQRY4aOd51BC+cOMSJnG4oVxcuN2UT0mp/LPj6WMBaChLkf1dhpKy9pMdT3yWv lm02VQ9ZvN2nvs8SsD1JI7WvWVILjP1UJRr6DwEcy6D+VAD41hF8kbGuZTZia/kX W8Avda3BGvRNeF5GLPwtK69CtE2w1+3ynoK45bAip5vkuiuSMsBwCFHMJmJ75j1U 8G+E3DMjrxb73qVuVGTMSkUy8cqsmbSl47zrgWo6p4axXeHll+IeWlry4TExxh5+ hx0ZWDjohfgXlNNNBim8fApDbIFUsPGtHmEJj4MBJ//3YI9wdw7AjikwN6+X8DCg == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedruddvhedgfeekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpeffhffvuffkfhggtggujgesghdtre ertddtvdenucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehp khhsrdhimheqnecuggftrfgrthhtvghrnhepheeghfdtfeeuffehkefgffduleffjedthf dvjeektdfhhedvlefgtefgvdettdfhnecukfhppeekledruddvrdefuddrvdefheenucev lhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehpshesphhksh drihhm X-ME-Proxy: Received: from vm-mail.pks.im (dynamic-089-012-031-235.89.12.pool.telefonica.de [89.12.31.235]) by mail.messagingengine.com (Postfix) with ESMTPA id 922603064AAA for ; Fri, 13 Nov 2020 07:16:19 -0500 (EST) Received: from localhost (ncase [10.192.0.11]) by vm-mail.pks.im (OpenSMTPD) with ESMTPSA id 44826035 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Fri, 13 Nov 2020 12:16:17 +0000 (UTC) Date: Fri, 13 Nov 2020 13:16:18 +0100 From: Patrick Steinhardt To: git@vger.kernel.org Subject: [PATCH 1/2] config: extract function to parse config pairs Message-ID: <6ced4b2ddd21828a21e935942e2c3da4deb34705.1605269465.git.ps@pks.im> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The function `git_config_parse_parameter` is responsible for parsing a `foo.bar=baz`-formatted configuration key, sanitizing the key and then processing it via the given callback function. Given that we're about to add a second user which is going to process keys in such which already has keys and values separated, this commit extracts a function `config_parse_pair` which only does the sanitization and processing part. Signed-off-by: Patrick Steinhardt --- config.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/config.c b/config.c index 2bdff4457b..3281b1374e 100644 --- a/config.c +++ b/config.c @@ -437,11 +437,26 @@ int git_config_key_is_valid(const char *key) return !git_config_parse_key_1(key, NULL, NULL, 1); } +static int config_parse_pair(const char *key, const char *value, + config_fn_t fn, void *data) +{ + char *canonical_name; + int ret; + + if (!strlen(key)) + return error(_("empty config key")); + if (git_config_parse_key(key, &canonical_name, NULL)) + return -1; + + ret = (fn(canonical_name, value, data) < 0) ? -1 : 0; + free(canonical_name); + return ret; +} + int git_config_parse_parameter(const char *text, config_fn_t fn, void *data) { const char *value; - char *canonical_name; struct strbuf **pair; int ret; @@ -462,12 +477,7 @@ int git_config_parse_parameter(const char *text, return error(_("bogus config parameter: %s"), text); } - if (git_config_parse_key(pair[0]->buf, &canonical_name, NULL)) { - ret = -1; - } else { - ret = (fn(canonical_name, value, data) < 0) ? -1 : 0; - free(canonical_name); - } + ret = config_parse_pair(pair[0]->buf, value, fn, data); strbuf_list_free(pair); return ret; } From patchwork Fri Nov 13 12:16:22 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 11903213 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 82F64C388F7 for ; Fri, 13 Nov 2020 12:16:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 30C5822240 for ; Fri, 13 Nov 2020 12:16:39 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="gG4u7NvO"; dkim=temperror (0-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="lwCpOUKL" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726583AbgKMMQi (ORCPT ); Fri, 13 Nov 2020 07:16:38 -0500 Received: from wout5-smtp.messagingengine.com ([64.147.123.21]:38351 "EHLO wout5-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726406AbgKMMQZ (ORCPT ); Fri, 13 Nov 2020 07:16:25 -0500 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.west.internal (Postfix) with ESMTP id 1815DCB9 for ; Fri, 13 Nov 2020 07:16:25 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute1.internal (MEProxy); Fri, 13 Nov 2020 07:16:25 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=date :from:to:subject:message-id:references:mime-version:content-type :in-reply-to; s=fm2; bh=h+KUNt+I082XHsBM0BmO+ips/WCh1M82vQXHgcAY ZjQ=; b=gG4u7NvOen7jIoeAHmolj6bgqpjKP0VFcPgfSfxCsEVjF9DKIlRxILg/ swFsCHV35JMe1Bnl0TsLZImPhZSfzwT/2EUsjbPzivkZwASvK1nZmA+ZUZD/X0wL BC90KgUkxL10kpRCYgLg0H2PTlyYSeT7soWj8x5eLiDAAZJC8u5Pxad0OCZaI9d2 1RAuuzov2MKOA0e9tIq3etsjSjI8uQyTuNWaeki+OMfSzEP2E8ABZu84rVyYJaxa zDW6ZaRKldD6ZjTkz1jUgXtym955lgal3G7LtDD6SjI9MB94k9uu6J65TT6iQ2QK oCDX1pnpIk9WJYXqzykpZawRFtgBwg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=h+KUNt +I082XHsBM0BmO+ips/WCh1M82vQXHgcAYZjQ=; b=lwCpOUKLLCVAXt3j/FRl18 u8l/QC+RZEjtb+pRB3wZ6kI24ASma6pw51OP/L72+jLcv5HTDhHjyasUQMNWKnob 1H7LSD8sqPIt2vcA1f+3zQ+0BIfJusb2NVx87fgWFpFcJbBeUmQ94gFTCV/3owmv t/U2ozHTf4MNZBy8Mr4z7iKE1ODf4zgzkNkqG8wCVB96esY1P+quI4NZ30YJIx4Z PqkommbweioJwi/P9N32H+Mn8VMWh2+r4Zl2yTKW9xrz+lYA92lsC+TNkPqRkWpu b/kdPtnASFBm68EkDzLbgpV39ddsNmE3hr5jUZrZ6neRsB1GkBpR9szKvPjGRetA == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedruddvhedgfeekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpeffhffvuffkfhggtggujgesghdtre ertddtvdenucfhrhhomheprfgrthhrihgtkhcuufhtvghinhhhrghrughtuceophhssehp khhsrdhimheqnecuggftrfgrthhtvghrnhepheeghfdtfeeuffehkefgffduleffjedthf dvjeektdfhhedvlefgtefgvdettdfhnecukfhppeekledruddvrdefuddrvdefheenucev lhhushhtvghrufhiiigvpedunecurfgrrhgrmhepmhgrihhlfhhrohhmpehpshesphhksh drihhm X-ME-Proxy: Received: from vm-mail.pks.im (dynamic-089-012-031-235.89.12.pool.telefonica.de [89.12.31.235]) by mail.messagingengine.com (Postfix) with ESMTPA id 217B23064AAF for ; Fri, 13 Nov 2020 07:16:24 -0500 (EST) Received: from localhost (ncase [10.192.0.11]) by vm-mail.pks.im (OpenSMTPD) with ESMTPSA id 7ee2c928 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Fri, 13 Nov 2020 12:16:22 +0000 (UTC) Date: Fri, 13 Nov 2020 13:16:22 +0100 From: Patrick Steinhardt To: git@vger.kernel.org Subject: [PATCH 2/2] config: allow specifying config entries via envvar pairs Message-ID: <44e8dd50c6ea7cbcc5e4fc35c9b9057c0a52038c.1605269465.git.ps@pks.im> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org While not document, it is currently possible to specify config entries via the environment by passing `GIT_CONFIG_PARAMETERS`. This variable is expected to hold one or multiple "section.key=value" entries separated by space. Next to being undocumented, this way of passing config entries has a major downside: the config keys need to be parsed. As such, it is left to the user to escape any potentially harmful characters in the value, which is quite hard to do if values are controlled by a third party. This commit thus adds a new way of adding config entries via the environment which doesn't require splitting of keys and values. The user can specify an config entry's key via `GIT_CONFIG_KEY_$n` and a value via `GIT_CONFIG_VALUE_$n`, where `n` is any number starting with 1. It is possible to add multiple entries via consecutively numbered envvars `GIT_CONFIG_KEY_1`, `GIT_CONFIG_KEY_2`, etc, where each of the keys may have a matching value. When no matching value exists, it's assumed to be the empty value. While the same can be achieved with `git -c =`, one may wish to not do so for potentially 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. Signed-off-by: Patrick Steinhardt --- Documentation/git-config.txt | 6 ++++++ config.c | 41 ++++++++++++++++++++++++++---------- t/t1300-config.sh | 23 ++++++++++++++++++++ 3 files changed, 59 insertions(+), 11 deletions(-) diff --git a/Documentation/git-config.txt b/Documentation/git-config.txt index 7573160f21..83fbac3705 100644 --- a/Documentation/git-config.txt +++ b/Documentation/git-config.txt @@ -335,6 +335,12 @@ GIT_CONFIG_NOSYSTEM:: Whether to skip reading settings from the system-wide $(prefix)/etc/gitconfig file. See linkgit:git[1] for details. +GIT_CONFIG_KEY_1,GIT_CONFIG_VALUE_1:: + Each pair of GIT_CONFIG_KEY_/GIT_CONFIG_VALUE_ is added to the process's + runtime configuration. It is possible to add multiple entries by adding + consecutively numbered pairs, starting at 1. If the value corresponding + to a key is not set, it is treated as if it was empty. + See also <>. diff --git a/config.c b/config.c index 3281b1374e..ab40479df2 100644 --- a/config.c +++ b/config.c @@ -485,37 +485,56 @@ int git_config_parse_parameter(const char *text, int git_config_from_parameters(config_fn_t fn, void *data) { const char *env = getenv(CONFIG_DATA_ENVIRONMENT); + struct strbuf envvar = STRBUF_INIT; int ret = 0; - char *envw; + char *envw = NULL; const char **argv = NULL; int nr = 0, alloc = 0; int i; struct config_source source; - if (!env) - return 0; - memset(&source, 0, sizeof(source)); source.prev = cf; source.origin_type = CONFIG_ORIGIN_CMDLINE; cf = &source; - /* sq_dequote will write over it */ - envw = xstrdup(env); + if (env) { + /* sq_dequote will write over it */ + envw = xstrdup(env); - if (sq_dequote_to_argv(envw, &argv, &nr, &alloc) < 0) { - ret = error(_("bogus format in %s"), CONFIG_DATA_ENVIRONMENT); - goto out; + if (sq_dequote_to_argv(envw, &argv, &nr, &alloc) < 0) { + ret = error(_("bogus format in %s"), CONFIG_DATA_ENVIRONMENT); + goto out; + } + + for (i = 0; i < nr; i++) { + if (git_config_parse_parameter(argv[i], fn, data) < 0) { + ret = -1; + goto out; + } + } } - for (i = 0; i < nr; i++) { - if (git_config_parse_parameter(argv[i], fn, data) < 0) { + for (i = 1; i; i++) { + const char *key, *value; + + strbuf_addf(&envvar, "GIT_CONFIG_KEY_%d", i); + if ((key = getenv(envvar.buf)) == NULL) + break; + strbuf_reset(&envvar); + + strbuf_addf(&envvar, "GIT_CONFIG_VALUE_%d", i); + value = getenv(envvar.buf); + strbuf_reset(&envvar); + + if (config_parse_pair(key, value, fn, data) < 0) { ret = -1; goto out; } } out: + strbuf_release(&envvar); free(argv); free(envw); cf = source.prev; diff --git a/t/t1300-config.sh b/t/t1300-config.sh index 825d9a184f..2ae9533aa8 100755 --- a/t/t1300-config.sh +++ b/t/t1300-config.sh @@ -1316,6 +1316,29 @@ test_expect_success 'detect bogus GIT_CONFIG_PARAMETERS' ' git config --get-regexp "env.*" ' +test_expect_success 'git config handles environment config pairs' ' + GIT_CONFIG_KEY_1="pair.one" GIT_CONFIG_VALUE_1="foo" \ + GIT_CONFIG_KEY_2="pair.two" GIT_CONFIG_VALUE_2="bar" \ + GIT_CONFIG_KEY_4="pair.four" GIT_CONFIG_VALUE_4="not-parsed" \ + git config --get-regexp "pair.*" >actual && + cat >expect <<-EOF && + pair.one foo + pair.two bar + EOF + test_cmp expect actual +' + +test_expect_success 'git config copes with missing config pair value' ' + GIT_CONFIG_KEY_1="pair.one" git config --get-regexp "pair.*" >actual && + echo pair.one >expect && + test_cmp expect actual +' + +test_expect_success 'git config fails with invalid config pair key' ' + test_must_fail env GIT_CONFIG_KEY_1= git config --list && + test_must_fail env GIT_CONFIG_KEY_1=missing-section git config --list +' + test_expect_success 'git config --edit works' ' git config -f tmp test.value no && echo test.value=yes >expect &&