Potential fix for code scanning alert no. 59: Information exposure through an exception #335
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Potential fix for https://github.com/microsoft/debug-gym/security/code-scanning/59
To fix the problem, the response sent to the user must not contain the raw exception message. Instead, the application should return a generic, non-sensitive error string and, if detailed diagnostics are needed, log the exception on the server. This prevents disclosure of filesystem paths or other environment details while preserving debuggability.
Concretely, in
analysis/json_log_viewer/json_log_viewer.pyaround line 580, replacereturn jsonify({"error": f"Permission denied: {str(e)}"}), 403with a response that uses a fixed, generic message such as"Permission denied". If the application wants to retain diagnostics, it can loge(for example, using Python’sloggingmodule) before returning the generic message. To do that without altering existing behavior from the caller’s perspective, we can add an import forlogging, configure a basic logger (or rely on defaults if already configured elsewhere), and log the exception inside theexceptblock, while still returning a simple"Permission denied"error to the client.Specifically:
import loggingnear the top ofjson_log_viewer.pyalongside the other imports.logging.basicConfig(level=logging.INFO)), though this is not strictly required for correctness.browse_directoryfunction’s outerexcept (OSError, PermissionError) as e:block, add a logging call likelogging.exception("Error browsing directory")(which logs the stack trace) and change the returned error JSON to a static message that does not includee’s text.Suggested fixes powered by Copilot Autofix. Review carefully before merging.