1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
#!/usr/bin/env python
import os
import tarfile
import tempfile
import zipfile
import mozfile
import mozunit
import pytest
import stubs
@pytest.fixture
def ensure_directory_contents():
"""ensure the directory contents match"""
def inner(directory):
for f in stubs.files:
path = os.path.join(directory, *f)
exists = os.path.exists(path)
if not exists:
print("%s does not exist" % (os.path.join(f)))
assert exists
if exists:
contents = open(path).read().strip()
assert contents == f[-1]
return inner
@pytest.fixture(scope="module")
def tarpath(tmpdir_factory):
"""create a stub tarball for testing"""
tmpdir = tmpdir_factory.mktemp("test_extract")
tempdir = tmpdir.join("stubs").strpath
stubs.create_stub(tempdir)
filename = tmpdir.join("bundle.tar").strpath
archive = tarfile.TarFile(filename, mode="w")
for path in stubs.files:
archive.add(os.path.join(tempdir, *path), arcname=os.path.join(*path))
archive.close()
assert os.path.exists(filename)
return filename
@pytest.fixture(scope="module")
def zippath(tmpdir_factory):
"""create a stub zipfile for testing"""
tmpdir = tmpdir_factory.mktemp("test_extract")
tempdir = tmpdir.join("stubs").strpath
stubs.create_stub(tempdir)
filename = tmpdir.join("bundle.zip").strpath
archive = zipfile.ZipFile(filename, mode="w")
for path in stubs.files:
archive.write(os.path.join(tempdir, *path), arcname=os.path.join(*path))
archive.close()
assert os.path.exists(filename)
return filename
@pytest.fixture(scope="module", params=["tar", "zip"])
def bundlepath(request, tarpath, zippath):
if request.param == "tar":
return tarpath
else:
return zippath
def test_extract(tmpdir, bundlepath, ensure_directory_contents):
"""test extracting a zipfile"""
dest = tmpdir.mkdir("dest").strpath
mozfile.extract(bundlepath, dest)
ensure_directory_contents(dest)
def test_extract_zipfile_missing_file_attributes(tmpdir):
"""if files do not have attributes set the default permissions have to be inherited."""
_zipfile = os.path.join(
os.path.dirname(__file__), "files", "missing_file_attributes.zip"
)
assert os.path.exists(_zipfile)
dest = tmpdir.mkdir("dest").strpath
# Get the default file permissions for the user
fname = os.path.join(dest, "foo")
with open(fname, "w"):
pass
default_stmode = os.stat(fname).st_mode
files = mozfile.extract_zip(_zipfile, dest)
for filename in files:
assert os.stat(os.path.join(dest, filename)).st_mode == default_stmode
def test_extract_non_archive(tarpath, zippath):
"""test the generalized extract function"""
# test extracting some non-archive; this should fail
fd, filename = tempfile.mkstemp()
os.write(fd, b"This is not a zipfile or tarball")
os.close(fd)
exception = None
try:
dest = tempfile.mkdtemp()
mozfile.extract(filename, dest)
except Exception as exc:
exception = exc
finally:
os.remove(filename)
os.rmdir(dest)
assert isinstance(exception, Exception)
def test_extract_ignore(tmpdir, bundlepath):
dest = tmpdir.mkdir("dest").strpath
ignore = ("foo", "**/fleem.txt", "read*.txt")
mozfile.extract(bundlepath, dest, ignore=ignore)
assert sorted(os.listdir(dest)) == ["bar.txt", "foo.txt"]
def test_tarball_escape(tmpdir):
"""Ensures that extracting a tarball can't write outside of the intended
destination directory.
"""
workdir = tmpdir.mkdir("workdir")
os.chdir(workdir)
# Generate a "malicious" bundle.
with open("bad.txt", "w") as fh:
fh.write("pwned!")
def change_name(tarinfo):
tarinfo.name = "../" + tarinfo.name
return tarinfo
with tarfile.open("evil.tar", "w:xz") as tar:
tar.add("bad.txt", filter=change_name)
with pytest.raises(RuntimeError):
mozfile.extract_tarball("evil.tar", workdir)
assert not os.path.exists(tmpdir.join("bad.txt"))
if __name__ == "__main__":
mozunit.main()
|