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
This commit is contained in:
freedomdb 2026-11-01 00:16:00 +08:00 • committed by GitHub
parent aba60068ad
commit e236a73a66
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 84 additions and 8 deletions

View file

@ -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

View file

@ -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

View file

@ -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")
}
}

View file

@ -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