Skip to content

[flake8-bandit] Fix S103 false positives and negatives in mask analysis#24424

Merged
MichaReiser merged 6 commits intoastral-sh:mainfrom
anishgirianish:fix-s103-bad-file-permissions
Apr 10, 2026
Merged

[flake8-bandit] Fix S103 false positives and negatives in mask analysis#24424
MichaReiser merged 6 commits intoastral-sh:mainfrom
anishgirianish:fix-s103-bad-file-permissions

Conversation

@anishgirianish
Copy link
Copy Markdown
Contributor

@anishgirianish anishgirianish commented Apr 5, 2026

Summary

Fixes #18863

Rewrites parse_mask as a known-bits abstract domain over u64, so partial bitwise expressions (|,&, ^) are tracked through unknown operands. This fixes:

  • mode | 0o777 — previously unflagged, now reports the statically-known dangerous bits.
  • 0o777777 & 0o700 — previously a false positive (u16 overflow), now correctly silent.
  • 0o777777 & 0o777 — now flagged as permissive, not "invalid mask".

"Invalid mask" now triggers when a bit outside 0o7777 is statically set, keeping 0o1000 (sticky) valid per RUF064 note.

Under preview, the dangerous-bit set matches upstream Bandit (0o33) instead of the current 0o12.

Test Plan

  • New fixture cases for each repro + partial |/& edges; stable snapshot updated, preview diff snapshot added.
  • cargo nextest run -p ruff_linter -- flake8_bandit .
  • Ecosystem checks ran locally

@astral-sh-bot astral-sh-bot Bot requested a review from ntBre April 5, 2026 22:21
@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot Bot commented Apr 5, 2026

ruff-ecosystem results

Linter (stable)

ℹ️ ecosystem check detected linter changes. (+1 -0 violations, +0 -0 fixes in 1 projects; 55 projects unchanged)

apache/airflow (+1 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --no-fix --output-format concise --no-preview --select ALL

+ airflow-e2e-tests/tests/airflow_e2e_tests/conftest.py:59:39: S103 `os.chmod` setting a permissive mask `0o111` on file or directory

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
S103 1 1 0 0 0

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+1 -0 violations, +0 -0 fixes in 1 projects; 55 projects unchanged)

apache/airflow (+1 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --no-fix --output-format concise --preview --select ALL

+ airflow-e2e-tests/tests/airflow_e2e_tests/conftest.py:59:39: S103 `os.chmod` setting a permissive mask `0o111` on file or directory

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
S103 1 1 0 0 0

@MichaReiser MichaReiser added rule Implementing or modifying a lint rule preview Related to preview mode features labels Apr 7, 2026
Copy link
Copy Markdown
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

This is clever. There's one false positve that we should look into

Comment thread crates/ruff_linter/src/rules/flake8_bandit/rules/bad_file_permissions.rs Outdated
Comment thread crates/ruff_linter/src/rules/flake8_bandit/rules/bad_file_permissions.rs Outdated
@anishgirianish
Copy link
Copy Markdown
Contributor Author

anishgirianish commented Apr 9, 2026

@MichaReiser Thank you so much for the review. Addressed all three comments:

  1. Added KnownBits::invalid() helper
  2. Added KnownBits::unknown(), replaced all default() calls
  3. Fixed the large-int false positive with an oversized flag on KnownBits, also resolved the ecosystem FP by guarding the Permissive check with is_fully_known()

Would like to request you for another look whenever you get a chance.

Thank you

Comment thread crates/ruff_linter/src/rules/flake8_bandit/rules/bad_file_permissions.rs Outdated
Comment thread crates/ruff_linter/src/rules/flake8_bandit/rules/bad_file_permissions.rs Outdated
@MichaReiser MichaReiser assigned MichaReiser and unassigned ntBre Apr 9, 2026
@anishgirianish
Copy link
Copy Markdown
Contributor Author

anishgirianish commented Apr 9, 2026

@MichaReiser Thank you so much for the re-review. Addressed both comments, would appreciate another look whenever you get a chance.

Thank you

@MichaReiser
Copy link
Copy Markdown
Member

Perfect, thank you

Comment thread crates/ruff_linter/resources/test/fixtures/flake8_bandit/S103.py Outdated
@MichaReiser MichaReiser merged commit e1f1875 into astral-sh:main Apr 10, 2026
44 checks passed
carljm added a commit that referenced this pull request Apr 10, 2026
* main:
  [ty] Fix bad diagnostic range for incorrect implicit `__init_subclass__` calls (#24541)
  [ty] Add a `SupportedPythonVersion` enum (#24412)
  [ty] Ignore unsupported editor-selected Python versions (#24498)
  [ty] Add snapshots for `__init_subclass__` diagnostics (#24539)
  [ty] Minor fix in tests (#24538)
  [ty] Allow `Final` variable assignments in `__post_init__` (#24529)
  [ty] Expand test suite for assignment errors (#24537)
  [ty] Use `map`, not `__map`, as the name of the mapping parameter in `TypedDict` `__init__` methods (#24535)
  [ty] Rework logic for synthesizing `TypedDict` methods (#24534)
  [flake8-bandit] Fix S103 false positives and negatives in mask analysis (#24424)
  [ty] mdtest.py: update dependencies (#24533)
  Rename patterns and arguments source order iterator method (#24532)
  [ty] Omit invalid keyword arguments from `TypedDict` signature (#24522)
  [ty] support super() in metaclass methods (#24483)
  [ty] Synthesize `__init__` for `TypedDict` (#24476)
carljm added a commit that referenced this pull request Apr 10, 2026
* main:
  Bump typing conformance suite commit to latest upstream (#24553)
  [ty] Reject deleting`Final` attributes (#24508)
  [ty] Respect property deleters in attribute deletion checks (#24500)
  [ty] stop unioning Unknown into types of un-annotated attributes (#24531)
  [ty] Fix bad diagnostic range for incorrect implicit `__init_subclass__` calls (#24541)
  [ty] Add a `SupportedPythonVersion` enum (#24412)
  [ty] Ignore unsupported editor-selected Python versions (#24498)
  [ty] Add snapshots for `__init_subclass__` diagnostics (#24539)
  [ty] Minor fix in tests (#24538)
  [ty] Allow `Final` variable assignments in `__post_init__` (#24529)
  [ty] Expand test suite for assignment errors (#24537)
  [ty] Use `map`, not `__map`, as the name of the mapping parameter in `TypedDict` `__init__` methods (#24535)
  [ty] Rework logic for synthesizing `TypedDict` methods (#24534)
  [flake8-bandit] Fix S103 false positives and negatives in mask analysis (#24424)
  [ty] mdtest.py: update dependencies (#24533)
  Rename patterns and arguments source order iterator method (#24532)
  [ty] Omit invalid keyword arguments from `TypedDict` signature (#24522)
  [ty] support super() in metaclass methods (#24483)
  [ty] Synthesize `__init__` for `TypedDict` (#24476)
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.

bad-file-permissions (S103) has false negatives and false positives

3 participants