feat(nodes): opt-in encryption at rest for the outbound node API token (#6186)

* node: encrypt outbound bearer token at rest

* fix(nodes): keep bearer tokens encrypted throughout

---------

Co-authored-by: n0ctal <293235942+n0ctal@users.noreply.github.com>
This commit is contained in:
n0ctalandn0ctal authored and GitHub committed 2026-08-15 16:48:50 +02:00 Unverified
1 parent 8e7fb144ee
commit 1793a9b8b4
9 files changed
+982 -15

No files matched your search

+21
View File
@@ -193,6 +193,27 @@ func GetDBDSN() string {
return strings.TrimSpace(os.Getenv("XUI_DB_DSN"))
}
// GetNodeTokenEncryptionMode returns off, migration, or required. Explicit
// policy prevents a missing key from silently downgrading encrypted storage.
func GetNodeTokenEncryptionMode() string {
return strings.TrimSpace(os.Getenv("NODE_TOKEN_ENCRYPTION"))
}
// GetNodeTokenKeyFile returns the mode-0600 keyring path, configurable through
// XUI_NODE_TOKEN_KEY_FILE.
func GetNodeTokenKeyFile() string {
if p := strings.TrimSpace(os.Getenv("XUI_NODE_TOKEN_KEY_FILE")); p != "" {
return p
}
return "/etc/x-ui/node_token_key.json"
}
// GetNodeTokenKeyEnv returns the name of the env var holding a single base64
// 32-byte node-token key (secondary to the key file). Empty value => unused.
func GetNodeTokenKeyEnv() string {
return "XUI_NODE_TOKEN_KEY"
}
// GetEnvFilePaths returns the candidate service environment file paths (the file
// systemd loads via EnvironmentFile) across the supported distro families.
func GetEnvFilePaths() []string {
+115
View File
@@ -0,0 +1,115 @@
package nodetoken
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"os"
"strings"
)
// KeySource loads a startup keyring from a protected file or environment.
// Keys are never accepted on the command line.
type KeySource interface {
Load() (*Keyring, error)
}
// keyFile identifies the active key and all base64-encoded rotation keys.
type keyFile struct {
Active string `json:"active"`
Keys map[string]string `json:"keys"`
}
func parseKeyring(active string, b64keys map[string]string) (*Keyring, error) {
if err := validateKeyID(active); err != nil {
return nil, fmt.Errorf("nodetoken: active key id: %w", err)
}
if active == "" {
return nil, errors.New("nodetoken: key source has no active key id")
}
if len(b64keys) == 0 {
return nil, errors.New("nodetoken: key source has no keys")
}
kr := &Keyring{ActiveID: active, Keys: make(map[string][keyLen]byte, len(b64keys))}
for id, b64 := range b64keys {
if err := validateKeyID(id); err != nil {
return nil, fmt.Errorf("nodetoken: key id %q: %w", id, err)
}
raw, err := decodeKey(b64)
if err != nil {
return nil, fmt.Errorf("nodetoken: key %q: %w", id, err)
}
kr.Keys[id] = raw
}
if _, ok := kr.Keys[active]; !ok {
return nil, fmt.Errorf("nodetoken: active key %q absent from keys", active)
}
return kr, nil
}
func validateKeyID(id string) error {
if id == "" {
return errors.New("must not be empty")
}
if strings.Contains(id, ":") {
return errors.New("must not contain ':'")
}
return nil
}
func decodeKey(b64 string) ([keyLen]byte, error) {
var out [keyLen]byte
raw, err := base64.StdEncoding.DecodeString(strings.TrimSpace(b64))
if err != nil {
// tolerate url-safe / unpadded encodings too
if raw2, err2 := base64.RawStdEncoding.DecodeString(strings.TrimSpace(b64)); err2 == nil {
raw = raw2
} else {
return out, fmt.Errorf("base64 decode: %w", err)
}
}
if len(raw) != keyLen {
return out, fmt.Errorf("key must be %d bytes, got %d", keyLen, len(raw))
}
copy(out[:], raw)
return out, nil
}
// FileKeySource accepts only key files that are mode 0600 or stricter.
type FileKeySource struct {
Path string
}
func (f FileKeySource) Load() (*Keyring, error) {
info, err := os.Stat(f.Path)
if err != nil {
return nil, fmt.Errorf("nodetoken: stat key file %s: %w", f.Path, err)
}
if perm := info.Mode().Perm(); perm&0o077 != 0 {
return nil, fmt.Errorf("nodetoken: key file %s has insecure mode %#o (want 0600)", f.Path, perm)
}
data, err := os.ReadFile(f.Path)
if err != nil {
return nil, fmt.Errorf("nodetoken: read key file %s: %w", f.Path, err)
}
var kf keyFile
if err := json.Unmarshal(data, &kf); err != nil {
return nil, fmt.Errorf("nodetoken: parse key file %s: %w", f.Path, err)
}
return parseKeyring(kf.Active, kf.Keys)
}
// EnvKeySource reads a single base64 32-byte key from an environment variable.
// The key id is fixed ("env"); for multi-key rotation prefer a key file.
type EnvKeySource struct {
Var string
}
func (e EnvKeySource) Load() (*Keyring, error) {
v := strings.TrimSpace(os.Getenv(e.Var))
if v == "" {
return nil, fmt.Errorf("nodetoken: env %s is empty", e.Var)
}
return parseKeyring("env", map[string]string{"env": v})
}
+236
View File
@@ -0,0 +1,236 @@
// Package nodetoken encrypts replayable per-node bearer tokens at rest with
// row-bound AES-GCM and versioned key IDs.
package nodetoken
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"errors"
"fmt"
"strings"
"sync"
)
// Mode is explicit so a missing key cannot silently downgrade encrypted
// deployments to plaintext.
type Mode int
const (
// ModeOff: legacy plaintext operation. Writes store plaintext; an encrypted
// value cannot be interpreted (no key) and is rejected rather than guessed.
ModeOff Mode = iota
// ModeMigration: key required. Reads accept plaintext OR ciphertext; writes
// always produce ciphertext. Used while migrating existing rows.
ModeMigration
// ModeRequired: key required (startup fails if it cannot load). Reads decrypt
// ciphertext (error on failure) and accept any still-unmigrated plaintext;
// writes always produce ciphertext.
ModeRequired
)
const (
encPrefix = "enc:"
encScheme = "enc:v1:"
keyLen = 32 // AES-256
nonceLen = 12 // GCM standard nonce
aadKeyFormat = "nodes/api_token/%d"
)
// ParseMode maps the NODE_TOKEN_ENCRYPTION env value to a Mode.
func ParseMode(s string) (Mode, error) {
switch strings.ToLower(strings.TrimSpace(s)) {
case "", "off":
return ModeOff, nil
case "migration":
return ModeMigration, nil
case "required":
return ModeRequired, nil
default:
return ModeOff, fmt.Errorf("nodetoken: unknown NODE_TOKEN_ENCRYPTION %q (want off|migration|required)", s)
}
}
// Keyring holds the active write key and previous decryption keys.
type Keyring struct {
ActiveID string
Keys map[string][keyLen]byte
}
func (kr *Keyring) active() ([keyLen]byte, error) {
k, ok := kr.Keys[kr.ActiveID]
if !ok {
return [keyLen]byte{}, fmt.Errorf("nodetoken: active key %q not in keyring", kr.ActiveID)
}
return k, nil
}
// Codec encrypts/decrypts node tokens under a fixed policy and keyring.
type Codec struct {
mode Mode
ring *Keyring // nil only in ModeOff
}
// NewCodec requires an active key outside ModeOff.
func NewCodec(mode Mode, ring *Keyring) (*Codec, error) {
if mode == ModeOff {
return &Codec{mode: ModeOff}, nil
}
if ring == nil || len(ring.Keys) == 0 {
return nil, errors.New("nodetoken: encryption mode requires a key, but none was loaded")
}
if _, err := ring.active(); err != nil {
return nil, err
}
return &Codec{mode: mode, ring: ring}, nil
}
// Enabled reports whether the codec writes ciphertext (mode != off).
func (c *Codec) Enabled() bool { return c.mode != ModeOff }
func aad(nodeID int) []byte { return []byte(fmt.Sprintf(aadKeyFormat, nodeID)) }
// IsEncrypted reports whether a stored value is in this package's ciphertext form.
func IsEncrypted(stored string) bool { return strings.HasPrefix(stored, encPrefix) }
// Encrypt returns plaintext in ModeOff or row-bound enc:v1 ciphertext otherwise.
// Empty and already-valid encrypted values remain unchanged.
func (c *Codec) Encrypt(nodeID int, plaintext string) (string, error) {
if c.mode == ModeOff || plaintext == "" {
return plaintext, nil
}
if IsEncrypted(plaintext) {
// Validate it actually decrypts for this node; if so keep verbatim.
if _, err := c.Decrypt(nodeID, plaintext); err != nil {
return "", fmt.Errorf("nodetoken: refusing to store undecryptable ciphertext: %w", err)
}
return plaintext, nil
}
key, err := c.ring.active()
if err != nil {
return "", err
}
gcm, err := newGCM(key)
if err != nil {
return "", err
}
nonce := make([]byte, nonceLen)
if _, err := rand.Read(nonce); err != nil {
return "", err
}
ct := gcm.Seal(nil, nonce, []byte(plaintext), aad(nodeID))
blob := append(nonce, ct...)
return encScheme + c.ring.ActiveID + ":" + base64.RawURLEncoding.EncodeToString(blob), nil
}
// Decrypt passes legacy plaintext through; enc: values must authenticate and
// are never reinterpreted as plaintext after an error.
func (c *Codec) Decrypt(nodeID int, stored string) (string, error) {
if c.mode == ModeOff {
return stored, nil
}
if !IsEncrypted(stored) {
return stored, nil
}
rest, ok := strings.CutPrefix(stored, encScheme)
if !ok {
return "", fmt.Errorf("nodetoken: unsupported ciphertext scheme in %q", firstN(stored, 12))
}
keyID, b64, ok := strings.Cut(rest, ":")
if !ok || keyID == "" {
return "", errors.New("nodetoken: malformed ciphertext (missing key id)")
}
if c.ring == nil {
return "", errors.New("nodetoken: encrypted token encountered but encryption is disabled (no key)")
}
key, ok := c.ring.Keys[keyID]
if !ok {
return "", fmt.Errorf("nodetoken: no key %q in keyring to decrypt token", keyID)
}
blob, err := base64.RawURLEncoding.DecodeString(b64)
if err != nil {
return "", fmt.Errorf("nodetoken: base64 decode: %w", err)
}
if len(blob) < nonceLen {
return "", errors.New("nodetoken: ciphertext too short")
}
gcm, err := newGCM(key)
if err != nil {
return "", err
}
pt, err := gcm.Open(nil, blob[:nonceLen], blob[nonceLen:], aad(nodeID))
if err != nil {
return "", fmt.Errorf("nodetoken: authentication failed for node %d: %w", nodeID, err)
}
return string(pt), nil
}
// ActiveKeyID returns the id new writes use (empty in ModeOff).
func (c *Codec) ActiveKeyID() string {
if c.ring == nil {
return ""
}
return c.ring.ActiveID
}
// EncryptedWithActive reports whether migration can skip a ciphertext row.
func (c *Codec) EncryptedWithActive(stored string) bool {
if c.ring == nil || !IsEncrypted(stored) {
return false
}
rest, ok := strings.CutPrefix(stored, encScheme)
if !ok {
return false
}
keyID, _, ok := strings.Cut(rest, ":")
return ok && keyID == c.ring.ActiveID
}
func newGCM(key [keyLen]byte) (cipher.AEAD, error) {
block, err := aes.NewCipher(key[:])
if err != nil {
return nil, err
}
return cipher.NewGCM(block)
}
func firstN(s string, n int) string {
if len(s) <= n {
return s
}
return s[:n]
}
// --- package singleton, initialized once at startup ---
var (
mu sync.RWMutex
current *Codec
)
// Init installs the process-wide codec. Call once during startup after building
// the keyring; in ModeOff a nil keyring is fine.
func Init(c *Codec) {
mu.Lock()
defer mu.Unlock()
current = c
}
// get returns the installed codec, or a permissive ModeOff codec if Init was
// never called (e.g. unit tests / sqlite dev) so callers never nil-panic.
func get() *Codec {
mu.RLock()
c := current
mu.RUnlock()
if c == nil {
return &Codec{mode: ModeOff}
}
return c
}
// Encrypt/Decrypt/Enabled operate on the process-wide codec.
func Encrypt(nodeID int, plaintext string) (string, error) { return get().Encrypt(nodeID, plaintext) }
func Decrypt(nodeID int, stored string) (string, error) { return get().Decrypt(nodeID, stored) }
func Enabled() bool { return get().Enabled() }
func Active() *Codec { return get() }
Loaded 3 of 9 files, more files were not shown because too many files have changed in this diff. Show more