git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: git@vger.kernel.org
Cc: Phillip Wood <phillip.wood123@gmail.com>
Subject: [PATCH v4 2/4] safe.directory: normalize the checked path
Date: Tue, 30 Jul 2024 11:43:50 -0700	[thread overview]
Message-ID: <20240730184352.2503276-3-gitster@pobox.com> (raw)
In-Reply-To: <20240730184352.2503276-1-gitster@pobox.com>

The pathname of a repository comes from getcwd() and it could be a
path aliased via symbolic links, e.g., the real directory may be
/home/u/repository but a symbolic link /home/u/repo may point at it,
and the clone request may come as "git clone file:///home/u/repo/".

A request to check if /home/u/repo is safe would be rejected if the
safe.directory configuration allows /home/u/repository/ but not its
alias /home/u/repo/.  Normalize the path being checked before
comparing with safe.directory value(s).

Suggested-by: Phillip Wood <phillip.wood123@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 setup.c                   | 16 ++++++++---
 t/t0033-safe-directory.sh | 57 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 69 insertions(+), 4 deletions(-)

diff --git a/setup.c b/setup.c
index 3177d010d1..54cce7219b 100644
--- a/setup.c
+++ b/setup.c
@@ -1215,7 +1215,7 @@ static int canonicalize_ceiling_entry(struct string_list_item *item,
 }
 
 struct safe_directory_data {
-	const char *path;
+	char *path;
 	int is_safe;
 };
 
@@ -1261,9 +1261,7 @@ static int ensure_valid_ownership(const char *gitfile,
 				  const char *worktree, const char *gitdir,
 				  struct strbuf *report)
 {
-	struct safe_directory_data data = {
-		.path = worktree ? worktree : gitdir
-	};
+	struct safe_directory_data data = { 0 };
 
 	if (!git_env_bool("GIT_TEST_ASSUME_DIFFERENT_OWNER", 0) &&
 	    (!gitfile || is_path_owned_by_current_user(gitfile, report)) &&
@@ -1271,6 +1269,15 @@ static int ensure_valid_ownership(const char *gitfile,
 	    (!gitdir || is_path_owned_by_current_user(gitdir, report)))
 		return 1;
 
+	/*
+	 * normalize the data.path for comparison with normalized paths
+	 * that come from the configuration file.  The path is unsafe
+	 * if it cannot be normalized.
+	 */
+	data.path = real_pathdup(worktree ? worktree : gitdir, 0);
+	if (!data.path)
+		return 0;
+
 	/*
 	 * data.path is the "path" that identifies the repository and it is
 	 * constant regardless of what failed above. data.is_safe should be
@@ -1278,6 +1285,7 @@ static int ensure_valid_ownership(const char *gitfile,
 	 */
 	git_protected_config(safe_directory_cb, &data);
 
+	free(data.path);
 	return data.is_safe;
 }
 
diff --git a/t/t0033-safe-directory.sh b/t/t0033-safe-directory.sh
index 5fe61f1291..07ac0f9a01 100755
--- a/t/t0033-safe-directory.sh
+++ b/t/t0033-safe-directory.sh
@@ -119,4 +119,61 @@ test_expect_success 'local clone of unowned repo accepted in safe directory' '
 	test_path_is_dir target
 '
 
+test_expect_success SYMLINKS 'checked paths are normalized' '
+	test_when_finished "rm -rf repository; rm -f repo" &&
+	(
+		sane_unset GIT_TEST_ASSUME_DIFFERENT_OWNER &&
+		git config --global --unset-all safe.directory
+	) &&
+	git init repository &&
+	ln -s repository repo &&
+	(
+		cd repository &&
+		sane_unset GIT_TEST_ASSUME_DIFFERENT_OWNER &&
+		test_commit sample
+	) &&
+
+	(
+		sane_unset GIT_TEST_ASSUME_DIFFERENT_OWNER &&
+		git config --global safe.directory "$(pwd)/repository"
+	) &&
+	git -C repository for-each-ref &&
+	git -C repository/ for-each-ref &&
+	git -C repo for-each-ref &&
+	git -C repo/ for-each-ref &&
+	test_must_fail git -C repository/.git for-each-ref &&
+	test_must_fail git -C repository/.git/ for-each-ref &&
+	test_must_fail git -C repo/.git for-each-ref &&
+	test_must_fail git -C repo/.git/ for-each-ref
+'
+
+test_expect_success SYMLINKS 'checked leading paths are normalized' '
+	test_when_finished "rm -rf repository; rm -f repo" &&
+	(
+		sane_unset GIT_TEST_ASSUME_DIFFERENT_OWNER &&
+		git config --global --unset-all safe.directory
+	) &&
+	mkdir -p repository &&
+	git init repository/s &&
+	ln -s repository repo &&
+	(
+		cd repository/s &&
+		sane_unset GIT_TEST_ASSUME_DIFFERENT_OWNER &&
+		test_commit sample
+	) &&
+
+	(
+		sane_unset GIT_TEST_ASSUME_DIFFERENT_OWNER &&
+		git config --global safe.directory "$(pwd)/repository/*"
+	) &&
+	git -C repository/s for-each-ref &&
+	git -C repository/s/ for-each-ref &&
+	git -C repo/s for-each-ref &&
+	git -C repo/s/ for-each-ref &&
+	git -C repository/s/.git for-each-ref &&
+	git -C repository/s/.git/ for-each-ref &&
+	git -C repo/s/.git for-each-ref &&
+	git -C repo/s/.git/ for-each-ref
+'
+
 test_done
-- 
2.46.0-77-g633c50689c


  parent reply	other threads:[~2024-07-30 18:44 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-20 22:09 [PATCH 0/2] safe.directory clean-up Junio C Hamano
2024-07-20 22:09 ` [PATCH 1/2] safe.directory: normalize the checked path Junio C Hamano
2024-07-20 22:09 ` [PATCH 2/2] safe.directory: normalize the configured path Junio C Hamano
2024-07-20 22:09 ` [PATCH 3/2] setup: use a single return path in setup_git_directory*() Junio C Hamano
2024-07-20 22:09 ` [PATCH 4/2] setup: cache normalized safe.directory configuration Junio C Hamano
2024-07-23  2:18 ` [PATCH v2 0/3] safe.directory clean-up Junio C Hamano
2024-07-23  2:18   ` [PATCH v2 1/3] safe.directory: normalize the checked path Junio C Hamano
2024-07-23  2:18   ` [PATCH v2 2/3] safe.directory: normalize the configured path Junio C Hamano
2024-07-25  9:45     ` Phillip Wood
2024-07-25 16:11       ` Junio C Hamano
2024-08-14 13:20         ` Phillip Wood
2024-08-14 17:15           ` Junio C Hamano
2024-08-15  9:51             ` Phillip Wood
2024-08-15 14:43               ` Junio C Hamano
2024-07-26  5:02     ` Jeff King
2024-07-26 15:02       ` Junio C Hamano
2024-07-27 22:05         ` Jeff King
2024-07-23  2:19   ` [PATCH v2 3/3] safe.directory: setting safe.directory="." allows the "current" directory Junio C Hamano
2024-07-25  9:45     ` Phillip Wood
2024-07-25 16:12       ` Junio C Hamano
2024-07-25  9:45   ` [PATCH v2 0/3] safe.directory clean-up Phillip Wood
2024-07-25 16:14     ` Junio C Hamano
2024-07-30  1:10 ` [PATCH v3 " Junio C Hamano
2024-07-30  1:10   ` [PATCH v3 1/3] safe.directory: normalize the checked path Junio C Hamano
2024-07-30  1:10   ` [PATCH v3 2/3] safe.directory: normalize the configured path Junio C Hamano
2024-07-30  7:31     ` Jeff King
2024-07-30 16:03       ` Junio C Hamano
2024-07-30 20:08         ` Jeff King
2024-07-30  7:43     ` Jeff King
2024-07-30 16:22       ` Junio C Hamano
2024-07-30 17:56         ` safe.directory: preliminary clean-up Junio C Hamano
2024-07-30 20:13           ` Jeff King
2024-07-30 20:10         ` [PATCH v3 2/3] safe.directory: normalize the configured path Jeff King
2024-07-30  1:10   ` [PATCH v3 3/3] safe.directory: setting safe.directory="." allows the "current" directory Junio C Hamano
2024-07-30 18:43 ` [PATCH v4 0/4] safe.directory clean-up Junio C Hamano
2024-07-30 18:43   ` [PATCH v4 1/4] safe.directory: preliminary clean-up Junio C Hamano
2024-07-30 18:43   ` Junio C Hamano [this message]
2024-07-30 18:43   ` [PATCH v4 3/4] safe.directory: normalize the configured path Junio C Hamano
2024-07-30 18:43   ` [PATCH v4 4/4] safe.directory: setting safe.directory="." allows the "current" directory Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240730184352.2503276-3-gitster@pobox.com \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=phillip.wood123@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).