Skip to content

Commit a0b7047

Browse files
Raise an error if someone is using an incorrect suffix in a config duration string (#18112)
Previously, a value like `5q` would be interpreted as 5 milliseconds. We should just raise an error instead of letting someone run with a misconfiguration.
1 parent 95a85b1 commit a0b7047

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

changelog.d/18112.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Raise an error if someone is using an incorrect suffix in a config duration string.

synapse/config/_base.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,13 @@ def parse_duration(value: Union[str, int]) -> int:
221221
The number of milliseconds in the duration.
222222
223223
Raises:
224-
TypeError, if given something other than an integer or a string
224+
TypeError: if given something other than an integer or a string, or the
225+
duration is using an incorrect suffix.
225226
ValueError: if given a string not of the form described above.
226227
"""
228+
# For integers, we prefer to use `type(value) is int` instead of
229+
# `isinstance(value, int)` because we want to exclude subclasses of int, such as
230+
# bool.
227231
if type(value) is int: # noqa: E721
228232
return value
229233
elif isinstance(value, str):
@@ -246,9 +250,20 @@ def parse_duration(value: Union[str, int]) -> int:
246250
if suffix in sizes:
247251
value = value[:-1]
248252
size = sizes[suffix]
253+
elif suffix.isdigit():
254+
# No suffix is treated as milliseconds.
255+
value = value
256+
size = 1
257+
else:
258+
raise TypeError(
259+
f"Bad duration suffix {value} (expected no suffix or one of these suffixes: {sizes.keys()})"
260+
)
261+
249262
return int(value) * size
250263
else:
251-
raise TypeError(f"Bad duration {value!r}")
264+
raise TypeError(
265+
f"Bad duration type {value!r} (expected int or string duration)"
266+
)
252267

253268
@staticmethod
254269
def abspath(file_path: str) -> str:

0 commit comments

Comments
 (0)