Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flake8-bandit/S506] Dont report violation when SafeLoader is imported from yaml.loader #9299

Merged
merged 3 commits into from
Dec 28, 2023

Conversation

mikaelarguedas
Copy link
Contributor

Summary

Hey there 👋 thanks for this great project!

On python code looking like the following

import yaml
from yaml.loader import SafeLoader

with MY_FILE_PATH.open("r") as my_file:
    my_data = yaml.load(my_file, Loader=SafeLoader)

ruff reports this error:

S506 Probable use of unsafe loader `SafeLoader` with `yaml.load`. Allows instantiation of arbitrary objects. Consider `yaml.safe_load`.

This PR is an attempt to support SafeLoader being imported for either yaml or yaml.loader

Disclaimer:
I am not familiar with Rust so this is likely not the better way of doing it. Interested in hearing how to adapt this PR to provide similar behavior in a better way

Test Plan

The S506.py file was updated accordingly to cover the use cases and test were confirmed to pass with this change.

Avoid false positives like: S506 Probable use of unsafe loader `SafeLoader` with `yaml.load`. Allows instantiation of arbitrary objects. Consider `yaml.safe_load`.

Signed-off-by: Mikael Arguedas <mikael.arguedas@gmail.com>
…l.loader

Signed-off-by: Mikael Arguedas <mikael.arguedas@gmail.com>
Copy link
Contributor

github-actions bot commented Dec 28, 2023

ruff-ecosystem results

Linter (stable)

ℹ️ ecosystem check encountered linter errors. (no lint changes; 1 project error)

pypa/setuptools (error)

ruff failed
  Cause: 'quote-style = preserve' is a preview only feature. Run with '--preview' to enable it.

Linter (preview)

✅ ecosystem check detected no linter changes.

Copy link
Member

@charliermarsh charliermarsh left a comment

Choose a reason for hiding this comment

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

Thanks, this is great! I just collapsed the matches! calls into a single pattern-match rather than two separate macros.

@charliermarsh charliermarsh added the bug Something isn't working label Dec 28, 2023
@charliermarsh charliermarsh enabled auto-merge (squash) December 28, 2023 14:25
@charliermarsh charliermarsh merged commit edfad46 into astral-sh:main Dec 28, 2023
16 checks passed
@mikaelarguedas mikaelarguedas deleted the fix_S506_safe_loader branch December 28, 2023 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants