Skip to content

Commit 10bfe14

Browse files
#1436 added checking for a special character sequence for when windows users use shift+ins to paste into a password field
1 parent 170418c commit 10bfe14

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

SoftLayer/CLI/environment.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,22 @@ def input(self, prompt, default=None, show_default=True):
6767

6868
def getpass(self, prompt, default=None):
6969
"""Provide a password prompt."""
70-
return click.prompt(prompt, hide_input=True, default=default)
70+
password = click.prompt(prompt, hide_input=True, default=default)
71+
72+
# https://github.com/softlayer/softlayer-python/issues/1436
73+
# click.prompt uses python's getpass() in the background
74+
# https://github.com/python/cpython/blob/3.9/Lib/getpass.py#L97
75+
# In windows, shift+insert actually inputs the below 2 characters
76+
# If we detect those 2 characters, need to manually read from the clipbaord instead
77+
# https://stackoverflow.com/questions/101128/how-do-i-read-text-from-the-clipboard
78+
if password == 'àR':
79+
# tkinter is a built in python gui, but it has clipboard reading functions.
80+
from tkinter import Tk
81+
tk_manager = Tk()
82+
password = tk_manager.clipboard_get()
83+
# keep the window from showing
84+
tk_manager.withdraw()
85+
return password
7186

7287
# Command loading methods
7388
def list_commands(self, *path):

tests/CLI/environment_tests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ def test_getpass(self, prompt_mock):
5555
prompt_mock.assert_called_with('input', default=None, hide_input=True)
5656
self.assertEqual(prompt_mock(), r)
5757

58+
@mock.patch('click.prompt')
59+
@mock.patch('tkinter.Tk.clipboard_get')
60+
def test_getpass_issues1436(self, tk, prompt_mock):
61+
tk.return_value = 'test_from_clipboard'
62+
prompt_mock.return_value = 'àR'
63+
r = self.env.getpass('input')
64+
prompt_mock.assert_called_with('input', default=None, hide_input=True)
65+
self.assertEqual('test_from_clipboard', r)
66+
5867
def test_resolve_alias(self):
5968
self.env.aliases = {'aliasname': 'realname'}
6069
r = self.env.resolve_alias('aliasname')

0 commit comments

Comments
 (0)