Skip to content

Commit 04d7e27

Browse files
scheelsigmavirus24
scheel
authored andcommitted
Sanitize URLs for logging/display purposes.
1 parent fb9fe50 commit 04d7e27

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

twine/commands/upload.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import fnmatch
1818
import logging
1919
import os.path
20+
import re
2021
from typing import Dict, List, NamedTuple, cast
2122

2223
import requests
@@ -148,6 +149,26 @@ def _split_inputs(
148149
return Inputs(dists, signatures, attestations_by_dist)
149150

150151

152+
def _sanitize_url(url) -> str:
153+
"""
154+
Sanitize URLs, removing any user:password combinations and replacing them with
155+
asterisks. Returns the original URL if the string is a non-matching pattern.
156+
157+
:param url:
158+
str containing a URL to sanitize.
159+
160+
return:
161+
str either sanitized or as entered depending on pattern match.
162+
"""
163+
pattern = "(.*https?://)(\w+:\w+)@(\w+\..*)"
164+
m = re.match(pattern, url)
165+
if m:
166+
newurl = f"{m.group(1)}*****:*****@{m.group(3)}"
167+
return newurl
168+
else:
169+
return url
170+
171+
151172
def upload(upload_settings: settings.Settings, dists: List[str]) -> None:
152173
"""Upload one or more distributions to a repository, and display the progress.
153174
@@ -189,7 +210,7 @@ def upload(upload_settings: settings.Settings, dists: List[str]) -> None:
189210
# Determine if the user has passed in pre-signed distributions or any attestations.
190211
uploads, signatures, attestations_by_dist = _split_inputs(dists)
191212

192-
print(f"Uploading distributions to {repository_url}")
213+
print(f"Uploading distributions to {_sanitize_url(repository_url)}")
193214

194215
packages_to_upload = [
195216
_make_package(

0 commit comments

Comments
 (0)