-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add timeout to requests
calls
#2
base: main
Are you sure you want to change the base?
Add timeout to requests
calls
#2
Conversation
Important Review skippedBot user detected. To trigger a single review, invoke the You can disable this status message by setting the 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Quality Gate failedFailed conditions |
def get_asset(asset: str, width: int = None, height: int = None): | ||
if not width and not height: | ||
try: | ||
return fastapi.responses.FileResponse(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/{asset}") |
Check failure
Code scanning / CodeQL
Uncontrolled data used in path expression High
user-provided value
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 5 days ago
To fix the problem, we need to ensure that the constructed file path is contained within a safe root folder. We can achieve this by normalizing the path using os.path.normpath
and then checking that the normalized path starts with the root folder. This will prevent path traversal attacks.
- Normalize the constructed file path using
os.path.normpath
. - Check that the normalized path starts with the intended base path.
- If the check fails, return a 404 response.
-
Copy modified lines R89-R93 -
Copy modified line R96 -
Copy modified line R101 -
Copy modified lines R103-R104 -
Copy modified line R106 -
Copy modified lines R108-R109 -
Copy modified line R111 -
Copy modified lines R113-R114
@@ -88,5 +88,10 @@ | ||
def get_asset(asset: str, width: int = None, height: int = None): | ||
base_path = pathlib.Path(__file__).parent.parent.resolve() / "assets" | ||
asset_path = base_path / asset | ||
normalized_path = os.path.normpath(asset_path) | ||
if not str(normalized_path).startswith(str(base_path)): | ||
return fastapi.responses.JSONResponse(status_code=404, content={"message": "This asset does not exist."}) | ||
if not width and not height: | ||
try: | ||
return fastapi.responses.FileResponse(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/{asset}") | ||
return fastapi.responses.FileResponse(normalized_path) | ||
except: | ||
@@ -95,16 +100,16 @@ | ||
if asset == "logo_no_bg": | ||
image = Image.open(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/Astroid Logo no bg.png") | ||
image = Image.open(base_path / "Astroid Logo no bg.png") | ||
new_image = image.resize((width, height)) | ||
new_image.save(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid Logo no bg.png") | ||
return fastapi.responses.FileResponse(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid Logo no bg{width}x{height}.png") | ||
new_image.save(base_path / f"resized/Astroid Logo no bg{width}x{height}.png") | ||
return fastapi.responses.FileResponse(base_path / f"resized/Astroid Logo no bg{width}x{height}.png") | ||
elif asset == "logo": | ||
image = Image.open(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/Astroid Logo.png") | ||
image = Image.open(base_path / "Astroid Logo.png") | ||
new_image = image.resize((width, height)) | ||
new_image.save(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid Logo.png") | ||
return fastapi.responses.FileResponse(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid Logo{width}x{height}.png") | ||
new_image.save(base_path / f"resized/Astroid Logo{width}x{height}.png") | ||
return fastapi.responses.FileResponse(base_path / f"resized/Astroid Logo{width}x{height}.png") | ||
elif asset == "banner": | ||
image = Image.open(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid-banner.png") | ||
image = Image.open(base_path / "resized/Astroid-banner.png") | ||
new_image = image.resize((width, height)) | ||
new_image.save(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid-banner.png") | ||
return fastapi.responses.FileResponse(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid-banner{width}x{height}.png") | ||
new_image.save(base_path / f"resized/Astroid-banner{width}x{height}.png") | ||
return fastapi.responses.FileResponse(base_path / f"resized/Astroid-banner{width}x{height}.png") | ||
else: |
image = Image.open(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/Astroid Logo no bg.png") | ||
new_image = image.resize((width, height)) | ||
new_image.save(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid Logo no bg.png") | ||
return fastapi.responses.FileResponse(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid Logo no bg{width}x{height}.png") |
Check failure
Code scanning / CodeQL
Uncontrolled data used in path expression High
user-provided value
This path depends on a
user-provided value
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 5 days ago
To fix the problem, we need to ensure that the constructed file paths are safe and do not allow for path traversal or access to unintended files. We can achieve this by validating the width
and height
parameters to ensure they are within acceptable ranges and by normalizing the paths to ensure they stay within the intended directory.
- Validate the
width
andheight
parameters to ensure they are positive integers. - Normalize the constructed file paths and ensure they start with the intended base path.
-
Copy modified line R89 -
Copy modified lines R92-R95 -
Copy modified lines R99-R100 -
Copy modified line R102 -
Copy modified lines R104-R105 -
Copy modified line R107 -
Copy modified lines R109-R110 -
Copy modified line R112 -
Copy modified lines R114-R115
@@ -88,5 +88,9 @@ | ||
def get_asset(asset: str, width: int = None, height: int = None): | ||
base_path = pathlib.Path(__file__).parent.parent.resolve() / "assets" | ||
if not width and not height: | ||
try: | ||
return fastapi.responses.FileResponse(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/{asset}") | ||
fullpath = (base_path / asset).resolve() | ||
if not str(fullpath).startswith(str(base_path)): | ||
raise Exception("not allowed") | ||
return fastapi.responses.FileResponse(fullpath) | ||
except: | ||
@@ -94,17 +98,19 @@ | ||
else: | ||
if width <= 0 or height <= 0: | ||
return fastapi.responses.JSONResponse(status_code=400, content={"message": "Invalid dimensions."}) | ||
if asset == "logo_no_bg": | ||
image = Image.open(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/Astroid Logo no bg.png") | ||
image = Image.open(base_path / "Astroid Logo no bg.png") | ||
new_image = image.resize((width, height)) | ||
new_image.save(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid Logo no bg.png") | ||
return fastapi.responses.FileResponse(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid Logo no bg{width}x{height}.png") | ||
new_image.save(base_path / f"resized/Astroid Logo no bg{width}x{height}.png") | ||
return fastapi.responses.FileResponse(base_path / f"resized/Astroid Logo no bg{width}x{height}.png") | ||
elif asset == "logo": | ||
image = Image.open(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/Astroid Logo.png") | ||
image = Image.open(base_path / "Astroid Logo.png") | ||
new_image = image.resize((width, height)) | ||
new_image.save(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid Logo.png") | ||
return fastapi.responses.FileResponse(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid Logo{width}x{height}.png") | ||
new_image.save(base_path / f"resized/Astroid Logo{width}x{height}.png") | ||
return fastapi.responses.FileResponse(base_path / f"resized/Astroid Logo{width}x{height}.png") | ||
elif asset == "banner": | ||
image = Image.open(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid-banner.png") | ||
image = Image.open(base_path / "resized/Astroid-banner.png") | ||
new_image = image.resize((width, height)) | ||
new_image.save(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid-banner.png") | ||
return fastapi.responses.FileResponse(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid-banner{width}x{height}.png") | ||
new_image.save(base_path / f"resized/Astroid-banner{width}x{height}.png") | ||
return fastapi.responses.FileResponse(base_path / f"resized/Astroid-banner{width}x{height}.png") | ||
else: |
image = Image.open(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/Astroid Logo.png") | ||
new_image = image.resize((width, height)) | ||
new_image.save(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid Logo.png") | ||
return fastapi.responses.FileResponse(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid Logo{width}x{height}.png") |
Check failure
Code scanning / CodeQL
Uncontrolled data used in path expression High
user-provided value
This path depends on a
user-provided value
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 5 days ago
To fix the problem, we need to ensure that the constructed file paths are safe and do not allow path traversal. We can achieve this by normalizing the paths and ensuring they are within a designated safe directory. Additionally, we should validate the width
and height
parameters to ensure they are within acceptable ranges.
- Normalize the constructed file paths using
os.path.normpath
. - Ensure the normalized paths start with the designated safe directory.
- Validate the
width
andheight
parameters to ensure they are within acceptable ranges.
-
Copy modified line R89 -
Copy modified lines R92-R95 -
Copy modified lines R99-R100 -
Copy modified line R102 -
Copy modified lines R104-R105 -
Copy modified line R107 -
Copy modified lines R109-R110 -
Copy modified line R112 -
Copy modified lines R114-R115
@@ -88,5 +88,9 @@ | ||
def get_asset(asset: str, width: int = None, height: int = None): | ||
base_path = pathlib.Path(__file__).parent.parent.resolve() / "assets" | ||
if not width and not height: | ||
try: | ||
return fastapi.responses.FileResponse(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/{asset}") | ||
fullpath = os.path.normpath(base_path / asset) | ||
if not str(fullpath).startswith(str(base_path)): | ||
raise Exception("Path traversal detected") | ||
return fastapi.responses.FileResponse(fullpath) | ||
except: | ||
@@ -94,17 +98,19 @@ | ||
else: | ||
if not (0 < width <= 2000 and 0 < height <= 2000): | ||
return fastapi.responses.JSONResponse(status_code=400, content={"message": "Invalid dimensions."}) | ||
if asset == "logo_no_bg": | ||
image = Image.open(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/Astroid Logo no bg.png") | ||
image = Image.open(base_path / "Astroid Logo no bg.png") | ||
new_image = image.resize((width, height)) | ||
new_image.save(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid Logo no bg.png") | ||
return fastapi.responses.FileResponse(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid Logo no bg{width}x{height}.png") | ||
new_image.save(base_path / f"resized/Astroid Logo no bg{width}x{height}.png") | ||
return fastapi.responses.FileResponse(base_path / f"resized/Astroid Logo no bg{width}x{height}.png") | ||
elif asset == "logo": | ||
image = Image.open(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/Astroid Logo.png") | ||
image = Image.open(base_path / "Astroid Logo.png") | ||
new_image = image.resize((width, height)) | ||
new_image.save(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid Logo.png") | ||
return fastapi.responses.FileResponse(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid Logo{width}x{height}.png") | ||
new_image.save(base_path / f"resized/Astroid Logo{width}x{height}.png") | ||
return fastapi.responses.FileResponse(base_path / f"resized/Astroid Logo{width}x{height}.png") | ||
elif asset == "banner": | ||
image = Image.open(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid-banner.png") | ||
image = Image.open(base_path / "resized/Astroid-banner.png") | ||
new_image = image.resize((width, height)) | ||
new_image.save(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid-banner.png") | ||
return fastapi.responses.FileResponse(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid-banner{width}x{height}.png") | ||
new_image.save(base_path / f"resized/Astroid-banner{width}x{height}.png") | ||
return fastapi.responses.FileResponse(base_path / f"resized/Astroid-banner{width}x{height}.png") | ||
else: |
image = Image.open(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid-banner.png") | ||
new_image = image.resize((width, height)) | ||
new_image.save(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid-banner.png") | ||
return fastapi.responses.FileResponse(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid-banner{width}x{height}.png") |
Check failure
Code scanning / CodeQL
Uncontrolled data used in path expression High
user-provided value
This path depends on a
user-provided value
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 5 days ago
To fix the problem, we need to validate the user-provided width
and height
parameters to ensure they are within acceptable ranges and do not contain any malicious input. Additionally, we should ensure that the constructed file paths are contained within a safe root directory.
- Validate the
width
andheight
parameters to ensure they are positive integers. - Normalize the constructed file paths and verify that they start with the intended base directory.
-
Copy modified line R89 -
Copy modified lines R92-R95 -
Copy modified lines R99-R100 -
Copy modified line R102 -
Copy modified lines R104-R105 -
Copy modified line R107 -
Copy modified lines R109-R110 -
Copy modified line R112 -
Copy modified lines R114-R115
@@ -88,5 +88,9 @@ | ||
def get_asset(asset: str, width: int = None, height: int = None): | ||
base_path = pathlib.Path(__file__).parent.parent.resolve() / "assets" | ||
if not width and not height: | ||
try: | ||
return fastapi.responses.FileResponse(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/{asset}") | ||
fullpath = (base_path / asset).resolve() | ||
if not str(fullpath).startswith(str(base_path)): | ||
raise Exception("Invalid path") | ||
return fastapi.responses.FileResponse(fullpath) | ||
except: | ||
@@ -94,17 +98,19 @@ | ||
else: | ||
if width <= 0 or height <= 0: | ||
return fastapi.responses.JSONResponse(status_code=400, content={"message": "Invalid dimensions."}) | ||
if asset == "logo_no_bg": | ||
image = Image.open(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/Astroid Logo no bg.png") | ||
image = Image.open(base_path / "Astroid Logo no bg.png") | ||
new_image = image.resize((width, height)) | ||
new_image.save(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid Logo no bg.png") | ||
return fastapi.responses.FileResponse(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid Logo no bg{width}x{height}.png") | ||
new_image.save(base_path / f"resized/Astroid Logo no bg{width}x{height}.png") | ||
return fastapi.responses.FileResponse(base_path / f"resized/Astroid Logo no bg{width}x{height}.png") | ||
elif asset == "logo": | ||
image = Image.open(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/Astroid Logo.png") | ||
image = Image.open(base_path / "Astroid Logo.png") | ||
new_image = image.resize((width, height)) | ||
new_image.save(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid Logo.png") | ||
return fastapi.responses.FileResponse(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid Logo{width}x{height}.png") | ||
new_image.save(base_path / f"resized/Astroid Logo{width}x{height}.png") | ||
return fastapi.responses.FileResponse(base_path / f"resized/Astroid Logo{width}x{height}.png") | ||
elif asset == "banner": | ||
image = Image.open(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid-banner.png") | ||
image = Image.open(base_path / "resized/Astroid-banner.png") | ||
new_image = image.resize((width, height)) | ||
new_image.save(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid-banner.png") | ||
return fastapi.responses.FileResponse(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid-banner{width}x{height}.png") | ||
new_image.save(base_path / f"resized/Astroid-banner{width}x{height}.png") | ||
return fastapi.responses.FileResponse(base_path / f"resized/Astroid-banner{width}x{height}.png") | ||
else: |
asset = await astroidapi.surrealdb_handler.AttachmentProcessor.get_attachment(assetId) | ||
try: | ||
if asset: | ||
return fastapi.responses.FileResponse(f"{pathlib.Path(__file__).parent.resolve()}/astroidapi/TMP_attachments/{assetId}.{asset['type']}") |
Check failure
Code scanning / CodeQL
Uncontrolled data used in path expression High
user-provided value
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 5 days ago
To fix the problem, we need to validate the assetId
to ensure it does not contain any malicious input that could lead to path traversal attacks. We can achieve this by normalizing the path and ensuring it remains within the intended directory. Additionally, we can use a whitelist of allowed characters to further sanitize the assetId
.
- Normalize the constructed file path using
os.path.normpath
. - Ensure the normalized path starts with the intended base directory.
- Optionally, sanitize the
assetId
to allow only specific characters.
-
Copy modified lines R165-R170
@@ -164,3 +164,8 @@ | ||
if asset: | ||
return fastapi.responses.FileResponse(f"{pathlib.Path(__file__).parent.resolve()}/astroidapi/TMP_attachments/{assetId}.{asset['type']}") | ||
base_path = pathlib.Path(__file__).parent.resolve() / "astroidapi/TMP_attachments" | ||
file_path = base_path / f"{assetId}.{asset['type']}" | ||
normalized_path = file_path.resolve() | ||
if not str(normalized_path).startswith(str(base_path)): | ||
raise HTTPException(status_code=400, detail="Invalid asset ID.") | ||
return fastapi.responses.FileResponse(normalized_path) | ||
else: |
return fastapi.responses.JSONResponse(status_code=200, content={"message": f"An error occurred: {e}", | ||
"details": "unexpectederror"}) |
Check warning
Code scanning / CodeQL
Information exposure through an exception Medium
Stack trace information
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 5 days ago
To fix the problem, we need to ensure that detailed error messages, including exception details, are not exposed to the user. Instead, we should log the detailed error on the server and return a generic error message to the user. This can be achieved by modifying the exception handling code to log the exception details and return a generic error message.
-
Copy modified lines R486-R487 -
Copy modified lines R493-R494
@@ -485,3 +485,4 @@ | ||
except astroidapi.errors.HealtCheckError.EndpointCheckError as e: | ||
return fastapi.responses.JSONResponse(status_code=200, content={"message": f"An error occurred: {e}", | ||
logging.exception("An error occurred during endpoint health check.") | ||
return fastapi.responses.JSONResponse(status_code=200, content={"message": "An unexpected error occurred.", | ||
"details": "unexpectederror"}) | ||
@@ -491,4 +492,4 @@ | ||
except astroidapi.errors.SurrealDBHandler.GetEndpointError as e: | ||
traceback.print_exc() | ||
return fastapi.responses.JSONResponse(status_code=404, content={"message": f"An error occurred: {e}", | ||
logging.exception("An error occurred while getting endpoint information.") | ||
return fastapi.responses.JSONResponse(status_code=404, content={"message": "An error occurred while retrieving endpoint information.", | ||
"details": "getendpointerror"}) |
return fastapi.responses.JSONResponse(status_code=404, content={"message": f"An error occurred: {e}", | ||
"details": "getendpointerror"}) |
Check warning
Code scanning / CodeQL
Information exposure through an exception Medium
Stack trace information
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 5 days ago
To fix the problem, we need to ensure that detailed error messages and stack traces are not exposed to end users. Instead, we should log the detailed error information on the server and return a generic error message to the user. This can be achieved by modifying the exception handling code to log the exception and return a generic error message.
- Modify the exception handling code to log the detailed error message using
logging.exception()
. - Return a generic error message to the user without including the exception details.
-
Copy modified lines R492-R493
@@ -491,4 +491,4 @@ | ||
except astroidapi.errors.SurrealDBHandler.GetEndpointError as e: | ||
traceback.print_exc() | ||
return fastapi.responses.JSONResponse(status_code=404, content={"message": f"An error occurred: {e}", | ||
logging.exception("An error occurred while getting the endpoint.") | ||
return fastapi.responses.JSONResponse(status_code=404, content={"message": "An error occurred while getting the endpoint.", | ||
"details": "getendpointerror"}) |
return fastapi.responses.JSONResponse(status_code=200, content={"message": "Repaired.", "summary": summary}) | ||
except Exception as e: | ||
logging.exception(traceback.print_exc()) | ||
return fastapi.responses.JSONResponse(status_code=500, content={"message": f"An error occurred: {e}"}) |
Check warning
Code scanning / CodeQL
Information exposure through an exception Medium
Stack trace information
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 5 days ago
To fix the problem, we need to ensure that detailed exception information is not exposed to the end user. Instead, we should log the detailed error message on the server and return a generic error message to the user. This can be achieved by using the logging
module to log the exception and returning a generic error message in the JSON response.
-
Copy modified lines R508-R509
@@ -507,4 +507,4 @@ | ||
except Exception as e: | ||
logging.exception(traceback.print_exc()) | ||
return fastapi.responses.JSONResponse(status_code=500, content={"message": f"An error occurred: {e}"}) | ||
logging.exception("An error occurred while repairing the endpoint", exc_info=True) | ||
return fastapi.responses.JSONResponse(status_code=500, content={"message": "An internal error has occurred."}) | ||
else: |
except KeyError: | ||
if token == Bot.config.MASTER_TOKEN: | ||
try: | ||
os.remove(f"{pathlib.Path(__file__).parent.resolve()}/endpoints/{endpoint}.json") |
Check failure
Code scanning / CodeQL
Uncontrolled data used in path expression High
user-provided value
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 5 days ago
To fix the problem, we need to ensure that the constructed file path is contained within a safe root folder. We can achieve this by normalizing the path using os.path.normpath
and then checking that the normalized path starts with the root folder. This will prevent path traversal attacks by ensuring that the user cannot escape the intended directory.
- Normalize the constructed file path using
os.path.normpath
. - Check that the normalized path starts with the intended root directory.
- Raise an exception or return an error response if the path validation fails.
-
Copy modified lines R597-R602
@@ -596,3 +596,8 @@ | ||
try: | ||
os.remove(f"{pathlib.Path(__file__).parent.resolve()}/endpoints/{endpoint}.json") | ||
base_path = pathlib.Path(__file__).parent.resolve() / "endpoints" | ||
file_path = base_path / f"{endpoint}.json" | ||
normalized_path = os.path.normpath(file_path) | ||
if not str(normalized_path).startswith(str(base_path)): | ||
return fastapi.responses.JSONResponse(status_code=400, content={"message": "Invalid endpoint path."}) | ||
os.remove(normalized_path) | ||
return fastapi.responses.JSONResponse(status_code=200, content={"message": "Deleted."}) |
await astroidapi.surrealdb_handler.sync_server_relations() | ||
return fastapi.responses.JSONResponse(status_code=200, content={"message": "Success."}) | ||
except Exception as e: | ||
return fastapi.responses.JSONResponse(status_code=500, content={"message": f"An error occurred: {e}"}) |
Check warning
Code scanning / CodeQL
Information exposure through an exception Medium
Stack trace information
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 5 days ago
To fix the problem, we need to ensure that detailed error messages and stack traces are not exposed to the end user. Instead, we should log the detailed error information on the server and return a generic error message to the user. This can be achieved by modifying the exception handling code to log the exception and return a generic error message.
Specifically, we will:
- Import the
logging
module if not already imported. - Log the exception using the
logging
module. - Return a generic error message to the user.
-
Copy modified lines R757-R758
@@ -756,3 +756,4 @@ | ||
except Exception as e: | ||
return fastapi.responses.JSONResponse(status_code=500, content={"message": f"An error occurred: {e}"}) | ||
logging.error("An error occurred while syncing server relations", exc_info=True) | ||
return fastapi.responses.JSONResponse(status_code=500, content={"message": "An internal error has occurred."}) | ||
|
Many developers will be surprised to learn that
requests
library calls do not include timeouts by default. This means that an attempted request could hang indefinitely if no connection is established or if no data is received from the server.The requests documentation suggests that most calls should explicitly include a
timeout
parameter. This codemod adds a default timeout value in order to set an upper bound on connection times and ensure that requests connect or fail in a timely manner. This value also ensures the connection will timeout if the server does not respond with data within a reasonable amount of time.While timeout values will be application dependent, we believe that this codemod adds a reasonable default that serves as an appropriate ceiling for most situations.
Our changes look like the following:
More reading
I have additional improvements ready for this repo! If you want to see them, leave the comment:
... and I will open a new PR right away!
🧚🤖 Powered by Pixeebot
Feedback | Community | Docs | Codemod ID: pixee:python/add-requests-timeouts