|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2020 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a check for use of insecure md4, md5, or sha1 hash |
|
8 functions in hashlib.new(). |
|
9 """ |
|
10 |
|
11 # |
|
12 # This is a modified version of the one found in the bandit package. |
|
13 # |
|
14 # Original Copyright 2014 Hewlett-Packard Development Company, L.P. |
|
15 # |
|
16 # SPDX-License-Identifier: Apache-2.0 |
|
17 # |
|
18 |
|
19 |
|
20 def getChecks(): |
|
21 """ |
|
22 Public method to get a dictionary with checks handled by this module. |
|
23 |
|
24 @return dictionary containing checker lists containing checker function and |
|
25 list of codes |
|
26 @rtype dict |
|
27 """ |
|
28 return { |
|
29 "Call": [ |
|
30 (checkHashlibNew, ("S324",)), |
|
31 ], |
|
32 } |
|
33 |
|
34 |
|
35 def checkHashlibNew(reportError, context, config): |
|
36 """ |
|
37 Function to check for use of insecure md4, md5, or sha1 hash functions |
|
38 in hashlib.new(). |
|
39 |
|
40 @param reportError function to be used to report errors |
|
41 @type func |
|
42 @param context security context object |
|
43 @type SecurityContext |
|
44 @param config dictionary with configuration data |
|
45 @type dict |
|
46 """ |
|
47 if config and "insecure_hashes" in config: |
|
48 insecureHashes = [h.lower() for h in config["insecure_hashes"]] |
|
49 else: |
|
50 insecureHashes = ['md4', 'md5', 'sha', 'sha1'] |
|
51 |
|
52 if isinstance(context.callFunctionNameQual, str): |
|
53 qualnameList = context.callFunctionNameQual.split('.') |
|
54 func = qualnameList[-1] |
|
55 if 'hashlib' in qualnameList and func == 'new': |
|
56 args = context.callArgs |
|
57 keywords = context.callKeywords |
|
58 name = args[0] if args else keywords['name'] |
|
59 if ( |
|
60 isinstance(name, str) and |
|
61 name.lower() in insecureHashes |
|
62 ): |
|
63 reportError( |
|
64 context.node.lineno - 1, |
|
65 context.node.col_offset, |
|
66 "S324", |
|
67 "M", |
|
68 "H", |
|
69 name.upper() |
|
70 ) |