Skip to content

Commit a87495a

Browse files
authored
Merge pull request #2446 from sxwangsxwang1/fix-dsn-username-slash
Encode slashes in DSN usernames
2 parents 18f800d + c421e77 commit a87495a

4 files changed

Lines changed: 44 additions & 1 deletion

File tree

‎changelog.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Upcoming (TBD)
33

44
Bug Fixes
55
--------
6+
* Percent-encode slashes in DSN usernames so aliases shown and saved by `/dsn` preserve the user and connection details when reused.
67
* Refresh completions after `CREATE`, `ALTER`, `DROP` or `RENAME` statements that start with a SQL comment.
78
* Reset completions after `USE`, `\u`, `\r` or `connect` statements that start with a SQL comment.
89
* Don't flag a single-table `UPDATE` with a `WHERE` clause as destructive when it starts with a `/* ... */` comment.

‎mycli/AUTHORS.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Contributors:
2525
* Andy Teijelo Pérez
2626
* Angelo Lupo
2727
* Artem Bezsmertnyi
28+
* Arvin Wang
2829
* bitkeen
2930
* bjarnagin
3031
* BuonOmo

‎mycli/packages/special_commands/special_command_utils.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def format_connection_dsn(
166166
vault_password_field: str | None = None,
167167
vault_username_field: str | None = None,
168168
) -> str:
169-
if user_part := urlquote(user or ''):
169+
if user_part := urlquote(user or '', safe=''):
170170
user_part = f'{user_part}@'
171171
host_part = bracket_ipv6_literal(host or 'localhost')
172172
port_part = f':{port}' if port is not None else ''

‎mycli_test/pytests/test_cli_runner.py‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
from pathlib import Path
34
from types import SimpleNamespace
45
from typing import Any
56

@@ -10,6 +11,8 @@
1011
from mycli.packages.credentials.password_sources import (
1112
KNOWN_PASSWORD_SOURCES,
1213
)
14+
from mycli.packages.special_commands.dsn_aliases import DsnAliases
15+
from mycli.packages.special_commands.special_command_utils import format_connection_dsn
1316

1417

1518
class DummyLogger:
@@ -93,6 +96,44 @@ def test_expand_dsn_alias_env_var_returns_none() -> None:
9396
assert cli_runner.expand_dsn_alias_env_var(None, 'prod') is None
9497

9598

99+
@pytest.mark.parametrize('username', ('team/alice', 'team%2Falice', 'name@example.com', '团队/用户'))
100+
@pytest.mark.parametrize('expand_env', (False, True))
101+
@pytest.mark.parametrize('socket', (None, '/tmp/mysql.sock'))
102+
def test_run_from_cli_args_preserves_saved_dsn_username(
103+
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, username: str, expand_env: bool, socket: str | None
104+
) -> None:
105+
dsn = format_connection_dsn(
106+
user=username,
107+
host='db.example.com',
108+
port=3307,
109+
database='prod',
110+
socket=socket,
111+
character_set='utf8mb4',
112+
)
113+
config_file = tmp_path / 'myclirc'
114+
config_file.write_text('[alias_dsn]\n', encoding='utf-8')
115+
aliases = DsnAliases(ConfigObj(str(config_file), encoding='utf-8', list_values=False))
116+
aliases.save('prod', dsn)
117+
saved_config = ConfigObj(str(config_file), encoding='utf-8', list_values=False)
118+
config = default_config()
119+
config['main']['expand_dsn_alias_env_vars'] = str(expand_env)
120+
config['alias_dsn'] = dict(saved_config['alias_dsn'])
121+
client = DummyMyCli(config=config)
122+
cli_args = make_cli_args()
123+
cli_args.dsn = 'prod'
124+
125+
run_with_client(monkeypatch, cli_args, client)
126+
127+
connection = client.connect_calls[-1]
128+
assert (connection['user'], connection['host'], connection['port'], connection['database'], connection['socket']) == (
129+
username,
130+
'db.example.com',
131+
None if socket else 3307,
132+
'prod',
133+
socket,
134+
)
135+
136+
96137
@pytest.mark.parametrize('expand_env', (False, True))
97138
@pytest.mark.parametrize(
98139
('path', 'expected'),

0 commit comments

Comments
 (0)