]> git.ipfire.org Git - thirdparty/git.git/commitdiff
fsck: warn about symlinked dotfiles we'll open with O_NOFOLLOW
authorJeff King <peff@peff.net>
Mon, 3 May 2021 20:43:25 +0000 (16:43 -0400)
committerJunio C Hamano <gitster@pobox.com>
Tue, 4 May 2021 02:52:02 +0000 (11:52 +0900)
In the commits merged in via 204333b015 (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 <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
fsck.c
fsck.h
t/t7450-bad-git-dotfiles.sh

diff --git a/fsck.c b/fsck.c
index db948178980be27c310f5f1e10621b3e191f252f..3ec500d707aaf602e4e1c12139ccad075feb8046 100644 (file)
--- a/fsck.c
+++ b/fsck.c
@@ -614,6 +614,24 @@ static int fsck_tree(const struct object_id *tree_oid,
                                                 ".gitmodules is a symbolic link");
                }
 
+               if (S_ISLNK(mode)) {
+                       if (is_hfs_dotgitignore(name) ||
+                           is_ntfs_dotgitignore(name))
+                               retval += report(options, tree_oid, OBJ_TREE,
+                                                FSCK_MSG_GITIGNORE_SYMLINK,
+                                                ".gitignore is a symlink");
+                       if (is_hfs_dotgitattributes(name) ||
+                           is_ntfs_dotgitattributes(name))
+                               retval += report(options, tree_oid, OBJ_TREE,
+                                                FSCK_MSG_GITATTRIBUTES_SYMLINK,
+                                                ".gitattributes is a symlink");
+                       if (is_hfs_dotmailmap(name) ||
+                           is_ntfs_dotmailmap(name))
+                               retval += report(options, tree_oid, OBJ_TREE,
+                                                FSCK_MSG_MAILMAP_SYMLINK,
+                                                ".mailmap is a symlink");
+               }
+
                if ((backslash = strchr(name, '\\'))) {
                        while (backslash) {
                                backslash++;
diff --git a/fsck.h b/fsck.h
index 7202c3c87e8b94d3ba7811a21dca5cdbb46f81e5..d07f7a2459e8a264fe8cb2b6457d0ea729bb9e49 100644 (file)
--- a/fsck.h
+++ b/fsck.h
@@ -67,6 +67,9 @@ enum fsck_msg_type {
        FUNC(NUL_IN_COMMIT, WARN) \
        /* infos (reported as warnings, but ignored by default) */ \
        FUNC(GITMODULES_PARSE, INFO) \
+       FUNC(GITIGNORE_SYMLINK, INFO) \
+       FUNC(GITATTRIBUTES_SYMLINK, INFO) \
+       FUNC(MAILMAP_SYMLINK, INFO) \
        FUNC(BAD_TAG_NAME, INFO) \
        FUNC(MISSING_TAGGER_ENTRY, INFO) \
        /* ignored (elevated when requested) */ \
index b494d72976a20141160ac6fa20cd6c464f6cfcf9..e2773bb06d4bcb64b57c16047719143947786da5 100755 (executable)
@@ -140,6 +140,18 @@ test_expect_success 'index-pack --strict works for non-repo pack' '
 '
 
 check_dotx_symlink () {
+       fsck_must_fail=test_must_fail
+       fsck_prefix=error
+       refuse_index=t
+       case "$1" in
+       --warning)
+               fsck_must_fail=
+               fsck_prefix=warning
+               refuse_index=
+               shift
+               ;;
+       esac
+
        name=$1
        type=$2
        path=$3
@@ -172,11 +184,12 @@ check_dotx_symlink () {
 
                        # Check not only that we fail, but that it is due to the
                        # symlink detector
-                       test_must_fail git fsck 2>output &&
-                       grep "tree $tree: ${name}Symlink" output
+                       $fsck_must_fail git fsck 2>output &&
+                       grep "$fsck_prefix.*tree $tree: ${name}Symlink" output
                )
        '
 
+       test -n "$refuse_index" &&
        test_expect_success "refuse to load symlinked $name into index ($type)" '
                test_must_fail \
                        git -C $dir \
@@ -193,6 +206,18 @@ check_dotx_symlink gitmodules vanilla .gitmodules
 check_dotx_symlink gitmodules ntfs ".gitmodules ."
 check_dotx_symlink gitmodules hfs ".${u200c}gitmodules"
 
+check_dotx_symlink --warning gitattributes vanilla .gitattributes
+check_dotx_symlink --warning gitattributes ntfs ".gitattributes ."
+check_dotx_symlink --warning gitattributes hfs ".${u200c}gitattributes"
+
+check_dotx_symlink --warning gitignore vanilla .gitignore
+check_dotx_symlink --warning gitignore ntfs ".gitignore ."
+check_dotx_symlink --warning gitignore hfs ".${u200c}gitignore"
+
+check_dotx_symlink --warning mailmap vanilla .mailmap
+check_dotx_symlink --warning mailmap ntfs ".mailmap ."
+check_dotx_symlink --warning mailmap hfs ".${u200c}mailmap"
+
 test_expect_success 'fsck detects non-blob .gitmodules' '
        git init non-blob &&
        (