Repository navigation
fix(gateway): reject a connection string with a bad port as 400, not 500 - #736
Open
leninworld wants to merge 1 commit into
Open
leninworld wants to merge 1 commit into
leninworld wants to merge 1 commit into
Conversation
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.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
A database connection string with a bad port, such as
postgresql://u:p@host:abc/dborpostgresql://u:p@host:99999/db, makes the gateway fail with an unhandledValueError(HTTP 500) instead of the 400invalid_connection_stringit returns for other malformed strings. It happens on kit launch with a database and onPOST /v1/mcp-test/database, which both go through_db_validate→_db_parse._db_parsealready wrapsurlparseintry/exceptfor that 400, buturlparsedoesn't check the port.u.portraises lazily ("Port could not be cast to integer value as 'abc'", "Port out of range 0-65535"), athost = u.hostname + (f":{u.port}" ...), outside thetry.Expected behavior
The same 400
invalid_connection_stringerror, on theconnection_stringfield, 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_parsereadsu.portonce insidetry/except ValueErrorand raisesuhp_error(400, "invalid_connection_string", "The port must be a number from 1 to 65535.", "connection_string"). An explicit port0gets 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 intests.yml):test_an_invalid_connection_string_port_is_a_safe_erroringateway/tests/test_database_mcp.pyis parametrized overabc,99999and0. It launches a kit with that connection string and expects a 400 with codeinvalid_connection_stringand no password in the message.abcand99999raiseValueError: Port could not be cast to integer value as 'abc'/Port out of range 0-65535, and0is 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).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.