Skip to content
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

Correctly handle keyring auth subprocess newlines on Windows #11759

Merged
merged 1 commit into from
Jan 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/pip/_internal/network/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,15 @@ def _get_password(self, service_name: str, username: str) -> Optional[str]:
)
if res.returncode:
return None
return res.stdout.decode("utf-8").strip("\n")
return res.stdout.decode("utf-8").strip(os.linesep)

def _set_password(self, service_name: str, username: str, password: str) -> None:
"""Mirror the implementation of keyring.set_password using cli"""
if self.keyring is None:
return None

cmd = [self.keyring, "set", service_name, username]
input_ = password.encode("utf-8") + b"\n"
input_ = (password + os.linesep).encode("utf-8")
env = os.environ.copy()
env["PYTHONIOENCODING"] = "utf-8"
res = subprocess.run(cmd, input=input_, env=env)
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/test_network_auth.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import functools
import os
import sys
from typing import Any, Dict, Iterable, List, Optional, Tuple

Expand Down Expand Up @@ -360,7 +361,7 @@ def __call__(
self.returncode = 1
else:
# Passwords are returned encoded with a newline appended
self.stdout = password.encode("utf-8") + b"\n"
self.stdout = (password + os.linesep).encode("utf-8")

if cmd[1] == "set":
assert stdin is None
Expand All @@ -369,7 +370,7 @@ def __call__(
assert input is not None

# Input from stdin is encoded
self.set_password(cmd[2], cmd[3], input.decode("utf-8").strip("\n"))
self.set_password(cmd[2], cmd[3], input.decode("utf-8").strip(os.linesep))

return self

Expand Down