|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2024 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing checks for the presence of unicode bidirectional control |
|
8 characters in Python source files. |
|
9 """ |
|
10 |
|
11 # |
|
12 # This is a modified version of the one found in the bandit package. |
|
13 # |
|
14 # Original Copyright (c) 2024 |
|
15 # |
|
16 # SPDX-License-Identifier: Apache-2.0 |
|
17 # |
|
18 |
|
19 from tokenize import detect_encoding |
|
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 "File": [ |
|
32 (checkTrojanSource, ("S613",)), |
|
33 ], |
|
34 } |
|
35 |
|
36 |
|
37 BIDI_CHARACTERS = ( |
|
38 "\u202A", |
|
39 "\u202B", |
|
40 "\u202C", |
|
41 "\u202D", |
|
42 "\u202E", |
|
43 "\u2066", |
|
44 "\u2067", |
|
45 "\u2068", |
|
46 "\u2069", |
|
47 "\u200F", |
|
48 ) |
|
49 |
|
50 |
|
51 def checkTrojanSource(reportError, context, _config): |
|
52 """ |
|
53 Function to check for the presence of unicode bidirectional control |
|
54 characters in Python source files. |
|
55 |
|
56 Those characters can be embedded in comments and strings to reorder |
|
57 source code characters in a way that changes its logic. |
|
58 |
|
59 @param reportError function to be used to report errors |
|
60 @type func |
|
61 @param context security context object |
|
62 @type SecurityContext |
|
63 @param _config dictionary with configuration data (unused) |
|
64 @type dict |
|
65 """ |
|
66 with open(context.filename, "rb") as srcFile: |
|
67 encoding, _ = detect_encoding(srcFile.readline) |
|
68 with open(context.filename, encoding=encoding) as srcFile: |
|
69 for lineno, line in enumerate(srcFile.readlines(), start=0): |
|
70 for char in BIDI_CHARACTERS: |
|
71 try: |
|
72 colOffset = line.index(char) |
|
73 except ValueError: |
|
74 continue |
|
75 reportError( |
|
76 lineno, |
|
77 colOffset, |
|
78 "S613", |
|
79 "H", |
|
80 "M", |
|
81 repr(char), |
|
82 ) |