Skip to content

Commit 6de7bab

Browse files
committed
Rewrite access check in create_archive with EAFP
Signed-off-by: Joffrey F <[email protected]>
1 parent 539b321 commit 6de7bab

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

docker/utils/utils.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,6 @@ def create_archive(root, files=None, fileobj=None, gzip=False):
9797
for path in files:
9898
full_path = os.path.join(root, path)
9999

100-
if os.lstat(full_path).st_mode & os.R_OK == 0:
101-
raise IOError(
102-
'Can not access file in context: {}'.format(full_path)
103-
)
104100
i = t.gettarinfo(full_path, arcname=path)
105101
if i is None:
106102
# This happens when we encounter a socket file. We can safely
@@ -121,7 +117,9 @@ def create_archive(root, files=None, fileobj=None, gzip=False):
121117
with open(full_path, 'rb') as f:
122118
t.addfile(i, f)
123119
except IOError:
124-
t.addfile(i, None)
120+
raise IOError(
121+
'Can not read file in context: {}'.format(full_path)
122+
)
125123
else:
126124
# Directories, FIFOs, symlinks... don't need to be read.
127125
t.addfile(i, None)

tests/unit/utils_test.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,10 @@ def test_tar_with_empty_directory(self):
933933
tar_data = tarfile.open(fileobj=archive)
934934
assert sorted(tar_data.getnames()) == ['bar', 'foo']
935935

936-
@pytest.mark.skipif(IS_WINDOWS_PLATFORM, reason='No chmod on Windows')
936+
@pytest.mark.skipif(
937+
IS_WINDOWS_PLATFORM or os.geteuid() == 0,
938+
reason='root user always has access ; no chmod on Windows'
939+
)
937940
def test_tar_with_inaccessible_file(self):
938941
base = tempfile.mkdtemp()
939942
full_path = os.path.join(base, 'foo')
@@ -944,8 +947,9 @@ def test_tar_with_inaccessible_file(self):
944947
with pytest.raises(IOError) as ei:
945948
tar(base)
946949

947-
assert 'Can not access file in context: {}'.format(full_path) in \
950+
assert 'Can not read file in context: {}'.format(full_path) in (
948951
ei.exconly()
952+
)
949953

950954
@pytest.mark.skipif(IS_WINDOWS_PLATFORM, reason='No symlinks on Windows')
951955
def test_tar_with_file_symlinks(self):

0 commit comments

Comments
 (0)