-
-
Notifications
You must be signed in to change notification settings - Fork 29.3k
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
getpass should have an option to fail if it cannot input a password without hiding it
#105629
Comments
|
@arhadthedev, @terryjreedy can I try to work on this issue ? |
|
@abdulsmapara As far as I am concerned you can do what you want, but a) a PR might be premature at this point and b) I am not going to touch this issue other than to note as I did that a new API is a feature addition. I don't know if or how this function is actually used and if the change is really a good idea. |
|
@terryjreedy Thanks for your comment. I won't like to make a PR unless we are sure it is required. My take on this would be to make a change to allow users to opt for an exception instead of echoing the password. However, we should ensure that it is backward compatible (May be by adding an optional parameter to the method and defaulting it to current implementation). |
|
A quick note to observe that, to the best of my understanding it might be possible to make So it may be just a matter of properly documenting how to do it or there may be an issue with the way in which the warning is generated. In either of these two cases a 'fix' might not result in an API change. Further investigation shows that if you call |
>>> import getpass
>>> import warnings
>>> warnings.simplefilter("error", category=getpass.GetPassWarning)
>>> getpass.fallback_getpass("Hello:")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/getpass.py", line 121, in fallback_getpass
warnings.warn("Can not control echo on the terminal.", GetPassWarning,
getpass.GetPassWarning: Can not control echo on the terminal.
>>> This appears to work for me (explicitly calling fallback_getpass to avoid having to arrange for not having a TTY available). I'm using the 3.12 beta, but the same also works with 3.9. |
|
Yes, that works. The problem is that it does not work anymore if you use |
|
A slightly more involved way to try to reproduce this without using QtConsole. Script.py: import os
os.setsid() # Drop controlling TTY
import getpass
import warnings
warnings.simplefilter("error", category=getpass.GetPassWarning)
value = getpass.getpass("Hello: ")
print(f"{value=}")Usage: The second At first glance I don't see any code in I don't know yet why the warning filter works for me, but doesn't work in your program using QtConsole. BTW. I'm on a macOS system, not Linux. That shouldn't matter here though. |
|
I have modified the previous script in this way: import os
try:
os.setsid() # Drop controlling TTY
except PermissionError:
pass
import getpass
import warnings
warnings.simplefilter("error")
value="Foo"
try:
value = getpass.getpass("Hello: ")
except getpass.GetPassWarning:
print("Insecure terminal")
print(f"{value=}")this lets the script be run as The result is surprising. In the first case, the exception is correctly generated and handled. In the second case, the password is asked in an insecure way and there is no exception. This suggests a bug in |
|
The problem with In[4]: getpass.getpass
Out[4]: <bound method Kernel.getpass of <ipykernel.ipkernel.IPythonKernel object at 0x107f7b850>>This shows that This is with Jupiter 1.0.0 ( |
|
Should have noticed it, thanks, sorry for the noise then! |
|
Thanks @ronaldoussoren , @callegar |
|
Opened ipython/ipykernel#1123 for the discussion to continue in the |
No problem, 3th-party modules monkey patching the stdlib luckily is fairly uncommon. I'm glad we found why the warnings filter doesn't work for you. |


Bug report
Currently
getpassfalls back to non-hiding the user input on non-compliant terminals (this includes the QtConsole).A warning is provided, but that will often not prevent users from entering a password in a visible way and in a terminal where a mere scroll-back can reveal it even later. The issue is that the code using
getpasscannot prevent it, because there is no documented interface for forcinggetpassto fail rather than asking for a password without hiding it. Havinggetpassfail, would let the code try other means to get the password.From the documentation, I see that there is a warning. However, trying to convert it into an exception does not seem to work. For instance:
does not prevent the password for being asked with non-hidden characters, as the exception appears to be generated later.
Your environment
The text was updated successfully, but these errors were encountered: