Skip to content

Fix add_query_params dropping port and userinfo from URLs#50

Merged
eaguad1337 merged 1 commit into
masonitedev:5.0from
aleksanm:fix/add-query-params-preserve-port
Jun 12, 2026
Merged

Fix add_query_params dropping port and userinfo from URLs#50
eaguad1337 merged 1 commit into
masonitedev:5.0from
aleksanm:fix/add-query-params-preserve-port

Conversation

@aleksanm

@aleksanm aleksanm commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Problem

masonite.utils.str.add_query_params rebuilds the base URL from urlsplit(url).hostname, which excludes the port and any userinfo. So any URL with a non-default port loses it:

add_query_params("http://127.0.0.1:9000/bucket/key", {})
# -> "http://127.0.0.1/bucket/key"   (the :9000 is gone)

Because Response.redirect(location=...) passes the location through add_query_params, redirecting to a URL on a non-default port produces a broken Location header. I hit this redirecting to a presigned S3/MinIO URL (http://host:9000/...?X-Amz-Signature=...): the port was stripped, so the browser hit port 80 and the download failed.

Fix

Use urlsplit(url).netloc instead of .hostname. netloc preserves host, port, and userinfo — hostname drops the port and userinfo. One line in src/masonite/utils/str.py.

Tests

Added to tests/core/utils/test_str.py:

  • non-default port and userinfo URLs are unchanged by an empty add_query_params(url, {})
  • a regression test asserting the port survives when params are added, and userinfo is preserved

Existing add_query_params test expectations are unaffected (verified).

add_query_params rebuilt the base URL from urlsplit().hostname, which excludes
the port and any userinfo. As a result a redirect to e.g.
http://127.0.0.1:9000/... came back as http://127.0.0.1/..., breaking links to
hosts on non-default ports (e.g. presigned S3/MinIO URLs passed through
Response.redirect()). Use .netloc instead, which preserves host, port and
userinfo.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@eaguad1337 eaguad1337 merged commit e1ce7a3 into masonitedev:5.0 Jun 12, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants