Skip to content

Validate host in SQLAlchemy URL helper#621

Open
hashhar wants to merge 1 commit into
trinodb:masterfrom
hashhar:hashhar/579-sqla-url-error
Open

Validate host in SQLAlchemy URL helper#621
hashhar wants to merge 1 commit into
trinodb:masterfrom
hashhar:hashhar/579-sqla-url-error

Conversation

@hashhar

@hashhar hashhar commented Jul 11, 2026

Copy link
Copy Markdown
Member

Description

trino.sqlalchemy.URL() built the connection string by concatenation and never validated the host argument. Passing a value that carries a scheme, port, path, or credentials (e.g. "https://example.com") produced a malformed URL that SQLAlchemy rejected with an opaque error:

ValueError: invalid literal for int() with base 10: ''

Reject such a host up front with a clear ArgumentError that names the offending value and shows the expected form.

Non-technical explanation

Validate host in SQLAlchemy URL helper. Fixes #579

Release notes

(x) This is not user-visible or docs only and no release notes are required.
( ) Release notes are required, please propose a release note for me.
( ) Release notes are required, with the following suggested text:

* Fix some things. ({issue}`issuenumber`)

trino.sqlalchemy.URL() built the connection string by concatenation and
never validated the host argument. Passing a value that carries a scheme,
port, path, or credentials (e.g. "https://example.com") produced a
malformed URL that SQLAlchemy rejected with an opaque error:

    ValueError: invalid literal for int() with base 10: ''

Reject such a host up front with a clear ArgumentError that names the
offending value and shows the expected form.
Comment thread trino/sqlalchemy/util.py
except ValueError:
# A malformed port raises here.
has_port = True
if urlparse(host).scheme or has_port or parsed.path or parsed.username or parsed.password:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

urlparse(host) may raise ValueError here, are we ok with that? Maybe catch it and wrap in exc.ArgumentError?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though as far as I can see, the errors are mostly raised for malformed IPv6 addresses

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

Port incorrectly processed when using trino.sqlalchemy.URL and sa.create_engine

3 participants