限制Linux管理服务回环监听并补齐安全回归
This commit is contained in:
1 parent
0b74886c45
commit
1150c3904e
13 files changed
+233
-4
No files matched your search
@@ -54,6 +54,9 @@ func New(ctx context.Context, cfg config.Config) (*App, error) {
|
||||
}
|
||||
|
||||
func NewWithConfigPath(ctx context.Context, cfg config.Config, configPath string) (*App, error) {
|
||||
if err := cfg.ValidateServerListener(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !cfg.SetupReady {
|
||||
return nil, fmt.Errorf("系统尚未初始化,请先启动 scert serve 完成首次配置")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
)
|
||||
|
||||
func TestNewRejectsNonLoopbackBeforeCreatingResources(t *testing.T) {
|
||||
cfg := config.Default(t.TempDir())
|
||||
cfg.SetupReady = true
|
||||
cfg.Server.Host = "::"
|
||||
application, err := NewWithConfigPath(context.Background(), cfg, "")
|
||||
if application != nil || err == nil || !strings.Contains(err.Error(), "只允许监听回环") {
|
||||
t.Fatalf("正式服务入口未在创建资源前拒绝非回环监听:%v", err)
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,9 @@ func Run(ctx context.Context, options ServerOptions) (bool, error) {
|
||||
if options.Config.SetupReady {
|
||||
return false, errors.New("已经初始化,不能启动临时初始化服务")
|
||||
}
|
||||
if err := options.Config.ValidateServerListener(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
firewallManager, err := firewall.NewManagerWithMode(options.FirewallMode)
|
||||
if err != nil {
|
||||
return false, err
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
)
|
||||
|
||||
func TestRunRejectsNonLoopbackBeforeInitialization(t *testing.T) {
|
||||
cfg := config.Default(t.TempDir())
|
||||
cfg.Server.Host = "0.0.0.0"
|
||||
completed, err := Run(context.Background(), ServerOptions{Config: cfg})
|
||||
if completed || err == nil || !strings.Contains(err.Error(), "只允许监听回环") {
|
||||
t.Fatalf("初始化入口未在创建资源前拒绝非回环监听:%v", err)
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,14 @@ func loadServeConfig(path string, defaults config.Config) (config.Config, error)
|
||||
if !info.Mode().IsRegular() {
|
||||
return config.Config{}, errors.New("服务配置必须是普通文件")
|
||||
}
|
||||
return config.LoadWithEnv(path)
|
||||
cfg, err := config.LoadWithEnv(path)
|
||||
if err != nil {
|
||||
return config.Config{}, err
|
||||
}
|
||||
if err := cfg.ValidateServerListener(); err != nil {
|
||||
return config.Config{}, err
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
if !errors.Is(err, os.ErrNotExist) {
|
||||
return config.Config{}, err
|
||||
@@ -36,6 +43,9 @@ func loadServeConfig(path string, defaults config.Config) (config.Config, error)
|
||||
if err := defaults.Validate(); err != nil {
|
||||
return config.Config{}, err
|
||||
}
|
||||
if err := defaults.ValidateServerListener(); err != nil {
|
||||
return config.Config{}, err
|
||||
}
|
||||
return defaults, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
)
|
||||
|
||||
func TestLoadServeConfigWithoutListenerOverride(t *testing.T) {
|
||||
t.Setenv("SCERT_SERVER_HOST", "")
|
||||
if err := os.Unsetenv("SCERT_SERVER_HOST"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
root := t.TempDir()
|
||||
path := filepath.Join(root, "scert.json")
|
||||
cfg := config.Default(root)
|
||||
loaded, err := loadServeConfig(path, cfg)
|
||||
if err != nil || loaded.Server.Host != "127.0.0.1" {
|
||||
t.Fatalf("未配置环境变量时默认监听不安全:%v", err)
|
||||
}
|
||||
cfg.Server.Host = "0.0.0.0"
|
||||
if err := cfg.Save(path); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := loadServeConfig(path, cfg); err == nil {
|
||||
t.Fatal("没有安全环境覆盖的历史通配监听未被拒绝")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadServeConfigEnforcesEffectiveLoopback(t *testing.T) {
|
||||
for _, existing := range []bool{false, true} {
|
||||
for _, host := range []string{"127.0.0.1", "::1", "0.0.0.0", "::", "192.0.2.1"} {
|
||||
t.Run(host+map[bool]string{false: "/new", true: "/existing"}[existing], func(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
cfg := config.Default(root)
|
||||
cfg.Server.Host = "0.0.0.0"
|
||||
path := filepath.Join(root, "scert.json")
|
||||
if existing {
|
||||
data, err := json.Marshal(cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(path, data, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
t.Setenv("SCERT_SERVER_HOST", host)
|
||||
loaded, err := loadServeConfig(path, cfg)
|
||||
allowed := host == "127.0.0.1" || host == "::1"
|
||||
if allowed && (err != nil || loaded.Server.Host != host) {
|
||||
t.Fatalf("回环环境覆盖未生效:%v", err)
|
||||
}
|
||||
if !allowed && err == nil {
|
||||
t.Fatal("环境覆盖后的非回环地址未被拒绝")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -131,7 +131,7 @@ func Default(baseDir string) Config {
|
||||
Version: CurrentVersion,
|
||||
SetupReady: false,
|
||||
Admin: AdminConfig{},
|
||||
Server: ServerConfig{Host: "0.0.0.0", Port: 8080, HTTPSMode: "direct"},
|
||||
Server: ServerConfig{Host: defaultServerHost(), Port: 8080, HTTPSMode: "direct"},
|
||||
Web: WebConfig{Enabled: true},
|
||||
Database: DatabaseConfig{
|
||||
Driver: "sqlite", DSN: filepath.Join(paths.Data, "scert.db"),
|
||||
@@ -330,7 +330,7 @@ func (c *Config) Normalize() {
|
||||
return
|
||||
}
|
||||
if c.Server.Host == "" {
|
||||
c.Server.Host = "0.0.0.0"
|
||||
c.Server.Host = defaultServerHost()
|
||||
}
|
||||
if c.Server.HTTPSMode == "" {
|
||||
c.Server.HTTPSMode = "direct"
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func defaultServerHost() string {
|
||||
if runtime.GOOS == "linux" {
|
||||
return "127.0.0.1"
|
||||
}
|
||||
return "0.0.0.0"
|
||||
}
|
||||
|
||||
// ValidateServerListener 校验环境覆盖后的实际监听配置,不通过域名解析判断回环地址。
|
||||
func (c Config) ValidateServerListener() error {
|
||||
if runtime.GOOS != "linux" {
|
||||
return nil
|
||||
}
|
||||
address := net.ParseIP(c.Server.Host)
|
||||
if address == nil || !address.IsLoopback() {
|
||||
return errors.New("Linux 管理服务只允许监听回环 IP 地址,请使用 127.0.0.1 或 ::1,并通过 Nginx 的 80/443 端口访问")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package config
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestLinuxServerDefaultsToLoopback(t *testing.T) {
|
||||
cfg := Default(t.TempDir())
|
||||
if cfg.Server.Host != "127.0.0.1" {
|
||||
t.Fatal("Linux 默认监听地址不是回环地址")
|
||||
}
|
||||
cfg.Server.Host = ""
|
||||
cfg.Normalize()
|
||||
if cfg.Server.Host != "127.0.0.1" {
|
||||
t.Fatal("空监听地址未回退到回环地址")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLinuxServerListenerRejectsNonLoopback(t *testing.T) {
|
||||
for _, host := range []string{"", "0.0.0.0", "::", "192.0.2.1", "10.0.0.1", "2001:db8::1", "localhost", "127.0.0.1.example.test", " 127.0.0.1", "127.0.0.1:8443"} {
|
||||
t.Run(host, func(t *testing.T) {
|
||||
cfg := Default(t.TempDir())
|
||||
cfg.Server.Host = host
|
||||
if err := cfg.ValidateServerListener(); err == nil {
|
||||
t.Fatal("非回环或模糊监听配置未被拒绝")
|
||||
}
|
||||
})
|
||||
}
|
||||
for _, host := range []string{"127.0.0.1", "::1", "::ffff:127.0.0.1"} {
|
||||
t.Run(host, func(t *testing.T) {
|
||||
cfg := Default(t.TempDir())
|
||||
cfg.Server.Host = host
|
||||
if err := cfg.ValidateServerListener(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ func TestProxyHTTPSPortValidationAndPersistence(t *testing.T) {
|
||||
if err := cfg.Validate(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
path := filepath.Join(cfg.Paths.Config, "scert.json")
|
||||
path := filepath.Join(t.TempDir(), "scert.json")
|
||||
if err := cfg.Save(path); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user