mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-09-17 15:47:14 +00:00
fix(inbounds): reject missing TLS certificates before saving (#6429)
An inbound could be saved with security "tls" and a certificate row carrying neither a file path nor inline content. Nothing rejected it, so the row reached xray-core, whose readFileOrString fails with "both file and bytes are empty" and takes the whole config build down with it — every other inbound included. Validate the credentials on both sides of the wire. validateInboundTLSCertificates follows xray's file-over-inline precedence, requires a private key for every non-verify certificate and insists on at least one server certificate, so a verify-only CA list no longer passes as a server config. The inbound form's Zod schema enforces the same rules per field and serializes only the editor mode the operator actually used, and a failed save jumps to the Security tab naming the certificate row that broke. On update the guard is scoped to a real TLS edit. A row already stored incomplete is grandfathered: it stays editable, and only a save that breaks a previously valid block is refused. A sub-node stores whatever the master pushes, and Remote.UpdateInbound falls back to AddInbound when the node does not yet hold the tag, so a grandfathered row could otherwise never be deployed or re-seeded — the rejection is swallowed to a logger.Debug line and the node stays on a stale config while the panel shows the client as cut off. The controller now marks a node-sync request (mTLS or a node-sync token) on a per-request copy of InboundService, and the guard steps aside for it on both add and update: the row was judged where the operator acted, and a node that refuses it only falls out of sync. Operator and admin-token saves are held to the guard as before. The security union is parameterised on its tlsSettings branch instead of copied, and tlsCertUsesFiles is the one file-vs-inline inference shared by the form schema and the adapter, so the mode the editor opens in and the pair of fields the save serializes cannot drift apart.
This commit is contained in:
@@ -34,6 +34,9 @@ import (
|
||||
type InboundService struct {
|
||||
clientService ClientService
|
||||
fallbackService FallbackService
|
||||
// FromNodeSync marks a master push: the row was validated where the operator
|
||||
// acted, and a node that refuses it only falls out of sync.
|
||||
FromNodeSync bool
|
||||
}
|
||||
|
||||
func normalizeTrafficResetDay(day int) int {
|
||||
@@ -610,6 +613,64 @@ func canonicalizeStreamNetworkKey(streamSettings string) string {
|
||||
return string(out)
|
||||
}
|
||||
|
||||
// validateInboundTLSCertificates rejects incomplete TLS credentials before a save
|
||||
// can restart Xray. File paths belong to the node, so only presence is checked.
|
||||
func validateInboundTLSCertificates(streamSettings string) error {
|
||||
if strings.TrimSpace(streamSettings) == "" {
|
||||
return nil
|
||||
}
|
||||
var stream struct {
|
||||
Security string `json:"security"`
|
||||
TLSSettings json.RawMessage `json:"tlsSettings"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(streamSettings), &stream); err != nil {
|
||||
return common.NewError("Invalid inbound stream settings: ", err)
|
||||
}
|
||||
if !strings.EqualFold(stream.Security, "tls") {
|
||||
return nil
|
||||
}
|
||||
var settings struct {
|
||||
Certificates []struct {
|
||||
CertificateFile string `json:"certificateFile"`
|
||||
KeyFile string `json:"keyFile"`
|
||||
Certificate []string `json:"certificate"`
|
||||
Key []string `json:"key"`
|
||||
Usage string `json:"usage"`
|
||||
} `json:"certificates"`
|
||||
}
|
||||
if len(stream.TLSSettings) > 0 {
|
||||
if err := json.Unmarshal(stream.TLSSettings, &settings); err != nil {
|
||||
return common.NewError("Invalid inbound TLS settings: ", err)
|
||||
}
|
||||
}
|
||||
hasServerCertificate := false
|
||||
for i, cert := range settings.Certificates {
|
||||
// Match Xray's file-over-inline precedence for each credential.
|
||||
certificate := cert.CertificateFile
|
||||
if certificate == "" {
|
||||
certificate = strings.Join(cert.Certificate, "\n")
|
||||
}
|
||||
if strings.TrimSpace(certificate) == "" {
|
||||
return common.NewErrorf("TLS certificate %d is missing. Configure a certificate file path or certificate content before saving the inbound.", i+1)
|
||||
}
|
||||
if strings.EqualFold(cert.Usage, "verify") {
|
||||
continue
|
||||
}
|
||||
key := cert.KeyFile
|
||||
if key == "" {
|
||||
key = strings.Join(cert.Key, "\n")
|
||||
}
|
||||
if strings.TrimSpace(key) == "" {
|
||||
return common.NewErrorf("TLS certificate %d is missing its private key. Configure a private key file path or private key content before saving the inbound.", i+1)
|
||||
}
|
||||
hasServerCertificate = true
|
||||
}
|
||||
if !hasServerCertificate {
|
||||
return common.NewError("TLS requires a server certificate and private key. Configure an encipherment or issue certificate before saving the inbound.")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// finalMaskRealityTcpMasks returns the stream's finalmask.tcp masks when the
|
||||
// stream uses REALITY security, or nil otherwise. A non-empty result means
|
||||
// this stream carries the finalmask+REALITY combination that panics
|
||||
@@ -968,6 +1029,11 @@ func (s *InboundService) AddInbound(inbound *model.Inbound) (*model.Inbound, boo
|
||||
inbound.TrafficResetDay = normalizeTrafficResetDay(inbound.TrafficResetDay)
|
||||
// Normalize streamSettings based on protocol
|
||||
s.normalizeStreamSettings(inbound)
|
||||
if !s.FromNodeSync {
|
||||
if err := validateInboundTLSCertificates(inbound.StreamSettings); err != nil {
|
||||
return inbound, false, err
|
||||
}
|
||||
}
|
||||
if err := validateFinalMaskRealityCombo(inbound.StreamSettings); err != nil {
|
||||
return inbound, false, err
|
||||
}
|
||||
@@ -1520,6 +1586,15 @@ func (s *InboundService) UpdateInbound(inbound *model.Inbound) (*model.Inbound,
|
||||
if err != nil {
|
||||
return inbound, false, err
|
||||
}
|
||||
// Grandfather a row that was already stored incomplete so it stays editable;
|
||||
// only a save that breaks a previously valid TLS block is refused.
|
||||
if !s.FromNodeSync {
|
||||
if err := validateInboundTLSCertificates(inbound.StreamSettings); err != nil {
|
||||
if validateInboundTLSCertificates(oldInbound.StreamSettings) == nil {
|
||||
return inbound, false, err
|
||||
}
|
||||
}
|
||||
}
|
||||
// Restore the stored NodeID before the port-conflict check so a node inbound
|
||||
// stays scoped to its own node (the payload's nodeId is unreliable, often absent).
|
||||
inbound.NodeID = oldInbound.NodeID
|
||||
|
||||
Reference in New Issue
Block a user