Skip to content

Commit 0490e52

Browse files
clatapieMaxJPReyRevathyvenugopal162
authored
docs: properly ignore bandit warnings (#623)
* docs: adding documentation on how to properly ignore bandit warnings * fix: typo * fix: vale issues * Update doc/source/how-to/vulnerabilities.rst Co-authored-by: Maxime Rey <[email protected]> * docs: adding warning section * fix: adding extra space * Update doc/source/how-to/vulnerabilities.rst Co-authored-by: Revathy Venugopal <[email protected]> --------- Co-authored-by: Maxime Rey <[email protected]> Co-authored-by: Revathy Venugopal <[email protected]>
1 parent 9c7d723 commit 0490e52

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

doc/source/how-to/vulnerabilities.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,13 @@ and the risk of command injection is significantly reduced.
204204
# Removing shell=True and using a list
205205
subprocess.run(["echo", user_input]) # User input is not executed as a shell command
206206
207+
.. note::
208+
209+
Bandit warning remains even after deactivating the `shell=True` argument.
210+
If you are sure that the command is safe, you can ignore the Bandit warning. Please
211+
check the `Ignore Bandit warnings`_ section for more information on how to do so.
212+
213+
207214

208215
**try except continue statements**
209216

@@ -285,3 +292,43 @@ provides a secure way to generate random numbers.
285292
286293
secure_random_number = secrets.randbelow(100) # Secure random number generation
287294
secure_random_letter = secrets.choice(["a", "b", "c"]) # Secure choice from a list
295+
296+
297+
Ignore Bandit warnings
298+
----------------------
299+
300+
In-line comment
301+
~~~~~~~~~~~~~~~
302+
303+
When using Bandit, you may encounter warnings that you believe are not relevant to your codebase
304+
or that you have already addressed. In such cases, you can ignore specific Bandit warnings by
305+
adding a comment to the end of the line that triggers the warning. The comment should be in the
306+
format ``# nosec <warning_id>``, where ``<warning_id>`` is the ID of the warning you want to ignore.
307+
308+
When you ignore a Bandit warning, it is essential to provide a clear comment explaining why
309+
the warning is being ignored. This helps maintainers and other developers understand the context
310+
and rationale behind the decision.
311+
312+
For example, to ignore the B404 warning, you would add `# nosec B404` to the end of the line:
313+
314+
.. code:: python
315+
316+
# Subprocess is needed to start the backend. But
317+
# the input is controlled by the library. Excluding bandit check.
318+
import subprocess # nosec B404
319+
320+
321+
.. warning::
322+
323+
Please note that ignoring Bandit warnings should be done with caution, and you should ensure
324+
that the code is safe and does not introduce any security risks. It is recommended to review the
325+
`bandit documentation`_ for more information on each warning and the potential risks involved.
326+
327+
328+
Security considerations file
329+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
330+
331+
In addition to ignoring specific Bandit warnings, it is a good practice to document the ignored
332+
advisories in a dedicated file. You can find an example of such a file in the `PyACP security
333+
considerations`_ documentation page. This way, you can provide to the users a clear overview of
334+
the vulnerabilities that need to be taken into account when using the library.

doc/source/links.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
.. _unzip: https://7-zip.org/
119119
.. _conventional_commit_types: https://github.com/commitizen/conventional-commit-types/blob/master/index.json
120120
.. _Test Driven Development (TDD): https://en.wikipedia.org/wiki/Test-driven_development
121+
.. _bandit documentation: https://bandit.readthedocs.io/en/latest/
121122
.. _blacklists bandit documentation: https://bandit.readthedocs.io/en/latest/blacklists/blacklist_calls.html
122123

123124
.. #Sphinx-related links

doc/styles/config/vocabularies/ANSYS/accept.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Microelectromechanical
5656
monospaced
5757
Muela
5858
[Nn]amespace
59+
nosec
5960
npm
6061
numpy
6162
[Nn]umpydoc

0 commit comments

Comments
 (0)