Skip to content

Commit

Permalink
Raise an error if someone is using an incorrect suffix in a config du…
Browse files Browse the repository at this point in the history
…ration 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.
  • Loading branch information
MadLittleMods authored Jan 30, 2025
1 parent 95a85b1 commit a0b7047
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog.d/18112.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Raise an error if someone is using an incorrect suffix in a config duration string.
19 changes: 17 additions & 2 deletions synapse/config/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,13 @@ def parse_duration(value: Union[str, int]) -> int:
The number of milliseconds in the duration.
Raises:
TypeError, if given something other than an integer or a string
TypeError: if given something other than an integer or a string, or the
duration is using an incorrect suffix.
ValueError: if given a string not of the form described above.
"""
# For integers, we prefer to use `type(value) is int` instead of
# `isinstance(value, int)` because we want to exclude subclasses of int, such as
# bool.
if type(value) is int: # noqa: E721
return value
elif isinstance(value, str):
Expand All @@ -246,9 +250,20 @@ def parse_duration(value: Union[str, int]) -> int:
if suffix in sizes:
value = value[:-1]
size = sizes[suffix]
elif suffix.isdigit():
# No suffix is treated as milliseconds.
value = value
size = 1
else:
raise TypeError(
f"Bad duration suffix {value} (expected no suffix or one of these suffixes: {sizes.keys()})"
)

return int(value) * size
else:
raise TypeError(f"Bad duration {value!r}")
raise TypeError(
f"Bad duration type {value!r} (expected int or string duration)"
)

@staticmethod
def abspath(file_path: str) -> str:
Expand Down

0 comments on commit a0b7047

Please sign in to comment.