Fix insecure default temp file path in session dump/load#750
Closed
eddieran wants to merge 1 commit intouqfoundation:masterfrom
Closed
Fix insecure default temp file path in session dump/load#750eddieran wants to merge 1 commit intouqfoundation:masterfrom
eddieran wants to merge 1 commit intouqfoundation:masterfrom
Conversation
The default /tmp/session.pkl path is shared and world-writable, making it vulnerable to symlink attacks. This commit: - Adds _check_symlink() to reject symlinks before reading files - Adds _safe_open_for_writing() using O_CREAT|O_EXCL for race-free file creation with 0o600 permissions - Emits SecurityWarning when using the default shared temp path - Adds tests for symlink rejection and the security warning Closes #749 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #749 — the default
/tmp/session.pklpath used bydump_module(),load_module(), andload_module_asdict()is insecure because/tmpis world-writable and shared, making it vulnerable to symlink attacks and race conditions.Changes
_check_symlink()that usesos.lstat()to reject symlinks before reading or writing session files._safe_open_for_writing()that usesos.open()withO_CREAT | O_EXCL | O_WRONLYand mode0o600for race-free exclusive file creation. If the file already exists, it verifies it is not a symlink before opening.SecurityWarningwhen the default shared/tmp/session.pklpath is used, advising users to pass an explicit filename.test_symlink_rejected()andtest_default_path_warning()totest_session.py.Files changed
dill/session.py— security helpers and warning indump_module,load_module,load_module_asdictdill/tests/test_session.py— new security testsTest plan
test_session.pytests passtest_symlink_rejectedverifies symlinks are rejected by all three functionstest_default_path_warningverifies SecurityWarning is emitted for the default path