Skip to content

fix(no-ticket): give a usage error for an api_host or proxy URL that does not parse - #589

Open
cloudsmith-iduffy wants to merge 3 commits into
masterfrom
iduffy/fix-malformed-url-usage-error
Open

cloudsmith-iduffy wants to merge 3 commits into
masterfrom
iduffy/fix-malformed-url-usage-error

Conversation

@cloudsmith-iduffy

Copy link
Copy Markdown
Collaborator

Description

Sentry CLOUDSMITH-CLI-26, 27 and 28 are ValueError: Invalid IPv6 URL from a doctor command that is not on master yet. Master has the same class of problem in released code. Both errors below came from a value that the user typed, but the CLI sent them to Sentry as crashes:

Command Before After
cloudsmith whoami --proxy http://[bad:3128 LocationParseError, exit 1, reported usage error, exit 2
cloudsmith credential-helper install terraform --proxy http://[bad ValueError: Invalid IPv6 URL, exit 1, reported usage error, exit 2
cloudsmith whoami --api-host https://[bad "Failed to parse", exit 0 usage error, exit 2

initialise_session now parses api_host and proxy with urllib3.util.parse_url before it creates the session. That function is the parser that requests uses, so a value that passes the check also works for the requests that follow. A value that does not parse gives:

Error: proxy "http://[bad" is not a valid URL. Check the host and the port.

The check covers values from the command line, the environment and config files. It runs before the untrusted-config guard, which also parses these URLs.

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update
  • Refactoring
  • Other (please describe)

Additional Notes

urllib3 is imported inside validate_url, because test_startup_imports forbids it at module level.

The doctor code that raised CLI-26 to 28 parses upstream URLs, not these options. It needs its own handling before it merges.

🤖 Generated with Claude Code

…does not parse

A proxy such as http://[bad made every API command stop with
LocationParseError, and credential-helper install terraform stop with
ValueError: Invalid IPv6 URL. The CLI sent both to Sentry, but the
cause is the value that the user gave.

Parse api_host and proxy with urllib3, the parser that requests uses,
when the session starts. A value that does not parse now gives a usage
error with exit status 2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 5, 2026 11:00

Copilot AI left a comment

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.

🟡 Changes recommended

A malformed proxy URL in config.ini still causes the root callback to crash before validation because resolve_default_proxy() lets urlsplit() raise an uncaught ValueError.

1 open issue
What changed in this PR

This PR adds early URL parsing to session initialization so malformed API host and proxy options produce usage errors instead of crashes.

Changes:

  • Add a URL validator using a lazy import of urllib3.util.parse_url.
  • Validate session endpoints before the untrusted-config guard.
  • Add malformed-URL regression tests and document the behavior.
File Description
cloudsmith_cli/​cli/​validators.py Add URL parsing with usage-error reporting.
cloudsmith_cli/​cli/​tests/​test_decorators.py Test malformed API host and proxy options.
cloudsmith_cli/​cli/​decorators.py Validate URLs during session initialization.
CHANGELOG.md Document malformed-URL error handling.

Review effort: Balanced

opts.api_headers = kwargs.pop("api_headers")

validators.validate_url("api_host", opts.api_host)
validators.validate_url("proxy", opts.proxy)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Confirmed and fixed. host_matches_suffixes now returns False for a URL that does not parse, so the untrusted-proxy guard rejects it and the session check gives the usage error. test_malformed_proxy_in_cwd_config_is_a_usage_error runs the real main with proxy = http://[bad in the cwd config.ini. It failed with ValueError: Invalid IPv6 URL before the fix.

…nt directory's config

The root callback runs the untrusted-proxy guard before the session
check. host_matches_suffixes let the urlsplit ValueError escape, so the
CLI still crashed and sent a report. A URL that does not parse now
matches no suffix. The guard rejects it, and the session check gives
the usage error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

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.

🟢 Approval recommended

There are no active review issues requiring further changes.

1 open issue

Review effort: Balanced

…/fix-malformed-url-usage-error

# Conflicts:
#	CHANGELOG.md

Co-authored-by: cloudsmith-iduffy <178375997+cloudsmith-iduffy@users.noreply.github.com>

This branch has not been deployed

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants