Skip to content

fix(gateway): reject a connection string with a bad port as 400, not 500 - #736

Open
leninworld wants to merge 1 commit into
HarnessRouter:mainfrom
leninworld:fix-gateway-db-connection-string-bad-port
Open

leninworld wants to merge 1 commit into
HarnessRouter:mainfrom
leninworld:fix-gateway-db-connection-string-bad-port

Conversation

@leninworld

Copy link
Copy Markdown
Contributor

The bug

A database connection string with a bad port, such as postgresql://u:p@host:abc/db or postgresql://u:p@host:99999/db, makes the gateway fail with an unhandled ValueError (HTTP 500) instead of the 400 invalid_connection_string it returns for other malformed strings. It happens on kit launch with a database and on POST /v1/mcp-test/database, which both go through _db_validate → _db_parse.

_db_parse already wraps urlparse in try/except for that 400, but urlparse doesn't check the port. u.port raises lazily ("Port could not be cast to integer value as 'abc'", "Port out of range 0-65535"), at host = u.hostname + (f":{u.port}" ...), outside the try.

Expected behavior

The same 400 invalid_connection_string error, on the connection_string field, saying what is wrong: here, the port. Like the other messages, it must not echo the connection string, since it holds the password.

The fix

_db_parse reads u.port once inside try/except ValueError and raises uhp_error(400, "invalid_connection_string", "The port must be a number from 1 to 65535.", "connection_string"). An explicit port 0 gets the same 400. Before, it was dropped from the host without a word. Nothing else changes.

Verification

From the repo root, with Python 3.12 and pip install -r gateway/requirements.txt pytest pytest-asyncio jsonschema (as in tests.yml):

  • New test test_an_invalid_connection_string_port_is_a_safe_error in gateway/tests/test_database_mcp.py is parametrized over abc, 99999 and 0. It launches a kit with that connection string and expects a 400 with code invalid_connection_string and no password in the message.
  • Without the fix, all three cases fail. abc and 99999 raise ValueError: Port could not be cast to integer value as 'abc' / Port out of range 0-65535, and 0 is accepted with a 200. With the fix, all three pass.
  • python -m pytest gateway/tests/test_database_mcp.py -q: 57 passed, 15 skipped (the skipped tests need a local PostgreSQL/MySQL).
  • Full python -m pytest gateway/tests -q (macOS, Python 3.12.16): 3034 passed, 31 skipped.

Not run: the console checks, since ui/ is unchanged, and the Docker self-hosted flow.

urlparse does not validate the port; u.port raises ValueError lazily
for a non-numeric or out-of-range port, outside _db_parse's try, so
kit launch and the database test endpoint answered 500. Read the port
once and return the same 400 invalid_connection_string the other
malformed strings get; an explicit port 0 is rejected too instead of
being dropped.
@vercel

vercel Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
unified-harness-protocol Ready Ready Preview Nov 23, 2026 9:02pm UTC

Request Review

This branch was successfully deployed

1 active deployment
Preview — 890eeaf6 Deployed Nov 23, 2026 by vercel[bot]
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.

1 participant