git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: "David E. Wheeler" <david@justatheory.com>
Cc: Junio C Hamano <gitster@pobox.com>, git@vger.kernel.org
Subject: [PATCH] config: reject bogus section names for --rename-section
Date: Wed, 25 Apr 2012 21:47:14 -0400	[thread overview]
Message-ID: <20120426014714.GC8590@sigill.intra.peff.net> (raw)
In-Reply-To: <9E01F5E2-FB51-418D-B50C-EB6DC63A4F84@justatheory.com>

You can feed junk to "git config --rename-section", which
will result in a config file that git will not even parse
(so you cannot fix it with git-config). We already have
syntactic sanity checks when setting a variable; let's do
the same for section names.

Signed-off-by: Jeff King <peff@peff.net>
---
On Mon, Apr 23, 2012 at 07:57:19PM -0700, David E. Wheeler wrote:

> I was fooling around and realized that I could assign an empty string
> as the name of a config section, like so:
> 
>     $ git config --rename-section my ''
> 
> This leaves the config file looking like this:
> 
>     []
>         foo = goodbye
> 
> Which does not appear to be valid:
> 
>     $ git config --get '.foo'          
>     fatal: bad config file line 13 in .git/config
> 
> So maybe git-config should disallow setting a section to an empty string?

Yes, we should definitely be more careful.

 config.c               |   24 +++++++++++++++++++++++-
 t/t1300-repo-config.sh |    8 ++++++++
 2 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/config.c b/config.c
index 68d3294..9ef947e 100644
--- a/config.c
+++ b/config.c
@@ -1552,20 +1552,42 @@ static int section_name_match (const char *buf, const char *name)
 	return 0;
 }
 
+static int section_name_is_ok(const char *name)
+{
+	/* Empty section names are bogus. */
+	if (!*name)
+		return 0;
+
+	/*
+	 * Before a dot, we must be alphanumeric or dash. After the first dot,
+	 * anything goes, so we can stop checking.
+	 */
+	for (; *name && *name != '.'; name++)
+		if (*name != '-' && !isalnum(*name))
+			return 0;
+	return 1;
+}
+
 /* if new_name == NULL, the section is removed instead */
 int git_config_rename_section_in_file(const char *config_filename,
 				      const char *old_name, const char *new_name)
 {
 	int ret = 0, remove = 0;
 	char *filename_buf = NULL;
-	struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);
+	struct lock_file *lock;
 	int out_fd;
 	char buf[1024];
 	FILE *config_file;
 
+	if (new_name && !section_name_is_ok(new_name)) {
+		ret = error("invalid section name: %s", new_name);
+		goto out;
+	}
+
 	if (!config_filename)
 		config_filename = filename_buf = git_pathdup("config");
 
+	lock = xcalloc(sizeof(struct lock_file), 1);
 	out_fd = hold_lock_file_for_update(lock, config_filename, 0);
 	if (out_fd < 0) {
 		ret = error("could not lock config file %s", config_filename);
diff --git a/t/t1300-repo-config.sh b/t/t1300-repo-config.sh
index 36e227b..a477453 100755
--- a/t/t1300-repo-config.sh
+++ b/t/t1300-repo-config.sh
@@ -550,6 +550,14 @@ EOF
 
 test_expect_success "rename succeeded" "test_cmp expect .git/config"
 
+test_expect_success 'renaming empty section name is rejected' '
+	test_must_fail git config --rename-section branch.zwei ""
+'
+
+test_expect_success 'renaming to bogus section is rejected' '
+	test_must_fail git config --rename-section branch.zwei "bogus name"
+'
+
 cat >> .git/config << EOF
   [branch "zwei"] a = 1 [branch "vier"]
 EOF
-- 
1.7.9.6.11.gd9951

  reply	other threads:[~2012-04-26  1:47 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-24  2:57 Use git-config to Break your Config File David E. Wheeler
2012-04-26  1:47 ` Jeff King [this message]
2012-04-26  4:19   ` [PATCH] config: reject bogus section names for --rename-section Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120426014714.GC8590@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=david@justatheory.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).