Skip to content

Fix security issue and bug in path handling in app.py#45

Merged
s-mandra merged 5 commits intomainfrom
fix-security-issue
Mar 4, 2026
Merged

Fix security issue and bug in path handling in app.py#45
s-mandra merged 5 commits intomainfrom
fix-security-issue

Conversation

@s-mandra
Copy link
Collaborator

@s-mandra s-mandra commented Mar 3, 2026

  • Resolve "Uncontrolled data used in path expression" by canonicalizing paths with Path.resolve() in load_file and dump_results.
  • Path(filename).exists was incorrectly referenced as a property rather than called as a method filename.exists()
  • Rename local open to open_ in dump_results to avoid shadowing the builtin open and maintain compatibility with Python 3.8.
  • Ensure all file operations use the resolved Path objects.

- Resolve "Uncontrolled data used in path expression" by canonicalizing
  paths with `Path.resolve()` in `load_file` and `dump_results`.
- Rename local `open` to `open_` in `dump_results` to avoid shadowing
  the builtin `open` and maintain compatibility with Python 3.8.
- Ensure all file operations use the resolved `Path` objects.
@s-mandra s-mandra self-assigned this Mar 3, 2026
@s-mandra s-mandra added the bug Something isn't working label Mar 3, 2026
@s-mandra
Copy link
Collaborator Author

s-mandra commented Mar 3, 2026

The persistent CodeQL flags on lines 112, 113 and 145 are expected because the application's core requirement is to allow users to load tensor network data from any filesystem path.

However, the underlying security risks have been addressed by this patch in two ways:

  • Path Normalization: By using Path(filename).resolve(), we have neutralized directory traversal attacks (e.g., ../../). The application now operates on a canonical absolute path.

  • Bug Fix: This patch fixes the broken logic at line 112, where Path(filename).exists was incorrectly referenced as a property rather than called as a method filename.exists().

Since full filesystem access is an intended feature for this local utility, I will dismiss the alerts as "Risk is acceptable" once this is reviewed.

@s-mandra s-mandra requested a review from mhucka March 3, 2026 15:14
Copy link
Collaborator

@mhucka mhucka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of simple changes. This is probably not going to be enough to address the security risk, though. I think you probably have to make sure that the path supplied by the user is limited in some way so that they don't do something like try to read files in /etc or some such. One way to address that concern would be to use the .is_relative_to() method on Path and limit the paths to (perhaps) the directory from which tnco is being executed.

s-mandra and others added 2 commits March 4, 2026 14:30
Co-authored-by: Michael Hucka <mhucka@google.com>
Co-authored-by: Michael Hucka <mhucka@google.com>
@s-mandra s-mandra merged commit 56011bc into main Mar 4, 2026
11 checks passed
@s-mandra s-mandra deleted the fix-security-issue branch March 4, 2026 22:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants