Skip to content

Commit d738b70

Browse files
committed
Added updates from comments
2 parents 010a78c + aaf3a4d commit d738b70

File tree

4 files changed

+35
-32
lines changed

4 files changed

+35
-32
lines changed

docs/Secure-Coding-Guide-for-Python/CWE-703/CWE-230/README.md

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
# CWE-230: Improper Handling of Missing Values
22

3-
The `NaN` value should be stripped before as they can cause surprising or undefined behaviours in the statistics functions that sort or count occurrences [[2024 doc.python.org]](https://docs.python.org/3/library/statistics.html).
4-
In python, some datasets use `NaN` (not-a-number) to represent the missing data. This can be problematic as the `NaN` values are unordered. Any ordered comparison of a number to a not-a-number value are `False`. A counter-intuitive implication is that `not-a-number` values are not equal to themselves.
3+
In python, some datasets use `NaN` (not-a-number) to represent the missing data. This can be problematic as the `NaN` values are unordered. The `NaN` value should be stripped before as they can cause surprising or undefined behaviours in the statistics functions that sort or count occurrences [[2024 doc.python.org]](https://docs.python.org/3/library/statistics.html) Any ordered comparison of a number to a not-a-number value are `False`. A counter-intuitive implication is that `not-a-number` values are not equal to themselves.
54

65
This behavior is compliant with IEEE 754[[2024 Wikipedia]](https://en.wikipedia.org/wiki/IEEE_754) a hardware induced compromise.
7-
The [example01.py](example01.py) code demonstrates various comparisons of float('NaN') all resulting in False
6+
The [example01.py](example01.py) code demonstrates various comparisons of `float('NaN')` all resulting in `False`.
7+
88
```python
99
# SPDX-FileCopyrightText: OpenSSF project contributors
1010
# SPDX-License-Identifier: MIT
1111
""" Code Example """
12-
12+
1313
foo = float('NaN')
1414
print(f"foo={foo} type = {type(foo)}")
15-
16-
15+
16+
1717
print(foo == float("NaN") or
1818
foo is float("NaN") or
1919
foo < 3 or
@@ -22,20 +22,19 @@ print(foo == float("NaN") or
2222
)
2323

2424
```
25-
## Non-Compliant Code Example
2625

27-
This noncompliant code example [[2024 docs.python.org]](https://docs.python.org/3/reference/expressions.html#value-comparisons) attempts a direct comparison with NaN in
26+
## Non-Compliant Code Example
2827

29-
_value == float("NaN").
28+
This noncompliant code example [[2024 docs.python.org]](https://docs.python.org/3/reference/expressions.html#value-comparisons) attempts a direct comparison with `NaN` in `_value == float("NaN")`.
3029

3130
*[noncompliant01.py](noncompliant01.py):*
3231

3332
```python
3433
# SPDX-FileCopyrightText: OpenSSF project contributors
3534
# SPDX-License-Identifier: MIT
3635
""" Non-compliant Code Example """
37-
38-
36+
37+
3938
def balance_is_positive(value: str) -> bool:
4039
"""Returns True if there is still enough value for a transaction"""
4140
_value = float(value)
@@ -45,8 +44,8 @@ def balance_is_positive(value: str) -> bool:
4544
return False
4645
else:
4746
return True
48-
49-
47+
48+
5049
#####################
5150
# attempting to exploit above code example
5251
#####################
@@ -56,31 +55,32 @@ print(balance_is_positive("NaN"))
5655

5756
```
5857

59-
The balance_is_positive method returns True for all 3 cases instead of throwing an ValureError exception for balance_is_positive("NaN")
58+
The `balance_is_positive` method returns `True` for all 3 cases instead of throwing an `ValureError` exception for `balance_is_positive("NaN")`.
6059

6160
## Compliant Solution
6261

6362
In the `compliant01.py` code example, the method `Decimal.quantize` is used to gain control over known rounding errors in floating point values.
6463

65-
The decision by the `balance_is_positive` method is to `ROUND_DOWN` instead of the default `ROUND_HALF_EVEN`.
64+
The decision by the balance_is_positive method is to `ROUND_DOWN` instead of the default `ROUND_HALF_EVEN`.
6665

6766
*[compliant01.py](compliant01.py):*
6867

6968
```python
7069
# SPDX-FileCopyrightText: OpenSSF project contributors
7170
# SPDX-License-Identifier: MIT
7271
""" Compliant Code Example """
72+
7373
from decimal import ROUND_DOWN, Decimal
74-
75-
74+
75+
7676
def balance_is_positive(value: str) -> bool:
7777
"""Returns True if there is still enough value for a transaction"""
7878
# TODO: additional input sanitation for expected type
7979
_value = Decimal(value)
8080
# TODO: exception handling
8181
return _value.quantize(Decimal(".01"), rounding=ROUND_DOWN) > Decimal("0.00")
82-
83-
82+
83+
8484
#####################
8585
# attempting to exploit above code example
8686
#####################
@@ -90,19 +90,20 @@ print(balance_is_positive("NaN"))
9090

9191
```
9292

93-
Decimal throws a decimal.InvalidOperation for NaN values, the controlled rounding causes only "0.01" to return True.
93+
`Decimal` throws a `decimal.InvalidOperation` for `NaN` values, the controlled rounding causes only `"0.01"` to return `True`.
9494

95-
In `compliant02.py` we use the `math.isnan` to verify if the value passed is a valid `float` value.
95+
In `compliant02.py` we use the math.isnan to very if the value passed is a valid `float` value.
9696

9797
*[compliant02.py](compliant02.py):*
9898

9999
```python
100100
# SPDX-FileCopyrightText: OpenSSF project contributors
101101
# SPDX-License-Identifier: MIT
102102
""" Compliant Code Example """
103+
103104
import math
104-
105-
105+
106+
106107
def balance_is_positive(value: str) -> bool:
107108
"""Returns True if there is still enough value for a transaction"""
108109
_value = float(value)
@@ -112,8 +113,8 @@ def balance_is_positive(value: str) -> bool:
112113
return False
113114
else:
114115
return True
115-
116-
116+
117+
117118
#####################
118119
# attempting to exploit above code example
119120
#####################
@@ -123,7 +124,7 @@ print(balance_is_positive("NaN"))
123124

124125
```
125126

126-
The balance_is_poitive method will raise an ValueError for NaN values.
127+
The `balance_is_poitive` method will raise an `ValueError` for `NaN` values.
127128

128129
## Automated Detection
129130

docs/Secure-Coding-Guide-for-Python/CWE-703/CWE-230/compliant01.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# SPDX-FileCopyrightText: OpenSSF project contributors
22
# SPDX-License-Identifier: MIT
3-
""" Non-compliant Code Example """
3+
""" Compliant Code Example """
44

55
from decimal import ROUND_DOWN, Decimal
66

docs/Secure-Coding-Guide-for-Python/CWE-703/CWE-230/compliant02.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# SPDX-FileCopyrightText: OpenSSF project contributors
22
# SPDX-License-Identifier: MIT
3-
""" Non-compliant Code Example """
3+
""" Compliant Code Example """
4+
45
import math
5-
6+
7+
68
def balance_is_positive(value: str) -> bool:
79
"""Returns True if there is still enough value for a transaction"""
810
_value = float(value)
@@ -12,8 +14,8 @@ def balance_is_positive(value: str) -> bool:
1214
return False
1315
else:
1416
return True
15-
16-
17+
18+
1719
#####################
1820
# attempting to exploit above code example
1921
#####################

docs/Secure-Coding-Guide-for-Python/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ It is __not production code__ and requires code-style or python best practices t
9292

9393
|[CWE-703: Improper Check or Handling of Exceptional Conditions](https://cwe.mitre.org/data/definitions/703.html)|Prominent CVE|
9494
|:----------------------------------------------------------------|:----|
95-
|[CWE-230: Improper Handling of Missing Values](CWE-703/CWE-230/.)||
95+
|[CWE-230: Improper Handling of Missing Values](CWE-703/CWE-230/README.md)||
9696
|[CWE-252: Unchecked Return Value](CWE-703/CWE-252/README.md)||
9797
|[CWE-390: Detection of Error Condition without Action](CWE-703/CWE-390/README.md)||
9898
|[CWE-392: Missing Report of Error Condition](CWE-703/CWE-392/README.md)||

0 commit comments

Comments
 (0)