Commit Graph

281 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 2fc2732721 Typo in the name of the YAML formatter test
The yaml test class was misnamed as JsonFormatterTests and not
properly as YamlFormatterTests.

Change-Id: Icbbd666f35ec4fc3251015c3f773b8585c5e8b78
2018-03-13 19:32:26 -07: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
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
shangxiaobj 1a3d28b440 [Trivialfix]Fix typos
Fix the typos in bandit.

Change-Id: I93db489ae27c3f4490e988d342802f3f29f9255f
2017-09-13 00:12:18 -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
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
sudhir_agarwal 2455f95640 Fixed order of arguments in assertEqual
Some tests used incorrect order of arguments in
assertEqual(observed, expected). The correct order expected
by testtool is assertEqual(expected, observed).

Change-Id: I64138c2b08c44a970e7fdd96a634e8a0acd2bfa4
2017-07-14 11:02:00 +05:30
Jenkins a23af1b72d Merge "Blacklist call of ssl._create_unverified_context" 2017-04-09 01:10:24 +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
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
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 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
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
Travis McPeak 77eda34120 Removing 'stats' from JSON output formatter
This commit removes 'stats' from the JSON output formatter.  The
same information is available in the metrics section and
duplicating the data is pointless.

Closes-Bug: #1643723
Change-Id: Ia80a177fdc03c9769c35c824d8d907c93da2ebf7
2016-12-20 11:34:55 -08:00
Travis McPeak e3f19b0dca Fixing partial path detection for Windows
This commit updates the check for a partial path in the shell
plugin to recognize Windows paths (c:\something\) as complete
paths.

Change-Id: I0e6e3b83f5464e2fe4b06bc72632bb950b5e3d7e
Closes-Bug: #1650392
2016-12-20 09:57:33 -08:00
Jenkins a9f47e5d03 Merge "Add functional tests for B308, B321, and B402" 2016-12-20 15:33:05 +00:00
Jenkins b40615b8d2 Merge "Handle curve keyword arg weak_cryptographic_key" 2016-12-20 15:31:27 +00:00
Jenkins 5833ed8508 Merge "Remove checking for special characters in shells" 2016-12-20 15:24:02 +00:00
David Wyde 2486ae68ad Make Bandit's HTML report pass markup validation
Bandit's HTML report previously generated invalid markup, according to
https://validator.w3.org/. Changes:

- Add a character set
- Fix duplicate IDs
- Avoid <div>s inside of <span>s
- Remove excess slashes from <br> tags
- Use double quotes for attributes (stylistic)

Change-Id: Ie811299b05bc5229d4e76511e06db6d8c89110d6
Closes-Bug: #1650391
2016-12-19 16:41:47 -06:00
Travis McPeak 8f74c51935 Remove checking for special characters in shells
This commit removes our logic that checks for special characters
in shell injection tests.  Really, all we care about is whether
format string characters are being used - if so we're probably
taking some kind of user input.  If not, it doesn't matter
whether we're calling something with special characters.

Change-Id: I7e6a8c45a25608e3a8ab8a7eca8d8f2de5dd9837
Closes-Bug: #1650393
2016-12-19 13:17:55 -08:00
Eric Brown ce17a94c06 Add functional tests for B308, B321, and B402
Several checks lack any functional test as described in the bug.
This patch adds mark_safe and ftplib tests.

There was also a typo in the calls doc where mark_safe was listed
under httpsconnection.

Also, the mark_safe check wasn't working because the full import
path for the call was not specified. That was also corrected.

Change-Id: I6f35fb65cb8c25a474175de99fcac04ea2b7d81e
Closes-Bug: #1648257
2016-12-19 09:41:12 -08:00
Eric Brown ff5ce820a2 Handle curve keyword arg weak_cryptographic_key
The weak_cryptographic_key check was missing the handling of a
curve keyword argument.

Change-Id: I716e4cde550866fe4a99011b7dc945c5f8357eae
Closes-Bug: #1650387
2016-12-18 23:03:19 -08:00
zhangyanxian 8330a3b3b9 Fix LOG marker to follow the Python 3 guideline
"logging.warn" which is functionally identical to "logging.warning",
is deprecated in Python 3 as guideline [1].
we'd better use "logging.warning" instead.

[1]https://docs.python.org/3/library/logging.html#logging.warning

Change-Id: I3567e04b3d2c358a3e5b520b8c21598e2f0be70d
2016-12-16 06:36:37 +00:00
Jenkins 1bbb7d5073 Merge "Add capability to pipe a file into bandit" 2016-11-29 23:58:58 +00:00
Jenkins 70172406cf Merge "Replace 'assertFalse(a in b)' with 'assertNotIn(a, b)'" 2016-11-29 16:14:05 +00:00
Eric Brown aae396e9bc Add capability to pipe a file into bandit
Allows someone to feed a file/text into bandit from a pipe rather
than just the 'targets' argument.

Usage example:
   cat examples/imports.py | bandit -

Change-Id: I1566684c0ae5476374960095816cb1720ff465a2
2016-11-28 23:00:45 -08:00
Cao Xuan Hoang 92e2d55309 Replace 'assertFalse(a in b)' with 'assertNotIn(a, b)'
trivialfix

Change-Id: I9147fb649a1eaf02a03dbfc746dd21480508669c
2016-11-28 13:11:52 +07:00
Eric Brown 3be3ca4de0 Trivial fixes based on pylint scan
* Constants should be in caps
* Redundant ( ) in if statements
* Use isinstance instead of type ==
* Indentation

Change-Id: I79fda14112a9dd02fe867f6d850762216e0ca9a1
2016-11-21 13:16:32 -08:00
Cao Xuan Hoang 438bc0d5a3 Replace 'assertTrue(a in b)' with 'assertIn(a, b)'
trivialfix

Change-Id: I5b063d7b1a783158c9c307422724bf4ca125128b
2016-11-16 09:16:54 +07:00
Eric Brown a6abf7fa61 Fix unit tests for newest GitPython
This patch fixes the two problems introduced with the latest version
of GitPython (namely 2.0.9).

The GitCommandNotFound exception now requires two new arguments,
'command' and 'cause'. And the 'command' parameter requires a
non-empty string.

Change-Id: Icf95ac057cc4df3c56da81c7d0a1ec2fd2104bf1
Closes-Bug: #1637143
2016-10-27 02:56:12 -07:00