You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/Secure-Coding-Guide-for-Python/CWE-664/CWE-409/README.md
+13-5Lines changed: 13 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -100,7 +100,7 @@ Prefabricated zip bombs and zip slip archives for testing can be found on: [[por
100
100
101
101
## Non-Compliant Code Example - No File Validation
102
102
103
-
The `noncompliant01.py`example simply extracts all the files in the archive without performing any verification. The `extractall()` method will attempt to normalize the path name. Any archive from an untrusted source should be inspected prior to extraction. There is no attempt to control where the files are extracted, which is the script current working directory.
103
+
The `extractall()` method in `noncompliant01.py`will attempt to normalize the path name while making no attempt to control where the files are extracted to. The script uses the current working directory as a starting point and allows to escape the default path. Any archive from an untrusted source must be inspected prior to extraction and extracted forced below a specific path in order to prevent traversal attacks.
104
104
105
105
_[noncompliant01.py](noncompliant01.py):_
106
106
@@ -117,13 +117,13 @@ with zipfile.ZipFile("zip_attack_test.zip", mode="r") as archive:
117
117
118
118
The `noncompliant01.py` code will extract any quantity of payloads. With a unmodified `example01.py` we get only `4 x 150MB``zipbombfileX.txt`'s that are much bigger than the `0.58MB``zip_attack_test.zip` archive.
119
119
120
-
The directory traversal payload will try to extract a `\Temp\zip_slip_windows.txt` for Windows and a `/tmp/zip_slip_posix.txt` for Unix based systems. Depending on the zip library in use the files may either end up in their indented target, under the same directory as the `zipbombfile.txt` files, or not at all.
120
+
The directory traversal payload will try to extract a `\Temp\zip_slip_windows.txt` for Windows and a `/tmp/zip_slip_posix.txt` for Unix based systems. Depending on the zip library in use the files may either end up in their intended target, under the same directory as the `zipbombfile.txt` files, or not at all.
121
121
122
122
## Non-Compliant Code Example - Incorrect File Validation
123
123
124
124
Experiment with the code by varying the `MAXSIZE`.
125
125
126
-
The `noncompliant02.py` code example tries to check the file_size from the `ZipInfo` instances provided by the `infolist()` method from `ZipFile`. This information is read from the `zip` archive metadata, so it is not reliable and can be forged by an attacker. The `extract()` method will attempt to normalize the path name. Again, there is no attempt to control where the files are extracted to in order to prevent traversal attacks. The underlaying zip library may or may not prevent traversal attacks.
126
+
The `noncompliant02.py` code example tries to check the `file_size` from the `ZipInfo` instances provided by the `infolist()` method from `ZipFile`. This information is read from the `zip` archive metadata, so it is not reliable and can be forged by an attacker. The `extract()` method will attempt to normalize the path name. Again, there is no attempt to control where the files are extracted to in order to prevent traversal attacks. The underlying zip library may or may not prevent traversal attacks.
127
127
128
128
_[noncompliant02.py](noncompliant02.py):_
129
129
@@ -146,7 +146,7 @@ with zipfile.ZipFile("zip_attack_test.zip", mode="r") as archive:
146
146
147
147
```
148
148
149
-
Depending on the underlaying zip library we should see `noncompliant02.py` prevent a zip bomb but not a traversal attack.
149
+
Depending on the underlying zip library we should see `noncompliant02.py` prevent a zip bomb but not a traversal attack.
<td>Base: <a href="https://cwe.mitre.org/data/definitions/209.html">CWE-209: Generation of Error Message Containing Sensitive Information</a></td>
321
+
</tr>
318
322
<tr>
319
323
<td>Secure Coding in Python</td>
320
324
<td>Base: <a href="../../CWE-707/CWE-180/README.md">CWE-180: Incorrect behavior order: Validate before Canonicalize</a></td>
321
325
</tr>
326
+
<tr>
327
+
<td>Secure Coding in Python</td>
328
+
<td>Base: <a href="../../CWE-664/CWE-209/README.md">CWE-209: Generation of Error Message Containing Sensitive Information</a></td>
329
+
</tr>
322
330
<tr>
323
331
<td><a href="https://wiki.sei.cmu.edu/confluence/display/java/SEI+CERT+Oracle+Coding+Standard+for+Java">[SEI CERT Oracle Coding Standard for Java]</a></td>
324
332
<td><a href="https://wiki.sei.cmu.edu/confluence/display/java/IDS04-J.+Safely+extract+files+from+ZipInputStream">IDS04-J, Safely extract files from ZipInputStream</a></td>
<td>Base: <a href="https://cwe.mitre.org/data/definitions/1335.html">[CWE-1335: Incorrect Bitwise Shift of Integer (4.12)]</a></td>
99
+
<td>Base or Class (choose which one it is based on the abstraction on the CWE page): <a href="https://cwe.mitre.org/data/definitions/1335.html">[CWE-1335: Incorrect Bitwise Shift of Integer (4.12)]</a></td>
100
100
</tr>
101
101
<tr>
102
102
<td><a href="https://wiki.sei.cmu.edu/confluence/display/java/SEI+CERT+Oracle+Coding+Standard+for+Java">[SEI CERT Oracle Coding Standard for Java]</a></td>
@@ -120,3 +120,4 @@ Short explanation of expected outcome of running the code example, e.g. "The cod
120
120
<td>CERT C Coding Standard [online]. Available from: <a href=https://www.securecoding.cert.org/confluence/display/seccode/CERT+C+Coding+Standard>https://www.securecoding.cert.org/confluence/display/seccode/CERT+C+Coding+Standard</a> [Accessed 6 May 2025]</td>
121
121
</tr>
122
122
</table>
123
+
When writing bibliography, follow the [Harvard reference guide](https://dkit.ie.libguides.com/harvard/citing-referencing)
0 commit comments