forked from kevin/meshtastic_mqtt_server
安全加固:LLM 会话按 (bot,peer) 隔离+历史上限50条,LLM 入队按 (bot,from_node) 限流(60s内5条),瓦片磁盘缓存单源配额(3000文件/300MB 按mtime淘汰),签到墙读接口限速+全站每日1000条封顶,敏感数据落盘 AES-256-GCM 加密(MESH_SECRET_KEY, 兼容明文迁移),admin 改密需验证当前密码+pwd_version 会话撤销,后端 v1.5.0
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
// Package secrets 提供敏感字段(API key、MQTT 口令、bot 私钥)落盘前的
|
||||
// AES-256-GCM 加密与读取时的透明解密。
|
||||
//
|
||||
// 用法:启动时以环境变量 MESH_SECRET_KEY 初始化(SetSecretKey)。
|
||||
// 未配置密钥时 Encrypt/Decrypt 均原样透传(兼容旧部署的明文数据);
|
||||
// 加密值带 "enc:v1:" 前缀,解密时对无前缀的历史明文直接返回。
|
||||
package secrets
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const prefix = "enc:v1:"
|
||||
|
||||
var (
|
||||
mu sync.RWMutex
|
||||
key []byte
|
||||
)
|
||||
|
||||
// SetSecretKey 用 MESH_SECRET_KEY 的内容派生 AES-256 密钥;传空则回到明文模式。
|
||||
func SetSecretKey(secret string) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
secret = strings.TrimSpace(secret)
|
||||
if secret == "" {
|
||||
key = nil
|
||||
return
|
||||
}
|
||||
sum := sha256.Sum256([]byte(secret))
|
||||
key = sum[:]
|
||||
}
|
||||
|
||||
// Enabled 报告是否已配置加密密钥。
|
||||
func Enabled() bool {
|
||||
mu.RLock()
|
||||
defer mu.RUnlock()
|
||||
return len(key) > 0
|
||||
}
|
||||
|
||||
// Encrypt 加密明文;未配置密钥时原样返回。
|
||||
func Encrypt(plain string) string {
|
||||
if plain == "" {
|
||||
return plain
|
||||
}
|
||||
mu.RLock()
|
||||
k := key
|
||||
mu.RUnlock()
|
||||
if len(k) == 0 {
|
||||
return plain
|
||||
}
|
||||
block, err := aes.NewCipher(k)
|
||||
if err != nil {
|
||||
return plain
|
||||
}
|
||||
gcm, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return plain
|
||||
}
|
||||
nonce := make([]byte, gcm.NonceSize())
|
||||
if _, err := rand.Read(nonce); err != nil {
|
||||
return plain
|
||||
}
|
||||
sealed := gcm.Seal(nonce, nonce, []byte(plain), nil)
|
||||
return prefix + base64.RawStdEncoding.EncodeToString(sealed)
|
||||
}
|
||||
|
||||
// Decrypt 解密 enc:v1: 前缀的值;无前缀(历史明文)或解密失败返回空串。
|
||||
func Decrypt(value string) string {
|
||||
if !strings.HasPrefix(value, prefix) {
|
||||
return value
|
||||
}
|
||||
mu.RLock()
|
||||
k := key
|
||||
mu.RUnlock()
|
||||
if len(k) == 0 {
|
||||
return value
|
||||
}
|
||||
raw, err := base64.RawStdEncoding.DecodeString(strings.TrimPrefix(value, prefix))
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
block, err := aes.NewCipher(k)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
gcm, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
if len(raw) < gcm.NonceSize() {
|
||||
return ""
|
||||
}
|
||||
nonce, ciphertext := raw[:gcm.NonceSize()], raw[gcm.NonceSize():]
|
||||
plain, err := gcm.Open(nil, nonce, ciphertext, nil)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(plain)
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package secrets
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRoundTrip(t *testing.T) {
|
||||
SetSecretKey("test-secret-key")
|
||||
t.Cleanup(func() { SetSecretKey("") })
|
||||
|
||||
enc := Encrypt("s3cret-value")
|
||||
if enc == "s3cret-value" {
|
||||
t.Fatal("value must be encrypted when key is set")
|
||||
}
|
||||
if !strings.HasPrefix(enc, "enc:v1:") {
|
||||
t.Fatalf("unexpected prefix: %q", enc)
|
||||
}
|
||||
if got := Decrypt(enc); got != "s3cret-value" {
|
||||
t.Fatalf("round trip failed: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlaintextPassthroughWhenNoKey(t *testing.T) {
|
||||
SetSecretKey("")
|
||||
if Encrypt("plain") != "plain" {
|
||||
t.Error("must pass through when no key")
|
||||
}
|
||||
if Decrypt("plain") != "plain" {
|
||||
t.Error("must pass through when no key")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLegacyPlaintextStillReadable(t *testing.T) {
|
||||
SetSecretKey("k1")
|
||||
t.Cleanup(func() { SetSecretKey("") })
|
||||
// 旧数据无前缀,解密应原样返回。
|
||||
if Decrypt("legacy-plaintext") != "legacy-plaintext" {
|
||||
t.Error("legacy plaintext must be returned as-is")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrongKeyFailsClosed(t *testing.T) {
|
||||
SetSecretKey("key-a")
|
||||
enc := Encrypt("top-secret")
|
||||
SetSecretKey("key-b")
|
||||
if Decrypt(enc) != "" {
|
||||
t.Error("decrypt with wrong key must return empty (fail closed)")
|
||||
}
|
||||
SetSecretKey("key-a")
|
||||
if Decrypt(enc) != "top-secret" {
|
||||
t.Error("decrypt with correct key must succeed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptyValue(t *testing.T) {
|
||||
SetSecretKey("k")
|
||||
t.Cleanup(func() { SetSecretKey("") })
|
||||
if Encrypt("") != "" {
|
||||
t.Error("empty value stays empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUniqueCiphertexts(t *testing.T) {
|
||||
SetSecretKey("k")
|
||||
t.Cleanup(func() { SetSecretKey("") })
|
||||
a := Encrypt("same")
|
||||
b := Encrypt("same")
|
||||
if a == b {
|
||||
t.Error("random nonce should produce different ciphertexts")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user