Skip to content

Commit

Permalink
chore: sanitize username password mongodb
Browse files Browse the repository at this point in the history
  • Loading branch information
gersmann committed Jul 20, 2024
1 parent c85028f commit 91c513e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
22 changes: 22 additions & 0 deletions django_mongodb/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from urllib.parse import unquote, urlparse


def _extract_username_password(url: str) -> tuple[str, str, str]:
url_components = urlparse(url)
username, password = url_components.username, url_components.password
if username and password:
url_components = url_components._replace(
netloc=url_components.hostname,
)

return username, unquote(password), url_components.geturl()


def sanitize_client_opts(client_opts):
# Sanitize client options.
if "host" in client_opts:
username, password, host = _extract_username_password(client_opts["host"])
client_opts["username"] = username
client_opts["password"] = password
client_opts["host"] = host
return client_opts
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ classifiers = [
"Intended Audience :: Developers",
"Operating System :: OS Independent",
]
version = "0.27.2"
version = "0.27.4"
description = ""
authors = ["gersmann"]
readme = "README.md"
Expand Down
4 changes: 3 additions & 1 deletion testproject/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

from dotenv import load_dotenv

from django_mongodb.utils import sanitize_client_opts

load_dotenv()

# Build paths inside the project like this: BASE_DIR / 'subdir'.
Expand Down Expand Up @@ -86,7 +88,7 @@
"mongodb": {
"ENGINE": "django_mongodb",
"NAME": "django_mongodb",
"CLIENT": {"host": os.environ.get("MONGODB_URL"), "connect": False},
"CLIENT": sanitize_client_opts({"host": os.environ.get("MONGODB_URL"), "connect": False}),
},
}
DATABASE_ROUTERS = ["testproject.router.DatabaseRouter"]
Expand Down

0 comments on commit 91c513e

Please sign in to comment.