From f85071e4b9873c1f18ce495eb9bef1d7f42e37cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antal=20Szab=C3=B3?= Date: Fri, 19 May 2023 13:48:06 +0200 Subject: [PATCH] fix: correctly escape path templates Previously, the resulting regex was incorrect, as the newly introduced control characters (when handling path parameters) were also escaped. This caused paths with parameters to not be generated. Also removes an effect-less `for` loop. --- mitmproxy2swagger/mitmproxy2swagger.py | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/mitmproxy2swagger/mitmproxy2swagger.py b/mitmproxy2swagger/mitmproxy2swagger.py index 49f5758..e5d73ea 100644 --- a/mitmproxy2swagger/mitmproxy2swagger.py +++ b/mitmproxy2swagger/mitmproxy2swagger.py @@ -23,18 +23,10 @@ from mitmproxy2swagger.mitmproxy_capture_reader import ( def path_to_regex(path): # replace the path template with a regex - path = path.replace("\\", "\\\\") - path = path.replace("{", "(?P<") - path = path.replace("}", ">[^/]+)") - path = path.replace("*", ".*") - path = path.replace("/", "\\/") - path = path.replace("?", "\\?") - path = path.replace("(", "\\(") - path = path.replace(")", "\\)") - path = path.replace(".", "\\.") - path = path.replace("+", "\\+") - path = path.replace("[", "\\[") - path = path.replace("]", "\\]") + path = re.escape(path) + path = path.replace(r"\{", "(?P<") + path = path.replace(r"\}", ">[^/]+)") + path = path.replace(r"\*", ".*") return "^" + path + "$" @@ -160,8 +152,6 @@ def main(override_args: Optional[Sequence[str]] = None): # new endpoints will be added here so that they can be added as comments in the swagger file new_path_templates = [] - for path in path_templates: - re.compile(path_to_regex(path)) path_template_regexes = [re.compile(path_to_regex(path)) for path in path_templates] try: