Skip to content

[flake8-bandit] Fix false positive when using non-standard CSafeLoader path (S506).#21830

Merged
MichaReiser merged 1 commit intoastral-sh:mainfrom
prakhar1144:fix-unsafe-yaml-load
Dec 7, 2025
Merged

[flake8-bandit] Fix false positive when using non-standard CSafeLoader path (S506).#21830
MichaReiser merged 1 commit intoastral-sh:mainfrom
prakhar1144:fix-unsafe-yaml-load

Conversation

@prakhar1144
Copy link
Contributor

@prakhar1144 prakhar1144 commented Dec 7, 2025

Summary

Using CSafeLoader from non-standard path i.e yaml.cyaml.CSafeLoader resulted in false reporting of unsafe-yaml-load.

Example:

import yaml

yaml.load("{}", Loader=yaml.cyaml.CSafeLoader)

resulted in:

S506 Probable use of unsafe loader `CSafeLoader` with `yaml.load`. Allows instantiation of arbitrary objects. Consider `yaml.safe_load`.
 --> /Users/foo/ruff-input/main.py:5:24
  |
3 | import yaml
4 |
5 | yaml.load("{}", Loader=yaml.cyaml.CSafeLoader)
  |                        ^^^^^^^^^^^^^^^^^^^^^^

The PR fixes the bug by considering yaml.cyaml.CSafeLoader as a possible way to use CSafeLoader.

Fixes #21673.

Test Plan

Added example scenarios in crates/ruff_linter/resources/test/fixtures/flake8_bandit/S506.py.

…r path (S506).

Using `CSafeLoader` from non-standard path i.e `yaml.cyaml.CSafeLoader`
resulted in false reporting of `unsafe-yaml-load`.

Example:
```py
import yaml

yaml.load("{}", Loader=yaml.cyaml.CSafeLoader)
```

resulted in:
```sh
S506 Probable use of unsafe loader `CSafeLoader` with `yaml.load`.
Allows instantiation of arbitrary objects. Consider `yaml.safe_load`.
 --> /Users/foo/ruff-input/main.py:5:24
  |
3 | import yaml
4 |
5 | yaml.load("{}", Loader=yaml.cyaml.CSafeLoader)
  |                        ^^^^^^^^^^^^^^^^^^^^^^
```

The PR fixes the bug by considering `yaml.cyaml.CSafeLoader` as a
possible way to use `CSafeLoader`.

Fixes astral-sh#21673.

Signed-off-by: Prakhar Pratyush <prakhar1144@gmail.com>
@astral-sh-bot
Copy link

astral-sh-bot bot commented Dec 7, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@prakhar1144 prakhar1144 marked this pull request as ready for review December 7, 2025 10:25
Copy link
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.

Thank you

@MichaReiser MichaReiser merged commit cbff09b into astral-sh:main Dec 7, 2025
37 checks passed
dcreager added a commit that referenced this pull request Dec 7, 2025
* origin/main:
  [ty] Add test case for fixed panic (#21832)
  [ty] Avoid double-analyzing tuple in `Final` subscript (#21828)
  [flake8-bandit] Fix false positive when using non-standard `CSafeLoader` path (S506). (#21830)
  Add minimal-size build profile (#21826)
dcreager added a commit that referenced this pull request Dec 7, 2025
* origin/main:
  [ty] Add test case for fixed panic (#21832)
  [ty] Avoid double-analyzing tuple in `Final` subscript (#21828)
  [flake8-bandit] Fix false positive when using non-standard `CSafeLoader` path (S506). (#21830)
  Add minimal-size build profile (#21826)
  [ty] Allow `tuple[Any, ...]` to assign to `tuple[int, *tuple[int, ...]]` (#21803)
  [ty] Support renaming import aliases (#21792)
  [ty] Add redeclaration LSP tests (#21812)
  [ty] more detailed description of "Size limit on unions of literals" in mdtest (#21804)
  [ty] Complete support for `ParamSpec` (#21445)
  [ty] Update benchmark dependencies (#21815)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[S506] unsafe-yaml-load reports false positive when using non-standard CSafeLoader path

2 participants