SCW Icons
hero bg no divider
Blog

Verstehe den Pfaddurchquerungsfehler im Tarfile-Modul von Python

Laura Verheyde
Published Oct 03, 2022
Last updated on Mar 08, 2026

Recently, a team of security researchers announced their finding of a fifteen year old bug in Python’s tar file extraction functionality. The vulnerability was first disclosed in 2007 and tracked as CVE-2007-4559. A note was added to the official Python documentation, but the bug itself was left unpatched.

This vulnerability could impact thousands of software projects yet many people are unfamiliar with the situation or how to handle it. That’s why, here at Secure Code Warrior, we’re giving you the opportunity to simulate exploiting this vulnerability yourself to see the  impact first-hand and get some hands-on experience in the mechanics of this persistent bug, so you can better protect your application!

Try the simulated Mission now.

The vulnerability: path traversal during tar file extraction

Path or directory traversal happens when unsanitized user input is used to construct a file path, allowing an attacker to gain access to and overwrite files, and even execute arbitrary code. 

The vulnerability exists in Python’s tarfile module. A tar (tape archive) file is a single file, called an archive. It packages together multiple files along with their metadata, and is usually recognized by having the .tar.gz or .tgz extension. Each member in the archive can be represented by a TarInfo object, which contains metadata, such as the file name, modification time, ownership, and more.

The risk arrises from the archives ability to be extracted again.

When being extracted, every member needs a path to be written to. This location is created by joining the base path with the file name: 

Snippet from Python’s Tarfile.py


Once this path is created, it’s passed on to the tarfile.extract or tarfile.extractall functions to perform the extraction: 

Snippet from Python’s Tarfile.py

The issue here is the lack of sanitization of the filename. An attacker could rename files to include path traversal characters, such as dot dot slash (../), which would cause the file to traverse out of the directory it was meant to be in and overwrite arbitrary files. This could eventually lead to remote code execution, which is ripe for exploitation. 

The vulnerability appears throughout other scenarios, if you know how to identify it. In addition to Python’s handling of tar files, the vulnerability exists in the extraction of zip files. You may be familiar with this under another name, such as the zip slip vulnerability, which has manifested itself in languages other than Python!

LINK TO MISSION 

How can you mitigate risk?

Despite the vulnerability being known for years, the Python maintainers consider the extraction functionality to be doing what it’s supposed to do. In this case, some may say “it’s a feature, not a bug.” Unfortunately, developers can’t always avoid extracting tar or zip files from an unknown source. It’s up to them to sanitize the untrusted input to prevent path traversal vulnerabilities as part of secure development practices.

Want to learn more about how to write secure code and mitigate risk with Python?

Try out our Python challenge for free.

If you’re interested in getting more free coding guidelines, check out Secure Code Coach to help you stay on top of secure coding practices.

Ressource ansehen
Ressource ansehen

Vor Kurzem gab ein Team von Sicherheitsforschern bekannt, dass es einen fünfzehn Jahre alten Fehler in Pythons Tardatei-Extraktionsfunktion entdeckt hat. Die Sicherheitslücke wurde erstmals 2007 aufgedeckt und als CVE-2007-4559 registriert. Der offiziellen Python-Dokumentation wurde ein Hinweis hinzugefügt, aber der Fehler selbst blieb ungepatcht.

Interessiert an mehr?

learn more

Secure Code Warrior ist für Ihr Unternehmen da, um Ihnen zu helfen, Code während des gesamten Softwareentwicklungszyklus zu sichern und eine Kultur zu schaffen, in der Cybersicherheit an erster Stelle steht. Ganz gleich, ob Sie AppSec-Manager, Entwickler, CISO oder jemand anderes sind, der sich mit Sicherheit befasst, wir können Ihrem Unternehmen helfen, die mit unsicherem Code verbundenen Risiken zu reduzieren.

Eine Demo buchen
Teilen auf:
linkedin brandsSocialx logo
Autor
Laura Verheyde
Published Oct 03, 2022

Laura Verheyde is a software developer at Secure Code Warrior focused on researching vulnerabilities and creating content for Missions and Coding labs.

Teilen auf:
linkedin brandsSocialx logo

Recently, a team of security researchers announced their finding of a fifteen year old bug in Python’s tar file extraction functionality. The vulnerability was first disclosed in 2007 and tracked as CVE-2007-4559. A note was added to the official Python documentation, but the bug itself was left unpatched.

This vulnerability could impact thousands of software projects yet many people are unfamiliar with the situation or how to handle it. That’s why, here at Secure Code Warrior, we’re giving you the opportunity to simulate exploiting this vulnerability yourself to see the  impact first-hand and get some hands-on experience in the mechanics of this persistent bug, so you can better protect your application!

Try the simulated Mission now.

The vulnerability: path traversal during tar file extraction

Path or directory traversal happens when unsanitized user input is used to construct a file path, allowing an attacker to gain access to and overwrite files, and even execute arbitrary code. 

The vulnerability exists in Python’s tarfile module. A tar (tape archive) file is a single file, called an archive. It packages together multiple files along with their metadata, and is usually recognized by having the .tar.gz or .tgz extension. Each member in the archive can be represented by a TarInfo object, which contains metadata, such as the file name, modification time, ownership, and more.

The risk arrises from the archives ability to be extracted again.

When being extracted, every member needs a path to be written to. This location is created by joining the base path with the file name: 

Snippet from Python’s Tarfile.py


Once this path is created, it’s passed on to the tarfile.extract or tarfile.extractall functions to perform the extraction: 

Snippet from Python’s Tarfile.py

The issue here is the lack of sanitization of the filename. An attacker could rename files to include path traversal characters, such as dot dot slash (../), which would cause the file to traverse out of the directory it was meant to be in and overwrite arbitrary files. This could eventually lead to remote code execution, which is ripe for exploitation. 

The vulnerability appears throughout other scenarios, if you know how to identify it. In addition to Python’s handling of tar files, the vulnerability exists in the extraction of zip files. You may be familiar with this under another name, such as the zip slip vulnerability, which has manifested itself in languages other than Python!

LINK TO MISSION 

How can you mitigate risk?

Despite the vulnerability being known for years, the Python maintainers consider the extraction functionality to be doing what it’s supposed to do. In this case, some may say “it’s a feature, not a bug.” Unfortunately, developers can’t always avoid extracting tar or zip files from an unknown source. It’s up to them to sanitize the untrusted input to prevent path traversal vulnerabilities as part of secure development practices.

Want to learn more about how to write secure code and mitigate risk with Python?

Try out our Python challenge for free.

If you’re interested in getting more free coding guidelines, check out Secure Code Coach to help you stay on top of secure coding practices.

Ressource ansehen
Ressource ansehen

Füllen Sie das unten stehende Formular aus, um den Bericht herunterzuladen

Wir bitten um Ihre Erlaubnis, Ihnen Informationen zu unseren Produkten und/oder verwandten Themen rund um sichere Codierung zuzusenden. Wir behandeln Ihre persönlichen Daten stets mit größter Sorgfalt und verkaufen sie niemals zu Marketingzwecken an andere Unternehmen.

Einreichen
scw success icon
scw error icon
Um das Formular abzusenden, aktivieren Sie bitte „Analytics“ -Cookies. Wenn Sie fertig sind, können Sie sie jederzeit wieder deaktivieren.

Recently, a team of security researchers announced their finding of a fifteen year old bug in Python’s tar file extraction functionality. The vulnerability was first disclosed in 2007 and tracked as CVE-2007-4559. A note was added to the official Python documentation, but the bug itself was left unpatched.

This vulnerability could impact thousands of software projects yet many people are unfamiliar with the situation or how to handle it. That’s why, here at Secure Code Warrior, we’re giving you the opportunity to simulate exploiting this vulnerability yourself to see the  impact first-hand and get some hands-on experience in the mechanics of this persistent bug, so you can better protect your application!

Try the simulated Mission now.

The vulnerability: path traversal during tar file extraction

Path or directory traversal happens when unsanitized user input is used to construct a file path, allowing an attacker to gain access to and overwrite files, and even execute arbitrary code. 

The vulnerability exists in Python’s tarfile module. A tar (tape archive) file is a single file, called an archive. It packages together multiple files along with their metadata, and is usually recognized by having the .tar.gz or .tgz extension. Each member in the archive can be represented by a TarInfo object, which contains metadata, such as the file name, modification time, ownership, and more.

The risk arrises from the archives ability to be extracted again.

When being extracted, every member needs a path to be written to. This location is created by joining the base path with the file name: 

Snippet from Python’s Tarfile.py


Once this path is created, it’s passed on to the tarfile.extract or tarfile.extractall functions to perform the extraction: 

Snippet from Python’s Tarfile.py

The issue here is the lack of sanitization of the filename. An attacker could rename files to include path traversal characters, such as dot dot slash (../), which would cause the file to traverse out of the directory it was meant to be in and overwrite arbitrary files. This could eventually lead to remote code execution, which is ripe for exploitation. 

The vulnerability appears throughout other scenarios, if you know how to identify it. In addition to Python’s handling of tar files, the vulnerability exists in the extraction of zip files. You may be familiar with this under another name, such as the zip slip vulnerability, which has manifested itself in languages other than Python!

LINK TO MISSION 

How can you mitigate risk?

Despite the vulnerability being known for years, the Python maintainers consider the extraction functionality to be doing what it’s supposed to do. In this case, some may say “it’s a feature, not a bug.” Unfortunately, developers can’t always avoid extracting tar or zip files from an unknown source. It’s up to them to sanitize the untrusted input to prevent path traversal vulnerabilities as part of secure development practices.

Want to learn more about how to write secure code and mitigate risk with Python?

Try out our Python challenge for free.

If you’re interested in getting more free coding guidelines, check out Secure Code Coach to help you stay on top of secure coding practices.

Webinar ansehen
Fangen Sie an
learn more

Klicken Sie auf den Link unten und laden Sie das PDF dieser Ressource herunter.

Secure Code Warrior ist für Ihr Unternehmen da, um Ihnen zu helfen, Code während des gesamten Softwareentwicklungszyklus zu sichern und eine Kultur zu schaffen, in der Cybersicherheit an erster Stelle steht. Ganz gleich, ob Sie AppSec-Manager, Entwickler, CISO oder jemand anderes sind, der sich mit Sicherheit befasst, wir können Ihrem Unternehmen helfen, die mit unsicherem Code verbundenen Risiken zu reduzieren.

Bericht ansehenEine Demo buchen
Ressource ansehen
Teilen auf:
linkedin brandsSocialx logo
Interessiert an mehr?

Teilen auf:
linkedin brandsSocialx logo
Autor
Laura Verheyde
Published Oct 03, 2022

Laura Verheyde is a software developer at Secure Code Warrior focused on researching vulnerabilities and creating content for Missions and Coding labs.

Teilen auf:
linkedin brandsSocialx logo

Recently, a team of security researchers announced their finding of a fifteen year old bug in Python’s tar file extraction functionality. The vulnerability was first disclosed in 2007 and tracked as CVE-2007-4559. A note was added to the official Python documentation, but the bug itself was left unpatched.

This vulnerability could impact thousands of software projects yet many people are unfamiliar with the situation or how to handle it. That’s why, here at Secure Code Warrior, we’re giving you the opportunity to simulate exploiting this vulnerability yourself to see the  impact first-hand and get some hands-on experience in the mechanics of this persistent bug, so you can better protect your application!

Try the simulated Mission now.

The vulnerability: path traversal during tar file extraction

Path or directory traversal happens when unsanitized user input is used to construct a file path, allowing an attacker to gain access to and overwrite files, and even execute arbitrary code. 

The vulnerability exists in Python’s tarfile module. A tar (tape archive) file is a single file, called an archive. It packages together multiple files along with their metadata, and is usually recognized by having the .tar.gz or .tgz extension. Each member in the archive can be represented by a TarInfo object, which contains metadata, such as the file name, modification time, ownership, and more.

The risk arrises from the archives ability to be extracted again.

When being extracted, every member needs a path to be written to. This location is created by joining the base path with the file name: 

Snippet from Python’s Tarfile.py


Once this path is created, it’s passed on to the tarfile.extract or tarfile.extractall functions to perform the extraction: 

Snippet from Python’s Tarfile.py

The issue here is the lack of sanitization of the filename. An attacker could rename files to include path traversal characters, such as dot dot slash (../), which would cause the file to traverse out of the directory it was meant to be in and overwrite arbitrary files. This could eventually lead to remote code execution, which is ripe for exploitation. 

The vulnerability appears throughout other scenarios, if you know how to identify it. In addition to Python’s handling of tar files, the vulnerability exists in the extraction of zip files. You may be familiar with this under another name, such as the zip slip vulnerability, which has manifested itself in languages other than Python!

LINK TO MISSION 

How can you mitigate risk?

Despite the vulnerability being known for years, the Python maintainers consider the extraction functionality to be doing what it’s supposed to do. In this case, some may say “it’s a feature, not a bug.” Unfortunately, developers can’t always avoid extracting tar or zip files from an unknown source. It’s up to them to sanitize the untrusted input to prevent path traversal vulnerabilities as part of secure development practices.

Want to learn more about how to write secure code and mitigate risk with Python?

Try out our Python challenge for free.

If you’re interested in getting more free coding guidelines, check out Secure Code Coach to help you stay on top of secure coding practices.

Inhaltsverzeichniss

PDF herunterladen
Ressource ansehen
Interessiert an mehr?

learn more

Secure Code Warrior ist für Ihr Unternehmen da, um Ihnen zu helfen, Code während des gesamten Softwareentwicklungszyklus zu sichern und eine Kultur zu schaffen, in der Cybersicherheit an erster Stelle steht. Ganz gleich, ob Sie AppSec-Manager, Entwickler, CISO oder jemand anderes sind, der sich mit Sicherheit befasst, wir können Ihrem Unternehmen helfen, die mit unsicherem Code verbundenen Risiken zu reduzieren.

Eine Demo buchenHerunterladen
Teilen auf:
linkedin brandsSocialx logo
Ressourcen-Hub

Ressourcen für den Einstieg

Mehr Beiträge
Ressourcen-Hub

Ressourcen für den Einstieg

Mehr Beiträge