From cb6b6c3f24c8565517779281b35ddcd49725c2a8 Mon Sep 17 00:00:00 2001 From: Mathias Petermann Date: Thu, 4 Jul 2024 09:56:20 +0200 Subject: [PATCH] openapi2jsonschema: Allow writting to subdirectories Verify that the target filename (templates from `FILENAME_FORMAT`) is in the current working directory and allow writting to arbitrary subdirectories if this is the case --- scripts/openapi2jsonschema.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/scripts/openapi2jsonschema.py b/scripts/openapi2jsonschema.py index 6cda017..6ebeb50 100755 --- a/scripts/openapi2jsonschema.py +++ b/scripts/openapi2jsonschema.py @@ -102,7 +102,16 @@ def write_schema_file(schema, filename): schemaJSON = json.dumps(schema, indent=2) # Dealing with user input here.. - filename = os.path.basename(filename) + cwd = Path.cwd() + abspath = Path(filename).resolve() + if cwd in abspath.parents: + filename = abspath.relative_to(cwd) + if not filename.parent.exists(): + filename.parent.mkdir(parents=True) + else: + print("Trying to write to a file outside of the current directory, aborting.") + exit(1) + f = open(filename, "w") print(schemaJSON, file=f) f.close()