forked from Mxmilu666/frp
Merge remote-tracking branch 'upstream/dev' into dev
# Conflicts: # .github/workflows/build-and-push-image.yml # cmd/frpc/sub/verify.go # go.mod # go.sum # pkg/util/version/version.go
This commit is contained in:
@@ -394,6 +394,10 @@ func LoadClientConfigResult(path string, strict bool) (*ClientConfigLoadResult,
|
||||
}
|
||||
}
|
||||
|
||||
if err := validateNoDuplicateNames(result.Proxies, result.Visitors); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -417,6 +421,31 @@ func LoadClientConfig(path string, strict bool) (
|
||||
return result.Common, proxyCfgs, visitorCfgs, result.IsLegacyFormat, nil
|
||||
}
|
||||
|
||||
// validateNoDuplicateNames rejects proxies or visitors that share a name. They are
|
||||
// keyed by name in the config sources, so a duplicate would otherwise be silently
|
||||
// overwritten and never started, with no error or log.
|
||||
func validateNoDuplicateNames(proxies []v1.ProxyConfigurer, visitors []v1.VisitorConfigurer) error {
|
||||
proxyNames := make(map[string]struct{}, len(proxies))
|
||||
for _, p := range proxies {
|
||||
name := p.GetBaseConfig().Name
|
||||
if _, ok := proxyNames[name]; ok {
|
||||
return fmt.Errorf("proxy name [%s] is duplicated", name)
|
||||
}
|
||||
proxyNames[name] = struct{}{}
|
||||
}
|
||||
|
||||
visitorNames := make(map[string]struct{}, len(visitors))
|
||||
for _, v := range visitors {
|
||||
name := v.GetBaseConfig().Name
|
||||
if _, ok := visitorNames[name]; ok {
|
||||
return fmt.Errorf("visitor name [%s] is duplicated", name)
|
||||
}
|
||||
visitorNames[name] = struct{}{}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func CompleteProxyConfigurers(proxies []v1.ProxyConfigurer) []v1.ProxyConfigurer {
|
||||
proxyCfgs := proxies
|
||||
for _, c := range proxyCfgs {
|
||||
|
||||
@@ -17,6 +17,8 @@ package config
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -462,6 +464,111 @@ func TestFilterClientConfigurers_FilterByStartAndEnabled(t *testing.T) {
|
||||
require.Equal("keep", proxies[0].GetBaseConfig().Name)
|
||||
}
|
||||
|
||||
func TestLoadClientConfigResult_DuplicateNames(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
content string
|
||||
errSubstr string
|
||||
}{
|
||||
{
|
||||
name: "duplicate proxy names",
|
||||
content: `
|
||||
serverAddr = "127.0.0.1"
|
||||
serverPort = 7000
|
||||
|
||||
[[proxies]]
|
||||
name = "dup"
|
||||
type = "tcp"
|
||||
localPort = 22
|
||||
remotePort = 6000
|
||||
|
||||
[[proxies]]
|
||||
name = "dup"
|
||||
type = "tcp"
|
||||
localPort = 3306
|
||||
remotePort = 6001
|
||||
`,
|
||||
errSubstr: "proxy name [dup] is duplicated",
|
||||
},
|
||||
{
|
||||
name: "duplicate visitor names",
|
||||
content: `
|
||||
serverAddr = "127.0.0.1"
|
||||
serverPort = 7000
|
||||
|
||||
[[visitors]]
|
||||
name = "dup"
|
||||
type = "stcp"
|
||||
serverName = "a"
|
||||
secretKey = "secret"
|
||||
bindPort = 9001
|
||||
|
||||
[[visitors]]
|
||||
name = "dup"
|
||||
type = "stcp"
|
||||
serverName = "b"
|
||||
secretKey = "secret"
|
||||
bindPort = 9002
|
||||
`,
|
||||
errSubstr: "visitor name [dup] is duplicated",
|
||||
},
|
||||
{
|
||||
name: "unique names",
|
||||
content: `
|
||||
serverAddr = "127.0.0.1"
|
||||
serverPort = 7000
|
||||
|
||||
[[proxies]]
|
||||
name = "p1"
|
||||
type = "tcp"
|
||||
localPort = 22
|
||||
remotePort = 6000
|
||||
|
||||
[[proxies]]
|
||||
name = "p2"
|
||||
type = "tcp"
|
||||
localPort = 3306
|
||||
remotePort = 6001
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "same name across proxy and visitor",
|
||||
content: `
|
||||
serverAddr = "127.0.0.1"
|
||||
serverPort = 7000
|
||||
|
||||
[[proxies]]
|
||||
name = "same"
|
||||
type = "tcp"
|
||||
localPort = 22
|
||||
remotePort = 6000
|
||||
|
||||
[[visitors]]
|
||||
name = "same"
|
||||
type = "stcp"
|
||||
serverName = "a"
|
||||
secretKey = "secret"
|
||||
bindPort = 9001
|
||||
`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
require := require.New(t)
|
||||
path := filepath.Join(t.TempDir(), "frpc.toml")
|
||||
require.NoError(os.WriteFile(path, []byte(tc.content), 0o600))
|
||||
|
||||
_, err := LoadClientConfigResult(path, false)
|
||||
if tc.errSubstr == "" {
|
||||
require.NoError(err)
|
||||
} else {
|
||||
require.ErrorContains(err, tc.errSubstr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestYAMLEdgeCases tests edge cases for YAML parsing, including non-map types
|
||||
func TestYAMLEdgeCases(t *testing.T) {
|
||||
require := require.New(t)
|
||||
|
||||
@@ -174,7 +174,7 @@ type ServerTransportConfig struct {
|
||||
// If negative, keep-alive probes are disabled.
|
||||
TCPKeepAlive int64 `json:"tcpKeepalive,omitempty"`
|
||||
// MaxPoolCount specifies the maximum pool size for each proxy. By default,
|
||||
// this value is 5.
|
||||
// this value is 5. Negative values are invalid.
|
||||
MaxPoolCount int64 `json:"maxPoolCount,omitempty"`
|
||||
// HeartBeatTimeout specifies the maximum time to wait for a heartbeat
|
||||
// before terminating the connection. It is not recommended to change this
|
||||
|
||||
@@ -51,14 +51,51 @@ func (v *ConfigValidator) ValidateClientCommonConfig(c *v1.ClientCommonConfig) (
|
||||
}
|
||||
|
||||
func validateFeatureGates(c *v1.ClientCommonConfig) (Warning, error) {
|
||||
gates := featuregate.NewFeatureGate()
|
||||
if err := gates.SetFromMap(c.FeatureGates); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if c.VirtualNet.Address != "" {
|
||||
if !featuregate.Enabled(featuregate.VirtualNet) {
|
||||
if !gates.Enabled(featuregate.VirtualNet) {
|
||||
return nil, fmt.Errorf("VirtualNet feature is not enabled; enable it by setting the appropriate feature gate flag")
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// ClientConfigRequirements describes runtime capabilities needed by a client configuration.
|
||||
type ClientConfigRequirements struct {
|
||||
VirtualNet bool
|
||||
}
|
||||
|
||||
// GetClientConfigRequirements returns the runtime capabilities needed by a client configuration.
|
||||
func GetClientConfigRequirements(
|
||||
common *v1.ClientCommonConfig,
|
||||
proxyCfgs []v1.ProxyConfigurer,
|
||||
visitorCfgs []v1.VisitorConfigurer,
|
||||
) ClientConfigRequirements {
|
||||
requirements := ClientConfigRequirements{}
|
||||
if common != nil && common.VirtualNet.Address != "" {
|
||||
requirements.VirtualNet = true
|
||||
}
|
||||
for _, cfg := range proxyCfgs {
|
||||
if cfg.GetBaseConfig().Plugin.Type == v1.PluginVirtualNet {
|
||||
requirements.VirtualNet = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !requirements.VirtualNet {
|
||||
for _, cfg := range visitorCfgs {
|
||||
if cfg.GetBaseConfig().Plugin.Type == v1.VisitorPluginVirtualNet {
|
||||
requirements.VirtualNet = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return requirements
|
||||
}
|
||||
|
||||
func (v *ConfigValidator) validateAuthConfig(c *v1.AuthClientConfig) (Warning, error) {
|
||||
var errs error
|
||||
if !slices.Contains(SupportedAuthMethods, c.Method) {
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
// Copyright 2026 The frp Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package validation
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
v1 "github.com/fatedier/frp/pkg/config/v1"
|
||||
"github.com/fatedier/frp/pkg/policy/featuregate"
|
||||
"github.com/fatedier/frp/pkg/policy/security"
|
||||
)
|
||||
|
||||
func validateClientFeatureGates(t *testing.T, gates map[string]bool, virtualNetAddress string) error {
|
||||
t.Helper()
|
||||
|
||||
cfg := &v1.ClientCommonConfig{
|
||||
FeatureGates: gates,
|
||||
VirtualNet: v1.VirtualNetConfig{
|
||||
Address: virtualNetAddress,
|
||||
},
|
||||
}
|
||||
require.NoError(t, cfg.Complete())
|
||||
|
||||
_, err := NewConfigValidator(security.NewUnsafeFeatures(nil)).ValidateClientCommonConfig(cfg)
|
||||
return err
|
||||
}
|
||||
|
||||
func TestValidateClientFeatureGates(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
featureGates map[string]bool
|
||||
virtualNetAddress string
|
||||
wantErr string
|
||||
}{
|
||||
{
|
||||
name: "VirtualNet enabled",
|
||||
featureGates: map[string]bool{"VirtualNet": true},
|
||||
virtualNetAddress: "100.86.0.4/24",
|
||||
},
|
||||
{
|
||||
name: "VirtualNet explicitly disabled",
|
||||
featureGates: map[string]bool{"VirtualNet": false},
|
||||
virtualNetAddress: "100.86.0.4/24",
|
||||
wantErr: "VirtualNet feature is not enabled",
|
||||
},
|
||||
{
|
||||
name: "VirtualNet disabled by default",
|
||||
virtualNetAddress: "100.86.0.4/24",
|
||||
wantErr: "VirtualNet feature is not enabled",
|
||||
},
|
||||
{
|
||||
name: "unknown feature gate",
|
||||
featureGates: map[string]bool{"UnknownFeature": true},
|
||||
wantErr: "unrecognized feature gate: UnknownFeature",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := validateClientFeatureGates(t, tc.featureGates, tc.virtualNetAddress)
|
||||
if tc.wantErr == "" {
|
||||
require.NoError(t, err)
|
||||
return
|
||||
}
|
||||
require.ErrorContains(t, err, tc.wantErr)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetClientConfigRequirements(t *testing.T) {
|
||||
virtualNetProxy := &v1.STCPProxyConfig{
|
||||
ProxyBaseConfig: v1.ProxyBaseConfig{
|
||||
ProxyBackend: v1.ProxyBackend{
|
||||
Plugin: v1.TypedClientPluginOptions{Type: v1.PluginVirtualNet},
|
||||
},
|
||||
},
|
||||
}
|
||||
virtualNetVisitor := &v1.STCPVisitorConfig{
|
||||
VisitorBaseConfig: v1.VisitorBaseConfig{
|
||||
Plugin: v1.TypedVisitorPluginOptions{Type: v1.VisitorPluginVirtualNet},
|
||||
},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
common *v1.ClientCommonConfig
|
||||
proxies []v1.ProxyConfigurer
|
||||
visitors []v1.VisitorConfigurer
|
||||
wantVNet bool
|
||||
}{
|
||||
{name: "no requirements"},
|
||||
{
|
||||
name: "common VirtualNet address",
|
||||
common: &v1.ClientCommonConfig{VirtualNet: v1.VirtualNetConfig{Address: "100.86.0.4/24"}},
|
||||
wantVNet: true,
|
||||
},
|
||||
{name: "VirtualNet proxy", proxies: []v1.ProxyConfigurer{virtualNetProxy}, wantVNet: true},
|
||||
{name: "VirtualNet visitor", visitors: []v1.VisitorConfigurer{virtualNetVisitor}, wantVNet: true},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := GetClientConfigRequirements(tc.common, tc.proxies, tc.visitors)
|
||||
require.Equal(t, tc.wantVNet, got.VirtualNet)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateClientFeatureGatesAreConfigScoped(t *testing.T) {
|
||||
defaultGatesBefore := featuregate.DefaultFeatureGates.String()
|
||||
|
||||
require.NoError(t, validateClientFeatureGates(
|
||||
t,
|
||||
map[string]bool{"VirtualNet": true},
|
||||
"100.86.0.4/24",
|
||||
))
|
||||
require.Equal(t, defaultGatesBefore, featuregate.DefaultFeatureGates.String())
|
||||
|
||||
err := validateClientFeatureGates(
|
||||
t,
|
||||
map[string]bool{"VirtualNet": false},
|
||||
"100.86.0.4/24",
|
||||
)
|
||||
require.ErrorContains(t, err, "VirtualNet feature is not enabled")
|
||||
require.Equal(t, defaultGatesBefore, featuregate.DefaultFeatureGates.String())
|
||||
}
|
||||
@@ -79,9 +79,11 @@ func validateDomainConfigForClient(c *v1.DomainConfig) error {
|
||||
}
|
||||
|
||||
func validateDomainConfigForServer(c *v1.DomainConfig, s *v1.ServerConfig) error {
|
||||
subDomainHost := strings.ToLower(s.SubDomainHost)
|
||||
for _, domain := range c.CustomDomains {
|
||||
if s.SubDomainHost != "" && len(strings.Split(s.SubDomainHost, ".")) < len(strings.Split(domain, ".")) {
|
||||
if strings.HasSuffix(domain, "."+s.SubDomainHost) {
|
||||
canonicalDomain := strings.ToLower(domain)
|
||||
if subDomainHost != "" && len(strings.Split(subDomainHost, ".")) < len(strings.Split(canonicalDomain, ".")) {
|
||||
if strings.HasSuffix(canonicalDomain, "."+subDomainHost) {
|
||||
return fmt.Errorf("custom domain [%s] should not belong to subdomain host [%s]", domain, s.SubDomainHost)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
// Copyright 2026 The frp Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package validation
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
v1 "github.com/fatedier/frp/pkg/config/v1"
|
||||
)
|
||||
|
||||
func TestValidateDomainConfigForServerRejectsSubdomainHostCaseInsensitively(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
subDomainHost string
|
||||
customDomain string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "lowercase subdomain",
|
||||
subDomainHost: "frp.example.com",
|
||||
customDomain: "victim.frp.example.com",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "mixed case custom domain",
|
||||
subDomainHost: "frp.example.com",
|
||||
customDomain: "victim.FRP.example.com",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "mixed case wildcard domain",
|
||||
subDomainHost: "frp.example.com",
|
||||
customDomain: "*.FRP.example.com",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "mixed case subdomain host",
|
||||
subDomainHost: "FRP.Example.Com",
|
||||
customDomain: "victim.frp.example.com",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "external domain",
|
||||
subDomainHost: "frp.example.com",
|
||||
customDomain: "victim.example.net",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := validateDomainConfigForServer(
|
||||
&v1.DomainConfig{CustomDomains: []string{tt.customDomain}},
|
||||
&v1.ServerConfig{SubDomainHost: tt.subDomainHost},
|
||||
)
|
||||
if tt.wantErr {
|
||||
require.ErrorContains(t, err, "should not belong to subdomain host")
|
||||
return
|
||||
}
|
||||
require.NoError(t, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -51,6 +51,9 @@ func (v *ConfigValidator) ValidateServerConfig(c *v1.ServerConfig) (Warning, err
|
||||
errs = AppendError(errs, ValidatePort(c.VhostHTTPPort, "vhostHTTPPort"))
|
||||
errs = AppendError(errs, ValidatePort(c.VhostHTTPSPort, "vhostHTTPSPort"))
|
||||
errs = AppendError(errs, ValidatePort(c.TCPMuxHTTPConnectPort, "tcpMuxHTTPConnectPort"))
|
||||
if c.Transport.MaxPoolCount < 0 {
|
||||
errs = AppendError(errs, fmt.Errorf("invalid transport.maxPoolCount, must be non-negative"))
|
||||
}
|
||||
|
||||
for _, p := range c.HTTPPlugins {
|
||||
if !lo.Every(SupportedHTTPPluginOps, p.Ops) {
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
// Copyright 2026 The frp Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package validation
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
v1 "github.com/fatedier/frp/pkg/config/v1"
|
||||
)
|
||||
|
||||
func TestValidateServerConfigMaxPoolCount(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
maxPoolCount int64
|
||||
wantErr bool
|
||||
}{
|
||||
{name: "negative", maxPoolCount: -1, wantErr: true},
|
||||
{name: "zero", maxPoolCount: 0},
|
||||
{name: "positive", maxPoolCount: 5},
|
||||
{name: "maximum int64", maxPoolCount: math.MaxInt64},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
cfg := validServerConfigWithAuth(v1.AuthServerConfig{Method: v1.AuthMethodToken})
|
||||
cfg.Transport.MaxPoolCount = tc.maxPoolCount
|
||||
require.NoError(t, cfg.Complete())
|
||||
|
||||
_, err := NewConfigValidator(nil).ValidateServerConfig(cfg)
|
||||
if tc.wantErr {
|
||||
require.ErrorContains(t, err, "invalid transport.maxPoolCount")
|
||||
require.ErrorContains(t, err, "must be non-negative")
|
||||
return
|
||||
}
|
||||
require.NoError(t, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user