-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[flake8-bandit] Implement tarfile-unsafe-members (S202)
- Loading branch information
Showing
8 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
crates/ruff_linter/resources/test/fixtures/flake8_bandit/S202.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import sys | ||
import tarfile | ||
import tempfile | ||
|
||
|
||
def unsafe_archive_handler(filename): | ||
tar = tarfile.open(filename) | ||
tar.extractall(path=tempfile.mkdtemp()) | ||
tar.close() | ||
|
||
|
||
def managed_members_archive_handler(filename): | ||
tar = tarfile.open(filename) | ||
tar.extractall(path=tempfile.mkdtemp(), members=members_filter(tar)) | ||
tar.close() | ||
|
||
|
||
def list_members_archive_handler(filename): | ||
tar = tarfile.open(filename) | ||
tar.extractall(path=tempfile.mkdtemp(), members=[]) | ||
tar.close() | ||
|
||
|
||
def provided_members_archive_handler(filename): | ||
tar = tarfile.open(filename) | ||
tarfile.extractall(path=tempfile.mkdtemp(), members=tar) | ||
tar.close() | ||
|
||
|
||
def members_filter(tarfile): | ||
result = [] | ||
for member in tarfile.getmembers(): | ||
if '../' in member.name: | ||
print('Member name container directory traversal sequence') | ||
continue | ||
elif (member.issym() or member.islnk()) and ('../' in member.linkname): | ||
print('Symlink to external resource') | ||
continue | ||
result.append(member) | ||
return result | ||
|
||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) > 1: | ||
filename = sys.argv[1] | ||
unsafe_archive_handler(filename) | ||
managed_members_archive_handler(filename) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
crates/ruff_linter/src/rules/flake8_bandit/rules/tarfile_unsafe_members.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use crate::checkers::ast::Checker; | ||
use ruff_diagnostics::Diagnostic; | ||
use ruff_diagnostics::Violation; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::{self as ast}; | ||
use ruff_text_size::Ranged; | ||
|
||
/// ## What it does | ||
/// Checks for uses of ``tarfile.extractall()`. | ||
/// | ||
/// ## Why is this bad? | ||
/// Use ``tarfile.extractall(members=function_name)`` and define a function | ||
/// that will inspect each member. Discard files that contain a directory | ||
/// traversal sequences such as ``../`` or ``\..`` along with all special filetypes | ||
/// unless you explicitly need them. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// import tarfile | ||
/// import tempfile | ||
/// | ||
/// tar = tarfile.open(filename) | ||
/// tar.extractall(path=tempfile.mkdtemp()) | ||
/// tar.close() | ||
/// ``` | ||
/// | ||
/// ## References | ||
/// - [Common Weakness Enumeration: CWE-22](https://cwe.mitre.org/data/definitions/22.html) | ||
/// - [Python Documentation: tarfile](https://docs.python.org/3/library/tarfile.html#tarfile.TarFile.extractall) | ||
#[violation] | ||
pub struct TarfileUnsafeMembers; | ||
|
||
impl Violation for TarfileUnsafeMembers { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Uses of `tarfile.extractall()`") | ||
} | ||
} | ||
|
||
/// S202 | ||
pub(crate) fn tarfile_unsafe_members(checker: &mut Checker, call: &ast::ExprCall) { | ||
if checker | ||
.semantic() | ||
.resolve_call_path(&call.func) | ||
.is_some_and(|call_path| matches!(call_path.as_slice(), ["tarfile", "extractall"])) | ||
|| call | ||
.func | ||
.as_attribute_expr() | ||
.is_some_and(|attr| attr.attr.as_str() == "extractall") | ||
{ | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(TarfileUnsafeMembers, call.func.range())); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...rules/flake8_bandit/snapshots/ruff_linter__rules__flake8_bandit__tests__S202_S202.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs | ||
--- | ||
S202.py:8:5: S202 Uses of `tarfile.extractall()` | ||
| | ||
6 | def unsafe_archive_handler(filename): | ||
7 | tar = tarfile.open(filename) | ||
8 | tar.extractall(path=tempfile.mkdtemp()) | ||
| ^^^^^^^^^^^^^^ S202 | ||
9 | tar.close() | ||
| | ||
|
||
S202.py:14:5: S202 Uses of `tarfile.extractall()` | ||
| | ||
12 | def managed_members_archive_handler(filename): | ||
13 | tar = tarfile.open(filename) | ||
14 | tar.extractall(path=tempfile.mkdtemp(), members=members_filter(tar)) | ||
| ^^^^^^^^^^^^^^ S202 | ||
15 | tar.close() | ||
| | ||
|
||
S202.py:20:5: S202 Uses of `tarfile.extractall()` | ||
| | ||
18 | def list_members_archive_handler(filename): | ||
19 | tar = tarfile.open(filename) | ||
20 | tar.extractall(path=tempfile.mkdtemp(), members=[]) | ||
| ^^^^^^^^^^^^^^ S202 | ||
21 | tar.close() | ||
| | ||
|
||
S202.py:26:5: S202 Uses of `tarfile.extractall()` | ||
| | ||
24 | def provided_members_archive_handler(filename): | ||
25 | tar = tarfile.open(filename) | ||
26 | tarfile.extractall(path=tempfile.mkdtemp(), members=tar) | ||
| ^^^^^^^^^^^^^^^^^^ S202 | ||
27 | tar.close() | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.