|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2020 - 2022 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 from Security.SecurityDefaults import SecurityDefaults |
|
20 |
|
21 |
|
22 def getChecks(): |
|
23 """ |
|
24 Public method to get a dictionary with checks handled by this module. |
|
25 |
|
26 @return dictionary containing checker lists containing checker function and |
|
27 list of codes |
|
28 @rtype dict |
|
29 """ |
|
30 return { |
|
31 "Call": [ |
|
32 (checkHashlibNew, ("S331",)), |
|
33 ], |
|
34 } |
|
35 |
|
36 |
|
37 def checkHashlibNew(reportError, context, config): |
|
38 """ |
|
39 Function to check for use of insecure md4, md5, or sha1 hash functions |
|
40 in hashlib.new(). |
|
41 |
|
42 @param reportError function to be used to report errors |
|
43 @type func |
|
44 @param context security context object |
|
45 @type SecurityContext |
|
46 @param config dictionary with configuration data |
|
47 @type dict |
|
48 """ |
|
49 insecureHashes = ( |
|
50 [h.lower() for h in config["insecure_hashes"]] |
|
51 if config and "insecure_hashes" in config else |
|
52 SecurityDefaults["insecure_hashes"] |
|
53 ) |
|
54 |
|
55 if isinstance(context.callFunctionNameQual, str): |
|
56 qualnameList = context.callFunctionNameQual.split('.') |
|
57 func = qualnameList[-1] |
|
58 if 'hashlib' in qualnameList and func == 'new': |
|
59 args = context.callArgs |
|
60 keywords = context.callKeywords |
|
61 name = args[0] if args else keywords['name'] |
|
62 if ( |
|
63 isinstance(name, str) and |
|
64 name.lower() in insecureHashes |
|
65 ): |
|
66 reportError( |
|
67 context.node.lineno - 1, |
|
68 context.node.col_offset, |
|
69 "S331", |
|
70 "M", |
|
71 "H", |
|
72 name.upper() |
|
73 ) |