Skip to content

Commit

Permalink
Adding documentation to CWE-617 as part of ossf#531
Browse files Browse the repository at this point in the history
Signed-off-by: edanhub <[email protected]>
  • Loading branch information
s19110 committed Oct 10, 2024
1 parent bf43c60 commit 06365ca
Show file tree
Hide file tree
Showing 5 changed files with 268 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ if Decimal("2").compare(t) == 0:
|[MITRE CWE](http://cwe.mitre.org/)|Base:<br>[CWE-681, Incorrect Conversion between Numeric Types](https://cwe.mitre.org/data/definitions/681.html)|
|[SEI CERT Oracle Coding Standard for Java](https://wiki.sei.cmu.edu/confluence/display/java/SEI+CERT+Oracle+Coding+Standard+for+Java)|[NUM11-J. Do not compare or inspect the string representation of floating-point values](https://wiki.sei.cmu.edu/confluence/display/java/NUM11-J.+Do+not+compare+or+inspect+the+string+representation+of+floating-point+values)|

## Related Guidelines
## Bibliography

|||
|:---|:---|
Expand Down
153 changes: 153 additions & 0 deletions docs/Secure-Coding-Guide-for-Python/CWE-691/CWE-617/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# CWE-617: Reachable Assertion

Assertions are a useful developer tool, but they cannot be relied upon to be present in a production environment. Incorrect function arguments should be handled by an appropriate exception.

Python removes assertions when a script is run with the `-O` and `-OO` options [[Python 3.9 Documentation](https://docs.python.org/3.9/using/cmdline.html?highlight=pythonoptimize#cmdoption-o)].

## Non-Compliant Code Example

The code is checking for invalid arguments by using assertions. In this example, any positive integer between 1-709 inclusive is valid, and any other argument is invalid.

If the script is run normally, the assertions will catch the invalid arguments. If the script is run in optimized mode, assertions are removed from the bytecode and the function will not work as intended. To simplify the exploit code, the specific exception raised by the argument is caught.

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

```py
""" Non-compliant Code Example """
import math


def my_exp(x):
assert x in range(
1, 710
), f"Argument {x} is not valid" # range(1, 709) produces 1-708
return math.exp(x)


#####################
# exploiting above code example
#####################

try:
print(my_exp(1))
except (AssertionError, OverflowError, TypeError, ValueError) as e:
print(e)

try:
print(my_exp(709))
except (AssertionError, OverflowError, TypeError, ValueError) as e:
print(e)

try:
print(my_exp(710))
except (AssertionError, OverflowError, TypeError, ValueError) as e:
print(e)

try:
print(my_exp(0))
except (AssertionError, OverflowError, TypeError, ValueError) as e:
print(e)

try:
print(my_exp("b"))
except (AssertionError, OverflowError, TypeError, ValueError) as e:
print(e)

# output

# $ python3.9 noncompliant01.py
# 2.718281828459045
# 8.218407461554972e+307
# Argument 710 is not valid
# Argument 0 is not valid
# Argument b is not valid
# $ python3.9 -O noncompliant01.py
# 2.718281828459045
# 8.218407461554972e+307
# math range error
# 1.0
# must be real number, not str

```

## Compliant Solution

The `my_exp()` function raises a `ValueError` exception if an invalid argument is supplied. This works if the script is run in an optimized mode or not.

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

```py
""" Compliant Code Example """
import math


def my_exp(x):
if x not in range(1, 710): # range(1, 709) produces 1-708
raise ValueError(f"Argument {x} is not valid")
return math.exp(x)


#####################
# exploiting above code example
#####################

try:
print(my_exp(1))
except (AssertionError, OverflowError, TypeError, ValueError) as e:
print(e)

try:
print(my_exp(709))
except (AssertionError, OverflowError, TypeError, ValueError) as e:
print(e)

try:
print(my_exp(710))
except (AssertionError, OverflowError, TypeError, ValueError) as e:
print(e)

try:
print(my_exp(0))
except (AssertionError, OverflowError, TypeError, ValueError) as e:
print(e)

try:
print(my_exp("b"))
except (AssertionError, OverflowError, TypeError, ValueError) as e:
print(e)

# output

# $ python3.9 compliant01.py
# 2.718281828459045
# 8.218407461554972e+307
# Argument 710 is not valid
# Argument 0 is not valid
# Argument b is not valid
# $ python3.9 -O compliant01.py
# 2.718281828459045
# 8.218407461554972e+307
# Argument 710 is not valid
# Argument 0 is not valid
# Argument b is not valid

```

## Automated Detection

|Tool|Version|Checker|Description|
|:---|:---|:---|:---|
|Bandit|1.6.2|B101:assert_used|Use of assert detected. The enclosed code will be removed when compiling to optimised byte code.|

## Related Guidelines

|||
|:---|:---|
|[MITRE CWE](http://cwe.mitre.org/)|Pillar<br>[CWE-691: Insufficient Control Flow Management (4.13) (mitre.org)](https://cwe.mitre.org/data/definitions/691.html)|
|[MITRE CWE](http://cwe.mitre.org/)|Base:<br>[CWE-617, Reachable Assertion](https://cwe.mitre.org/data/definitions/617.html)|

## Bibliography

|||
|:---|:---|
|[[Python 3.9 Documentation](https://docs.python.org/3.9/using/cmdline.html?highlight=pythonoptimize#cmdoption-o)]|Python Software Foundation. (2024). Command line and environment - cmdoption -o [online].<br>Available from: [https://docs.python.org/3.9/using/cmdline.html?highlight=pythonoptimize#cmdoption-o](https://docs.python.org/3.9/using/cmdline.html?highlight=pythonoptimize#cmdoption-o)<br>[accessed 10 October 2024].|
55 changes: 55 additions & 0 deletions docs/Secure-Coding-Guide-for-Python/CWE-691/CWE-617/compliant01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# SPDX-FileCopyrightText: OpenSSF project contributors
# SPDX-License-Identifier: MIT
import math


def my_exp(x):
if x not in range(1, 710): # range(1, 709) produces 1-708
raise ValueError(f"Argument {x} is not valid")
return math.exp(x)


#####################
# exploiting above code example
#####################

try:
print(my_exp(1))
except (AssertionError, OverflowError, TypeError, ValueError) as e:
print(e)

try:
print(my_exp(709))
except (AssertionError, OverflowError, TypeError, ValueError) as e:
print(e)

try:
print(my_exp(710))
except (AssertionError, OverflowError, TypeError, ValueError) as e:
print(e)

try:
print(my_exp(0))
except (AssertionError, OverflowError, TypeError, ValueError) as e:
print(e)

try:
print(my_exp("b"))
except (AssertionError, OverflowError, TypeError, ValueError) as e:
print(e)

# output

# $ python3.9 compliant01.py
# 2.718281828459045
# 8.218407461554972e+307
# Argument 710 is not valid
# Argument 0 is not valid
# Argument b is not valid
# $ python3.9 -O compliant01.py
# 2.718281828459045
# 8.218407461554972e+307
# Argument 710 is not valid
# Argument 0 is not valid
# Argument b is not valid

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# SPDX-FileCopyrightText: OpenSSF project contributors
# SPDX-License-Identifier: MIT
import math


def my_exp(x):
assert x in range(
1, 710
), f"Argument {x} is not valid" # range(1, 709) produces 1-708
return math.exp(x)


#####################
# exploiting above code example
#####################

try:
print(my_exp(1))
except (AssertionError, OverflowError, TypeError, ValueError) as e:
print(e)

try:
print(my_exp(709))
except (AssertionError, OverflowError, TypeError, ValueError) as e:
print(e)

try:
print(my_exp(710))
except (AssertionError, OverflowError, TypeError, ValueError) as e:
print(e)

try:
print(my_exp(0))
except (AssertionError, OverflowError, TypeError, ValueError) as e:
print(e)

try:
print(my_exp("b"))
except (AssertionError, OverflowError, TypeError, ValueError) as e:
print(e)

# output

# $ python3.9 noncompliant01.py
# 2.718281828459045
# 8.218407461554972e+307
# Argument 710 is not valid
# Argument 0 is not valid
# Argument b is not valid
# $ python3.9 -O noncompliant01.py
# 2.718281828459045
# 8.218407461554972e+307
# math range error
# 1.0
# must be real number, not str
4 changes: 4 additions & 0 deletions docs/Secure-Coding-Guide-for-Python/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ It is **not production code** and requires code-style or python best practices t
|[CWE-1335: Promote readability and compatibility by using mathematical written code with arithmetic operations instead of bit-wise operations](CWE-682/CWE-1335/01/README.md)||
|[CWE-1339: Insufficient Precision or Accuracy of a Real Number](CWE-682/CWE-1339/.) ||

|[CWE-691: Insufficient Control Flow Management](https://cwe.mitre.org/data/definitions/691.html)|Prominent CVE|
|:---------------------------------------------------------------------------------------------------------------|:----|
|[CWE-617: Reachable Assertion](CWE-691/CWE-617/README.md)||

|[CWE-693: Protection Mechanism Failure](https://cwe.mitre.org/data/definitions/693.html)|Prominent CVE|
|:----------------------------------------------------------------|:----|
|[CWE-184: Incomplete List of Disallowed Input](CWE-693/CWE-184/.)||
Expand Down

0 comments on commit 06365ca

Please sign in to comment.