-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Python 3.13.0 REPL loads local files unexpectedly, causing conflicts and security issues #125140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Confirmed on current main as well. Weirdly enough, it does not seem to happen when I shadow some other stdlib modules which are also imported from |
Any chance this does not happen with abc, last, sys, os but happens with others? E.g. with dataclasses, I get:
I believe that some modules are loaded before $the current working directory is added to sys.path by the site module (?). |
Hmm, I wouldn't say this is a major security issue. I don't think the REPL is a very common attack vector, so I'm hesitant to add the security label. cc @pablogsal (I'm not sure if you get notified when |
This issue poses a significant security risk as it potentially allows malicious code to be executed without the user's knowledge or consent. The current functionality permits users to navigate the file tree and access the Python REPL, where arbitrary code can be run at the current privilege level. As a user, I often utilize the Python REPL for quick calculations or other brief tasks. However, I would never assume that this action could damage the system, especially considering the risk depends on the current folder location. This misconception highlights the danger of the current implementation. This vulnerability could be exploited for various malicious purposes, including:
The risk is particularly concerning because users may inadvertently run harmful code while performing routine tasks, unaware of the potential consequences. Given the severity of potential exploits and the ease with which users might unknowingly expose themselves to risk, I recommend prioritizing this issue for immediate attention and resolution. |
…g pyrepl Signed-off-by: Pablo Galindo <[email protected]>
Why is this considered new? I guess because the REPL now imports more stdlib modules than it did in the past before the prompt is shown? Python has always put the current directory first in sys.path as As for why people consider this a security problem of late: People with Python installed are increasingly being targeted by drive by download and or other file placement opportunistic attacks. Get something to save a stdlibmodulename.py in a browser Downloads directory - then anytime later for any reason when the user runs python while their current directory is Downloads or as It isn't a flaw in Python. It's merely a well known existing attack vector that has expanded a little in 3.13. It is a long standing feature relied upon by a lot of the world at the same time as being problematic in this type of scenario. Mitigation wise to prevent 3.13 surprise, I think you're basically asking for |
Going to agree with @gpshead that this doesn't represent any additional risk of using PyREPL compared to the typical Python REPL or any Python application. Even if this specific issue is fixed ("PyREPL no longer loading The overall behavior of being able to shadow stdlib module names in local programs would be worth fixing from a security POV, but this specific behavior from PyREPL isn't any worse than that status-quo. |
The |
Previously, I needed to open a REPL and run a command that imports something to trigger the problem. Now I open a REPL and it imports modules from the current directory on its own right away. That's new. Even if I am aware of the Python behavior and my intention in the REPL is to never import anything (e.g. I plan to use it as a calculator) or to immediately |
…g pyrepl Signed-off-by: Pablo Galindo <[email protected]>
The reason this was not a problem before is some happy coincidence: Line 220 in 7d2c397
As that line runs too early, nothing gets pulled from the local dir because at that time |
Not new but has been fixed in the past: |
…pl (GH-125212) Signed-off-by: Pablo Galindo <[email protected]> Co-authored-by: Łukasz Langa <[email protected]> Co-authored-by: Peter Bierma <[email protected]>
…g pyrepl (pythonGH-125212) (cherry picked from commit c7d5d1d) Co-authored-by: Pablo Galindo Salgado <[email protected]> Signed-off-by: Pablo Galindo <[email protected]> Co-authored-by: Łukasz Langa <[email protected]> Co-authored-by: Peter Bierma <[email protected]>
#125212 will do the trick |
Uh oh!
There was an error while loading. Please reload this page.
Bug report
Bug description:
Description
When starting the Python 3.13.0 REPL in a directory containing a file named
code.py
, the REPL attempts to load this local file instead of the standard librarycode
module. This causes conflicts and errors when initializing the interactive environment. This is also a major security issue.Steps to Reproduce
code.py
in this directoryExpected Behavior
The Python REPL should start normally, using the standard library
code
module for its interactive features.Actual Behavior
The REPL fails to initialize properly, producing an error message indicating that it's attempting to use the local
code.py
file instead of the standard library module:Additional Context
System Information
Possible Solution
The REPL initialization process should be modified to ensure it uses the standard library
code
module, regardless of local files in the current directory.CPython versions tested on:
3.13
Operating systems tested on:
Linux
Linked PRs
The text was updated successfully, but these errors were encountered: