Skip to content

[security] Implement tarfile-ignore-zeros-true - #28680

Merged
ntBre merged 4 commits into
astral-sh:mainfrom
saberoueslati:flake8-bandit/tarfile-ignore-zeros
Nov 3, 2026
Merged

ntBre merged 4 commits into
astral-sh:mainfrom
saberoueslati:flake8-bandit/tarfile-ignore-zeros

Conversation

@saberoueslati

@saberoueslati saberoueslati commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds the preview rule tarfile-ignore-zeros-true for statically truthy ignore_zeros in tarfile openers. This setting skips empty and invalid blocks, so an archive may yield members that a parser stopping at those blocks would not see. The rule covers tarfile.open, TarFile(...), and TarFile opening class methods.

Closes #27310

Test Plan

The new mdtest covers flagged and unflagged calls. The targeted mdtest passes.

@astral-sh-bot

astral-sh-bot Bot commented Sep 18, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

βœ… ecosystem check detected no linter changes.

Linter (preview)

βœ… ecosystem check detected no linter changes.

@saberoueslati
saberoueslati force-pushed the flake8-bandit/tarfile-ignore-zeros branch from 12b644a to e2eb335 Compare September 23, 2026 15:01
@saberoueslati
saberoueslati marked this pull request as ready for review September 23, 2026 15:21
@astral-sh-bot
astral-sh-bot Bot requested a review from ntBre September 23, 2026 15:21

@ntBre ntBre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into this. I had one question about the covered APIs and a question about the rule code.

Could you say how you chose the linter and rule code for this rule? I don't see this in the list of upstream codes, so I don't think we should useS203 here. Given our new categories, I think we could just make this a security rule (as you've already done) without assigning a code at all, like this rule:

// Rules that belong only to categories, without linter groups or codes.
() => rules::ruff::rules::PytestFixtureAutouse,

Comment on lines +41 to +45
tarfile.TarFile.taropen("archive.tar", ignore_zeros=True) # error: [tarfile-ignore-zeros-true]
tarfile.TarFile.gzopen("archive.tar.gz", ignore_zeros=True) # error: [tarfile-ignore-zeros-true]
tarfile.TarFile.bz2open("archive.tar.bz2", ignore_zeros=True) # error: [tarfile-ignore-zeros-true]
tarfile.TarFile.xzopen("archive.tar.xz", ignore_zeros=True) # error: [tarfile-ignore-zeros-true]
tarfile.TarFile.zstopen("archive.tar.zst", ignore_zeros=True) # error: [tarfile-ignore-zeros-true]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these documented somewhere? I don't see them in the docs.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They aren't. Those class methods exist in CPython and typeshed, but the tarfile docs don't cover them, so I've removed them from the rule and the tests. The rule now checks tarfile.open, TarFile.open, and the TarFile constructor, which matches the documented API and #27310.

@ntBre ntBre added rule Implementing or modifying a lint rule preview Related to preview-mode features labels Sep 23, 2026
@saberoueslati

Copy link
Copy Markdown
Contributor Author

@ntBre done as asked.

@saberoueslati
saberoueslati requested a review from ntBre September 24, 2026 12:55
@ntBre ntBre changed the title [flake8-bandit] Implement tarfile-ignore-zeros-true (S203) [security] Implement tarfile-ignore-zeros-true Nov 3, 2026

@ntBre ntBre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I took care of the merge conflict and added the rule to the call_with_qualified_name runner added today.

@ntBre
ntBre merged commit 19c01a6 into astral-sh:main Nov 3, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

preview Related to preview-mode features rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rule idea: flag ignore_zeros=True in tarfile usage

2 participants