Skip to content

Replace insecure temporary file creation in chem/molecular_data.py#1243

Open
mhucka wants to merge 4 commits intoquantumlib:mainfrom
mhucka:fix-insecure-temp-file-creation-molecular-data-15634551765368123518
Open

Replace insecure temporary file creation in chem/molecular_data.py#1243
mhucka wants to merge 4 commits intoquantumlib:mainfrom
mhucka:fix-insecure-temp-file-creation-molecular-data-15634551765368123518

Conversation

@mhucka
Copy link
Copy Markdown
Contributor

@mhucka mhucka commented Mar 30, 2026

Replace the use of uuid.uuid4() for temporary file names with tempfile.NamedTemporaryFile. This ensures that temporary files are created with restricted permissions (0600) and in a secure manner. The temporary file is now created in the same directory as the target file to ensure an atomic shutil.move.

In addition, to avoid excessively nested code and to improve modularity, the code is slightly refactored to split out the data-writing steps into a separate private function.

mhucka and others added 3 commits March 30, 2026 04:21
…ata.save

Replaced the use of `uuid.uuid4()` for temporary file names with
`tempfile.NamedTemporaryFile`. This ensures that temporary files are
created with restricted permissions (0600) and in a secure manner.
The temporary file is now created in the same directory as the target
file to ensure an atomic `shutil.move`. Removed the now-unused `uuid`
import.
@mhucka mhucka marked this pull request as ready for review March 30, 2026 04:50
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request replaces the use of uuid with tempfile.NamedTemporaryFile for temporary file creation within the save method of molecular_data.py. A review comment identifies a potential resource leak where temporary files might not be cleaned up if an error occurs before the file is moved, suggesting the implementation of a try...finally block for better robustness.

This also pulls out the code to write the data into a separate method,
but better readability and modularity.
@mhucka
Copy link
Copy Markdown
Contributor Author

mhucka commented Mar 30, 2026

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the save method in molecular_data.py to use tempfile.NamedTemporaryFile and extracts the HDF5 writing logic into a private method. Feedback indicates a potential TOCTOU race condition introduced by closing the temporary file before opening it with h5py, with a suggestion to pass the file object directly instead.

Comment on lines +840 to +842
tmp_file.close()
try:
with h5py.File(tmp_name, "w") as hdf5_file:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

security-critical critical

While using tempfile.NamedTemporaryFile is a great improvement for secure temporary file creation, closing the file and then re-opening it by name introduces a Time-of-Check to Time-of-Use (TOCTOU) race condition. An attacker could potentially replace the temporary file between the close() and h5py.File() calls, leading to writing data to an unintended file.

To prevent this, you should pass the file object tmp_file directly to h5py.File, which supports file-like objects. This avoids referencing the file by name after it has been created.

Suggested change
tmp_file.close()
try:
with h5py.File(tmp_name, "w") as hdf5_file:
try:
with h5py.File(tmp_file, "w") as hdf5_file:

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.

1 participant