mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-03 21:18:19 +07:00
[PR #6186] [MERGED] feat(nodes): opt-in encryption at rest for the outbound node API token #6148
Labels
No labels
backlog
bug
clarification needed
documentation
duplicate
enhancement
github_actions
good first issue
invalid
javascript
New Feature
PR welcomed
pull-request
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
MHSanaei/3x-ui#6148
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 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)
4e5d4a4node: encrypt outbound bearer token at rest9e9fe25fix(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 isoff.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_tokenis 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
internal/crypto/nodetoken: AEAD encrypt/decrypt bound to the node id, plus aKeyringwith key ids so a token can be re-encrypted under a new key without a flag day.KeySourcewith two implementations: a mode-0600 JSON key file (/etc/x-ui/node_token_key.json, override withXUI_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 inps.NODE_TOKEN_ENCRYPTIONpolicy: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.node.goencrypts 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 onmainatece16559.migrationhas to tolerate.Risk
None for existing installs: with
NODE_TOKEN_ENCRYPTIONunset 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
requiredmode 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:
offmode. The earlier "byte for byte" wording was too strong. The precise contract is: inoffmode a stored legacy value is passed through unchanged, including values that happen to begin with theenc:marker — those are no longer claimed by the codec under the default policy.:are rejected at keyring load time (:is the ciphertext delimiter), and the migration authenticates a row withDecryptbefore treating it as already current.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.