Skip to content

document that random.choices() isn't secure either#728

Merged
ericwb merged 2 commits into
PyCQA:masterfrom
taybin:random_choices
Aug 24, 2021
Merged

document that random.choices() isn't secure either#728
ericwb merged 2 commits into
PyCQA:masterfrom
taybin:random_choices

Conversation

@taybin
Copy link
Copy Markdown
Contributor

@taybin taybin commented Aug 20, 2021

Because random.choices() wasn't explicitly listed in the "don't use this" list, I initially thought it was safer version of random.choice().

I realized my mistake and used secret.choice() eventually, but this PR is to add random.choices() to the unsafe list.

@sigmavirus24
Copy link
Copy Markdown
Member

I believe this also needs to be added to a test/example file to ensure no regression

@taybin
Copy link
Copy Markdown
Contributor Author

taybin commented Aug 22, 2021

@sigmavirus24 Something like this?

Comment thread examples/random_module.py Outdated
bad = random.randint()
bad = random.choice()
if sys.version_info.major >= 3 and sys.version_info.minor >= 6:
bad = random.choices()
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 code is never actually ran, so there's little reason to have this guard I think?

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.

That makes a lot of sense. Fixed.

Copy link
Copy Markdown
Member

@ericwb ericwb left a comment

Choose a reason for hiding this comment

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

Just need a quick fix to the functional tests.

Comment thread examples/random_module.py
bad = random.randrange()
bad = random.randint()
bad = random.choice()
bad = random.choices()
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.

You've introduced a new functional test by including this line. As a result the tests will fail, because Bandit will flag this line as a vulnerability. You'll want to update test_functional.test_random_module()

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.

I see. Okay, thank you.

@ericwb ericwb enabled auto-merge (squash) August 24, 2021 00:33
Copy link
Copy Markdown
Member

@ericwb ericwb left a comment

Choose a reason for hiding this comment

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

LGTM

@ericwb ericwb merged commit d4faa78 into PyCQA:master Aug 24, 2021
@taybin taybin deleted the random_choices branch August 31, 2021 13:38
mikespallino pushed a commit to mikespallino/bandit that referenced this pull request Jan 7, 2022
* document that random.choices() isn't secure either

* add random.choices() to tests
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.

3 participants