File tree 4 files changed +37
-9
lines changed 4 files changed +37
-9
lines changed Original file line number Diff line number Diff line change 29
29
30
30
<!-- Changes to how Black can be configured -->
31
31
32
+ - Fix symlink handling, properly catch and ignore symlinks that point outside of root
33
+ (#4161 )
32
34
- Fix cache mtime logic that resulted in false positive cache hits (#4128 )
33
35
34
36
### Packaging
Original file line number Diff line number Diff line change 49
49
find_user_pyproject_toml ,
50
50
gen_python_files ,
51
51
get_gitignore ,
52
+ get_root_relative_path ,
52
53
normalize_path_maybe_ignore ,
53
54
parse_pyproject_toml ,
54
55
path_is_excluded ,
@@ -700,7 +701,10 @@ def get_sources(
700
701
701
702
# Compare the logic here to the logic in `gen_python_files`.
702
703
if is_stdin or path .is_file ():
703
- root_relative_path = path .absolute ().relative_to (root ).as_posix ()
704
+ root_relative_path = get_root_relative_path (path , root , report )
705
+
706
+ if root_relative_path is None :
707
+ continue
704
708
705
709
root_relative_path = "/" + root_relative_path
706
710
Original file line number Diff line number Diff line change @@ -259,14 +259,7 @@ def normalize_path_maybe_ignore(
259
259
try :
260
260
abspath = path if path .is_absolute () else Path .cwd () / path
261
261
normalized_path = abspath .resolve ()
262
- try :
263
- root_relative_path = normalized_path .relative_to (root ).as_posix ()
264
- except ValueError :
265
- if report :
266
- report .path_ignored (
267
- path , f"is a symbolic link that points outside { root } "
268
- )
269
- return None
262
+ root_relative_path = get_root_relative_path (normalized_path , root , report )
270
263
271
264
except OSError as e :
272
265
if report :
@@ -276,6 +269,21 @@ def normalize_path_maybe_ignore(
276
269
return root_relative_path
277
270
278
271
272
+ def get_root_relative_path (
273
+ path : Path ,
274
+ root : Path ,
275
+ report : Optional [Report ] = None ,
276
+ ) -> Optional [str ]:
277
+ """Returns the file path relative to the 'root' directory"""
278
+ try :
279
+ root_relative_path = path .absolute ().relative_to (root ).as_posix ()
280
+ except ValueError :
281
+ if report :
282
+ report .path_ignored (path , f"is a symbolic link that points outside { root } " )
283
+ return None
284
+ return root_relative_path
285
+
286
+
279
287
def _path_is_ignored (
280
288
root_relative_path : str ,
281
289
root : Path ,
Original file line number Diff line number Diff line change @@ -2592,6 +2592,20 @@ def test_symlinks(self) -> None:
2592
2592
outside_root_symlink .resolve .assert_called_once ()
2593
2593
ignored_symlink .resolve .assert_not_called ()
2594
2594
2595
+ def test_get_sources_with_stdin_symlink_outside_root (
2596
+ self ,
2597
+ ) -> None :
2598
+ path = THIS_DIR / "data" / "include_exclude_tests"
2599
+ stdin_filename = str (path / "b/exclude/a.py" )
2600
+ outside_root_symlink = Path ("/target_directory/a.py" )
2601
+ with patch ("pathlib.Path.resolve" , return_value = outside_root_symlink ):
2602
+ assert_collected_sources (
2603
+ root = Path ("target_directory/" ),
2604
+ src = ["-" ],
2605
+ expected = [],
2606
+ stdin_filename = stdin_filename ,
2607
+ )
2608
+
2595
2609
@patch ("black.find_project_root" , lambda * args : (THIS_DIR .resolve (), None ))
2596
2610
def test_get_sources_with_stdin (self ) -> None :
2597
2611
src = ["-" ]
You can’t perform that action at this time.
0 commit comments