Skip to content

Commit

Permalink
Sanitize URLs for logging/display purposes.
Browse files Browse the repository at this point in the history
  • Loading branch information
scheel authored and sigmavirus24 committed May 15, 2024
1 parent fb9fe50 commit 04d7e27
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion twine/commands/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import fnmatch
import logging
import os.path
import re
from typing import Dict, List, NamedTuple, cast

import requests
Expand Down Expand Up @@ -148,6 +149,26 @@ def _split_inputs(
return Inputs(dists, signatures, attestations_by_dist)


def _sanitize_url(url) -> str:
"""
Sanitize URLs, removing any user:password combinations and replacing them with
asterisks. Returns the original URL if the string is a non-matching pattern.
:param url:
str containing a URL to sanitize.
return:
str either sanitized or as entered depending on pattern match.
"""
pattern = "(.*https?://)(\w+:\w+)@(\w+\..*)"
m = re.match(pattern, url)
if m:
newurl = f"{m.group(1)}*****:*****@{m.group(3)}"
return newurl
else:
return url


def upload(upload_settings: settings.Settings, dists: List[str]) -> None:
"""Upload one or more distributions to a repository, and display the progress.
Expand Down Expand Up @@ -189,7 +210,7 @@ def upload(upload_settings: settings.Settings, dists: List[str]) -> None:
# Determine if the user has passed in pre-signed distributions or any attestations.
uploads, signatures, attestations_by_dist = _split_inputs(dists)

print(f"Uploading distributions to {repository_url}")
print(f"Uploading distributions to {_sanitize_url(repository_url)}")

packages_to_upload = [
_make_package(
Expand Down

0 comments on commit 04d7e27

Please sign in to comment.