Skip to content

Conversation

@MrSidims
Copy link
Owner

Potential fix for https://github.com/MrSidims/PytorchExplorer/security/code-scanning/2

To fix the issue, we need to ensure that detailed exception information, such as stack traces, is not exposed to external users. Instead, we should log the detailed error messages on the server for debugging purposes and return a generic error message to the user. This approach protects sensitive information while still allowing developers to diagnose issues.

  1. Modify the functions run_external_opt_tool_file, apply_optional_passes, and other relevant functions to log detailed error messages instead of returning them directly.
  2. Replace the returned error messages with a generic message like "An internal error occurred."
  3. Use Python's logging module to log the detailed error messages to a file or console for debugging purposes.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

…ough an exception

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
import io
import sys
from contextlib import redirect_stdout, redirect_stderr
import logging

Check warning

Code scanning / Pylint (reported by Codacy)

standard import "import logging" should be placed before "from fastapi import FastAPI" Warning

standard import "import logging" should be placed before "from fastapi import FastAPI"
import io
import sys
from contextlib import redirect_stdout, redirect_stderr
import logging

Check warning

Code scanning / Pylintpython3 (reported by Codacy)

standard import "import logging" should be placed before "from fastapi import FastAPI" Warning

standard import "import logging" should be placed before "from fastapi import FastAPI"
return (result.returncode == 0, result.stderr if result.stderr else "")
except Exception as e:
return (False, f"Error running {tool}: {str(e)}")
logging.error(f"Error running {tool}: {str(e)}")

Check notice

Code scanning / Pylintpython3 (reported by Codacy)

Use lazy % formatting in logging functions Note

Use lazy % formatting in logging functions
)
if not success:
output += f"\n\n===== {tool} failed =====\n{stderr}"
logging.error(f"{tool} failed with error: {stderr}")

Check notice

Code scanning / Pylintpython3 (reported by Codacy)

Use lazy % formatting in logging functions Note

Use lazy % formatting in logging functions
return apply_optional_passes(str(traced_model.graph), pipeline, dump_each)
except Exception as e:
return f"Error generating TorchScript Graph IR: {str(e)}"
logging.error(f"Error generating TorchScript Graph IR: {str(e)}")

Check notice

Code scanning / Pylintpython3 (reported by Codacy)

Use lazy % formatting in logging functions Note

Use lazy % formatting in logging functions
)
except Exception as e:
return f"Error executing user code: {str(e)}"
logging.error(f"Error executing user code: {str(e)}")

Check notice

Code scanning / Pylintpython3 (reported by Codacy)

Use lazy % formatting in logging functions Note

Use lazy % formatting in logging functions
return (result.returncode == 0, result.stderr if result.stderr else "")
except Exception as e:
return (False, f"Error running {tool}: {str(e)}")
logging.error(f"Error running {tool}: {str(e)}")

Check warning

Code scanning / Prospector (reported by Codacy)

Use lazy % formatting in logging functions (logging-fstring-interpolation) Warning

Use lazy % formatting in logging functions (logging-fstring-interpolation)
)
if not success:
output += f"\n\n===== {tool} failed =====\n{stderr}"
logging.error(f"{tool} failed with error: {stderr}")

Check warning

Code scanning / Prospector (reported by Codacy)

Use lazy % formatting in logging functions (logging-fstring-interpolation) Warning

Use lazy % formatting in logging functions (logging-fstring-interpolation)
return apply_optional_passes(str(traced_model.graph), pipeline, dump_each)
except Exception as e:
return f"Error generating TorchScript Graph IR: {str(e)}"
logging.error(f"Error generating TorchScript Graph IR: {str(e)}")

Check warning

Code scanning / Prospector (reported by Codacy)

Use lazy % formatting in logging functions (logging-fstring-interpolation) Warning

Use lazy % formatting in logging functions (logging-fstring-interpolation)
)
except Exception as e:
return f"Error executing user code: {str(e)}"
logging.error(f"Error executing user code: {str(e)}")

Check warning

Code scanning / Prospector (reported by Codacy)

Use lazy % formatting in logging functions (logging-fstring-interpolation) Warning

Use lazy % formatting in logging functions (logging-fstring-interpolation)
@MrSidims MrSidims closed this May 11, 2025
@MrSidims MrSidims deleted the alert-autofix-2 branch May 12, 2025 08:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants