Skip to content

Commit 966dfac

Browse files
committed
Merge pull request #1065 from aanand/fix-dockerignore-path-traversal
Resolve path traversal in .dockerignore patterns
2 parents 8b41679 + 5ebf4b8 commit 966dfac

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

docker/utils/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ def get_paths(root, exclude_patterns, include_patterns, has_exceptions=False):
199199

200200
def match_path(path, pattern):
201201
pattern = pattern.rstrip('/')
202+
if pattern:
203+
pattern = os.path.relpath(pattern)
204+
202205
pattern_components = pattern.split('/')
203206
path_components = path.split('/')[:len(pattern_components)]
204207
return fnmatch('/'.join(path_components), pattern)

tests/unit/utils_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,9 @@ def test_exclude_dockerfile_child(self):
802802
def test_single_filename(self):
803803
assert self.exclude(['a.py']) == self.all_paths - set(['a.py'])
804804

805+
def test_single_filename_leading_dot_slash(self):
806+
assert self.exclude(['./a.py']) == self.all_paths - set(['a.py'])
807+
805808
# As odd as it sounds, a filename pattern with a trailing slash on the
806809
# end *will* result in that file being excluded.
807810
def test_single_filename_trailing_slash(self):
@@ -831,6 +834,11 @@ def test_question_mark(self):
831834
def test_single_subdir_single_filename(self):
832835
assert self.exclude(['foo/a.py']) == self.all_paths - set(['foo/a.py'])
833836

837+
def test_single_subdir_with_path_traversal(self):
838+
assert self.exclude(['foo/whoops/../a.py']) == self.all_paths - set([
839+
'foo/a.py',
840+
])
841+
834842
def test_single_subdir_wildcard_filename(self):
835843
assert self.exclude(['foo/*.py']) == self.all_paths - set([
836844
'foo/a.py', 'foo/b.py',

0 commit comments

Comments
 (0)