Skip to content

Commit b1c878b

Browse files
mchadesCopilot
andcommitted
Validate username in basic_auth helper
- Raise ValueError if username is empty - Raise ValueError if username contains a colon character (colon is the RFC 7617 separator between username and password) - Add two unit tests covering both invalid cases Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 1cbb777 commit b1c878b

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/adp_sdk/client/session.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,19 @@ def basic_auth(username: str, password: str = "") -> str:
6767
"""Build a Basic Authorization header value.
6868
6969
Args:
70-
username: The username.
70+
username: The username. Must be non-empty and must not contain a colon.
7171
password: The password (defaults to empty string).
7272
7373
Returns:
7474
A string in the format ``"Basic <base64(username:password)>"``.
75+
76+
Raises:
77+
ValueError: If ``username`` is empty or contains a colon character.
7578
"""
79+
if not username:
80+
raise ValueError("username must not be empty")
81+
if ":" in username:
82+
raise ValueError("username must not contain a colon character")
7683
credentials = base64.b64encode(f"{username}:{password}".encode()).decode()
7784
return f"Basic {credentials}"
7885

tests/test_client_session.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,3 +542,11 @@ def test_basic_auth_round_trip(self) -> None:
542542
_, _, payload = auth.partition(" ")
543543
decoded = base64.b64decode(payload).decode()
544544
self.assertEqual(decoded, "user:pass")
545+
546+
def test_basic_auth_empty_username_raises(self) -> None:
547+
with self.assertRaises(ValueError, msg="username must not be empty"):
548+
basic_auth("")
549+
550+
def test_basic_auth_username_with_colon_raises(self) -> None:
551+
with self.assertRaises(ValueError, msg="username must not contain a colon character"):
552+
basic_auth("user:name", "pass")

0 commit comments

Comments
 (0)