From ac70c5aab9abff4ac78d567c1904ec0c1cbcd544 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antal=20Szab=C3=B3?= Date: Fri, 19 May 2023 14:42:05 +0200 Subject: [PATCH 1/2] feat: add customizable param regex and suppression This adds 2 new command line flags: - `-r`, `--param-regex`: Allows the user to customize what path segment value will be considered as a parameter. - `s`, `--suppress-params`: Only outputs the "templated" path with the parameter variables, but not the originals with the actual parameters. --- mitmproxy2swagger/mitmproxy2swagger.py | 32 ++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/mitmproxy2swagger/mitmproxy2swagger.py b/mitmproxy2swagger/mitmproxy2swagger.py index 49f5758..0620172 100644 --- a/mitmproxy2swagger/mitmproxy2swagger.py +++ b/mitmproxy2swagger/mitmproxy2swagger.py @@ -98,8 +98,26 @@ def main(override_args: Optional[Sequence[str]] = None): choices=["flow", "har"], help="Override the input file format auto-detection.", ) + parser.add_argument( + "-r", + "--param-regex", + default="[0-9]+", + help="Regex to match parameters in the API paths. Path segments that match this regex will be turned into parameter placeholders.", + ) + parser.add_argument( + "-s", + "--suppress-params", + action="store_true", + help="Do not include API paths that have the original parameter values, only the ones with placeholders.", + ) args = parser.parse_args(override_args) + try: + args.param_regex = re.compile("^" + args.param_regex + "$") + except re.error as e: + print(f"{console_util.ANSI_RED}Invalid path parameter regex: {e}{console_util.ANSI_RESET}") + sys.exit(1) + yaml = ruamel.yaml.YAML() capture_reader: Union[MitmproxyCaptureReader, HarCaptureReader] @@ -340,20 +358,24 @@ def main(override_args: Optional[Sequence[str]] = None): ) sys.exit(1) + def is_param(param_value): + return args.param_regex.match(param_value) is not None + new_path_templates.sort() # add suggested path templates # basically inspects urls and replaces segments containing only numbers with a parameter new_path_templates_with_suggestions = [] - for idx, path in enumerate(new_path_templates): + for path in new_path_templates: # check if path contains number-only segments segments = path.split("/") - if any(segment.isdigit() for segment in segments): + has_param = any(is_param(segment) for segment in segments) + if has_param: # replace digit segments with {id}, {id1}, {id2} etc new_segments = [] param_id = 0 for segment in segments: - if segment.isdigit(): + if is_param(segment): param_name = "id" + str(param_id) if param_id == 0: param_name = "id" @@ -365,7 +387,9 @@ def main(override_args: Optional[Sequence[str]] = None): # prepend the suggested path to the new_path_templates list if suggested_path not in new_path_templates_with_suggestions: new_path_templates_with_suggestions.append("ignore:" + suggested_path) - new_path_templates_with_suggestions.append("ignore:" + path) + + if not has_param or not args.suppress_params: + new_path_templates_with_suggestions.append("ignore:" + path) # remove the ending comments not to add them twice From 6c1a99a18fc8462d7486f4f17d3e0351aa2c2dae Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 19 May 2023 12:46:56 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mitmproxy2swagger/mitmproxy2swagger.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mitmproxy2swagger/mitmproxy2swagger.py b/mitmproxy2swagger/mitmproxy2swagger.py index 0620172..09a3d86 100644 --- a/mitmproxy2swagger/mitmproxy2swagger.py +++ b/mitmproxy2swagger/mitmproxy2swagger.py @@ -115,7 +115,9 @@ def main(override_args: Optional[Sequence[str]] = None): try: args.param_regex = re.compile("^" + args.param_regex + "$") except re.error as e: - print(f"{console_util.ANSI_RED}Invalid path parameter regex: {e}{console_util.ANSI_RESET}") + print( + f"{console_util.ANSI_RED}Invalid path parameter regex: {e}{console_util.ANSI_RESET}" + ) sys.exit(1) yaml = ruamel.yaml.YAML()