eric7/Plugins/CheckerPlugins/CodeStyleChecker/Security/Checks/tryExcept.py

branch
eric7
changeset 8312
800c432b34c8
parent 8259
2bbec88047dd
child 8881
54e42bc2437a
equal deleted inserted replaced
8311:4e8b98454baa 8312:800c432b34c8
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2020 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing checks for insecure except blocks.
8 """
9
10 #
11 # This is a modified version of the one found in the bandit package.
12 #
13 # Original Copyright 2014 Hewlett-Packard Development Company, L.P.
14 #
15 # SPDX-License-Identifier: Apache-2.0
16 #
17
18 import ast
19
20 from Security.SecurityDefaults import SecurityDefaults
21
22
23 def getChecks():
24 """
25 Public method to get a dictionary with checks handled by this module.
26
27 @return dictionary containing checker lists containing checker function and
28 list of codes
29 @rtype dict
30 """
31 return {
32 "ExceptHandler": [
33 (checkTryExceptPass, ("S110",)),
34 (checkTryExceptContinue, ("S112",)),
35 ],
36 }
37
38
39 def checkTryExceptPass(reportError, context, config):
40 """
41 Function to check for a pass in the except block.
42
43 @param reportError function to be used to report errors
44 @type func
45 @param context security context object
46 @type SecurityContext
47 @param config dictionary with configuration data
48 @type dict
49 """
50 checkTypedException = (
51 config["check_typed_exception"]
52 if config and "check_typed_exception" in config else
53 SecurityDefaults["check_typed_exception"]
54 )
55
56 node = context.node
57 if len(node.body) == 1:
58 if (
59 not checkTypedException and
60 node.type is not None and
61 getattr(node.type, 'id', None) != 'Exception'
62 ):
63 return
64
65 if isinstance(node.body[0], ast.Pass):
66 reportError(
67 context.node.lineno - 1,
68 context.node.col_offset,
69 "S110",
70 "L",
71 "H",
72 )
73
74
75 def checkTryExceptContinue(reportError, context, config):
76 """
77 Function to check for a continue in the except block.
78
79 @param reportError function to be used to report errors
80 @type func
81 @param context security context object
82 @type SecurityContext
83 @param config dictionary with configuration data
84 @type dict
85 """
86 checkTypedException = (
87 config["check_typed_exception"]
88 if config and "check_typed_exception" in config else
89 SecurityDefaults["check_typed_exception"]
90 )
91
92 node = context.node
93 if len(node.body) == 1:
94 if (
95 not checkTypedException and
96 node.type is not None and
97 getattr(node.type, 'id', None) != 'Exception'
98 ):
99 return
100
101 if isinstance(node.body[0], ast.Continue):
102 reportError(
103 context.node.lineno - 1,
104 context.node.col_offset,
105 "S112",
106 "L",
107 "H",
108 )

eric ide

mercurial