fix(xray): bound what the in-process config loader is asked to expand

The panel runs xray-core's conf.Build() in its own process on inbounds
and on outbounds that come from pushes and subscriptions. Some values
make it allocate or loop without bound (an XHTTP sessionIDLength, udphop
port lists, any number of finalmask entries, each xmc one costing an RSA
key), and certificate paths are read whole whatever they name.

Refuse those before any build: sessionIDLength above 1024, more than one
port range's worth of udphop ports per stream, more than 16 masks per
stream, more than 64 xmc masks per outbound subscription, and certificate
or key files that are not regular files of at most 1 MiB. Inbound saves,
the TUIC readers and the certificate-pin helper apply the file rule too;
the pin helper also stops looking at paths that name files on a node.
This commit is contained in:
MHSanaei
2026-10-31 08:43:30 +00:00
parent db20e491e6
commit 61ee202750
12 changed files with 390 additions and 2 deletions
+31 -1
View File
@@ -486,9 +486,14 @@ func (s *OutboundSubscriptionService) fetchOutbounds(sub *model.OutboundSubscrip
// persisted (filtered) list, so positions must be counted in that same list.
var dropped []string
ownPorts := xrayOwnPorts()
xmcBudget := maxSubscriptionXMCMasks
keptLinks, keptIdentities := parsed[:0], identities[:0]
for i, ob := range parsed {
if reason := internalDialTarget(map[string]any(ob), sub.AllowPrivate, ownPorts); reason != "" {
reason := internalDialTarget(map[string]any(ob), sub.AllowPrivate, ownPorts)
if reason == "" && !spendXMCBudget(map[string]any(ob), &xmcBudget) {
reason = fmt.Sprintf("the subscription uses more than %d xmc masks", maxSubscriptionXMCMasks)
}
if reason != "" {
logger.Warningf("outbound sub %d: dropping outbound %.64q: %s", sub.Id, ob["tag"], reason)
dropped = append(dropped, fmt.Sprintf("%.64s: %s", ob["tag"], reason))
continue
@@ -531,6 +536,27 @@ func (s *OutboundSubscriptionService) fetchOutbounds(sub *model.OutboundSubscrip
return fetched, nil
}
// maxSubscriptionXMCMasks bounds the RSA keys one subscription costs the core per build;
// the masks can be spread over links, so a per-stream cap alone does not bound it.
const maxSubscriptionXMCMasks = 64
func spendXMCBudget(outbound any, budget *int) bool {
ob, ok := outbound.(map[string]any)
if !ok {
return true
}
stream, err := json.Marshal(ob["streamSettings"])
if err != nil {
return true
}
n := xray.CountXMCMasks(stream)
if n > *budget {
return false
}
*budget -= n
return true
}
// withoutRequestURL keeps only the host of the URL net/url and net/http quote in their
// errors: a subscription URL is a credential, and these errors are logged and stored.
func withoutRequestURL(err error) error {
@@ -652,6 +678,7 @@ func (s *OutboundSubscriptionService) activeOutboundsSplit() (prepend []any, app
}
ownPorts := xrayOwnPorts()
for _, sub := range subs {
xmcBudget := maxSubscriptionXMCMasks
if strings.TrimSpace(sub.LastFetchedOutbounds) == "" {
continue
}
@@ -663,6 +690,9 @@ func (s *OutboundSubscriptionService) activeOutboundsSplit() (prepend []any, app
// Rows stored before the fetch refused these, or while allowPrivate was on.
arr = slices.DeleteFunc(arr, func(ob any) bool {
reason := internalDialTarget(ob, sub.AllowPrivate, ownPorts)
if reason == "" && !spendXMCBudget(ob, &xmcBudget) {
reason = "over the subscription's xmc mask budget"
}
if reason != "" {
logger.Warningf("outbound sub %d: dropping a stored outbound: %s", sub.Id, reason)
}