{"thread":{"id":"66886","subject":"[PATCH] attr: do not traverse leading directory symlinks","startedAt":"2026-11-19T19:23:02Z","lastAt":"2026-11-19T19:23:02Z","messageCount":1,"participants":["Dominik Maier"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"558879","messageId":"20261119192302.2681052-1-dmnk@google.com","threadId":"66886","inReplyTo":null,"subject":"[PATCH] attr: do not traverse leading directory symlinks","fromName":"Dominik Maier","fromEmail":"dmnk@google.com","sentAt":"2026-11-19T19:23:02Z","receivedAt":"2026-11-19T19:23:02Z","isPatch":true,"sender":{"key":"dmnk@google.com","avatar":null},"body":"Commit 2ef579e261 (attr: do not respect symlinks for in-tree\n.gitattributes, 2021-02-16) taught read_attr_from_file() to open in-tree\n.gitattributes files with open_nofollow() so worktree lookups match\nindex lookups and do not follow symlinks outside the repository.\n\nopen_nofollow() only checks O_NOFOLLOW on the final \".gitattributes\"\ncomponent. When an explicit path such as \"symdir/file\" is passed to\n\"git check-attr\" and \"symdir\" is a directory symlink,\nprepare_attr_stack() still traverses \"symdir\" to read\n\"symdir/.gitattributes\", and builtin_object_mode_attr() calls\nlstat(\"symdir/file\") across the link.\n\nCheck has_symlink_leading_path() before opening READ_ATTR_NOFOLLOW\nfiles in read_attr_from_file() and before stating paths in\nbuiltin_object_mode_attr().\n\nSigned-off-by: Dominik Maier <dmnk@google.com>\n---\n attr.c                | 10 ++++++++--\n t/t0003-attributes.sh | 10 ++++++++++\n 2 files changed, 18 insertions(+), 2 deletions(-)\n\ndiff --git a/attr.c b/attr.c\nindex 0e63f1b6de..50e25a3ee6 100644\n--- a/attr.c\n+++ b/attr.c\n@@ -24,6 +24,7 @@\n #include \"revision.h\"\n #include \"odb.h\"\n #include \"setup.h\"\n+#include \"symlinks.h\"\n #include \"thread-utils.h\"\n #include \"tree-walk.h\"\n #include \"object-name.h\"\n@@ -699,10 +700,13 @@ static struct attr_stack *read_attr_from_file(const char *path, unsigned flags)\n \tint lineno = 0;\n \tstruct stat st;\n \n-\tif (flags & READ_ATTR_NOFOLLOW)\n+\tif (flags & READ_ATTR_NOFOLLOW) {\n+\t\tif (has_symlink_leading_path(path, strlen(path)))\n+\t\t\treturn NULL;\n \t\tfd = open_nofollow(path, O_RDONLY);\n-\telse\n+\t} else {\n \t\tfd = open(path, O_RDONLY);\n+\t}\n \n \tif (fd < 0) {\n \t\twarn_on_fopen_errors(path);\n@@ -1274,6 +1278,8 @@ static const char *builtin_object_mode_attr(struct index_state *istate, const ch\n \tif (direction == GIT_ATTR_CHECKIN) {\n \t\tstruct object_id oid;\n \t\tstruct stat st;\n+\t\tif (has_symlink_leading_path(path, strlen(path)))\n+\t\t\treturn ATTR__UNSET;\n \t\tif (lstat(path, &st))\n \t\t\tdie_errno(_(\"unable to stat '%s'\"), path);\n \t\tmode = canon_mode(st.st_mode);\ndiff --git a/t/t0003-attributes.sh b/t/t0003-attributes.sh\nindex 582e207aa1..c252e7ed55 100755\n--- a/t/t0003-attributes.sh\n+++ b/t/t0003-attributes.sh\n@@ -533,6 +533,16 @@ test_expect_success SYMLINKS 'symlinks not respected in-tree' '\n \ttest_grep \"unable to access.*gitattributes\" err\n '\n \n+test_expect_success SYMLINKS 'leading directory symlinks not traversed in-tree' '\n+\ttest_when_finished \"rm -rf ext_dir symdir\" &&\n+\tmkdir ext_dir &&\n+\techo \"* test\" >ext_dir/.gitattributes &&\n+\t>ext_dir/file &&\n+\tln -s ext_dir symdir &&\n+\tattr_check_basic symdir/file unspecified &&\n+\tattr_check_object_mode symdir/file unspecified\n+'\n+\n test_expect_success 'large attributes line ignored in tree' '\n \ttest_when_finished \"rm .gitattributes\" &&\n \tprintf \"path %02043d\" 1 >.gitattributes &&\n-- \n2.56.1.750.gf70dc682cf-goog\n\n\n"}]}