加固PostgreSQL连接参数与凭据脱敏并完成重复回归
This commit is contained in:
1 parent
16428f05b3
commit
781da9f32c
7 files changed
+278
-42
No files matched your search
@@ -479,6 +479,11 @@ func (c Config) Validate() error {
|
||||
if databaseDSNContainsPassword(c.Database.Driver, c.Database.DSN) {
|
||||
return fmt.Errorf("数据库连接信息不能包含密码,请使用加密凭据引用")
|
||||
}
|
||||
if c.Database.Driver == "postgres" {
|
||||
if err := ValidatePostgresDSN(c.Database.DSN); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
if c.Database.MaxOpenConns < 1 || c.Database.MaxIdleConns < 0 || c.Database.MaxIdleConns > c.Database.MaxOpenConns {
|
||||
return fmt.Errorf("数据库连接池配置无效")
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ValidatePostgresDSN 同时用于配置落盘和数据库连接前检查,不读取环境或外部文件。
|
||||
// 账号只能由加密凭据注入;查询参数不能改写验证过的主机、数据库或凭据来源。
|
||||
func ValidatePostgresDSN(dsn string) error {
|
||||
parsed, err := url.Parse(strings.TrimSpace(dsn))
|
||||
if err != nil || (parsed.Scheme != "postgres" && parsed.Scheme != "postgresql") {
|
||||
return errors.New("PostgreSQL 数据库连接信息必须使用不含账号密码的 postgres:// URL")
|
||||
}
|
||||
if parsed.User != nil {
|
||||
return errors.New("PostgreSQL 用户名和密码只能保存到加密凭据中")
|
||||
}
|
||||
if strings.TrimSpace(parsed.Hostname()) == "" || strings.Trim(strings.TrimSpace(parsed.Path), "/") == "" {
|
||||
return errors.New("PostgreSQL 连接信息必须包含主机和数据库")
|
||||
}
|
||||
if parsed.Opaque != "" || parsed.Fragment != "" {
|
||||
return errors.New("PostgreSQL 连接信息不能包含片段或不透明地址")
|
||||
}
|
||||
query, err := url.ParseQuery(parsed.RawQuery)
|
||||
if err != nil {
|
||||
return errors.New("PostgreSQL 连接参数编码无效")
|
||||
}
|
||||
for key, values := range query {
|
||||
if key == "" || key != strings.ToLower(strings.TrimSpace(key)) || len(values) != 1 {
|
||||
return errors.New("PostgreSQL 连接参数必须为小写且不能重复")
|
||||
}
|
||||
switch key {
|
||||
case "user", "password", "sslpassword", "host", "hostaddr", "port", "dbname", "database",
|
||||
"service", "servicefile", "passfile":
|
||||
return errors.New("PostgreSQL 查询参数不能包含账号密码或覆盖连接目标及凭据来源")
|
||||
}
|
||||
}
|
||||
if query.Get("sslmode") != "verify-full" {
|
||||
return errors.New("PostgreSQL 连接必须使用 sslmode=verify-full")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPostgresUnsafeDSNCannotBeSaved(t *testing.T) {
|
||||
const base = "postgres://db.example.test:5432/scert?sslmode=verify-full"
|
||||
for _, suffix := range []string{
|
||||
"&password=unsafe-secret", "&pass%77ord=unsafe-secret", "&user=other-user",
|
||||
"&host=other.example.test", "&port=5433", "&dbname=other", "&database=other",
|
||||
"&service=other", "&servicefile=/tmp/service", "&passfile=/tmp/passwords",
|
||||
"&sslpassword=unsafe-secret", "&sslmode=disable", "&application_name=%zz",
|
||||
"#unsafe-secret", "&Password=unsafe-secret",
|
||||
} {
|
||||
t.Run(suffix, func(t *testing.T) {
|
||||
cfg := Default(t.TempDir())
|
||||
cfg.Database.Driver = "postgres"
|
||||
cfg.Database.DSN = base + suffix
|
||||
cfg.Database.CredentialRef = "secret://database/test"
|
||||
path := filepath.Join(t.TempDir(), "scert.json")
|
||||
err := cfg.Save(path)
|
||||
if err == nil {
|
||||
t.Fatal("含凭据或连接覆盖参数的配置未被拒绝")
|
||||
}
|
||||
if strings.Contains(err.Error(), "unsafe-secret") {
|
||||
t.Fatal("配置错误泄露了输入凭据")
|
||||
}
|
||||
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
||||
t.Fatal("无效配置不应写入磁盘")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresSafeConfigRoundTrips(t *testing.T) {
|
||||
for _, dsn := range []string{
|
||||
"postgres://db.example.test:5432/scert?sslmode=verify-full",
|
||||
"postgresql://[2001:db8::10]:5432/scert?sslmode=verify-full&application_name=scert&connect_timeout=10",
|
||||
"postgres://db.example.test/scert?sslmode=verify-full&sslrootcert=%2Fetc%2Fscert%2Froot.pem",
|
||||
} {
|
||||
cfg := Default(t.TempDir())
|
||||
cfg.Database.Driver = "postgres"
|
||||
cfg.Database.DSN = dsn
|
||||
cfg.Database.CredentialRef = "secret://database/test"
|
||||
path := filepath.Join(t.TempDir(), "scert.json")
|
||||
if err := cfg.Save(path); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
loaded, err := Load(path)
|
||||
if err != nil || loaded.Database != cfg.Database {
|
||||
t.Fatal("合法 PostgreSQL 连接配置不能保存并重新加载")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func FuzzValidatePostgresDSN(f *testing.F) {
|
||||
for _, dsn := range []string{
|
||||
"postgres://db.example.test/scert?sslmode=verify-full",
|
||||
"postgres://db.example.test/scert?sslmode=verify-full&password=fuzz-secret",
|
||||
"postgres://db.example.test/scert?sslmode=verify-full&sslmode=disable",
|
||||
"postgres://db.example.test/scert?sslmode=verify-full&password=%zz",
|
||||
} {
|
||||
f.Add(dsn)
|
||||
}
|
||||
f.Fuzz(func(t *testing.T, dsn string) {
|
||||
err := ValidatePostgresDSN(dsn)
|
||||
if err != nil && strings.Contains(err.Error(), "fuzz-secret") {
|
||||
t.Fatal("校验错误不能包含输入凭据")
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user