Skip to content

Commit bb6832d

Browse files
peffgitster
authored andcommitted
fsck: warn about symlinked dotfiles we'll open with O_NOFOLLOW
In the commits merged in via 204333b (Merge branch 'jk/open-dotgitx-with-nofollow', 2021-03-22), we stopped following symbolic links for .gitattributes, .gitignore, and .mailmap files. Let's teach fsck to warn that these symlinks are not going to do anything. Note that this is just a warning, and won't block the objects via transfer.fsckObjects, since there are reported to be cases of this in the wild (and even once fixed, they will continue to exist in the commit history of those projects, but are not particularly dangerous). Note that we won't add these to the existing gitmodules block in the fsck code. The logic for gitmodules is a bit more complicated, as we also check the content of non-symlink instances we find. But for these new files, there is no content check; we're just looking at the name and mode of the tree entry (and we can avoid even the complicated name checks in the common case that the mode doesn't indicate a symlink). We can reuse the test helper function we defined for .gitmodules, though (it needs some slight adjustments for the fsck error code, and because we don't block these symlinks via verify_path()). Note that I didn't explicitly test the transfer.fsckObjects case here (nor does the existing .gitmodules test that it blocks a push). The translation of fsck severities to outcomes is covered in general in t5504. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 801ed01 commit bb6832d

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

fsck.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,24 @@ static int fsck_tree(const struct object_id *tree_oid,
614614
".gitmodules is a symbolic link");
615615
}
616616

617+
if (S_ISLNK(mode)) {
618+
if (is_hfs_dotgitignore(name) ||
619+
is_ntfs_dotgitignore(name))
620+
retval += report(options, tree_oid, OBJ_TREE,
621+
FSCK_MSG_GITIGNORE_SYMLINK,
622+
".gitignore is a symlink");
623+
if (is_hfs_dotgitattributes(name) ||
624+
is_ntfs_dotgitattributes(name))
625+
retval += report(options, tree_oid, OBJ_TREE,
626+
FSCK_MSG_GITATTRIBUTES_SYMLINK,
627+
".gitattributes is a symlink");
628+
if (is_hfs_dotmailmap(name) ||
629+
is_ntfs_dotmailmap(name))
630+
retval += report(options, tree_oid, OBJ_TREE,
631+
FSCK_MSG_MAILMAP_SYMLINK,
632+
".mailmap is a symlink");
633+
}
634+
617635
if ((backslash = strchr(name, '\\'))) {
618636
while (backslash) {
619637
backslash++;

fsck.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ enum fsck_msg_type {
6767
FUNC(NUL_IN_COMMIT, WARN) \
6868
/* infos (reported as warnings, but ignored by default) */ \
6969
FUNC(GITMODULES_PARSE, INFO) \
70+
FUNC(GITIGNORE_SYMLINK, INFO) \
71+
FUNC(GITATTRIBUTES_SYMLINK, INFO) \
72+
FUNC(MAILMAP_SYMLINK, INFO) \
7073
FUNC(BAD_TAG_NAME, INFO) \
7174
FUNC(MISSING_TAGGER_ENTRY, INFO) \
7275
/* ignored (elevated when requested) */ \

t/t7450-bad-git-dotfiles.sh

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@ test_expect_success 'index-pack --strict works for non-repo pack' '
140140
'
141141

142142
check_dotx_symlink () {
143+
fsck_must_fail=test_must_fail
144+
fsck_prefix=error
145+
refuse_index=t
146+
case "$1" in
147+
--warning)
148+
fsck_must_fail=
149+
fsck_prefix=warning
150+
refuse_index=
151+
shift
152+
;;
153+
esac
154+
143155
name=$1
144156
type=$2
145157
path=$3
@@ -172,11 +184,12 @@ check_dotx_symlink () {
172184
173185
# Check not only that we fail, but that it is due to the
174186
# symlink detector
175-
test_must_fail git fsck 2>output &&
176-
grep "tree $tree: ${name}Symlink" output
187+
$fsck_must_fail git fsck 2>output &&
188+
grep "$fsck_prefix.*tree $tree: ${name}Symlink" output
177189
)
178190
'
179191

192+
test -n "$refuse_index" &&
180193
test_expect_success "refuse to load symlinked $name into index ($type)" '
181194
test_must_fail \
182195
git -C $dir \
@@ -193,6 +206,18 @@ check_dotx_symlink gitmodules vanilla .gitmodules
193206
check_dotx_symlink gitmodules ntfs ".gitmodules ."
194207
check_dotx_symlink gitmodules hfs ".${u200c}gitmodules"
195208

209+
check_dotx_symlink --warning gitattributes vanilla .gitattributes
210+
check_dotx_symlink --warning gitattributes ntfs ".gitattributes ."
211+
check_dotx_symlink --warning gitattributes hfs ".${u200c}gitattributes"
212+
213+
check_dotx_symlink --warning gitignore vanilla .gitignore
214+
check_dotx_symlink --warning gitignore ntfs ".gitignore ."
215+
check_dotx_symlink --warning gitignore hfs ".${u200c}gitignore"
216+
217+
check_dotx_symlink --warning mailmap vanilla .mailmap
218+
check_dotx_symlink --warning mailmap ntfs ".mailmap ."
219+
check_dotx_symlink --warning mailmap hfs ".${u200c}mailmap"
220+
196221
test_expect_success 'fsck detects non-blob .gitmodules' '
197222
git init non-blob &&
198223
(

0 commit comments

Comments
 (0)