Skip to content

fix(no-ticket): show an error when the API host returns an HTML page - #560

Open
cloudsmith-iduffy wants to merge 1 commit into
masterfrom
iduffy/fix-html-api-response
Open

cloudsmith-iduffy wants to merge 1 commit into
masterfrom
iduffy/fix-html-api-response

Conversation

@cloudsmith-iduffy

Copy link
Copy Markdown
Collaborator

Description

Sometimes the API host answers with an HTML page and status 200. This happens with a TLS-intercepting proxy login page, a captive portal, or an --api-host that points to a website. The REST client accepted any 2xx body. The SDK then turned the HTML into empty models, and each command failed somewhere different with an unexpected error that also went to Sentry.

The REST client now rejects a 2xx response with a text/html content type. It raises the same status-0 API error that connection failures use, so every command goes through its normal error handling. The v2 client (metadata list, workspace domains) uses the same REST client, so it gets the fix too.

Before:

$ cloudsmith entitlements list my-org/my-repo
Getting list of entitlements for the my-repo repository ... OK
Error: TypeError: '<' not supported between instances of 'NoneType' and 'NoneType'

$ cloudsmith check service
The service version is:  1.0 Error: ValueError: 1.0 is not valid SemVer string

After:

$ cloudsmith entitlements list my-org/my-repo
Getting list of entitlements for the my-repo repository ... ERROR
Failed to get list of entitlements! (status: 0 - Unknown Status)

Detail: The API host returned an HTML page, not an API response. Check the API host and your proxy settings.

I checked these commands against a local server that returns an HTML page. Each one now prints its "Failed to ..." or "Could not ..." message, exits with code 1, and sends no error report: entitlements list, list distros, metrics entitlements, metrics packages, check rates, check service, tokens list, whoami --verbose, list dependencies, tags list, metadata list, and quota history.

Fixes Sentry issues CLOUDSMITH-CLI-D, -F, -N, -W, -V, -A, -T, -S, -Q, -P, and -R.

Type of Change

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

Additional Notes

  • The check looks only at text/html. A JSON API never sends that type, so no endpoint that works today can break. A portal that sends HTML as text/plain or with no content type still gets through. A check for "not JSON" would cover that, but it is a larger change in a shared client.
  • fix(no-ticket): set a default timeout on each API request #538 also edits RestClient.request. This change adds one call after the status check and a new helper at the end of the file, so the two PRs do not touch the same lines.

🤖 Generated with Claude Code

A TLS-intercepting proxy, a captive portal, or a wrong --api-host can
return an HTML page with status 200. The SDK turned the page into empty
models, and each command then stopped with an unexpected error and sent
an error report. The REST client now rejects a 2xx HTML response with
an API error, so commands show the normal "Failed to ..." message and
exit with code 1.

Fixes CLOUDSMITH-CLI-D
Fixes CLOUDSMITH-CLI-F
Fixes CLOUDSMITH-CLI-N
Fixes CLOUDSMITH-CLI-W
Fixes CLOUDSMITH-CLI-V
Fixes CLOUDSMITH-CLI-A
Fixes CLOUDSMITH-CLI-T
Fixes CLOUDSMITH-CLI-S
Fixes CLOUDSMITH-CLI-Q
Fixes CLOUDSMITH-CLI-P
Fixes CLOUDSMITH-CLI-R

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 24, 2026 18:10
@cloudsmith-iduffy
cloudsmith-iduffy marked this pull request as ready for review October 24, 2026 18:13
@cloudsmith-iduffy
cloudsmith-iduffy requested a review from a team as a code owner October 24, 2026 18:13

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

Non-UTF-8 HTML bodies can raise a decoding exception before validation, bypassing the intended API error handling.

1 open issue
What changed in this PR

This PR adds HTML-response validation to the shared REST client so API commands report normal API errors when a proxy, captive portal, or incorrect API host returns an HTML page.

Changes:

  • Reject successful text/html responses with a status-0 API error.
  • Add REST-client and entitlements CLI regression tests.
  • Document the corrected error behavior in the changelog.
File Description
cloudsmith_cli/​core/​tests/​test_rest.py Tests rejection of a successful HTML response.
cloudsmith_cli/​core/​rest.py Adds shared HTML content-type validation.
cloudsmith_cli/​cli/​tests/​commands/​test_entitlements.py Tests normal CLI error handling for HTML responses.
CHANGELOG.md Documents the HTML-response handling fix.

Review effort: Balanced

if not 200 <= rest_resp.status <= 299:
raise ApiException(http_resp=rest_resp)

_raise_for_html(rest_resp)

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