Commit dbc03de479eec4f96ce3273bf76f49daccbeb496

Edward Thomson 2021-03-20T14:28:25

apply: ensure we validate paths There was no test ensuring that we validate `.git` paths. We do, but let's add a test to make sure that we never regress this.

diff --git a/src/path.c b/src/path.c
index a4c289e..50a0b46 100644
--- a/src/path.c
+++ b/src/path.c
@@ -1923,7 +1923,7 @@ GIT_INLINE(bool) should_validate_longpaths(git_repository *repo)
 int git_path_validate_workdir(git_repository *repo, const char *path)
 {
 	if (should_validate_longpaths(repo))
-		return git_path_validate_ondisk(path, strlen(path));
+		return git_path_validate_filesystem(path, strlen(path));
 
 	return 0;
 }
@@ -1934,7 +1934,7 @@ int git_path_validate_workdir_with_len(
 	size_t path_len)
 {
 	if (should_validate_longpaths(repo))
-		return git_path_validate_ondisk(path, path_len);
+		return git_path_validate_filesystem(path, path_len);
 
 	return 0;
 }
diff --git a/tests/apply/apply_helpers.h b/tests/apply/apply_helpers.h
index 2d0019a..8209477 100644
--- a/tests/apply/apply_helpers.h
+++ b/tests/apply/apply_helpers.h
@@ -474,6 +474,15 @@
 	"-asparagus which had been laid by, boil it until these last articles are\n" \
 	"-sufficiently done, thicken with flour, butter and milk, and serve it up.\n"
 
+#define DIFF_ADD_INVALID_FILENAME \
+	"diff --git a/.git/hello_world.txt b/.git/hello_world.txt\n" \
+	"new file mode 100644\n" \
+	"index 0000000..f75ba05\n" \
+	"--- /dev/null\n" \
+	"+++ b/.git/hello_world.txt\n" \
+	"@@ -0,0 +1 @@\n" \
+	"+Hello, world.\n"
+
 void validate_apply_workdir(
 	git_repository *repo,
 	struct merge_index_entry *workdir_entries,
diff --git a/tests/apply/both.c b/tests/apply/both.c
index 400df5e..1089632 100644
--- a/tests/apply/both.c
+++ b/tests/apply/both.c
@@ -734,3 +734,14 @@ void test_apply_both__cant_remove_file_twice(void)
 
 	git_diff_free(diff);
 }
+
+void test_apply_both__cant_add_invalid_filename(void)
+{
+	git_diff *diff;
+
+	cl_git_pass(git_diff_from_buffer(&diff, DIFF_ADD_INVALID_FILENAME,
+		strlen(DIFF_ADD_INVALID_FILENAME)));
+	cl_git_fail(git_apply(repo, diff, GIT_APPLY_LOCATION_BOTH, NULL));
+
+	git_diff_free(diff);
+}