[PR #6186] [MERGED] feat(nodes): opt-in encryption at rest for the outbound node API token #6148

Closed
opened 2026-11-02 04:06:42 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/MHSanaei/3x-ui/pull/6186
Author: @n0ctal
Created: 8/8/2026
Status: ✅ Merged
Merged: 8/15/2026
Merged by: @MHSanaei

Base: main ← Head: upstream-node-token-encryption


📝 Commits (2)

  • 4e5d4a4 node: encrypt outbound bearer token at rest
  • 9e9fe25 fix(nodes): keep bearer tokens encrypted throughout

📊 Changes

9 files changed (+982 additions, -15 deletions)

View changed files

📝 internal/config/config.go (+21 -0)
➕ internal/crypto/nodetoken/keysource.go (+115 -0)
➕ internal/crypto/nodetoken/nodetoken.go (+236 -0)
➕ internal/crypto/nodetoken/nodetoken_test.go (+226 -0)
📝 internal/web/runtime/remote.go (+6 -1)
📝 internal/web/service/node.go (+118 -8)
➕ internal/web/service/node_token_encryption_test.go (+180 -0)
📝 main.go (+68 -6)
➕ main_test.go (+12 -0)

📄 Description

Summary

Add opt-in encryption at rest for the outbound node API token, with an explicit three-state policy (off / migration / required) and a rotatable keyring. Default is off.

Like the port-reservation PR, this is a proposal — it adds a package and a key file, so I would rather agree the shape than land it unannounced.

Why

nodes.api_token is the bearer token the panel presents to each worker. It is stored in plaintext, so anyone who reads the database reads a live credential for every node: a database backup, a copied SQLite file, a support dump, a restore onto a machine that should not have had it, an operator with read access to the panel database but not to the nodes.

The token is not derivable, expiring, or scoped — possession is sufficient to drive the node's API. That makes the panel database a single artefact whose disclosure compromises every node behind it, and database copies move around far more casually than node credentials do.

Scope

  • New internal/crypto/nodetoken: AEAD encrypt/decrypt bound to the node id, plus a Keyring with key ids so a token can be re-encrypted under a new key without a flag day.
  • KeySource with two implementations: a mode-0600 JSON key file (/etc/x-ui/node_token_key.json, override with XUI_NODE_TOKEN_KEY_FILE) and a single-key environment variable (XUI_NODE_TOKEN_KEY). The key is never accepted as a command-line argument, where it would be visible in ps.
  • NODE_TOKEN_ENCRYPTION policy:
    • off (default) — plaintext, exactly as today.
    • migration — read plaintext and ciphertext, always write ciphertext. This is the path onto encryption without downtime.
    • required — the key must load or startup fails.
  • The policy is explicit on purpose: a deployment that has already encrypted its tokens must not silently fall back to plaintext because a key file went missing. That failure should be loud.
  • node.go encrypts on write and decrypts on read inside the existing transactions; a plaintext row passes through unchanged so a partially migrated table keeps working.

Validation

  • go build ./internal/... clean.
  • go test ./internal/crypto/nodetoken/ ./internal/web/service/ green on main at ece16559.
  • Tests cover round-trip, node-id binding (a token encrypted for one node does not decrypt for another), key rotation across ids, malformed keys, and all three policy states including the mixed plaintext/ciphertext table that migration has to tolerate.

Risk

None for existing installs: with NODE_TOKEN_ENCRYPTION unset the code path is the current one.

The operational cost is real and worth stating — enabling this means a key file that has to survive alongside the database, and losing it while in required mode means the panel will not start until it is restored or the tokens are reissued. That is a deliberate trade: I would rather a lost key stop the panel than quietly return it to plaintext. If you would prefer a different default or a simpler single-key form without the keyring, I am happy to cut it down.

Update after review

Two corrections to the contract described above:

  • off mode. The earlier "byte for byte" wording was too strong. The precise contract is: in off mode a stored legacy value is passed through unchanged, including values that happen to begin with the enc: marker — those are no longer claimed by the codec under the default policy.
  • No plaintext in the initial tuple. The id-bound ciphertext can only be produced once the row has an id, and the first draft inserted the plaintext and then overwrote it. A transaction does not undo that: PostgreSQL retains the original tuple in the WAL. The insert now carries an empty token and only the encrypted value is written, so the plaintext never reaches storage.
  • Key ids containing : are rejected at keyring load time (: is the ciphertext delimiter), and the migration authenticates a row with Decrypt before treating it as already current.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/MHSanaei/3x-ui/pull/6186 **Author:** [@n0ctal](https://github.com/n0ctal) **Created:** 8/8/2026 **Status:** ✅ Merged **Merged:** 8/15/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `upstream-node-token-encryption` --- ### 📝 Commits (2) - [`4e5d4a4`](https://github.com/MHSanaei/3x-ui/commit/4e5d4a4f8ed98b57a95602d20fb64d821d00b023) node: encrypt outbound bearer token at rest - [`9e9fe25`](https://github.com/MHSanaei/3x-ui/commit/9e9fe252d2ebd15274b82e5a2b751c5584158132) fix(nodes): keep bearer tokens encrypted throughout ### 📊 Changes **9 files changed** (+982 additions, -15 deletions) <details> <summary>View changed files</summary> 📝 `internal/config/config.go` (+21 -0) ➕ `internal/crypto/nodetoken/keysource.go` (+115 -0) ➕ `internal/crypto/nodetoken/nodetoken.go` (+236 -0) ➕ `internal/crypto/nodetoken/nodetoken_test.go` (+226 -0) 📝 `internal/web/runtime/remote.go` (+6 -1) 📝 `internal/web/service/node.go` (+118 -8) ➕ `internal/web/service/node_token_encryption_test.go` (+180 -0) 📝 `main.go` (+68 -6) ➕ `main_test.go` (+12 -0) </details> ### 📄 Description ## Summary Add opt-in encryption at rest for the outbound node API token, with an explicit three-state policy (`off` / `migration` / `required`) and a rotatable keyring. Default is `off`. Like the port-reservation PR, this is a proposal — it adds a package and a key file, so I would rather agree the shape than land it unannounced. ## Why `nodes.api_token` is the bearer token the panel presents to each worker. It is stored in plaintext, so anyone who reads the database reads a live credential for every node: a database backup, a copied SQLite file, a support dump, a restore onto a machine that should not have had it, an operator with read access to the panel database but not to the nodes. The token is not derivable, expiring, or scoped — possession is sufficient to drive the node's API. That makes the panel database a single artefact whose disclosure compromises every node behind it, and database copies move around far more casually than node credentials do. ## Scope - New `internal/crypto/nodetoken`: AEAD encrypt/decrypt bound to the node id, plus a `Keyring` with key ids so a token can be re-encrypted under a new key without a flag day. - `KeySource` with two implementations: a mode-0600 JSON key file (`/etc/x-ui/node_token_key.json`, override with `XUI_NODE_TOKEN_KEY_FILE`) and a single-key environment variable (`XUI_NODE_TOKEN_KEY`). The key is never accepted as a command-line argument, where it would be visible in `ps`. - `NODE_TOKEN_ENCRYPTION` policy: - `off` (default) — plaintext, exactly as today. - `migration` — read plaintext *and* ciphertext, always write ciphertext. This is the path onto encryption without downtime. - `required` — the key must load or startup fails. - The policy is explicit on purpose: a deployment that has already encrypted its tokens must not silently fall back to plaintext because a key file went missing. That failure should be loud. - `node.go` encrypts on write and decrypts on read inside the existing transactions; a plaintext row passes through unchanged so a partially migrated table keeps working. ## Validation - `go build ./internal/...` clean. - `go test ./internal/crypto/nodetoken/ ./internal/web/service/` green on `main` at `ece16559`. - Tests cover round-trip, node-id binding (a token encrypted for one node does not decrypt for another), key rotation across ids, malformed keys, and all three policy states including the mixed plaintext/ciphertext table that `migration` has to tolerate. ## Risk None for existing installs: with `NODE_TOKEN_ENCRYPTION` unset the code path is the current one. The operational cost is real and worth stating — enabling this means a key file that has to survive alongside the database, and losing it while in `required` mode means the panel will not start until it is restored or the tokens are reissued. That is a deliberate trade: I would rather a lost key stop the panel than quietly return it to plaintext. If you would prefer a different default or a simpler single-key form without the keyring, I am happy to cut it down. ## Update after review Two corrections to the contract described above: - **`off` mode.** The earlier "byte for byte" wording was too strong. The precise contract is: in `off` mode a stored legacy value is passed through unchanged, including values that happen to begin with the `enc:` marker — those are no longer claimed by the codec under the default policy. - **No plaintext in the initial tuple.** The id-bound ciphertext can only be produced once the row has an id, and the first draft inserted the plaintext and then overwrote it. A transaction does not undo that: PostgreSQL retains the original tuple in the WAL. The insert now carries an empty token and only the encrypted value is written, so the plaintext never reaches storage. - Key ids containing `:` are rejected at keyring load time (`:` is the ciphertext delimiter), and the migration authenticates a row with `Decrypt` before treating it as already current. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 04:06:42 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
MHSanaei/3x-ui#6148
No description provided.