Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5335416

Browse files
committed
Test that rmtree doesn't chmod outside the tree
This adds a test in test_util that reveals the bug wheregit.util.rmtree will change the permissions on files outside thetree being removed, if the tree being removed contains a symlink toa file outside the tree, and the symlink is in a (sub)directorywhose own permissions prevent the symlink itself from beingremoved.The new test failure shows how git.util.rmtree currently callsos.chmod in a way that dereferences symlinks, including those thatpoint from inside the tree being deleted to outside it.Another similar demonstration is temporarily included in theperm.sh script. That script served as scratchwork for writing theunit test, and it can be removed soon (while keeping the test).
1 parentd5d897c commit5335416

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

‎perm.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
mkdir dir1
6+
touch dir1/file
7+
chmod -w dir1/file
8+
printf'Permissions BEFORE rmtree call:\n'
9+
ls -l dir1/file
10+
printf'\n'
11+
12+
mkdir dir2
13+
ln -s ../dir1/file dir2/symlink
14+
chmod -w dir2
15+
python -c'from git.util import rmtree; rmtree("dir2")'||true
16+
printf'\nPermissions AFTER rmtree call:\n'
17+
ls -l dir1/file

‎test/test_util.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,35 @@ def test_deletes_dir_with_readonly_files(self, tmp_path):
105105

106106
assertnottd.exists()
107107

108+
@pytest.mark.skipif(
109+
sys.platform=="cygwin",
110+
reason="Cygwin can't set the permissions that make the test meaningful.",
111+
)
112+
deftest_avoids_changing_permissions_outside_tree(self,tmp_path:pathlib.Path):
113+
# Automatically works on Windows, but on Unix requires either special handling
114+
# or refraining from attempting to fix PermissionError by making chmod calls.
115+
116+
dir1=tmp_path/"dir1"
117+
dir1.mkdir()
118+
(dir1/"file").write_bytes(b"")
119+
(dir1/"file").chmod(stat.S_IRUSR)
120+
old_mode= (dir1/"file").stat().st_mode
121+
122+
dir2=tmp_path/"dir2"
123+
dir2.mkdir()
124+
(dir2/"symlink").symlink_to(dir1/"file")
125+
dir2.chmod(stat.S_IRUSR|stat.S_IXUSR)
126+
127+
try:
128+
rmtree(dir2)
129+
exceptPermissionError:
130+
pass# On Unix, dir2 is not writable, so dir2/symlink may not be deleted.
131+
exceptSkipTestasex:
132+
self.fail(f"rmtree unexpectedly attempts skip:{ex!r}")
133+
134+
new_mode= (dir1/"file").stat().st_mode
135+
assertold_mode==new_mode,f"Should stay{old_mode:#o}, became{new_mode:#o}."
136+
108137
@pytest.mark.skipif(
109138
sys.platform=="cygwin",
110139
reason="Cygwin can't set the permissions that make the test meaningful.",

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp