git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 0/8] Directory traversal fixes

From
Elijah Newren via GitGitGadget <gitgitgadget@gmail.com>
Date
May 8, 2021, 19:58 UTC
Message-ID
<pull.1020.v3.git.git.1620503945.gitgitgadget@gmail.com>
In-Reply-To
<pull.1020.v2.git.git.1620432500.gitgitgadget@gmail.com>

This patchset fixes a few directory traversal issues, where fill_directory() would traverse into directories that it shouldn't and not traverse into directories that it should (one of which was originally reported on this list at [1]). And it includes a few cleanups

Changes since v2:
 * Move the RFC patches to the front
 * Deletes all the ugly test code that stole reviewer attention away from
   the rest of the series. :-) The RFC patches being first allow the test to
   be dramatically simplified and rewritten.
 * Included cleanups suggested by Phillip Oakley and Eric Sunshine (the
   cleanups suggested by others are obsolete with the test rewrite).
Patches 1-3 are RFC because
 * (1) I'm not that familiar with trace1 & trace2; I've only used trace2 for
   region_enter() and region_leave() calls before. And I'm unsure if
   removing trace1 counts as a backward compatibility issue or not, though
   the trace2 documentation claims it's meant to replace trace1.
 * (2) The ls-files -i handling to print an error instead of operating as
   before might be considered a backward incompatible change. I want to hear
   others' opinions on that.

Also, if anyone has any ideas about a better place to put the "Some sidenotes" from the sixth commit message rather than keeping them in a random commit message, that might be helpful too.

[1] See https://lore.kernel.org/git/DM6PR00MB06829EC5B85E0C5AC595004E894E9@DM6PR00MB0682.namprd00.prod.outlook.com/ or alternatively https://github.com/git-for-windows/git/issues/2732.

Derrick Stolee (1):
  dir: update stale description of treat_directory()
Elijah Newren (7):
  [RFC] dir: convert trace calls to trace2 equivalents
  [RFC] dir: report number of visited directories and paths with trace2
  [RFC] ls-files: error out on -i unless -o or -c are specified
  t7300: add testcase showing unnecessary traversal into ignored
    directory
  t3001, t7300: add testcase showcasing missed directory traversal
  dir: avoid unnecessary traversal into ignored directory
  dir: traverse into untracked directories if they may have ignored
    subfiles
 builtin/ls-files.c                 |   3 +
 dir.c                              | 103 +++++++++------
 dir.h                              |   4 +
 t/t1306-xdg-files.sh               |   2 +-
 t/t3001-ls-files-others-exclude.sh |   5 +
 t/t3003-ls-files-exclude.sh        |   4 +-
 t/t7063-status-untracked-cache.sh  | 194 ++++++++++++++++-------------
 t/t7300-clean.sh                   |  41 ++++++
 t/t7519-status-fsmonitor.sh        |   8 +-
 9 files changed, 238 insertions(+), 126 deletions(-)
base-commit: 311531c9de557d25ac087c1637818bd2aad6eb3a
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1020%2Fnewren%2Fdirectory-traversal-fixes-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1020/newren/directory-traversal-fixes-v3
Pull-Request: https://github.com/git/git/pull/1020
Range-diff vs v2:
 7:  3a2394506a53 = 1:  9f1c0d78d739 [RFC] dir: convert trace calls to trace2 equivalents
 8:  fba4d65b78c7 ! 2:  8b511f228af8 [RFC] dir: reported number of visited directories and paths with trace2
     @@ Metadata
      Author: Elijah Newren <newren@gmail.com>
      
       ## Commit message ##
     -    [RFC] dir: reported number of visited directories and paths with trace2
     +    [RFC] dir: report number of visited directories and paths with trace2
      
     -    Previously, tests that wanted to verify that we don't traverse into a
     -    deep directory hierarchy that is ignored had no easy way to verify and
     -    enforce that behavior.  Record information about the number of
     -    directories and paths we inspect while traversing the directory
     -    hierarchy in read_directory(), and when trace2 is enabled, print these
     -    statistics.
     -
     -    Make use of these statistics in t7300 to simplify (and vastly improve
     -    the performance of) the "avoid traversing into ignored directories"
     -    test.
     +    Provide more statistics in trace2 output that include the number of
     +    directories and total paths visited by the directory traversal logic.
     +    Subsequent patches will take advantage of this to ensure we do not
     +    unnecessarily traverse into ignored directories.
      
          Signed-off-by: Elijah Newren <newren@gmail.com>
      
     @@ t/t7063-status-untracked-cache.sh: get_relevant_traces() {
       	    | cut -d "|" -f 9 \
       	    >$OUTPUT_FILE
       }
     -
     - ## t/t7300-clean.sh ##
     -@@ t/t7300-clean.sh: test_expect_success 'clean untracked paths by pathspec' '
     - '
     - 
     - test_expect_success 'avoid traversing into ignored directories' '
     --	test_when_finished rm -f output error &&
     -+	test_when_finished rm -f output error trace.* &&
     - 	test_create_repo avoid-traversing-deep-hierarchy &&
     - 	(
     - 		cd avoid-traversing-deep-hierarchy &&
     - 
     --		>directory-random-file.txt &&
     --		# Put this file under directory400/directory399/.../directory1/
     --		depth=400 &&
     --		for x in $(test_seq 1 $depth); do
     --			mkdir "tmpdirectory$x" &&
     --			mv directory* "tmpdirectory$x" &&
     --			mv "tmpdirectory$x" "directory$x"
     --		done &&
     --
     --		git clean -ffdxn -e directory$depth >../output 2>../error &&
     --
     --		test_must_be_empty ../output &&
     --		# We especially do not want things like
     --		#   "warning: could not open directory "
     --		# appearing in the error output.  It is true that directories
     --		# that are too long cannot be opened, but we should not be
     --		# recursing into those directories anyway since the very first
     --		# level is ignored.
     --		test_must_be_empty ../error &&
     --
     --		# alpine-linux-musl fails to "rm -rf" a directory with such
     --		# a deeply nested hierarchy.  Help it out by deleting the
     --		# leading directories ourselves.  Super slow, but, what else
     --		# can we do?  Without this, we will hit a
     --		#     error: Tests passed but test cleanup failed; aborting
     --		# so do this ugly manual cleanup...
     --		while test ! -f directory-random-file.txt; do
     --			name=$(ls -d directory*) &&
     --			mv $name/* . &&
     --			rmdir $name
     --		done
     -+		mkdir -p untracked/subdir/with/a &&
     -+		>untracked/subdir/with/a/random-file.txt &&
     -+
     -+		GIT_TRACE2_PERF="$TRASH_DIRECTORY/trace.output" \
     -+		git clean -ffdxn -e untracked &&
     -+
     -+		grep data.*read_directo.*visited ../trace.output \
     -+			| cut -d "|" -f 9 >../trace.relevant &&
     -+		cat >../trace.expect <<-EOF &&
     -+		 directories-visited:1
     -+		 paths-visited:4
     -+		EOF
     -+		test_cmp ../trace.expect ../trace.relevant
     - 	)
     - '
     - 
 5:  3d8dd00ccd10 = 3:  44a1322c4402 [RFC] ls-files: error out on -i unless -o or -c are specified
 1:  a3bd253fa8e8 ! 4:  dc3d3f247141 t7300: add testcase showing unnecessary traversal into ignored directory
     @@ Metadata
       ## Commit message ##
          t7300: add testcase showing unnecessary traversal into ignored directory
      
     -    PNPM is apparently creating deeply nested (but ignored) directory
     -    structures; traversing them is costly performance-wise, unnecessary, and
     -    in some cases is even throwing warnings/errors because the paths are too
     -    long to handle on various platforms.  Add a testcase that demonstrates
     -    this problem.
     +    The PNPM package manager is apparently creating deeply nested (but
     +    ignored) directory structures; traversing them is costly
     +    performance-wise, unnecessary, and in some cases is even throwing
     +    warnings/errors because the paths are too long to handle on various
     +    platforms.  Add a testcase that checks for such unnecessary directory
     +    traversal.
      
     -    Initial-test-by: Jason Gore <Jason.Gore@microsoft.com>
     -    Helped-by: brian m. carlson <sandals@crustytoothpaste.net>
          Signed-off-by: Elijah Newren <newren@gmail.com>
      
       ## t/t7300-clean.sh ##
     @@ t/t7300-clean.sh: test_expect_success 'clean untracked paths by pathspec' '
       '
       
      +test_expect_failure 'avoid traversing into ignored directories' '
     -+	test_when_finished rm -f output error &&
     ++	test_when_finished rm -f output error trace.* &&
      +	test_create_repo avoid-traversing-deep-hierarchy &&
      +	(
      +		cd avoid-traversing-deep-hierarchy &&
      +
     -+		>directory-random-file.txt &&
     -+		# Put this file under directory400/directory399/.../directory1/
     -+		depth=400 &&
     -+		for x in $(test_seq 1 $depth); do
     -+			mkdir "tmpdirectory$x" &&
     -+			mv directory* "tmpdirectory$x" &&
     -+			mv "tmpdirectory$x" "directory$x"
     -+		done &&
     ++		mkdir -p untracked/subdir/with/a &&
     ++		>untracked/subdir/with/a/random-file.txt &&
      +
     -+		git clean -ffdxn -e directory$depth >../output 2>../error &&
     ++		GIT_TRACE2_PERF="$TRASH_DIRECTORY/trace.output" \
     ++		git clean -ffdxn -e untracked
     ++	) &&
      +
     -+		test_must_be_empty ../output &&
     -+		# We especially do not want things like
     -+		#   "warning: could not open directory "
     -+		# appearing in the error output.  It is true that directories
     -+		# that are too long cannot be opened, but we should not be
     -+		# recursing into those directories anyway since the very first
     -+		# level is ignored.
     -+		test_must_be_empty ../error &&
     -+
     -+		# alpine-linux-musl fails to "rm -rf" a directory with such
     -+		# a deeply nested hierarchy.  Help it out by deleting the
     -+		# leading directories ourselves.  Super slow, but, what else
     -+		# can we do?  Without this, we will hit a
     -+		#     error: Tests passed but test cleanup failed; aborting
     -+		# so do this ugly manual cleanup...
     -+		while test ! -f directory-random-file.txt; do
     -+			name=$(ls -d directory*) &&
     -+			mv $name/* . &&
     -+			rmdir $name
     -+		done
     -+	)
     ++	grep data.*read_directo.*visited trace.output \
     ++		| cut -d "|" -f 9 >trace.relevant &&
     ++	cat >trace.expect <<-EOF &&
     ++	 directories-visited:1
     ++	 paths-visited:4
     ++	EOF
     ++	test_cmp trace.expect trace.relevant
      +'
      +
       test_done
 2:  aa3a41e26eca ! 5:  73b03a1e8e05 t3001, t7300: add testcase showcasing missed directory traversal
     @@ t/t3001-ls-files-others-exclude.sh: EOF
      
       ## t/t7300-clean.sh ##
      @@ t/t7300-clean.sh: test_expect_failure 'avoid traversing into ignored directories' '
     - 	)
     + 	test_cmp trace.expect trace.relevant
       '
       
      +test_expect_failure 'traverse into directories that may have ignored entries' '
 3:  3c3f6111da13 ! 6:  66ffc7f02d08 dir: avoid unnecessary traversal into ignored directory
     @@ t/t7300-clean.sh: test_expect_success 'clean untracked paths by pathspec' '
       
      -test_expect_failure 'avoid traversing into ignored directories' '
      +test_expect_success 'avoid traversing into ignored directories' '
     - 	test_when_finished rm -f output error &&
     + 	test_when_finished rm -f output error trace.* &&
       	test_create_repo avoid-traversing-deep-hierarchy &&
       	(
 4:  fad048339b81 ! 7:  acde436b220e dir: traverse into untracked directories if they may have ignored subfiles
     @@ t/t3001-ls-files-others-exclude.sh: EOF
      
       ## t/t7300-clean.sh ##
      @@ t/t7300-clean.sh: test_expect_success 'avoid traversing into ignored directories' '
     - 	)
     + 	test_cmp trace.expect trace.relevant
       '
       
      -test_expect_failure 'traverse into directories that may have ignored entries' '
 6:  1d825dfdc70b = 8:  57135c357774 dir: update stale description of treat_directory()
-- 
gitgitgadget
Previous: Elijah Newren via GitGitGadgetNext: Elijah Newren via GitGitGadget
Message 35 of 90 in “Directory traversal fixes”
  1. 0/5 Directory traversal fixesElijah Newren via GitGitGadget, May 7, 2021
  2. 1/5 t7300: add testcase showing unnecessary traversal into ignored directoryElijah Newren via GitGitGadget, May 7, 2021
  3. Eric SunshineMay 7, 2021
  4. Elijah NewrenMay 7, 2021
  5. Eric SunshineMay 7, 2021
  6. Elijah NewrenMay 7, 2021
  7. Eric SunshineMay 7, 2021
  8. Jeff KingMay 7, 2021
  9. Eric SunshineMay 7, 2021
  10. Elijah NewrenMay 8, 2021
  11. Eric SunshineMay 8, 2021
  12. Elijah NewrenMay 8, 2021
  13. Philip OakleyMay 8, 2021
  14. Elijah NewrenMay 8, 2021
  15. 2/5 t3001, t7300: add testcase showcasing missed directory traversalElijah Newren via GitGitGadget, May 7, 2021
  16. 3/5 dir: avoid unnecessary traversal into ignored directoryElijah Newren via GitGitGadget, May 7, 2021
  17. 4/5 dir: traverse into untracked directories if they may have ignored subfilesElijah Newren via GitGitGadget, May 7, 2021
  18. 5/5 [RFC] ls-files: error out on -i unless -o or -c are specifiedElijah Newren via GitGitGadget, May 7, 2021
  19. 6/5 dir: update stale description of treat_directory()Derrick Stolee, May 7, 2021
  20. Elijah NewrenMay 7, 2021
  21. Derrick StoleeMay 7, 2021
  22. 0/8 Directory traversal fixesElijah Newren via GitGitGadget, May 8, 2021
  23. 1/8 t7300: add testcase showing unnecessary traversal into ignored directoryElijah Newren via GitGitGadget, May 8, 2021
  24. Junio C HamanoMay 8, 2021
  25. Elijah NewrenMay 8, 2021
  26. Junio C HamanoMay 8, 2021
  27. Elijah NewrenMay 8, 2021
  28. 3/8 dir: avoid unnecessary traversal into ignored directoryElijah Newren via GitGitGadget, May 8, 2021
  29. 2/8 t3001, t7300: add testcase showcasing missed directory traversalElijah Newren via GitGitGadget, May 8, 2021
  30. 4/8 dir: traverse into untracked directories if they may have ignored subfilesElijah Newren via GitGitGadget, May 8, 2021
  31. 5/8 [RFC] ls-files: error out on -i unless -o or -c are specifiedElijah Newren via GitGitGadget, May 8, 2021
  32. 6/8 dir: update stale description of treat_directory()Derrick Stolee via GitGitGadget, May 8, 2021
  33. 7/8 [RFC] dir: convert trace calls to trace2 equivalentsElijah Newren via GitGitGadget, May 8, 2021
  34. 8/8 [RFC] dir: reported number of visited directories and paths with trace2Elijah Newren via GitGitGadget, May 8, 2021
  35. 0/8 Directory traversal fixesElijah Newren via GitGitGadget, May 8, 2021
  36. 1/8 [RFC] dir: convert trace calls to trace2 equivalentsElijah Newren via GitGitGadget, May 8, 2021
  37. Junio C HamanoMay 10, 2021
  38. Elijah NewrenMay 11, 2021
  39. Jeff HostetlerMay 11, 2021
  40. Elijah NewrenMay 11, 2021
  41. 2/8 [RFC] dir: report number of visited directories and paths with trace2Elijah Newren via GitGitGadget, May 8, 2021
  42. Junio C HamanoMay 10, 2021
  43. 3/8 [RFC] ls-files: error out on -i unless -o or -c are specifiedElijah Newren via GitGitGadget, May 8, 2021
  44. Junio C HamanoMay 10, 2021
  45. Elijah NewrenMay 11, 2021
  46. Junio C HamanoMay 11, 2021
  47. 4/8 t7300: add testcase showing unnecessary traversal into ignored directoryElijah Newren via GitGitGadget, May 8, 2021
  48. Junio C HamanoMay 10, 2021
  49. Elijah NewrenMay 11, 2021
  50. Junio C HamanoMay 11, 2021
  51. Elijah NewrenMay 12, 2021
  52. Junio C HamanoMay 12, 2021
  53. 5/8 t3001, t7300: add testcase showcasing missed directory traversalElijah Newren via GitGitGadget, May 8, 2021
  54. 6/8 dir: avoid unnecessary traversal into ignored directoryElijah Newren via GitGitGadget, May 8, 2021
  55. Junio C HamanoMay 10, 2021
  56. Elijah NewrenMay 11, 2021
  57. 7/8 dir: traverse into untracked directories if they may have ignored subfilesElijah Newren via GitGitGadget, May 8, 2021
  58. 8/8 dir: update stale description of treat_directory()Derrick Stolee via GitGitGadget, May 8, 2021
  59. 0/8 Directory traversal fixesElijah Newren via GitGitGadget, May 11, 2021
  60. 1/8 dir: convert trace calls to trace2 equivalentsElijah Newren via GitGitGadget, May 11, 2021
  61. Jeff HostetlerMay 11, 2021
  62. Elijah NewrenMay 11, 2021
  63. Jeff HostetlerMay 11, 2021
  64. Elijah NewrenMay 12, 2021
  65. Jeff HostetlerMay 12, 2021
  66. Elijah NewrenMay 12, 2021
  67. 3/8 ls-files: error out on -i unless -o or -c are specifiedElijah Newren via GitGitGadget, May 11, 2021
  68. 2/8 dir: report number of visited directories and paths with trace2Elijah Newren via GitGitGadget, May 11, 2021
  69. 4/8 t7300: add testcase showing unnecessary traversal into ignored directoryElijah Newren via GitGitGadget, May 11, 2021
  70. 5/8 t3001, t7300: add testcase showcasing missed directory traversalElijah Newren via GitGitGadget, May 11, 2021
  71. 7/8 dir: traverse into untracked directories if they may have ignored subfilesElijah Newren via GitGitGadget, May 11, 2021
  72. 6/8 dir: avoid unnecessary traversal into ignored directoryElijah Newren via GitGitGadget, May 11, 2021
  73. 8/8 dir: update stale description of treat_directory()Derrick Stolee via GitGitGadget, May 11, 2021
  74. 0/9 Directory traversal fixesElijah Newren via GitGitGadget, May 12, 2021
  75. 1/9 dir: convert trace calls to trace2 equivalentsElijah Newren via GitGitGadget, May 12, 2021
  76. 2/9 dir: report number of visited directories and paths with trace2Elijah Newren via GitGitGadget, May 12, 2021
  77. 4/9 t7300: add testcase showing unnecessary traversal into ignored directoryElijah Newren via GitGitGadget, May 12, 2021
  78. 6/9 dir: avoid unnecessary traversal into ignored directoryElijah Newren via GitGitGadget, May 12, 2021
  79. 8/9 dir: update stale description of treat_directory()Derrick Stolee via GitGitGadget, May 12, 2021
  80. Derrick StoleeMay 17, 2021
  81. Junio C HamanoMay 17, 2021
  82. Elijah NewrenMay 18, 2021
  83. Junio C HamanoMay 19, 2021
  84. 7/9 dir: traverse into untracked directories if they may have ignored subfilesElijah Newren via GitGitGadget, May 12, 2021
  85. 3/9 ls-files: error out on -i unless -o or -c are specifiedElijah Newren via GitGitGadget, May 12, 2021
  86. 5/9 t3001, t7300: add testcase showcasing missed directory traversalElijah Newren via GitGitGadget, May 12, 2021
  87. 9/9 dir: introduce readdir_skip_dot_and_dotdot() helperElijah Newren via GitGitGadget, May 12, 2021
  88. Derrick StoleeMay 17, 2021
  89. Elijah NewrenMay 18, 2021
  90. Derrick StoleeMay 17, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.