Skip to content

Commit 25d1ccf

Browse files
committed
Fixed linter errors for CWE-175
Signed-off-by: edanhub <[email protected]>
1 parent 95a905d commit 25d1ccf

File tree

8 files changed

+74
-64
lines changed

8 files changed

+74
-64
lines changed

docs/Secure-Coding-Guide-for-Python/CWE-707/CWE-175/README.md

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ WORD = "Title"
3030
print(word.upper())
3131
locale.setlocale(locale.LC_ALL, "tr_TR.utf8")
3232
print(word.upper())
33+
3334
```
3435

3536
This code example incorrectly assumes that the uppercasing rules in Turkish will be followed. The expected output is "TİTLE" (with captial dotted-i), but instead the value outputted by the code is "TITLE" (with capital dotless-i). The only way to ensure capitalization is handled correctly is either manually mapping upper-case characters or using an external library, such as [PyICU](https://pypi.org/project/PyICU/).
@@ -47,10 +48,12 @@ In this example, `strftime("%B")` in the English (Ireland) locale returns "March
4748

4849
```python
4950
""" Non-compliant Code Example """
50-
import datetime, locale
51-
51+
import datetime
52+
import locale
53+
5254
dt = datetime.datetime(2022, 3, 9, 12, 55, 35, 000000)
53-
55+
56+
5457
def get_date(date):
5558
# Return year month day tuple e.g. 2022, March, 09
5659
return date.strftime("%Y"), date.strftime("%B"), date.strftime("%d")
@@ -59,21 +62,23 @@ def get_date(date):
5962
# Trying to exploit above code example
6063
#####################
6164

65+
6266
CURRENT_LOCALE = 'en_IE.utf8'
6367
OTHER_LOCALE = 'uk_UA.utf8'
64-
68+
6569
locale.setlocale(locale.LC_ALL, CURRENT_LOCALE)
6670
# Month is 'March'
6771
curryear, currmonth, currdate = get_date(dt)
68-
72+
6973
locale.setlocale(locale.LC_ALL, OTHER_LOCALE)
7074
# Month is 'березень', i.e. berezen’
7175
otheryear, othermonth, otherdate = get_date(dt)
72-
76+
7377
if currmonth == othermonth:
7478
print("Locale-dependent months are equal")
7579
else:
7680
print("Locale-dependent months are not equal")
81+
7782
```
7883

7984
## Compliant Solution (`datetime`)
@@ -85,10 +90,14 @@ When using `setlocale()`, ensure that it is not set in libraries or set more tha
8590

8691
```python
8792
""" Compliant Code Example """
88-
import datetime, locale
89-
93+
import datetime
94+
import locale
95+
9096
dt = datetime.datetime(2022, 3, 9, 12, 55, 35, 000000)
9197

98+
CURRENT_LOCALE = 'en_IE.utf8'
99+
OTHER_LOCALE = 'uk_UA.utf8'
100+
92101
#####################
93102
# Trying to exploit above code example
94103
#####################
@@ -99,11 +108,12 @@ currmonth = dt.month
99108
locale.setlocale(locale.LC_ALL, OTHER_LOCALE)
100109
# Month is 'березень', i.e. berezen’
101110
othermonth = dt.month
102-
111+
103112
if currmonth == othermonth:
104113
print("Locale-independent months are equal")
105114
else:
106115
print("Locale-independent months are not equal")
116+
107117
```
108118

109119
## Compliant Solution (Explicit Locale)
@@ -113,10 +123,11 @@ Set the locale to the locale the program was developed or validated against, to
113123
*[example02.py](example02.py):*
114124

115125
```python
116-
""" Compliant Code Example """
126+
""" Code Example """
117127
import locale
118128
CURRENT_LOCALE = 'en_IE.utf8'
119129
locale.setlocale(locale.LC_ALL, CURRENT_LOCALE)
130+
120131
```
121132

122133
For example, reading values from a data file values might be misinterpreted if the developer is unaware that the program locale does not accommodate the data locale.
@@ -132,31 +143,35 @@ When using `setlocale()`, ensure that it is not set in libraries or set more tha
132143
*[example03.py](example03.py):*
133144

134145
```python
135-
""" Non-compliant Code Example """
146+
""" Code Example """
136147
import locale
137148
ORIGINAL_NUMBER = 12.345 # This will read as 12,345 in German
138-
149+
150+
139151
def compare_number(number):
140152
input_number = locale.atof(input("Enter a number: "))
141153
# Test if inputted number equals current number
142154
return number == input_number
143155

156+
144157
print(f"Locale is {locale.getlocale()}")
145158
print(f"Do the numbers match? {compare_number(ORIGINAL_NUMBER)}")
146159

147-
## Locale is ('English_Ireland', '1252')
148-
## Enter a number: 12,345
149-
## Do the numbers match? False
160+
# Console output:
161+
# Locale is ('English_Ireland', '1252')
162+
# Enter a number: 12,345
163+
# Do the numbers match? False
150164

151165
# After setting the locale
152166

153167
locale.setlocale(locale.LC_ALL, 'de_DE.utf8')
154168
print(f"Locale is {locale.getlocale()}")
155169
print(f"Do the numbers match? {compare_number(ORIGINAL_NUMBER)}")
156170

157-
## Locale is ('de_DE', 'UTF-8')
158-
## Enter a number: 12,345
159-
## Do the numbers match? True
171+
# Console output:
172+
# Locale is ('de_DE', 'UTF-8')
173+
# Enter a number: 12,345
174+
# Do the numbers match? True
160175

161176
```
162177

@@ -169,12 +184,12 @@ The developer should be aware of the text encoding that is used for input data a
169184
```python
170185
""" Non-compliant Code Example """
171186
import io
172-
187+
173188
LOREM = """Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
174189
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
175190
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."""
176-
177-
output = io.BytesIO()
191+
192+
output = io.BytesIO()
178193
wrapper = io.TextIOWrapper(output, encoding='utf-8', line_buffering=True)
179194
wrapper.write(LOREM)
180195
wrapper.seek(0, 0)
@@ -183,6 +198,7 @@ print(f"{len(output.getvalue().decode('utf-16le'))} characters in string")
183198
# exploiting above code example
184199
#####################
185200
# UnicodeDecodeError: 'utf-16-le' codec can't decode byte 0x2e in position 1336: truncated data
201+
186202
```
187203

188204
## Compliant Solution (Encoding)
@@ -194,12 +210,12 @@ The correct text encoding, UTF-8 for the LOREM `TextIOWrapper` stream has been i
194210
```python
195211
""" Compliant Code Example """
196212
import io
197-
213+
198214
LOREM = """Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
199215
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
200216
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."""
201-
202-
output = io.BytesIO()
217+
218+
output = io.BytesIO()
203219
wrapper = io.TextIOWrapper(output, encoding='utf-8', line_buffering=True)
204220
wrapper.write(LOREM)
205221
wrapper.seek(0, 0)
@@ -208,6 +224,7 @@ print(f"{len(output.getvalue().decode('utf-8'))} characters in string")
208224
# exploiting above code example
209225
#####################
210226
# 1337 characters in string
227+
211228
```
212229

213230
## Automated Detection

docs/Secure-Coding-Guide-for-Python/CWE-707/CWE-175/compliant01.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
""" Compliant Code Example """
44
import datetime
55
import locale
6-
6+
77
dt = datetime.datetime(2022, 3, 9, 12, 55, 35, 000000)
88

9+
CURRENT_LOCALE = 'en_IE.utf8'
10+
OTHER_LOCALE = 'uk_UA.utf8'
11+
912
#####################
1013
# Trying to exploit above code example
1114
#####################
@@ -16,8 +19,8 @@
1619
locale.setlocale(locale.LC_ALL, OTHER_LOCALE)
1720
# Month is 'березень', i.e. berezen’
1821
othermonth = dt.month
19-
22+
2023
if currmonth == othermonth:
2124
print("Locale-independent months are equal")
2225
else:
23-
print("Locale-independent months are not equal")
26+
print("Locale-independent months are not equal")

docs/Secure-Coding-Guide-for-Python/CWE-707/CWE-175/compliant02.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
# SPDX-License-Identifier: MIT
33
""" Compliant Code Example """
44
import io
5-
5+
66
LOREM = """Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
77
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
88
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."""
9-
9+
1010
output = io.BytesIO()
1111
wrapper = io.TextIOWrapper(output, encoding='utf-8', line_buffering=True)
1212
wrapper.write(LOREM)
@@ -15,4 +15,4 @@
1515
#####################
1616
# exploiting above code example
1717
#####################
18-
# 1337 characters in string
18+
# 1337 characters in string

docs/Secure-Coding-Guide-for-Python/CWE-707/CWE-175/example01.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# SPDX-License-Identifier: MIT
33
""" Code Example """
44
import locale
5-
word = "Title"
5+
WORD = "Title"
66
print(word.upper())
77
locale.setlocale(locale.LC_ALL, "tr_TR.utf8")
8-
print(word.upper())
8+
print(word.upper())
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# SPDX-FileCopyrightText: OpenSSF project contributors
22
# SPDX-License-Identifier: MIT
3-
""" Compliant Code Example """
3+
""" Code Example """
44
import locale
55
CURRENT_LOCALE = 'en_IE.utf8'
6-
locale.setlocale(locale.LC_ALL, CURRENT_LOCALE)
6+
locale.setlocale(locale.LC_ALL, CURRENT_LOCALE)
Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,31 @@
11
# SPDX-FileCopyrightText: OpenSSF project contributors
22
# SPDX-License-Identifier: MIT
3-
""" Non-compliant Code Example """
4-
import locale
5-
6-
def compare_number(number):
7-
input_number = locale.atof(input(f"Enter a number {number}: "))
8-
print(f"Locale is {locale.getlocale()}, you entered {input_number}.")
9-
print(f"Does the number {number} match {input_number}? {number == input_number}")
10-
11-
locale.setlocale(locale.LC_ALL, 'de_DE.utf8')
12-
compare_number(12.345)
13-
14-
# SPDX-License-Identifier: MIT
15-
""" Non-compliant Code Example """
3+
""" Code Example """
164
import locale
175
ORIGINAL_NUMBER = 12.345 # This will read as 12,345 in German
186

19-
7+
208
def compare_number(number):
219
input_number = locale.atof(input("Enter a number: "))
2210
# Test if inputted number equals current number
2311
return number == input_number
24-
12+
2513

2614
print(f"Locale is {locale.getlocale()}")
2715
print(f"Do the numbers match? {compare_number(ORIGINAL_NUMBER)}")
2816

29-
## Locale is ('English_Ireland', '1252')
30-
## Enter a number: 12,345
31-
## Do the numbers match? False
17+
# Console output:
18+
# Locale is ('English_Ireland', '1252')
19+
# Enter a number: 12,345
20+
# Do the numbers match? False
3221

3322
# After setting the locale
3423

3524
locale.setlocale(locale.LC_ALL, 'de_DE.utf8')
3625
print(f"Locale is {locale.getlocale()}")
3726
print(f"Do the numbers match? {compare_number(ORIGINAL_NUMBER)}")
3827

39-
## Locale is ('de_DE', 'UTF-8')
40-
## Enter a number: 12,345
41-
## Do the numbers match? True
28+
# Console output:
29+
# Locale is ('de_DE', 'UTF-8')
30+
# Enter a number: 12,345
31+
# Do the numbers match? True

docs/Secure-Coding-Guide-for-Python/CWE-707/CWE-175/noncompliant01.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
""" Non-compliant Code Example """
44
import datetime
55
import locale
6-
6+
77
dt = datetime.datetime(2022, 3, 9, 12, 55, 35, 000000)
88

9-
9+
1010
def get_date(date):
1111
# Return year month day tuple e.g. 2022, March, 09
1212
return date.strftime("%Y"), date.strftime("%B"), date.strftime("%d")
@@ -18,16 +18,16 @@ def get_date(date):
1818

1919
CURRENT_LOCALE = 'en_IE.utf8'
2020
OTHER_LOCALE = 'uk_UA.utf8'
21-
21+
2222
locale.setlocale(locale.LC_ALL, CURRENT_LOCALE)
2323
# Month is 'March'
2424
curryear, currmonth, currdate = get_date(dt)
25-
25+
2626
locale.setlocale(locale.LC_ALL, OTHER_LOCALE)
2727
# Month is 'березень', i.e. berezen’
2828
otheryear, othermonth, otherdate = get_date(dt)
29-
29+
3030
if currmonth == othermonth:
3131
print("Locale-dependent months are equal")
3232
else:
33-
print("Locale-dependent months are not equal")
33+
print("Locale-dependent months are not equal")

docs/Secure-Coding-Guide-for-Python/CWE-707/CWE-175/noncompliant02.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
# SPDX-License-Identifier: MIT
33
""" Non-compliant Code Example """
44
import io
5-
5+
66
LOREM = """Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
77
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
88
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."""
9-
9+
1010
output = io.BytesIO()
1111
wrapper = io.TextIOWrapper(output, encoding='utf-8', line_buffering=True)
1212
wrapper.write(LOREM)
@@ -15,4 +15,4 @@
1515
#####################
1616
# exploiting above code example
1717
#####################
18-
# UnicodeDecodeError: 'utf-16-le' codec can't decode byte 0x2e in position 1336: truncated data
18+
# UnicodeDecodeError: 'utf-16-le' codec can't decode byte 0x2e in position 1336: truncated data

0 commit comments

Comments
 (0)