Skip to content

Commit

Permalink
Avoid DoS on carefully crafted spec files (fix #61)
Browse files Browse the repository at this point in the history
  • Loading branch information
kraptor authored and bkircher committed Sep 24, 2023
1 parent 0c548d5 commit e3e8c53
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions pyrpm/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ def from_string(cls, string: str) -> "Spec":
return spec


def replace_macros(string: str, spec: Spec) -> str:
def replace_macros(string: str, spec: Spec, max_attempts: int = 1000) -> str:
"""Replace all macros in given string with corresponding values.
For example, a string '%{name}-%{version}.tar.gz' will be transformed to 'foo-2.0.tar.gz'.
Expand Down Expand Up @@ -550,9 +550,13 @@ def get_replacement_string(match: re.Match) -> str:
# Recursively expand macros
# Note: If macros are not defined in the spec file, this won't try to
# expand them.
while True:
attempt = 0
ret = ""
while attempt < max_attempts:
attempt += 1
ret = re.sub(_macro_pattern, get_replacement_string, string)
if ret != string:
string = ret
continue
return ret
break
return ret

0 comments on commit e3e8c53

Please sign in to comment.