From 86f57fb05d4ff331fa43ac06cfb846df06c18d63 Mon Sep 17 00:00:00 2001 From: "Vladimir E. Tkachuk" Date: Thu, 1 Oct 2026 12:37:48 +1000 Subject: [PATCH] fix(kernel): drop XHTTP ranges the core would panic on --- service/kernel/serverObj/links_test.go | 87 ++++++++++++++++++++++++++ service/kernel/serverObj/v2ray.go | 20 ++++-- 2 files changed, 101 insertions(+), 6 deletions(-) diff --git a/service/kernel/serverObj/links_test.go b/service/kernel/serverObj/links_test.go index 6397ba68..3d351dba 100644 --- a/service/kernel/serverObj/links_test.go +++ b/service/kernel/serverObj/links_test.go @@ -451,3 +451,90 @@ func TestVlessXHTTPPrefersOwnParamsOver3xUI(t *testing.T) { t.Errorf("XHTTPHeaders = %q, want the own parameter's JSON", v.XHTTPHeaders) } } + +// the core panics on a scMaxEachPostBytes with a non-positive minimum, a +// negative scMaxBufferedPosts and a negative xPaddingBytes, so such values +// must not reach the generated config; they stay visible in the model. +func TestVlessXHTTPNegativeRangesDoNotReachConfig(t *testing.T) { + link := "vless://b831381d-6324-4d53-ad4f-8cda48b30811@1.2.3.4:443?encryption=none&type=xhttp&xhttpMode=packet-up" + + "&xPaddingBytesFrom=-200&xPaddingBytesTo=1500&scMaxEachPostBytesFrom=-5&scMaxEachPostBytesTo=10&scMaxBufferedPosts=-3#node" + obj, err := NewFromLink("vless", link) + if err != nil { + t.Fatal(err) + } + v := obj.(*V2Ray) + if v.XPaddingBytesFrom != -200 || v.XPaddingBytesTo != 1500 { + t.Errorf("XPaddingBytes = %d-%d, want -200-1500 (the model keeps the value)", v.XPaddingBytesFrom, v.XPaddingBytesTo) + } + if v.ScMaxEachPostBytesFrom != -5 || v.ScMaxEachPostBytesTo != 10 { + t.Errorf("ScMaxEachPostBytes = %d-%d, want -5-10 (the model keeps the value)", v.ScMaxEachPostBytesFrom, v.ScMaxEachPostBytesTo) + } + if v.ScMaxBufferedPosts != -3 { + t.Errorf("ScMaxBufferedPosts = %d, want -3 (the model keeps the value)", v.ScMaxBufferedPosts) + } + cfg, err := obj.Configuration(PriorInfo{Tag: "t"}) + if err != nil { + t.Fatalf("Configuration: %v", err) + } + b, err := json.Marshal(cfg.CoreOutbound.StreamSettings.XHTTPSettings) + if err != nil { + t.Fatalf("marshal: %v", err) + } + for _, absent := range []string{`"xPaddingBytes"`, `"scMaxEachPostBytes"`, `"scMaxBufferedPosts"`} { + if strings.Contains(string(b), absent) { + t.Errorf("generated config carries a value the core refuses, %s: %s", absent, b) + } + } +} + +// a zero minimum is what the core accepts for xPaddingBytes (no padding), so +// it must still reach the config. +func TestVlessXHTTPZeroMinimumPaddingIsEmitted(t *testing.T) { + link := "vless://b831381d-6324-4d53-ad4f-8cda48b30811@1.2.3.4:443?encryption=none&type=xhttp&xhttpMode=packet-up&xPaddingBytesFrom=0&xPaddingBytesTo=100#node" + obj, err := NewFromLink("vless", link) + if err != nil { + t.Fatal(err) + } + cfg, err := obj.Configuration(PriorInfo{Tag: "t"}) + if err != nil { + t.Fatalf("Configuration: %v", err) + } + b, err := json.Marshal(cfg.CoreOutbound.StreamSettings.XHTTPSettings) + if err != nil { + t.Fatalf("marshal: %v", err) + } + if !strings.Contains(string(b), `"xPaddingBytes":"0-100"`) { + t.Errorf("generated config lacks the 0-100 padding range: %s", b) + } +} + +// a link value above the int32 max truncates to a negative endpoint when the +// config is built, which the core panics on; the bound must be checked on the +// truncated value, so an out-of-int32 value must not reach the config. +func TestVlessXHTTPInt32OverflowRangesDoNotReachConfig(t *testing.T) { + link := "vless://b831381d-6324-4d53-ad4f-8cda48b30811@1.2.3.4:443?encryption=none&type=xhttp&xhttpMode=packet-up" + + "&xPaddingBytesFrom=3000000000&xPaddingBytesTo=1500" + + "&scMaxEachPostBytesFrom=3000000000&scMaxEachPostBytesTo=10#node" + obj, err := NewFromLink("vless", link) + if err != nil { + t.Fatal(err) + } + v := obj.(*V2Ray) + if v.ScMaxEachPostBytesFrom != 3000000000 || v.XPaddingBytesFrom != 3000000000 { + t.Errorf("model = %d-%d / %d-%d, want the 64-bit link values kept", + v.ScMaxEachPostBytesFrom, v.ScMaxEachPostBytesTo, v.XPaddingBytesFrom, v.XPaddingBytesTo) + } + cfg, err := obj.Configuration(PriorInfo{Tag: "t"}) + if err != nil { + t.Fatalf("Configuration: %v", err) + } + b, err := json.Marshal(cfg.CoreOutbound.StreamSettings.XHTTPSettings) + if err != nil { + t.Fatalf("marshal: %v", err) + } + for _, absent := range []string{`"xPaddingBytes"`, `"scMaxEachPostBytes"`} { + if strings.Contains(string(b), absent) { + t.Errorf("generated config carries a value the core refuses, %s: %s", absent, b) + } + } +} diff --git a/service/kernel/serverObj/v2ray.go b/service/kernel/serverObj/v2ray.go index fa97b209..3ea624e2 100644 --- a/service/kernel/serverObj/v2ray.go +++ b/service/kernel/serverObj/v2ray.go @@ -725,21 +725,29 @@ func (v *V2Ray) Configuration(info PriorInfo) (c Configuration, err error) { xs.Headers = hdrs } } - // Range configs - if v.ScMaxEachPostBytesFrom != 0 || v.ScMaxEachPostBytesTo != 0 { - xs.ScMaxEachPostBytes = &coreObj.XHTTPRangeConfig{From: int32(v.ScMaxEachPostBytesFrom), To: int32(v.ScMaxEachPostBytesTo)} + // Range configs. The core panics on a non-positive + // scMaxEachPostBytes, a negative scMaxBufferedPosts (channel + // size) and a negative xPaddingBytes (repeat count); emit those + // only when the core accepts them, and let its defaults apply + // otherwise. The bounds are checked on the int32 the core + // receives, since the model keeps 64-bit values and a link value + // out of int32 range would truncate negative. Negative values of + // the other fields mean nothing to the core (a skipped sleep, a + // disabled limit) and pass through. + if from, to := int32(v.ScMaxEachPostBytesFrom), int32(v.ScMaxEachPostBytesTo); min(from, to) > 0 { + xs.ScMaxEachPostBytes = &coreObj.XHTTPRangeConfig{From: from, To: to} } if v.ScMinPostsIntervalFrom != 0 || v.ScMinPostsIntervalTo != 0 { xs.ScMinPostsIntervalMs = &coreObj.XHTTPRangeConfig{From: int32(v.ScMinPostsIntervalFrom), To: int32(v.ScMinPostsIntervalTo)} } - if v.ScMaxBufferedPosts != 0 { + if v.ScMaxBufferedPosts > 0 { xs.ScMaxBufferedPosts = int64(v.ScMaxBufferedPosts) } if v.ScStreamUpServerFrom != 0 || v.ScStreamUpServerTo != 0 { xs.ScStreamUpServerSecs = &coreObj.XHTTPRangeConfig{From: int32(v.ScStreamUpServerFrom), To: int32(v.ScStreamUpServerTo)} } - if v.XPaddingBytesFrom != 0 || v.XPaddingBytesTo != 0 { - xs.XPaddingBytes = &coreObj.XHTTPRangeConfig{From: int32(v.XPaddingBytesFrom), To: int32(v.XPaddingBytesTo)} + if from, to := int32(v.XPaddingBytesFrom), int32(v.XPaddingBytesTo); (from != 0 || to != 0) && min(from, to) >= 0 { + xs.XPaddingBytes = &coreObj.XHTTPRangeConfig{From: from, To: to} } // Xmux xmux := &coreObj.XHTTPXmux{HKeepAlivePeriod: v.XmuxHKeepAlive}