From e236a73a66b140acc16d4e1a100a113d64ca1d41 Mon Sep 17 00:00:00 2001 From: freedomdb <34159843+freedomdb@users.noreply.github.com> Date: Sun, 1 Nov 2026 00:16:00 +0800 Subject: [PATCH] fix: replace bare type assertions in tag unmarshal and WARP API handling (#1310) Two UnmarshalJSON implementations asserted raw["tag"] directly while every sibling type in the same package uses the comma-ok form: database/model/outbounds.go: o.Tag = raw["tag"].(string) database/model/endpoints.go: o.Tag = raw["tag"].(string) A saved outbound/endpoint whose tag is missing or not a string -- hand-written API call, import script, third-party client; the panel UI always sends one -- panicked the api/save and apiv2/save handlers (gin's Recovery turns it into a 500 and a stack trace in the log). Same class as #1296. service/warp.go trusted the Cloudflare client API's response shape the same way, in the WARP registration flow an admin runs from the endpoints page: - rspData["id"] / ["token"] bare assertions - rspData["account"] asserted before the license lookup, so a missing account panicked before the existing ok check ran - warpConfig["peers"][0] indexed without a length check, so a response with no peers panicked - the error path in SetWarpLicense indexed response["errors"][0] bare, so a failed response with an empty or missing errors array panicked instead of returning the error it exists for All of them now use comma-ok assertions and return a warp error, like the license check already did. Regression tests cover the two UnmarshalJSON cases (missing, numeric, null and object tags, plus the normal string case); the WARP paths need the live Cloudflare API, so they are covered by inspection and go vet. Prepared by muse ai --- database/model/endpoints.go | 2 +- database/model/outbounds.go | 2 +- database/model/tag_unmarshal_test.go | 58 ++++++++++++++++++++++++++++ service/warp.go | 30 +++++++++++--- 4 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 database/model/tag_unmarshal_test.go diff --git a/database/model/endpoints.go b/database/model/endpoints.go index 8d73264..b59d684 100644 --- a/database/model/endpoints.go +++ b/database/model/endpoints.go @@ -34,7 +34,7 @@ func (o *Endpoint) UnmarshalJSON(data []byte) error { delete(raw, "id") o.Type, _ = raw["type"].(string) delete(raw, "type") - o.Tag = raw["tag"].(string) + o.Tag, _ = raw["tag"].(string) delete(raw, "tag") // Dropped rather than stored: an older panel sent tls_id alongside the // endpoint, and sing-box rejects the unknown key. `tls` is kept, since it diff --git a/database/model/outbounds.go b/database/model/outbounds.go index 733c1be..54eab6b 100644 --- a/database/model/outbounds.go +++ b/database/model/outbounds.go @@ -23,7 +23,7 @@ func (o *Outbound) UnmarshalJSON(data []byte) error { delete(raw, "id") o.Type, _ = raw["type"].(string) delete(raw, "type") - o.Tag = raw["tag"].(string) + o.Tag, _ = raw["tag"].(string) delete(raw, "tag") // Remaining fields diff --git a/database/model/tag_unmarshal_test.go b/database/model/tag_unmarshal_test.go new file mode 100644 index 0000000..16f4cc0 --- /dev/null +++ b/database/model/tag_unmarshal_test.go @@ -0,0 +1,58 @@ +package model + +import "testing" + +// Regression: Outbound and Endpoint used a bare raw["tag"].(string) +// assertion in UnmarshalJSON while their sibling types used the comma-ok +// form, so a syntactically valid saved object with a missing or +// non-string tag (hand-written API call, import script, third-party +// client) panicked the save handler instead of saving with an empty tag. +func TestOutboundUnmarshalMissingOrBadTag(t *testing.T) { + for _, data := range []string{ + `{"type":"direct"}`, + `{"type":"direct","tag":123}`, + `{"type":"direct","tag":null}`, + `{"type":"direct","tag":{"x":1}}`, + } { + var outbound Outbound + if err := outbound.UnmarshalJSON([]byte(data)); err != nil { + t.Fatalf("unmarshal %s: %v", data, err) + } + if outbound.Tag != "" { + t.Fatalf("unmarshal %s: tag = %q, want empty", data, outbound.Tag) + } + } + + var outbound Outbound + if err := outbound.UnmarshalJSON([]byte(`{"type":"direct","tag":"out"}`)); err != nil { + t.Fatalf("unmarshal valid: %v", err) + } + if outbound.Tag != "out" { + t.Fatalf("tag = %q, want %q", outbound.Tag, "out") + } +} + +func TestEndpointUnmarshalMissingOrBadTag(t *testing.T) { + for _, data := range []string{ + `{"type":"wireguard"}`, + `{"type":"wireguard","tag":123}`, + `{"type":"wireguard","tag":null}`, + `{"type":"wireguard","tag":{"x":1}}`, + } { + var endpoint Endpoint + if err := endpoint.UnmarshalJSON([]byte(data)); err != nil { + t.Fatalf("unmarshal %s: %v", data, err) + } + if endpoint.Tag != "" { + t.Fatalf("unmarshal %s: tag = %q, want empty", data, endpoint.Tag) + } + } + + var endpoint Endpoint + if err := endpoint.UnmarshalJSON([]byte(`{"type":"wireguard","tag":"ep"}`)); err != nil { + t.Fatalf("unmarshal valid: %v", err) + } + if endpoint.Tag != "ep" { + t.Fatalf("tag = %q, want %q", endpoint.Tag, "ep") + } +} diff --git a/service/warp.go b/service/warp.go index 1f03d3b..2fc8d95 100644 --- a/service/warp.go +++ b/service/warp.go @@ -86,9 +86,19 @@ func (s *WarpService) RegisterWarp(ep *model.Endpoint) error { return err } - deviceId := rspData["id"].(string) - token := rspData["token"].(string) - license, ok := rspData["account"].(map[string]interface{})["license"].(string) + deviceId, ok := rspData["id"].(string) + if !ok { + return common.NewError("warp: error accessing device id value") + } + token, ok := rspData["token"].(string) + if !ok { + return common.NewError("warp: error accessing token value") + } + account, ok := rspData["account"].(map[string]interface{}) + if !ok { + return common.NewError("warp: error accessing account value") + } + license, ok := account["license"].(string) if !ok { return common.NewError("warp: error accessing license value") } @@ -111,7 +121,11 @@ func (s *WarpService) RegisterWarp(ep *model.Endpoint) error { addresses, _ := interfaceConfig["addresses"].(map[string]interface{}) v4, _ := addresses["v4"].(string) v6, _ := addresses["v6"].(string) - peer, _ := warpConfig["peers"].([]interface{})[0].(map[string]interface{}) + peerList, _ := warpConfig["peers"].([]interface{}) + if len(peerList) == 0 { + return common.NewError("warp: error accessing peers value") + } + peer, _ := peerList[0].(map[string]interface{}) peerEndpoint, _ := peer["endpoint"].(map[string]interface{})["host"].(string) peerEpAddress, peerEpPort, err := net.SplitHostPort(peerEndpoint) if err != nil { @@ -220,8 +234,12 @@ func (s *WarpService) SetWarpLicense(old_license string, ep *model.Endpoint) err if success, ok := response["success"].(bool); ok && success == false { errorArr, _ := response["errors"].([]interface{}) - errorObj := errorArr[0].(map[string]interface{}) - return common.NewError(errorObj["code"], errorObj["message"]) + if len(errorArr) > 0 { + if errorObj, ok := errorArr[0].(map[string]interface{}); ok { + return common.NewError(errorObj["code"], errorObj["message"]) + } + } + return common.NewError("warp: request failed without an error detail") } return nil