Skip to content

Commit 73dcdd6

Browse files
wing328padznich
andauthored
Update python sdk to strip any directory traversal in filename (#22965)
* update python sdk Strip any directory traversal * rebased * update samples, docs * fallback case --------- Co-authored-by: Pavel Slabko <slabkopg@gmail.com>
1 parent a8ccfad commit 73dcdd6

7 files changed

Lines changed: 21 additions & 7 deletions

File tree

modules/openapi-generator/src/main/resources/python/api_client.mustache

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,9 @@ class ApiClient:
724724
content_disposition
725725
)
726726
assert m is not None, "Unexpected 'content-disposition' header value"
727-
filename = m.group(1)
727+
filename = os.path.basename(m.group(1)) # Strip any directory traversal
728+
if filename in ("", ".", ".."): # fall back to tmp filename
729+
filename = os.path.basename(path)
728730
path = os.path.join(os.path.dirname(path), filename)
729731

730732
with open(path, "wb") as f:

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/api_client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,9 @@ def __deserialize_file(self, response):
708708
content_disposition
709709
)
710710
assert m is not None, "Unexpected 'content-disposition' header value"
711-
filename = m.group(1)
711+
filename = os.path.basename(m.group(1)) # Strip any directory traversal
712+
if filename in ("", ".", ".."): # fall back to tmp filename
713+
filename = os.path.basename(path)
712714
path = os.path.join(os.path.dirname(path), filename)
713715

714716
with open(path, "wb") as f:

samples/client/echo_api/python/openapi_client/api_client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,9 @@ def __deserialize_file(self, response):
708708
content_disposition
709709
)
710710
assert m is not None, "Unexpected 'content-disposition' header value"
711-
filename = m.group(1)
711+
filename = os.path.basename(m.group(1)) # Strip any directory traversal
712+
if filename in ("", ".", ".."): # fall back to tmp filename
713+
filename = os.path.basename(path)
712714
path = os.path.join(os.path.dirname(path), filename)
713715

714716
with open(path, "wb") as f:

samples/openapi3/client/petstore/python-aiohttp/petstore_api/api_client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,9 @@ def __deserialize_file(self, response):
717717
content_disposition
718718
)
719719
assert m is not None, "Unexpected 'content-disposition' header value"
720-
filename = m.group(1)
720+
filename = os.path.basename(m.group(1)) # Strip any directory traversal
721+
if filename in ("", ".", ".."): # fall back to tmp filename
722+
filename = os.path.basename(path)
721723
path = os.path.join(os.path.dirname(path), filename)
722724

723725
with open(path, "wb") as f:

samples/openapi3/client/petstore/python-httpx/petstore_api/api_client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,9 @@ def __deserialize_file(self, response):
717717
content_disposition
718718
)
719719
assert m is not None, "Unexpected 'content-disposition' header value"
720-
filename = m.group(1)
720+
filename = os.path.basename(m.group(1)) # Strip any directory traversal
721+
if filename in ("", ".", ".."): # fall back to tmp filename
722+
filename = os.path.basename(path)
721723
path = os.path.join(os.path.dirname(path), filename)
722724

723725
with open(path, "wb") as f:

samples/openapi3/client/petstore/python-lazyImports/petstore_api/api_client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,9 @@ def __deserialize_file(self, response):
714714
content_disposition
715715
)
716716
assert m is not None, "Unexpected 'content-disposition' header value"
717-
filename = m.group(1)
717+
filename = os.path.basename(m.group(1)) # Strip any directory traversal
718+
if filename in ("", ".", ".."): # fall back to tmp filename
719+
filename = os.path.basename(path)
718720
path = os.path.join(os.path.dirname(path), filename)
719721

720722
with open(path, "wb") as f:

samples/openapi3/client/petstore/python/petstore_api/api_client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,9 @@ def __deserialize_file(self, response):
714714
content_disposition
715715
)
716716
assert m is not None, "Unexpected 'content-disposition' header value"
717-
filename = m.group(1)
717+
filename = os.path.basename(m.group(1)) # Strip any directory traversal
718+
if filename in ("", ".", ".."): # fall back to tmp filename
719+
filename = os.path.basename(path)
718720
path = os.path.join(os.path.dirname(path), filename)
719721

720722
with open(path, "wb") as f:

0 commit comments

Comments
 (0)