fix(xray): reject configs xray-core refuses, and check the fixtures against it

The frontend's golden fixtures are the panel's model of an xray config, but
nothing ever asked xray-core whether it would accept them: the snapshots only
prove the Zod schemas agree with themselves. Building every fixture through the
same config builders the panel hands its config to — conf.InboundDetourConfig
for the full-config and AddInbound paths, conf.RouterConfig for
ApplyRoutingConfig, conf.DNSConfig for the dns section — found seven the core
refuses, three of them reachable from the panel's own UI. A refusal is not
scoped to one inbound: the config fails to load and every inbound stays down.

Hysteria: xray-core builds version 2 only, in both the protocol settings and
the transport settings, but the inbound settings schema accepted any version
from 1 up and its comment claimed upstream still supported v1. Both fixtures
carried version 1. The schema now pins 2, GenXrayInboundConfig heals stored
rows on the way out the way it already heals shadowsocks ciphers and wireguard
peers, and the share link drops the dead hysteria:// scheme — the subscription
server already emitted hysteria2:// for the same inbound.

XHTTP uplinkDataPlacement: both transport forms offered "query", which the core
has never accepted for that field (auto and body always, cookie and header in
packet-up mode). Replaced with auto, which was missing, and the default label
now names auto rather than body.

FinalMask items: switching an item to the rand-driven array kind wrote
packet:[] next to the rand. xray-core counts an empty array as a packet and
every item kind is exclusive, so noise answers "len(item.Packet) > 0 &&
item.Rand.To > 0" and header-custom "exactly one item kind must be set". The
editor now clears the packet, and GetXrayConfig strips the residue from rows
already saved with it.

The remaining four were stale fixtures: an xmc mask still on the usernames
shape v26.7.28 replaced with profiles, a fragment mask with no length, and
header-custom and noise items passing an array to the string packet kind — all
shapes the panel's own editors cannot produce.

golden_fixtures_xray_test.go keeps this from drifting again: every fixture in
every category is built through xray-core on each run, with a self-signed pair
standing in for the deployment certificate paths, so the next core bump reports
which fixture it broke.
This commit is contained in:
Sanaei
2026-07-28 14:43:55 +02:00
parent fea6a20f7c
commit dc6a16019e
25 changed files with 931 additions and 69 deletions
@@ -0,0 +1,151 @@
package model
import (
"encoding/json"
"strings"
"testing"
)
func settingsVersion(t *testing.T, settings string) any {
t.Helper()
var parsed map[string]any
if err := json.Unmarshal([]byte(settings), &parsed); err != nil {
t.Fatalf("unmarshal settings: %v", err)
}
return parsed["version"]
}
func TestHealHysteriaVersion(t *testing.T) {
tests := []struct {
name string
settings string
wantChanged bool
wantVersion any
}{
{
name: "legacy v1 row",
settings: `{"version":1,"clients":[{"auth":"tok","email":"a@x"}]}`,
wantChanged: true,
wantVersion: float64(2),
},
{
name: "no version at all",
settings: `{"clients":[{"auth":"tok","email":"a@x"}]}`,
wantChanged: true,
wantVersion: float64(2),
},
{
name: "already v2",
settings: `{"version":2,"clients":[]}`,
wantChanged: false,
wantVersion: float64(2),
},
{
name: "version as a string",
settings: `{"version":"1","clients":[]}`,
wantChanged: true,
wantVersion: float64(2),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
healed, changed := HealHysteriaVersion(tt.settings)
if changed != tt.wantChanged {
t.Fatalf("changed = %v, want %v", changed, tt.wantChanged)
}
if got := settingsVersion(t, healed); got != tt.wantVersion {
t.Fatalf("version = %#v, want %#v", got, tt.wantVersion)
}
})
}
}
func TestHealHysteriaVersionKeepsClients(t *testing.T) {
healed, changed := HealHysteriaVersion(`{"version":1,"clients":[{"auth":"tok","email":"a@x"}]}`)
if !changed {
t.Fatal("a v1 row must be healed")
}
if !strings.Contains(healed, `"auth": "tok"`) || !strings.Contains(healed, `"email": "a@x"`) {
t.Fatalf("healing dropped client data: %s", healed)
}
}
func TestHealHysteriaVersionLeavesUnparsableSettings(t *testing.T) {
const broken = `{"version":1,`
healed, changed := HealHysteriaVersion(broken)
if changed || healed != broken {
t.Fatalf("unparsable settings must be left alone, got changed=%v %q", changed, healed)
}
if healed, changed := HealHysteriaVersion(""); changed || healed != "" {
t.Fatalf("empty settings must be left alone, got changed=%v %q", changed, healed)
}
}
func TestHealHysteriaStreamVersion(t *testing.T) {
healed, changed := HealHysteriaStreamVersion(`{"network":"hysteria","hysteriaSettings":{"version":1,"udpIdleTimeout":60}}`)
if !changed {
t.Fatal("a v1 transport must be healed")
}
var parsed map[string]any
if err := json.Unmarshal([]byte(healed), &parsed); err != nil {
t.Fatalf("unmarshal: %v", err)
}
hysteria, _ := parsed["hysteriaSettings"].(map[string]any)
if hysteria["version"] != float64(2) {
t.Fatalf("version = %#v, want 2", hysteria["version"])
}
if hysteria["udpIdleTimeout"] != float64(60) {
t.Fatalf("healing dropped transport settings: %#v", hysteria)
}
}
func TestHealHysteriaStreamVersionWithoutHysteriaSettings(t *testing.T) {
const stream = `{"network":"tcp","tcpSettings":{}}`
healed, changed := HealHysteriaStreamVersion(stream)
if changed || healed != stream {
t.Fatalf("a stream without hysteriaSettings must be left alone, got changed=%v %q", changed, healed)
}
}
// TestGenXrayInboundConfigHealsHysteriaVersion is the regression for a stored
// v1 row: xray-core answers "version != 2" and rejects the whole config, so
// every other inbound on the server stays offline until the row is fixed.
func TestGenXrayInboundConfigHealsHysteriaVersion(t *testing.T) {
in := Inbound{
Protocol: Hysteria,
Port: 36715,
Listen: "127.0.0.1",
Tag: "in-hysteria",
Settings: `{"version":1,"clients":[{"auth":"tok","email":"a@x"}]}`,
StreamSettings: `{"network":"hysteria","hysteriaSettings":{"version":1,"udpIdleTimeout":60}}`,
}
cfg := in.GenXrayInboundConfig()
if got := settingsVersion(t, string(cfg.Settings)); got != float64(2) {
t.Fatalf("generated settings.version = %#v, want 2", got)
}
var stream map[string]any
if err := json.Unmarshal(cfg.StreamSettings, &stream); err != nil {
t.Fatalf("unmarshal generated streamSettings: %v", err)
}
hysteria, _ := stream["hysteriaSettings"].(map[string]any)
if hysteria["version"] != float64(2) {
t.Fatalf("generated hysteriaSettings.version = %#v, want 2", hysteria["version"])
}
if !strings.Contains(in.Settings, `"version":1`) {
t.Fatal("the stored row must keep its own value; only the generated config is healed")
}
}
func TestGenXrayInboundConfigLeavesOtherProtocolsAlone(t *testing.T) {
in := Inbound{
Protocol: VLESS,
Port: 443,
Tag: "in-vless",
Settings: `{"clients":[],"decryption":"none"}`,
}
if got := settingsVersion(t, string(in.GenXrayInboundConfig().Settings)); got != nil {
t.Fatalf("a non-hysteria inbound must not gain a version key, got %#v", got)
}
}
+60
View File
@@ -229,6 +229,57 @@ func jsonStringFieldFromRaw(r json.RawMessage) string {
return string(trimmed)
}
// hysteriaConfigVersion is the only hysteria version xray-core builds. Both
// the protocol settings and the transport settings answer anything else with
// "version != 2", and that error rejects the whole config — every other
// inbound on the server goes down with it, not just the hysteria one.
const hysteriaConfigVersion = 2
// HealHysteriaVersion pins a hysteria inbound's settings.version to the
// version xray-core accepts. Rows written before the panel settled on v2, or
// through the API and the raw JSON editor, can still carry the legacy 1 or no
// version at all, either of which stops the core from starting.
func HealHysteriaVersion(settings string) (string, bool) {
return healVersionField(settings, nil)
}
// HealHysteriaStreamVersion does the same for the transport half,
// streamSettings.hysteriaSettings.version, which xray-core validates
// separately. An absent hysteriaSettings object is left alone.
func HealHysteriaStreamVersion(streamSettings string) (string, bool) {
return healVersionField(streamSettings, []string{"hysteriaSettings"})
}
// healVersionField rewrites the "version" key of the object reached by path to
// hysteriaConfigVersion, reporting whether anything changed. A path that does
// not resolve to an object leaves the input untouched.
func healVersionField(raw string, path []string) (string, bool) {
if raw == "" {
return raw, false
}
var parsed map[string]any
if err := json.Unmarshal([]byte(raw), &parsed); err != nil {
return raw, false
}
target := parsed
for _, key := range path {
next, ok := target[key].(map[string]any)
if !ok {
return raw, false
}
target = next
}
if version, ok := target["version"].(float64); ok && version == hysteriaConfigVersion {
return raw, false
}
target["version"] = hysteriaConfigVersion
out, err := json.MarshalIndent(parsed, "", " ")
if err != nil {
return raw, false
}
return string(out), true
}
// StripInboundXhttpClientFields removes xHTTP knobs that belong on the
// client dialer and subscription share-link extras only. xray-core's XHTTP
// inbound listener does not consume them; the panel still stores them on
@@ -298,11 +349,20 @@ func (i *Inbound) GenXrayInboundConfig() *xray.InboundConfig {
if converted, ok := WireguardClientsToPeers(settings); ok {
settings = converted
}
case Hysteria:
if healed, ok := HealHysteriaVersion(settings); ok {
settings = healed
}
}
streamSettings := i.StreamSettings
if stripped, ok := StripInboundXhttpClientFields(streamSettings); ok {
streamSettings = stripped
}
if i.Protocol == Hysteria {
if healed, ok := HealHysteriaStreamVersion(streamSettings); ok {
streamSettings = healed
}
}
return &xray.InboundConfig{
Listen: json_util.RawMessage(listen),
Port: i.Port,