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

Đang mở Phù hợp với người mới
#194 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
72/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
go
Lĩnh vực
api, cli

Hướng nghiên cứu

Vấn đề nằm ở internal/api/client.go, nơi body của response được đưa vào json.Unmarshal (khoảng dòng 154) trước khi resp.StatusCode được kiểm tra. Hãy đọc luồng xử lý response đó, rồi tái hiện nó bằng một test server trả về 404 dạng plain-text. Hoàn thành nghĩa là một phản hồi lỗi không phải JSON báo cáo HTTP status và phần đầu của body thay vì lỗi cú pháp JSON. Xác nhận với các maintainer trước khi mở PR, vì issue hỏi liệu một PR như vậy có được chấp nhận không.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Go
Star
70
Fork
26
Merge trung bình
4 giờ 19 phút
Pull request đã merge (30 ngày)
11

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của fosrl/cli

Tất cả issue của fosrl/cli

Issue tương tự

Thêm issue về Go

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.