Skip to content

Commit adcd9f5

Browse files
peffgitster
authored andcommitted
mailmap: do not respect symlinks for in-tree .mailmap
As with .gitattributes and .gitignore, we would like to make sure that .mailmap files are handled consistently whether read from the a blob (as is the default behavior in a bare repo) or from the filesystem. Likewise, we would like to avoid reading out-of-tree files pointed to by a symlink, which could have security implications in certain setups. We can cover both by using open_nofollow() when opening the in-tree files. We'll continue to follow links for mailmap.file, as well as when reading .mailmap from the current directory when outside of a repository entirely. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent feb9b77 commit adcd9f5

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

mailmap.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,20 +157,30 @@ static void read_mailmap_line(struct string_list *map, char *buffer)
157157
add_mapping(map, name1, email1, name2, email2);
158158
}
159159

160-
static int read_mailmap_file(struct string_list *map, const char *filename)
160+
/* Flags for read_mailmap_file() */
161+
#define MAILMAP_NOFOLLOW (1<<0)
162+
163+
static int read_mailmap_file(struct string_list *map, const char *filename,
164+
unsigned flags)
161165
{
162166
char buffer[1024];
163167
FILE *f;
168+
int fd;
164169

165170
if (!filename)
166171
return 0;
167172

168-
f = fopen(filename, "r");
169-
if (!f) {
173+
if (flags & MAILMAP_NOFOLLOW)
174+
fd = open_nofollow(filename, O_RDONLY);
175+
else
176+
fd = open(filename, O_RDONLY);
177+
178+
if (fd < 0) {
170179
if (errno == ENOENT)
171180
return 0;
172181
return error_errno("unable to open mailmap at %s", filename);
173182
}
183+
f = xfdopen(fd, "r");
174184

175185
while (fgets(buffer, sizeof(buffer), f) != NULL)
176186
read_mailmap_line(map, buffer);
@@ -225,10 +235,12 @@ int read_mailmap(struct string_list *map)
225235
if (!git_mailmap_blob && is_bare_repository())
226236
git_mailmap_blob = "HEAD:.mailmap";
227237

228-
err |= read_mailmap_file(map, ".mailmap");
238+
err |= read_mailmap_file(map, ".mailmap",
239+
startup_info->have_repository ?
240+
MAILMAP_NOFOLLOW : 0);
229241
if (startup_info->have_repository)
230242
err |= read_mailmap_blob(map, git_mailmap_blob);
231-
err |= read_mailmap_file(map, git_mailmap_file);
243+
err |= read_mailmap_file(map, git_mailmap_file, 0);
232244
return err;
233245
}
234246

t/t4203-mailmap.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,4 +889,35 @@ test_expect_success 'empty syntax: setup' '
889889
test_cmp expect actual
890890
'
891891

892+
test_expect_success SYMLINKS 'set up symlink tests' '
893+
git commit --allow-empty -m foo --author="Orig <[email protected]>" &&
894+
echo "New <[email protected]> <[email protected]>" >map &&
895+
rm -f .mailmap
896+
'
897+
898+
test_expect_success SYMLINKS 'symlinks respected in mailmap.file' '
899+
test_when_finished "rm symlink" &&
900+
ln -s map symlink &&
901+
git -c mailmap.file="$(pwd)/symlink" log -1 --format=%aE >actual &&
902+
echo "[email protected]" >expect &&
903+
test_cmp expect actual
904+
'
905+
906+
test_expect_success SYMLINKS 'symlinks respected in non-repo shortlog' '
907+
git log -1 >input &&
908+
test_when_finished "nongit rm .mailmap" &&
909+
nongit ln -sf "$TRASH_DIRECTORY/map" .mailmap &&
910+
nongit git shortlog -s <input >actual &&
911+
echo " 1 New" >expect &&
912+
test_cmp expect actual
913+
'
914+
915+
test_expect_success SYMLINKS 'symlinks not respected in-tree' '
916+
test_when_finished "rm .mailmap" &&
917+
ln -s map .mailmap &&
918+
git log -1 --format=%aE >actual &&
919+
echo "[email protected]" >expect&&
920+
test_cmp expect actual
921+
'
922+
892923
test_done

0 commit comments

Comments
 (0)