Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
a4b2381b
Commit
a4b2381b
authored
Dec 23, 2006
by
Lars Gustäbel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Patch #1262036: Prevent TarFiles from being added to themselves under
certain conditions. Will backport to 2.5.
parent
6baa5027
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
23 additions
and
27 deletions
+23
-27
Lib/tarfile.py
Lib/tarfile.py
+6
-27
Lib/test/test_tarfile.py
Lib/test/test_tarfile.py
+14
-0
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Lib/tarfile.py
View file @
a4b2381b
...
@@ -1052,7 +1052,7 @@ class TarFile(object):
...
@@ -1052,7 +1052,7 @@ class TarFile(object):
can be determined, `mode' is overridden by `fileobj's mode.
can be determined, `mode' is overridden by `fileobj's mode.
`fileobj' is not closed, when TarFile is closed.
`fileobj' is not closed, when TarFile is closed.
"""
"""
self
.
name
=
name
self
.
name
=
os
.
path
.
abspath
(
name
)
if
len
(
mode
)
>
1
or
mode
not
in
"raw"
:
if
len
(
mode
)
>
1
or
mode
not
in
"raw"
:
raise
ValueError
(
"mode must be 'r', 'a' or 'w'"
)
raise
ValueError
(
"mode must be 'r', 'a' or 'w'"
)
...
@@ -1064,7 +1064,7 @@ class TarFile(object):
...
@@ -1064,7 +1064,7 @@ class TarFile(object):
self
.
_extfileobj
=
False
self
.
_extfileobj
=
False
else
:
else
:
if
self
.
name
is
None
and
hasattr
(
fileobj
,
"name"
):
if
self
.
name
is
None
and
hasattr
(
fileobj
,
"name"
):
self
.
name
=
fileobj
.
name
self
.
name
=
os
.
path
.
abspath
(
fileobj
.
name
)
if
hasattr
(
fileobj
,
"mode"
):
if
hasattr
(
fileobj
,
"mode"
):
self
.
mode
=
fileobj
.
mode
self
.
mode
=
fileobj
.
mode
self
.
_extfileobj
=
True
self
.
_extfileobj
=
True
...
@@ -1200,24 +1200,12 @@ class TarFile(object):
...
@@ -1200,24 +1200,12 @@ class TarFile(object):
except
(
ImportError
,
AttributeError
):
except
(
ImportError
,
AttributeError
):
raise
CompressionError
(
"gzip module is not available"
)
raise
CompressionError
(
"gzip module is not available"
)
pre
,
ext
=
os
.
path
.
splitext
(
name
)
pre
=
os
.
path
.
basename
(
pre
)
if
ext
==
".tgz"
:
ext
=
".tar"
if
ext
==
".gz"
:
ext
=
""
tarname
=
pre
+
ext
if
fileobj
is
None
:
if
fileobj
is
None
:
fileobj
=
file
(
name
,
mode
+
"b"
)
fileobj
=
file
(
name
,
mode
+
"b"
)
if
mode
!=
"r"
:
name
=
tarname
try
:
try
:
t
=
cls
.
taropen
(
tarname
,
mode
,
t
=
cls
.
taropen
(
name
,
mode
,
gzip
.
GzipFile
(
name
,
mode
,
compresslevel
,
fileobj
)
gzip
.
GzipFile
(
name
,
mode
,
compresslevel
,
fileobj
))
)
except
IOError
:
except
IOError
:
raise
ReadError
(
"not a gzip file"
)
raise
ReadError
(
"not a gzip file"
)
t
.
_extfileobj
=
False
t
.
_extfileobj
=
False
...
@@ -1236,21 +1224,13 @@ class TarFile(object):
...
@@ -1236,21 +1224,13 @@ class TarFile(object):
except
ImportError
:
except
ImportError
:
raise
CompressionError
(
"bz2 module is not available"
)
raise
CompressionError
(
"bz2 module is not available"
)
pre
,
ext
=
os
.
path
.
splitext
(
name
)
pre
=
os
.
path
.
basename
(
pre
)
if
ext
==
".tbz2"
:
ext
=
".tar"
if
ext
==
".bz2"
:
ext
=
""
tarname
=
pre
+
ext
if
fileobj
is
not
None
:
if
fileobj
is
not
None
:
fileobj
=
_BZ2Proxy
(
fileobj
,
mode
)
fileobj
=
_BZ2Proxy
(
fileobj
,
mode
)
else
:
else
:
fileobj
=
bz2
.
BZ2File
(
name
,
mode
,
compresslevel
=
compresslevel
)
fileobj
=
bz2
.
BZ2File
(
name
,
mode
,
compresslevel
=
compresslevel
)
try
:
try
:
t
=
cls
.
taropen
(
tar
name
,
mode
,
fileobj
)
t
=
cls
.
taropen
(
name
,
mode
,
fileobj
)
except
IOError
:
except
IOError
:
raise
ReadError
(
"not a bzip2 file"
)
raise
ReadError
(
"not a bzip2 file"
)
t
.
_extfileobj
=
False
t
.
_extfileobj
=
False
...
@@ -1455,8 +1435,7 @@ class TarFile(object):
...
@@ -1455,8 +1435,7 @@ class TarFile(object):
arcname
=
name
arcname
=
name
# Skip if somebody tries to archive the archive...
# Skip if somebody tries to archive the archive...
if
self
.
name
is
not
None
\
if
self
.
name
is
not
None
and
os
.
path
.
abspath
(
name
)
==
self
.
name
:
and
os
.
path
.
abspath
(
name
)
==
os
.
path
.
abspath
(
self
.
name
):
self
.
_dbg
(
2
,
"tarfile: Skipped %r"
%
name
)
self
.
_dbg
(
2
,
"tarfile: Skipped %r"
%
name
)
return
return
...
...
Lib/test/test_tarfile.py
View file @
a4b2381b
...
@@ -290,6 +290,20 @@ class WriteTest(BaseTest):
...
@@ -290,6 +290,20 @@ class WriteTest(BaseTest):
else
:
else
:
self
.
dst
.
addfile
(
tarinfo
,
f
)
self
.
dst
.
addfile
(
tarinfo
,
f
)
def
test_add_self
(
self
):
dstname
=
os
.
path
.
abspath
(
self
.
dstname
)
self
.
assertEqual
(
self
.
dst
.
name
,
dstname
,
"archive name must be absolute"
)
self
.
dst
.
add
(
dstname
)
self
.
assertEqual
(
self
.
dst
.
getnames
(),
[],
"added the archive to itself"
)
cwd
=
os
.
getcwd
()
os
.
chdir
(
dirname
())
self
.
dst
.
add
(
dstname
)
os
.
chdir
(
cwd
)
self
.
assertEqual
(
self
.
dst
.
getnames
(),
[],
"added the archive to itself"
)
class
Write100Test
(
BaseTest
):
class
Write100Test
(
BaseTest
):
# The name field in a tar header stores strings of at most 100 chars.
# The name field in a tar header stores strings of at most 100 chars.
...
...
Misc/NEWS
View file @
a4b2381b
...
@@ -103,6 +103,9 @@ Core and builtins
...
@@ -103,6 +103,9 @@ Core and builtins
Library
Library
-------
-------
-
Patch
#
1262036
:
Prevent
TarFiles
from
being
added
to
themselves
under
certain
conditions
.
-
Patch
#
1230446
:
tarfile
.
py
:
fix
ExFileObject
so
that
read
()
and
tell
()
-
Patch
#
1230446
:
tarfile
.
py
:
fix
ExFileObject
so
that
read
()
and
tell
()
work
correctly
together
with
readline
().
work
correctly
together
with
readline
().
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment