You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `noncompliant02.py` example tries to use `time.localtime()` to get `x` hours in the future but causes integer overflow as the given Python `int` is too large to convert to `C long`. This is possible because `time` implements C representations of integers with all the security vulnerabilities as if you were using `C`.
67
+
The `noncompliant02.py` example uses `datetime.timedelta()` to get `x` hours in the future or past for time travelers. The `datetime` is interfacing with the operating system through the `libpython` library written in `C`. Overall the Georgian calender ISO 8601 is limited to 1 - 9999 years [Python datetime 2025](https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes).
currtime (datetime): A datetime object with the starting datetime.
85
+
hours (int): Hours going forward or backwards
86
+
87
+
Returns:
88
+
datetime: A datetime object
89
+
"""
90
+
return currtime + timedelta(hours=hours)
91
+
92
+
86
93
#####################
87
-
#exploiting above code example
94
+
#attempting to exploit above code example
88
95
#####################
89
-
print(get_time_in_future(23**74))
96
+
datetime.fromtimestamp(0)
97
+
currtime = datetime.fromtimestamp(1) # 1st Jan 1970
98
+
99
+
# OK values are expected to work
100
+
# NOK values trigger OverflowErrors in libpython written in C
101
+
hours_list = [
102
+
0, # OK
103
+
1, # OK
104
+
70389526, # OK
105
+
70389527, # NOK
106
+
51539700001, # NOK
107
+
24000000001, # NOK
108
+
-1, # OK
109
+
-17259889, # OK
110
+
-17259890, # NOK
111
+
-23999999999, # NOK
112
+
-51539699999, # NOK
113
+
]
114
+
for hours in hours_list:
115
+
try:
116
+
result = get_datetime(currtime, hours)
117
+
print(f"{hours} OK, datetime='{result}'")
118
+
exceptExceptionas exception:
119
+
print(f"{hours}{repr(exception)}")
90
120
```
91
121
122
+
The `noncompliant02.py` code is triggering various `OverflowError` exceptions in the `libpython` library:
123
+
124
+
-`date value out of range`
125
+
-`OverflowError('Python int too large to convert to C int')`
126
+
-`days=1000000000; must have magnitude <= 999999999`
127
+
92
128
## Compliant Solution
93
129
94
-
This `compliant02.py` solution handles `OverflowError`Exception when a too large value is given to `get_time_in_future`.
130
+
This `compliant02.py` solution is preventing `OverflowError`exception in `libpython` by safeguarding the upper and lower limits in the provided `hours`. Upper and lower limit for `currtime` as well as input sanitization and secure logging are missing and must be added when interfacing with a lesser trusted entity.
currtime = datetime.fromtimestamp(1) # 1st Jan 1970
184
+
185
+
# OK values are expected to work
186
+
# NOK values trigger OverflowErrors in libpython written in C
187
+
hours_list = [
188
+
0, # OK
189
+
1, # OK
190
+
70389526, # OK
191
+
70389527, # NOK
192
+
51539700001, # NOK
193
+
24000000001, # NOK
194
+
-1, # OK
195
+
-17259889, # OK
196
+
-17259890, # NOK
197
+
-23999999999, # NOK
198
+
-51539699999, # NOK
199
+
]
200
+
for hours in hours_list:
201
+
try:
202
+
result = get_datetime(currtime, hours)
203
+
print(f"{hours} OK, datetime='{result}'")
204
+
exceptExceptionas exception:
205
+
print(f"{hours}{repr(exception)}")
120
206
```
121
207
208
+
The `compliant02.py` example is protecting the lower level c-lib from an `OverflowError` by setting boundaries for valid values in `hours`. Similar issues occure with any functionality provided through the operating system.
209
+
122
210
## Non-Compliant Code Example
123
211
124
212
The `noncompliant03.py` code example results in a `OverflowError: math range error`. This is due to `math.exp` being a `C` implementation behind the scenes for better performance. So while it returns a `Python float` it does use `C` type of variables internally for the calculation in `mathmodule.c`[[cpython 2024]](https://github.com/python/cpython/blob/main/Modules/mathmodule.c).
|[[Python 2024]](https://docs.python.org/3.9/library/stdtypes.html)|Format String Syntax. Available from: <https://docs.python.org/3.9/library/stdtypes.html>\[Accessed 20 June 2024]|
187
-
|[[cpython 2024]](https://github.com/python/cpython/blob/main/Modules/mathmodule.c)|mathmodule.c. Available from: <https://github.com/python/cpython/blob/main/Modules/mathmodule.c)>\[Accessed 20 June 2024]|
274
+
|[[Python 2024]](https://docs.python.org/3.9/library/stdtypes.html)|Format String Syntax. [online] Available from: <https://docs.python.org/3.9/library/stdtypes.html>\[Accessed 20 June 2024]|
275
+
|[[cpython 2024]](https://github.com/python/cpython/blob/main/Modules/mathmodule.c)|mathmodule.c. [online] Available from: <https://github.com/python/cpython/blob/main/Modules/mathmodule.c)>\[Accessed 20 June 2024]|
276
+
|[Python datetime 2025]|datetime strftime() and strptime() Format Codes [online], Available from: <https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes>[Accessed 27 March 2025]|
0 commit comments