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
This commit is contained in:
Mathias Petermann 2024-07-04 09:56:20 +02:00 committed by GitHub
parent 347cd5e4c9
commit cb6b6c3f24
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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()