Non-JSON error responses (e.g. proxy 404 page) surface as "failed to unmarshal response: invalid character"

未关闭 适合新手
#194 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
72/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
go
领域
api, cli

调研方向

问题出在 internal/api/client.go,在检查 resp.StatusCode 之前,响应体就被传给了 json.Unmarshal(大约在第 154 行)。阅读这条响应处理路径,然后用一个返回纯文本 404 的测试服务器复现它。完成的标准是:非 JSON 的错误响应会报告 HTTP 状态码和响应体开头,而不是 JSON 语法错误。在开 PR 之前先与维护者确认,因为该 issue 询问是否会接受这样一个 PR。

由索引模型根据 Issue 内容生成。

描述

We use pangolin ssh (CLI 0.23.0, macOS arm64) against a self-hosted Pangolin that sits behind a reverse proxy. For a few hours one of the proxy's upstreams was misrouted and answered every second request with a plain-text 404 page not found. The CLI reported this as:

SSH error: failed to unmarshal response: invalid character 'p' after top-level value

It took us a while to work out that this was an HTTP 404 from the proxy and not a Pangolin or SSH problem, because the status code and body never show up in the error.

What happens

In internal/api/client.go the response body goes straight into json.Unmarshal (around line 154 on main today), before resp.StatusCode is looked at. So any non-JSON error body, like a proxy 404/502 page or a captive portal, comes out as a JSON syntax error. I'd have expected something like HTTP 404: 404 page not found.

What we have ruled out

The fault was on our side (a stale upstream connection in our proxy), and we've fixed it. The Pangolin server itself was healthy. Requests that reached it got normal JSON replies. This issue is only about the error message the CLI shows when a reply isn't JSON.

Reproduction

Point the CLI at any endpoint that returns a non-JSON body, e.g. a reverse proxy route with no backend that answers 404 page not found, then run any command that calls the API (pangolin ssh sign <resource>).

A guess at a fix: when unmarshalling fails, include resp.StatusCode and the first ~100 bytes of the body in the error, or check for a non-2xx status first. Would you take a PR for that? I had an agent go through the client code with me, so please correct me if I've misread the flow.

主要语言
Go
星标
70
派生
26
平均合并
4 小时 19 分钟
30 天内合并 PR
11

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

fosrl/cli 的其他 Issue

查看 fosrl/cli 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。