[PR #6195] [MERGED] fix(nodes): persist the master mTLS credential atomically and stop silent reissue #6156

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

📋 Pull Request Information

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

Base: main ← Head: upstream-mtls-credential-persistence


📝 Commits (3)

  • 427c0c4 fix: harden master mTLS credential persistence
  • 2000e00 fix(mtls): validate and serialize credential persistence
  • 82260a2 Merge branch 'main' into upstream-mtls-credential-persistence

📊 Changes

4 files changed (+225 additions, -2 deletions)

View changed files

📝 internal/web/service/setting.go (+1 -0)
📝 internal/web/service/setting_factory_defaults_test.go (+1 -0)
📝 internal/web/service/setting_mtls.go (+74 -2)
📝 internal/web/service/setting_mtls_test.go (+149 -0)

📄 Description

Summary

Make the master mTLS credential persist atomically and refuse to silently reissue when the stored state is incomplete or inconsistent.

Why

EnsureMasterClientCert stores the certificate, the key and the pin as separate writes. A failure between them leaves a state nobody planned for: a pin without a credential, or a credential that does not match the pin. On the next start the panel cannot tell "never issued" from "issued and partly lost", and mints a replacement — silently stranding every node that already enforces the old leaf identity.

Scope

  • Certificate, key and pin are written in one transaction.
  • A mutex around the ensure path, so two concurrent callers cannot both decide to mint.
  • Strict PEM fingerprinting rather than a substring comparison.
  • An existing pair with no stored pin gets the pin backfilled instead of being treated as absent.
  • An incomplete or mismatched credential is a loud error, not a reissue.

Validation

  • go test ./... green; go test -race ./internal/web/service -run TestEnsureMasterClientCert green.
  • Injected SQLite trigger failure on the pin write: the transaction rolls back and cert, key and pin are all left empty rather than partially written.

Not proven: the same rollback was not separately provoked on PostgreSQL. It is verified on SQLite and follows from the shared GORM transaction path.

Risk

Low. An installation with a complete, consistent credential sees no change. The new refusals replace cases that previously produced a silent reissue.

Update after review

The premise in the Why section above overstated the consequence, and the code now matches reality rather than that premise:

  • Upstream nodes verify the client chain against ClientCAs (internal/web/web_mtls.go) and API auth accepts any verified chain. A replacement leaf is therefore trusted by existing nodes as long as the CA pair survives — a lost leaf is a recoverable condition, not the fleet-wide outage the original wording implied.
  • Accordingly the sentinel no longer turns a recoverable leaf loss into a hard failure; reissue proceeds when the CA is intact.
  • What is now rejected is a genuinely broken pair: the certificate and private key are validated together before a missing pin is backfilled, so the panel cannot report readiness for a credential that would later fail in tls.X509KeyPair.
  • One-mint concurrency is now exercised by a test that releases several callers together and asserts they observe the same certificate.

🔄 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/6195 **Author:** [@n0ctal](https://github.com/n0ctal) **Created:** 8/10/2026 **Status:** ✅ Merged **Merged:** 8/14/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `upstream-mtls-credential-persistence` --- ### 📝 Commits (3) - [`427c0c4`](https://github.com/MHSanaei/3x-ui/commit/427c0c4ca3de6e443ebb884c3c058efca3c858a4) fix: harden master mTLS credential persistence - [`2000e00`](https://github.com/MHSanaei/3x-ui/commit/2000e00c6cb59e19f6d4936ee92a2b77f9a5a256) fix(mtls): validate and serialize credential persistence - [`82260a2`](https://github.com/MHSanaei/3x-ui/commit/82260a24447bebcd2d23f7f6c96e210816bd1926) Merge branch 'main' into upstream-mtls-credential-persistence ### 📊 Changes **4 files changed** (+225 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `internal/web/service/setting.go` (+1 -0) 📝 `internal/web/service/setting_factory_defaults_test.go` (+1 -0) 📝 `internal/web/service/setting_mtls.go` (+74 -2) 📝 `internal/web/service/setting_mtls_test.go` (+149 -0) </details> ### 📄 Description ## Summary Make the master mTLS credential persist atomically and refuse to silently reissue when the stored state is incomplete or inconsistent. ## Why `EnsureMasterClientCert` stores the certificate, the key and the pin as separate writes. A failure between them leaves a state nobody planned for: a pin without a credential, or a credential that does not match the pin. On the next start the panel cannot tell "never issued" from "issued and partly lost", and mints a replacement — silently stranding every node that already enforces the old leaf identity. ## Scope - Certificate, key and pin are written in one transaction. - A mutex around the ensure path, so two concurrent callers cannot both decide to mint. - Strict PEM fingerprinting rather than a substring comparison. - An existing pair with no stored pin gets the pin backfilled instead of being treated as absent. - An incomplete or mismatched credential is a loud error, not a reissue. ## Validation - `go test ./...` green; `go test -race ./internal/web/service -run TestEnsureMasterClientCert` green. - Injected SQLite trigger failure on the pin write: the transaction rolls back and cert, key and pin are all left empty rather than partially written. Not proven: the same rollback was not separately provoked on PostgreSQL. It is verified on SQLite and follows from the shared GORM transaction path. ## Risk Low. An installation with a complete, consistent credential sees no change. The new refusals replace cases that previously produced a silent reissue. ## Update after review The premise in the Why section above overstated the consequence, and the code now matches reality rather than that premise: - Upstream nodes verify the client chain against `ClientCAs` (`internal/web/web_mtls.go`) and API auth accepts any verified chain. A replacement leaf is therefore **trusted by existing nodes as long as the CA pair survives** — a lost leaf is a recoverable condition, not the fleet-wide outage the original wording implied. - Accordingly the sentinel no longer turns a recoverable leaf loss into a hard failure; reissue proceeds when the CA is intact. - What is now rejected is a genuinely broken pair: the certificate and private key are validated together before a missing pin is backfilled, so the panel cannot report readiness for a credential that would later fail in `tls.X509KeyPair`. - One-mint concurrency is now exercised by a test that releases several callers together and asserts they observe the same certificate. --- <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:45 +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#6156
No description provided.