Skip to content

Silence bandit warnings about random usage in test_proxyprotocol.py#590

Closed
cuu508 wants to merge 1 commit into
aio-libs:masterfrom
cuu508:silence_bandit_random_warnings
Closed

Silence bandit warnings about random usage in test_proxyprotocol.py#590
cuu508 wants to merge 1 commit into
aio-libs:masterfrom
cuu508:silence_bandit_random_warnings

Conversation

@cuu508

@cuu508 cuu508 commented May 9, 2026

Copy link
Copy Markdown
Contributor

What do these changes do?

I added # nosec in test_proxyprotocol.py in the places where random.getrandbits is used.

Bandit warns that standard pseudo-random generators are not suitable for security/cryptographic purposes. The random.getrandbits() usage in the test case is questionable (it makes the test non-deterministic) but, in any case, it is not used for security/cryptographic purpose.

After applying these changes, bandit does not produce any warnings, and tox can proceed to running tests. Before, I could not get tox to run tests because it would exit early due to bandit warnings. The only environment I could run tests was py310 because tox.ini is configured to not run bandit on py310.

As an alternative to adding # nosec, perhaps the test_tcp4_random and test_tcp6_random test cases can be removed altogether?

Are there changes in behavior for the user?

No, the only changes are in tests.

Related issue number

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • tox testenvs have been executed in the following environments:
    • Linux (Ubuntu 24.04)
    • Windows (7, 10): {py36,py37,py38,py39}-{nocov,cov,diffcov}
    • WSL 1.0 (Ubuntu 18.04): {py36,py37,py38,py39}-{nocov,cov,diffcov}, pypy3-{nocov,cov}, qa, docs
    • FreeBSD (12.2, 12.1, 11.4): {py36,pypy3}-{nocov,cov,diffcov}, qa
    • Cygwin: py36-{nocov,cov,diffcov}, qa, docs
  • Documentation reflects the changes
  • Add a news fragment into the NEWS.rst file
    • Add under the "aiosmtpd-next" section, creating one if necessary
      • You may create subsections to group the changes, if you like
    • Use full sentences with correct case and punctuation
    • Refer to relevant Issue if applicable

Bandit warns that standard pseudo-random generators are not suitable
for security/cryptographic purposes. The random.getrandbits()
in the test case is questionable (it makes the test non-deterministic)
but, in any case, it is not used for security/cryptographic purpose.
@loqs

loqs commented May 9, 2026

Copy link
Copy Markdown
Contributor

Did you miss #583?

@cuu508

cuu508 commented May 9, 2026

Copy link
Copy Markdown
Contributor Author

Indeed I did 😬

@cuu508 cuu508 closed this May 9, 2026
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.

2 participants