Skip to content

Commit 94a35b1

Browse files
peffgitster
authored andcommitted
config: reject bogus section names for --rename-section
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fdec2eb commit 94a35b1

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

config.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1552,20 +1552,42 @@ static int section_name_match (const char *buf, const char *name)
15521552
return 0;
15531553
}
15541554

1555+
static int section_name_is_ok(const char *name)
1556+
{
1557+
/* Empty section names are bogus. */
1558+
if (!*name)
1559+
return 0;
1560+
1561+
/*
1562+
* Before a dot, we must be alphanumeric or dash. After the first dot,
1563+
* anything goes, so we can stop checking.
1564+
*/
1565+
for (; *name && *name != '.'; name++)
1566+
if (*name != '-' && !isalnum(*name))
1567+
return 0;
1568+
return 1;
1569+
}
1570+
15551571
/* if new_name == NULL, the section is removed instead */
15561572
int git_config_rename_section_in_file(const char *config_filename,
15571573
const char *old_name, const char *new_name)
15581574
{
15591575
int ret = 0, remove = 0;
15601576
char *filename_buf = NULL;
1561-
struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);
1577+
struct lock_file *lock;
15621578
int out_fd;
15631579
char buf[1024];
15641580
FILE *config_file;
15651581

1582+
if (new_name && !section_name_is_ok(new_name)) {
1583+
ret = error("invalid section name: %s", new_name);
1584+
goto out;
1585+
}
1586+
15661587
if (!config_filename)
15671588
config_filename = filename_buf = git_pathdup("config");
15681589

1590+
lock = xcalloc(sizeof(struct lock_file), 1);
15691591
out_fd = hold_lock_file_for_update(lock, config_filename, 0);
15701592
if (out_fd < 0) {
15711593
ret = error("could not lock config file %s", config_filename);

t/t1300-repo-config.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,14 @@ EOF
550550

551551
test_expect_success "rename succeeded" "test_cmp expect .git/config"
552552

553+
test_expect_success 'renaming empty section name is rejected' '
554+
test_must_fail git config --rename-section branch.zwei ""
555+
'
556+
557+
test_expect_success 'renaming to bogus section is rejected' '
558+
test_must_fail git config --rename-section branch.zwei "bogus name"
559+
'
560+
553561
cat >> .git/config << EOF
554562
[branch "zwei"] a = 1 [branch "vier"]
555563
EOF

0 commit comments

Comments
 (0)