Skip to content

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

Merged
MichaReiser merged 6 commits into
astral-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 into
astral-sh:mainfrom
anishgirianish:fix-s103-bad-file-permissions

Conversation

@anishgirianish

@anishgirianish anishgirianish commented Apr 5, 2026

Copy link
Copy Markdown
Contributor

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

astral-sh-bot Bot commented Apr 5, 2026

Copy link
Copy Markdown

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

@MichaReiser MichaReiser left a comment

Copy link
Copy Markdown
Member

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

anishgirianish commented Apr 9, 2026

Copy link
Copy Markdown
Contributor Author

@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

anishgirianish commented Apr 9, 2026

Copy link
Copy Markdown
Contributor Author

@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)
nicopauss pushed a commit to Intersec/lib-common that referenced this pull request Jun 4, 2026
##### [\`v0.15.12\`](https://github.com/astral-sh/ruff/blob/HEAD/CHANGELOG.md#01512)

Released on 2026-04-24.

##### Preview features

- Implement `#ruff:file-ignore` file-level suppressions ([#23599](astral-sh/ruff#23599))
- Implement `#ruff:ignore` logical-line suppressions ([#23404](astral-sh/ruff#23404))
- Revert preview changes to displayed diagnostic severity in LSP ([#24789](astral-sh/ruff#24789))
- \[`airflow`] Implement `task-branch-as-short-circuit` (`AIR004`) ([#23579](astral-sh/ruff#23579))
- \[`flake8-bugbear`] Fix `break`/`continue` handling in `loop-iterator-mutation` (`B909`) ([#24440](astral-sh/ruff#24440))
- \[`pylint`] Fix `PLC2701` for type parameter scopes ([#24576](astral-sh/ruff#24576))

##### Rule changes

- \[`pandas-vet`] Suggest `.array` as well in `PD011` ([#24805](astral-sh/ruff#24805))

##### CLI

- Respect default Unix permissions for cache files ([#24794](astral-sh/ruff#24794))

##### Documentation

- \[`pylint`] Fix `PLR0124` description not to claim self-comparison always returns the same value ([#24749](astral-sh/ruff#24749))
- \[`pyupgrade`] Expand docs on reusable `TypeVar`s and scoping (`UP046`) ([#24153](astral-sh/ruff#24153))
- Improve rules table accessibility ([#24711](astral-sh/ruff#24711))

##### Contributors

- [@dylwil3](https://github.com/dylwil3)
- [@AlexWaygood](https://github.com/AlexWaygood)
- [@woodruffw](https://github.com/woodruffw)
- [@avasis-ai](https://github.com/avasis-ai)
- [@Dev-iL](https://github.com/Dev-iL)
- [@denyszhak](https://github.com/denyszhak)
- [@ShipItAndPray](https://github.com/ShipItAndPray)
- [@anishgirianish](https://github.com/anishgirianish)
- [@augustelalande](https://github.com/augustelalande)
- [@amyreese](https://github.com/amyreese)
- [@majiayu000](https://github.com/majiayu000)
##### [\`v0.15.11\`](https://github.com/astral-sh/ruff/blob/HEAD/CHANGELOG.md#01511)

Released on 2026-04-16.

##### Preview features

- \[`ruff`] Ignore `RUF029` when function is decorated with `asynccontextmanager` ([#24642](astral-sh/ruff#24642))
- \[`airflow`] Implement `airflow-xcom-pull-in-template-string` (`AIR201`) ([#23583](astral-sh/ruff#23583))
- \[`flake8-bandit`] Fix `S103` false positives and negatives in mask analysis ([#24424](astral-sh/ruff#24424))

##### Bug fixes

- \[`flake8-async`] Omit overridden methods for `ASYNC109` ([#24648](astral-sh/ruff#24648))

##### Documentation

- \[`flake8-async`] Add override mention to `ASYNC109` docs ([#24666](astral-sh/ruff#24666))
- Update Neovim config examples to use `vim.lsp.config` ([#24577](astral-sh/ruff#24577))

##### Contributors

- [@augustelalande](https://github.com/augustelalande)
- [@anishgirianish](https://github.com/anishgirianish)
- [@benberryallwood](https://github.com/benberryallwood)
- [@charliermarsh](https://github.com/charliermarsh)
- [@Dev-iL](https://github.com/Dev-iL)

Renovate-Branch: renovate/2024.6-ruff-0.15.x
Change-Id: I2c5de44f14ce3133db71161eae18c7b43f7ba09b
Priv-Id: 9c1a7f10043a2db2338a90de9a62a4d7989df14d
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