You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

coding_guild_python_en.md 16 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <!-- TOC -->
  2. - [Note](#note)
  3. - [Scope](#scope)
  4. - [1. Code Style](#1-code-style)
  5. - [1.1 Naming](#11-naming)
  6. - [1.2 Format](#12-format)
  7. - [1.3 Comment](#13-comment)
  8. - [1.4 Log](#14-log)
  9. - [2. General Coding](#2-general-coding)
  10. - [2.1 Interface Declaration](#21-interface-declaration)
  11. - [2.2 Data Verification](#22-data-verification)
  12. - [2.3 Abnormal Behavior](#23-abnormal-behavior)
  13. - [2.4 Serialization and Deserialization](#24-serialization-and-deserialization)
  14. <!-- /TOC -->
  15. ## Note
  16. This document is developed based on [PEP 8](https://www.python.org/dev/peps/pep-0008/), Huawei Python Coding Style Guide, Huawei Python Secure Coding Standard, and industry consensus. To participate in the MindSpore community, please comply with this style guide (for contents conflict with the PEP 8 style guide), and then with PEP 8.
  17. If you disagree with the rules, you are advised to submit an issue and provide reasons. The issue can take effect after being reviewed, accepted, and modified by the MindSpore community operation team.
  18. ## Scope
  19. MindSpore open source community
  20. ------------------------
  21. ### 1. Code Style
  22. #### 1.1 Naming
  23. **Rule 1.1.1 Package names and module names are in lowercase and cannot contain underscores (_).**
  24. **Rule 1.1.2 Class names are in the CamelCase style. The first letter must be capitalized, and the prefix is a private class underscore (_).**
  25. ```python
  26. class _Foo:
  27. _instance = None
  28. pass
  29. ```
  30. **Rule 1.1.3 Function names and variable names are in lowercase and separated by underscores (_) when containing multiple words.**
  31. ```python
  32. def _func_example(path):
  33. pass
  34. ```
  35. **Recommendation 1.1.4 Do not use single-character names except for iterators and counters.**
  36. #### 1.2 Format
  37. **Rule 1.2.1 Ensure that each line contains a maximum of 120 characters.**
  38. If a line contains more than 120 characters, start a new line properly.
  39. **Rule 1.2.2 Use spaces to indent, four at a time. Tab indent is forbidden.**
  40. **Rule 1.2.3 The import sequence is as follows: standard library, third-party, and customization module.**
  41. **Rule 1.2.4 Do not use parentheses in return statements and conditional statements.**
  42. **Rule 1.2.5 There are two blank lines between a module-level function and a class, and one blank line between class member functions. Add blank lines between comments and code as needed. In principle, there should be no more than two blank lines.**
  43. **Rule 1.2.6 Delete invalid or redundant code directly. Do not retain the code in the form of comments or TODO. You are advised to submit an issue record.**
  44. #### 1.3 Comment
  45. **Rule 1.3.1 File header comments must contain copyright statements.**
  46. All Python files must contain the following copyright statements:
  47. ```python
  48. # Copyright 2019 Huawei Technologies Co., Ltd
  49. #
  50. # Licensed under the Apache License, Version 2.0 (the "License");
  51. # you may not use this file except in compliance with the License.
  52. # You may obtain a copy of the License at
  53. #
  54. # http://www.apache.org/licenses/LICENSE-2.0
  55. #
  56. # Unless required by applicable law or agreed to in writing, software
  57. # distributed under the License is distributed on an "AS IS" BASIS,
  58. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  59. # See the License for the specific language governing permissions and
  60. # limitations under the License.
  61. # ============================================================================
  62. """
  63. Add notes.
  64. """
  65. import xxx
  66. ```
  67. > Notes:
  68. > Files created in 2020 should contain `Copyright 2020 Huawei Technologies Co., Ltd`.
  69. > Files created in 2019 and modified in 2020 should contain `Copyright 2019-2020 Huawei Technologies Co., Ltd`.
  70. **Rule 1.3.2 Comply with the comment formats of external classes, methods, operators, and cells:**
  71. - The comment formats of `class` and `def` are the same. Use Python comments which is generally accepted by the industry, and indent the comments under a declaration. All `class` and `def`should be commented. You can write only one introduction for the classes and methods in the module.
  72. - For details about the comment formats, see [MindSpore Comment Specifications](https://gitee.com/mindspore/community/blob/master/security/comments_specification_en.md).
  73. **Rule 1.3.3 Do not use comments to mask Pylint alarms.**
  74. #### 1.4 Log
  75. **Rule 1.4.1 Capitalize the first letter of the exception log text.**
  76. **Rule 1.4.2 Variable names in log texts must be marked with single quotation marks.**
  77. ### 2. General Coding
  78. #### 2.1 Interface Declaration
  79. **Rule 2.1.1 User interfaces are described in __all__ of a file, and __all__ is placed between import and code.**
  80. **Rule 2.1.2 Use underscores (_) to prefixe the non-external methods used by the current file. Method names used across modules do not need underscore prefixes. User interfaces are declared in __all__ of a file.**
  81. #### 2.2 Data Verification
  82. **Rule 2.2.1 Check the validity of all external data, including but not limited to function input parameters, external input named lines, file formats, file sizes, environment variables, and user data.**
  83. **Recommendation 2.2.2 File paths must be canonicalized before use.**
  84. A file path that comes from external data must be canonicalized first. If the file path is not canonicalized, attackers can construct a malicious file path to access the file without authorization.
  85. For example, an attacker can construct ../../../etc/passwd to access any file.
  86. For example, use the realpath() function in Linux and the PathCanonicalize() function in Windows for file path canonicalization.
  87. [Noncompliant Code Example]
  88. The following code obtains the file name from an external system, concatenates the file name into a file path, and directly reads the file content. As a result, the attacker can read the content of any file.
  89. ```python
  90. The following is an example of error code:
  91. ```
  92. [Compliant Code Example]
  93. Canonicalize the file path and then check whether the path is valid in the program.
  94. ```python
  95. The following is an example of correct code:
  96. ```
  97. [Exceptions]
  98. Command line programs that run on the console, or file paths that are manually entered on the console are exempted from this rule.
  99. **Rule 2.2.3 Do not invoke the OS command parser to run commands or programs.**
  100. If untrusted input that is not verified is used as a parameter or part of a system command, vulnerability may occur in a command injection. For the command injection vulnerability, the command is executed at the same privilege level as the Python application. It provides a function similar to shells for attackers. In Python, os.system or os.popen is often used to call a new process. If the command to be executed comes from external input, command and parameter injection may occur.
  101. When running the command, pay attention to the following points:
  102. 1. Do not concatenate the input parameters in the character string for command execution. If the input parameters must be concatenated, perform whitelist filtering on the input parameters.
  103. 2. Verify the type of the input parameters. For example, integer data can be mandatorily converted into integers.
  104. 3. Ensure that the formatted string is correct. For example, use %d instead of %s to concatenate parameters of the int type.
  105. [Noncompliant Code Example 1]
  106. The attacker can find the value of the environment variable APPHOME and place the attacking program against the constant INITCMD in the corresponding directory
  107. to execute the attack.
  108. ```python
  109. home = os.getenv('APPHOME')
  110. cmd = os.path.join(home, INITCMD)
  111. os.system(cmd)
  112. ```
  113. [Noncompliant Code Example 2]
  114. The value of the backuptype attribute is not verified. The value is entered by the user and may be attacked. For example, the value entered by the user is && del.
  115. c:\\dbms\\*.* ":
  116. ```python
  117. # The value is obtained from the user configuration.
  118. btype = req.field('backuptype')
  119. cmd = "cmd.exe /K \"c:\\util\\rmanDB.bat " + btype + "&&c:\\util\\cleanup.bat\""
  120. os.system(cmd)
  121. ```
  122. [Noncompliant Code Example 3]
  123. The value of the backuptype attribute is not verified. The value is entered by the user, which may be attacked. For example, the value entered by the user is && del.
  124. c:\\dbms\\*.* ":
  125. ```python
  126. import os
  127. import sys
  128. try:
  129. print(os.system("ls " + sys.argv[1]))
  130. except Exception as ex:
  131. print('exception:', ex)
  132. ```
  133. Attackers can run the following command to exploit this vulnerability:
  134. ```python
  135. python test.py ". && echo bad"
  136. ```
  137. Actually, the following two commands are executed:
  138. ```python
  139. ls .
  140. echo bad
  141. ```
  142. [Compliant Code Example]
  143. Do not use os.system. You can use standard APIs instead of running system commands to complete the tasks.
  144. ```python
  145. import os
  146. import sys
  147. try:
  148. print(os.listdir(sys.argv[1]))
  149. except Exception as ex:
  150. print(ex)
  151. ```
  152. #### 2.3 Abnormal Behavior
  153. **Rule 2.3.1 Exceptions must be properly handled. Do not suppress or ignore exceptions found in the check results.**
  154. Ensure that the programs in each except block continue to operate only when they are valid. The except block must either recover from an exception or throw another exception suitable for the context of the current catch block to allow the most adjacent outer try-except statement block to recover.
  155. [Compliant Code Example]
  156. The correct method is to avoid using os.system. You can use standard APIs instead of running system commands to complete the tasks.
  157. ```python
  158. validFlag = False
  159. while not validFlag:
  160. try:
  161. # If requested file does not exist, throws FileNotFoundError
  162. # If requested file exists, sets validFlag to true
  163. validFlag = True
  164. except FileNotFoundError:
  165. import traceback
  166. traceback.print_exc()
  167. ```
  168. [Exceptions]
  169. 1. If the resource release failure does not affect the subsequent program behavior, the exception that occurs during resource release can be suppressed. Examples of releasing resources include closing files, network sockets, threads, and so on. These resources are usually released in the except or fianlly block and will not be used during subsequent program operation. Therefore, unless the resources are exhausted, these exceptions cannot affect the subsequent behavior of the program. When the resource exhaustion is resolved, you only need to purify the exceptions and record logs (for future improvement). In this case, there is no need to handle other errors.
  170. 2. If it is impossible to recover from an exception at a specific abstraction level, the code at that level does not need to handle the exception. Instead, the code at that level should throw an appropriate exception so that higher-level code can catch the exception and attempt to recover it. In this case, the most common implementation method is to omit the catch statement block and allow the exception to be broadcast.
  171. **Rule 2.3.2 When using try…except… to protect the code, use finally… to ensure that operation objects are released after an exception occurs.**
  172. When using try…except… to protect the code, if an exception occurs during code execution, use finally… to ensure that operation objects can be released.
  173. [Compliant Code Example]
  174. ```python
  175. handle = open(r"/tmp/sample_data.txt") # May raise IOError
  176. try:
  177. data = handle.read() # May raise UnicodeDecodeError
  178. except UnicodeDecodeError as decode_error:
  179. print(decode_error)
  180. finally:
  181. handle.close() # Always run after try:
  182. ```
  183. **Rule 2.3.3 Do not capture all exceptions by executing the "except:" statement.**
  184. Note that Python is tolerant of exceptions. The "except:" statement can capture any errors, including those in Python syntax. Executing the "except:" statement hides potential bugs. Therefore, specify exceptions to be handled when using try…except… to protect the code. The Exception class is the base class of most runtime exceptions and should not be used in the "except:" statement. The "try" statement should contain only exceptions that must be handled at the current location. The "except:" statement should only capture exceptions that must be handled. For example, for the code for opening files, the "try" statement should contain only the "open" statement. The "except:" statement only captures the FileNotFoundError exceptions. Other unexpected exceptions are captured by functions in the upper layer, or are transparently transmitted to external programs for exposure.
  185. [Noncompliant Code Example]
  186. Two types of exceptions may occur in the following code. When executing the "except:" statement for unified handle, if exceptions occur in the open statement execution, and the "except:" statement handle is invalid, the close method will be called and an error that the reported handle is undefined will be reported.
  187. ```python
  188. try:
  189. handle = open(r"/tmp/sample_data.txt") # May raise IOError
  190. data = handle.read() # May raise UnicodeDecodeError
  191. except:
  192. handle.close()
  193. ```
  194. [Compliant Code Example]
  195. ```python
  196. try:
  197. handle = open(r"/tmp/sample_data.txt") # May raise IOError
  198. try:
  199. data = handle.read() # May raise UnicodeDecodeError
  200. except UnicodeDecodeError as decode_error:
  201. print(decode_error)
  202. finally:
  203. handle.close()
  204. except(FileNotFoundError, IOError) as file_open_except:
  205. print(file_open_except)
  206. ```
  207. **Rule 2.3.4 The raise keyword that is not contained in the "except:" statement must have exceptions specified.**
  208. **Note**: The raise keyword can be used only in the "try-except" statement and re-throw exceptions captured by the "except:" statement.
  209. [Noncompliant Code Example]
  210. ```python
  211. a = 1
  212. if a==1:
  213. raise
  214. ```
  215. [Compliant Code Example 1] Raise an exception or a custom exception.
  216. ```python
  217. a = 1
  218. if a==1:
  219. raise Exception
  220. ```
  221. [Compliant Code Example 2] Use the raise keyword in the "try-except" statement.
  222. ```python
  223. try:
  224. f = open('myfile.txt')
  225. s = f.readline()
  226. i = int(s.strip())
  227. except IOError as e:
  228. print("I/O error({0}): {1}".format(e.errno, e.strerror))
  229. except ValueError:
  230. print("Could not convert data to an integer.")
  231. except Exception:
  232. print("Unexpected error:", sys.exc_info()[0])
  233. raise
  234. ```
  235. #### 2.4 Serialization and Deserialization
  236. **Rule 2.4.1 When pickle has security risks, do not use the pickle.load, cPickle.load, or shelve module to load untrusted data.**
  237. **Rule 2.4.2 Use secure random numbers.**
  238. Python implements the random number generation function in the random module, and implements various distributed pseudo-random number generators. The generated random numbers can be
  239. evenly distributed, in Gaussian distribution, logarithmic normal distribution, negative exponential distribution, alpha distribution, or beta distribution manners. However, these random numbers are pseudo-random numbers, and
  240. cannot be used for applications requiring security encryption.
  241. Use /dev/random to generate secure random numbers, or use the secrets module introduced by Python 3.6 to generate secure random numbers.
  242. [Noncompliant Code Example]
  243. ```python
  244. import random
  245. # Pseudo-random numbers
  246. func = random.SystemRandom()
  247. print(func.random())
  248. print(func.randint(0, 10))
  249. ```
  250. [Compliant Code Example]
  251. ```python
  252. import platform
  253. # For details about the length, see the cryptographic algorithm specifications. The length varies according to the scenario.
  254. randLength = 16
  255. if platform.system() == 'Linux':
  256. with open("/dev/random", 'rb') as file:
  257. sr = file.read(randLength)
  258. print(sr)
  259. ```
  260. **Rule 2.4.3 The assert statement is usually used only in test code. Do not include the assert function in released versions.**
  261. The assert statement is used only for internal tests during R&D. If AssertionError occurs, it indicates that errors exist in software design or the code.
  262. The software should be modified to resolve this issue. Do not include the assert function in externally released versions for production.
  263. ------------------------