Commit Graph

537 Commits

Author SHA1 Message Date
lhinds 2d2170273b Project Migration to PyCQA
This change rehomes the project to PyCQA[1] as reported to the
openstack-dev mailing list [1]

[0] https://github.com/PyCQA/bandit
[1] http://lists.openstack.org/pipermail/openstack-dev/2018-April/129386.html

Change-Id: I6aad329a60799ea24a3d9bc49e35c3c35ed9dc3b
2018-05-04 06:59:50 +02:00
Eric Brown 9d848dd79e Add bandit ID to prefix of more_info link
In a recent commit [1], the names of the plugin doc files changed
to include the bandit ID as a prefix. Unfortunately, the doc_utils
wasn't updated at the time, so it still pointed to the previous
docs, thus resulting in 404 errors when browsing to the link.

This patch modifies doc_utils to properly prefix the bandit ID to
reference the doc for a particular plugin.

[1] https://review.openstack.org/#/c/540170/

Change-Id: Ia4b4c87e880ba39a677a84fc53943bc7a37849ef
Closes-Bug: #1761254
2018-04-04 16:03:56 -07:00
Tin Lam 91a796b805 Fix false positives for pyCrypto
This patch set fixes an issue where modules whose names begin with
string ``Crypto`` are incorrectly flagged for pyCrypto imports.  The
fix will now explicitly calls out pyCrypto module one sub-level to
avoid the false positives.

Change-Id: Iafd3fae2fc7a13a0a93800ee570c4e1354be1391
Closes-Bug: #1749603
Signed-off-by: Tin Lam <tin@irrational.io>
2018-02-18 21:26:48 -06:00
Zuul 6fd7fccf70 Merge "Add pycrypto to blacklist" 2018-02-12 22:45:50 +00:00
Zuul a41f4ecab5 Merge "Fix infinite loop issue" 2018-02-08 16:12:12 +00:00
Tin Lam dc3ff2d917 Add pycrypto to blacklist
This patch set adds pyCrypto to bandit's blacklist, so bandit will
strongly advise against using pyCrypto. As mentioned in the bug,
this may cause false positives if people use pyCrytodome, but will be
tracked and addressed in follow up patch set.

Depends-On: I0b1a90c3a47ad6d3b18597e5315e9f017854a146
Change-Id: I81f695cd31dee393ab4530dbcdb20dd925bbece2
Closes-Bug: #1655973
2018-02-05 01:00:33 -06:00
Eric Brown ab4b04fc6d Add more_info URL to the YAML output
Currently, outputting bandit findings as YAML does not put the
``more_info`` URL in the output as it would if the output format
is HTML or JSON.  This patch set updates the YAML formatter to
include the ``more_info`` URL to be inline with the HTML and JSON
formatters.

Change-Id: Ice134e3bbf67c59feb7a88b299e60838b6ad80d5
Closes-Bug: #1746827
2018-02-01 15:16:39 -08:00
Tin Lam 90f031c973 Fix infinite loop issue
Running bandit using relative paths inside a subdirectory when the current
directory contains __init__.py causes bandit to be stuck in an infinite
loop.

Co-Authored-By: Calvin Li
Closes-Bug: #1743042
Change-Id: I247108c1365847134ee561073ea0eb43c57b54cc
2018-02-01 13:10:16 -06:00
Gage Hugo 2fc7dda850 Update docs links
The links for more_info were pointing to the old location for
bandit's documents, which would redirect to an index.html page.

This change updates the docs to the new location in order to
allow the "more info" link to point to the correct page.

Change-Id: I950ea4601248065dce68a5d21b144703817cf675
Closes-Bug: #1745006
2018-01-25 19:17:49 -06:00
Tin Lam 32c27bdcdf Update documentation
In patch [0], B605 no longer will return a MEDIUM severity, instead
it will return a LOW severity. However, the documentation was never
updated to match the change [1]. This patch set updates the
documentation to match the update.

[0] 8f74c51935 (diff-5abc40c6b98a9c677ed95d1ffec0ad66L41)
[1] https://docs.openstack.org/bandit/latest/plugins/start_process_with_a_shell.html

Closes-Bug: #1742493

Change-Id: I201c259313aef09b3d68cca54380dc47033a5b26
Signed-off-by: Tin Lam <tin@irrational.io>
2018-01-11 12:49:13 -06:00
Zuul 71f4df31b6 Merge "Add more_info URL to the JSON output" 2018-01-09 17:46:30 +00:00
Tin Lam 185a2a8a42 Add more_info URL to the JSON output
Currently, outputting bandit findings as JSON does not put the ``more_info``
URL in the output as it would if the output format is HTML.  This patch
set updates the JSON formatter to include the ``more_info`` URL to be inline
with the HTML display.

Change-Id: I58a8490b427fe146d517a8aff124f4443562f48b
Closes-Bug: #1695890
Signed-off-by: Tin Lam <tin@irrational.io>
2018-01-03 19:32:39 -06:00
Tin Lam bb1bf81856 Add module loaded through importlib
Bandit only checks if imports is done using keyword ``import`` or
``__import__()`` and does not check for blacklisted module loaded
via importlib.  This patch set adds additional check for blacklisted
modules loaded via importlib.

Change-Id: I97ed93af1066fa39dfc5be0868ab814c8eadd147
Closes-Bug: #1718516
Signed-off-by: Tin Lam <tin@irrational.io>
2017-12-28 04:12:35 -06:00
Zuul 03b390b59b Merge "Allow specifying targets in ini file" 2017-12-07 17:41:28 +00:00
Zuul 8f09d8b208 Merge "Custom formatter" 2017-11-24 16:25:22 +00:00
Marek Cermak d159335700 Custom formatter
Implements: custom formatter

Custom formatter can be used to output a machine-readable, easily
parsable and customizable format using set of predefined tags
to suite various needs.

Output string is formatted using python string.format() standards
and therefore provides familiar usage.

Usage: bandit --format custom [--msg-template MSG-TEMPLATE] targets

See bandit --help for additional information and list of available tags

modified:   bandit/cli/main.py
modified:   bandit/core/manager.py
modified:   README.rst
modified:   setup.cfg
new file:   bandit/formatters/custom.py

Change-Id: I900c9689cddb048db58608c443305e05e7a4be14
Signed-off-by: Marek Cermak <macermak@redhat.com>
2017-11-13 13:53:40 +01:00
Pavlo Shchelokovskyy 446e7f7249 Allow specifying targets in ini file
this patch makes 'targets' args optional and allows to specify them
in the ini file.
This makes it possible to keep most of bandit configuration right in
the ini file.
OpenStack projects can now populate their tox.ini with [bandit] section
and do 'bandit --ini {toxinidir}/tox.ini -r' almost uniformly
accross all projects.

Change-Id: Ia0153e0aaa602171690ca8f66635fbea69b1cfab
Closes-Bug: #1730307
2017-11-06 12:10:06 +02:00
Rajath Agasthya a98519927b Plugin to flag insecure hash functions created using hashlib.new()
Currently, insecure hash function usage by calling hashlib.md5()
is flagged in B303. But these hash functions can also be obtained using
hashlib.new(), by passing 'md4' or 'md5' as an argument. This plugin
checks such usage.

Change-Id: I8d368aea287e1287e5f638b48c4297d355037839
Closes-Bug: #1708582
2017-09-28 21:50:27 -07:00
Jenkins 85e5667ddb Merge "Add sha-1 to list of insecure hashes" 2017-09-13 04:13:35 +00:00
sudhir_agarwal 277daaf094 Remove unused None from dict.get()
Since the default value is None when can't get a key from a dict,
So there is no need to use dict.get('key', None).

Change-Id: If22a4a6dbfd010a0b9574b42c23ba19a2c54dd6d
2017-09-04 11:37:22 -05:00
Jenkins 120b24295d Merge "Adds simple handler to provide failed line numbers" 2017-08-31 22:19:40 +00:00
Jenkins 5d79d201af Merge "Incorrect Test ID in docstring" 2017-08-31 22:15:57 +00:00
lhinds f4800cdbac Incorrect Test ID in docstring
B223 was incorrectly referenced with B222

Change-Id: I922fcb69a04e8c3a21ff71aac0d53679b50e928f
Signed-off-by: lhinds <lhinds@redhat.com>
2017-08-25 15:27:47 +01:00
lhinds 05c52da559 Adds simple handler to provide failed line numbers
Change adds `as err` handler to provide line number, if config yaml
fails to parse.

Example output

[config]  ERROR   while scanning a simple key
  in "config.yaml", line 5, column 1
could not find expected ':'
  in "config.yaml", line 6, column 1
[main]  ERROR   config.yaml : Error parsing file.

Change-Id: If764a123c0dd8871dcd98f58be48c6bf0034f1d4
Closes-Bug: #1621552
2017-08-24 19:38:21 +01:00
Rajath Agasthya 8f1b50b5cc Do not flag new way of escaping in jinja2 plugin
Makes escaping using select_autoescape function valid by checking
for ast.Call instance and if func id == select_autoescape.

Example:

from jinja2 import Environment, select_autoescape
env = Environment(autoescape=select_autoescape(['html', 'htm', 'xml']),
                    loader=PackageLoader('mypackage'))

Change-Id: I47c6b346332a6d9f7c4c57dd45ab7636c78996a1
Closes-Bug: #1684249
2017-08-02 15:54:56 -07:00
Jenkins a23af1b72d Merge "Blacklist call of ssl._create_unverified_context" 2017-04-09 01:10:24 +00:00
Jenkins 95b01334f3 Merge "Allow config for high and medium severity key sizes" 2017-04-07 04:31:41 +00:00
M V P Nitesh 693f57faae Replace six.iteritems() with .items()
1.As mentioned in [1], we should avoid using six.iteritems to achieve
iterators. We can use dict.items instead, as it will return iterators
in PY3 as well. And dict.items/keys will more readable.
2.In py2, the performance about list should be negligible, see the
link [2].
[1] https://wiki.openstack.org/wiki/Python3
[2] http://lists.openstack.org/pipermail/openstack-dev/2015-June/066391.html

Change-Id: I5340fa3d71b6fba76e8fcf75f9f30432329023d2
2017-04-04 15:32:55 +05:30
Eric Brown e40af23ff6 Blacklist call of ssl._create_unverified_context
The ssl._create_unverified_context creates a context for use with
such classes as HTTPSConnection which will do no certificate or
hostname verification. This should be flagged.

Change-Id: I326316e20ee11034c0a794f41c1bd8ae75720142
2017-03-20 12:19:36 -07:00
Eric Brown 085c789490 Correct the yaml doc example to be actually yaml
The current doc for the yaml formatter shows an example of CSV
output, not yaml.

Change-Id: I75b01cab5455559738d89b0803eb64261c445967
2017-03-09 14:33:19 -08:00
Jenkins c59ac73afb Merge "Yet Another Formatter (yaml)" 2017-03-03 15:24:33 +00:00
Jenkins 1e83fb1271 Merge "Dump bandit config file lists vertically" 2017-03-02 09:28:45 +00:00
Jenkins 049a6774bd Merge "HTTPSConnection is secure in newer Python" 2017-03-02 09:27:57 +00:00
Jenkins 7b3e43d0c9 Merge "Refactor check_example to be clearer on error" 2017-03-02 09:24:15 +00:00
Jenkins 61e8a86c08 Merge "Repair the more info links for two blacklist calls" 2017-03-02 09:15:06 +00:00
Eric Brown fbd4e83efe Yet Another Formatter (yaml)
This patch adds a yaml formatter to the output options of bandit.

Change-Id: Ibbe0cff062ce2c11138b746f95109f31de10f5b1
2017-02-27 16:23:17 -08:00
Eric Brown be0483a603 Repair the more info links for two blacklist calls
The blacklist calls has some of documentation anchors combined [1].
As a result, the links don't correct point to the proper anchor in
the html. Therefore we need some exception cases for checks that
have doc combined. Namely B304-B305 and B313-B320.

This patch also fixes links where there is an underscore in the
plugin name and replaces it with a dash. Apparently sphinx will
substitute _ for - when building the doc anchors.

[1]: https://docs.openstack.org/developer/bandit/blacklists/blacklist_calls.html#b304-b305-ciphers-and-modes

Change-Id: I4dfa905425f2631fa488a9a066c427d4145f4aac
2017-02-27 16:20:05 -08:00
Eric Brown 6a1a4b120d Docs for B319 listed twice
The blacklist calls doc lists B319 twice. This patch removes the
duplicate.

[1]: https://docs.openstack.org/developer/bandit/blacklists/blacklist_calls.html#b313-b320-xml

Change-Id: I94ca7cb1201f6d74ce8672294d2ba421ea5a608c
2017-02-27 15:48:44 -08:00
Eric Brown 35e35446b0 Add sha-1 to list of insecure hashes
With the news of a first collison implemented [1], bandit should
now start blacklisting the use of sha-1.

The sha-1 hash was added to the existing blacklist check B303 which
currently checks for MD5 and variants.

[1]: https://security.googleblog.com/2017/02/announcing-first-sha1-collision.html

Change-Id: I411d8d4aeb4d740635c60b559ecda72ab951b629
2017-02-25 15:39:37 -08:00
Eric Brown 87c8b70e7b Refactor check_example to be clearer on error
Currently the check_example in test_functional computes sums and
on error tells the developer the difference in sums, which is
confusing and error prone.

It also leads to false positives where sums may be correct, but
the exact number of MEDIUM, HIGH, etc is different. This was the
case for two tests: test_xml and test_secret_config_option.

The general_hardcoded_password test was also broken for py35
because it was assuming function args are ast.Name not ast.arg.
But surprisingly the tests passed because of a syntax error in
the example.

Change-Id: Icd06fb7ca27a8a01d6442f199775d474d436371b
2017-02-23 19:01:46 -08:00
Eric Brown a38056fafa Dump bandit config file lists vertically
Currently when using the bandit-config-generator to dump out a
config file, it looks rather messy because config option values
that are lists are dumped onto one long line.

So rather than dumping on one line, use the vertical yaml list
format by specifying default_flow_style=False.

Change-Id: Ic0dc97f19d067471b507421dcb98ac749874e49c
2017-02-20 14:06:31 -08:00
Eric Brown 4cf3af7d4c Allow config for high and medium severity key sizes
The severity level of various key sizes of RSA, DSA, and EC are
currently hard-coded in the weak_cryptographic_key.py itself. This
patch allows the values to be overriden via the config file mechanism.

Change-Id: I38ad5384e0e6012818bbac10f449840de6fb14ed
2017-02-20 13:25:31 -08:00
Eric Brown c924b2b12f HTTPSConnection is secure in newer Python
In Python 2.7.9 [1] and 3.4.3 [2], the HTTPSConnection class has
been fixed to perform all the necessary certificate and hostname
checks by default.

Therefore, Bandit's warning is only applicable if the module is
using older versions of Python. Even though Bandit could detect
the version of Python used for its scan, it cannot ensure that is
the same version used for running the said scanned module.

This patch modifies the warning message to make this clearer.

[1]: https://docs.python.org/2/library/httplib.html#httplib.HTTPSConnection
[2]: https://docs.python.org/3.4/library/http.client.html#http.client.HTTPSConnection

Change-Id: I8105137d2cbbf0eb000729a18f43c3db443644d7
2017-02-20 11:25:48 -08:00
Anh Tran e17af5cac2 Typo fix: targetting => targeting
Change-Id: Iebfb2186e2824e47f57f53f9480776a9cbf67398
2017-02-07 10:13:43 +07:00
Eric Brown 52c4b9be68 Use https for references to openstack.org
The openstack.org pages now support https and our references to
the site should by default be one signed by the organization.

Change-Id: I83d2df500e2e30047494c201a2ab39820ffd1502
2017-01-30 14:08:03 -08:00
Philip Jones 6ce60806ca Alter SQL injection plugin to consider .format strings
This considers `"{}".format()` style alongside `"%s" % ` string
formatting for possible SQL injection vulnerabilities.

Change-Id: If7b09083bd2cc5e48e5d3fd3e8d5e6142fdb67ed
2017-01-22 13:59:24 +00:00
Jenkins 2516e40d86 Merge "Add Cryptodome to blacklist and weak ciphers/hash" 2017-01-13 13:16:45 +00:00
Eric Brown d4e213445a Add Cryptodome to blacklist and weak ciphers/hash
As stated in the bug, the PyCryptodomex package reintroduces
PyCrypto, but with a different namespace. Therefore Bandit should
also include Cryptodome in its checks.

Change-Id: I6a02f97747420cedfb4523917ea0083ed5792d7a
Closes-Bug: #1655975
2017-01-12 23:53:24 -08:00
Philip Jones 96632b53eb Alter SQL Injection plugin SQL check
The previous version assumed the SQL query would start with `select`,
`insert into`, `update` or `delete from` which rules out queries that
are not so simple, for example queries using `with` such as:

   WITH cte AS (query)
   SELECT something FROM cte;

This version losens the criteria and considers any string with simple
SQL grammar (e.g. `select` followed by `from` anywhere within) as SQL.

Change-Id: I4c95842474e71aed61abc4bc878f3565a907f7c7
2017-01-11 20:56:30 +00:00
Timothy Kelsey 17c737a391 Fixing some UTF8 encoding issues in file names
The names of skipped files were not being encoded properly in
output reports.

Change-Id: I38055512d71b3268b5241d50f1aa01a4b28ed332
Closes-Bug: #1647925
2017-01-06 17:33:11 +00:00