[PR #6189] [MERGED] fix(web): fallback to default secret when database setting is empty #6151

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

📋 Pull Request Information

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

Base: main ← Head: fix/empty-secret-fallback


📝 Commits (4)

  • ff8dfe8 fix(web): fallback to default secret when database setting is empty
  • 5f31383 Merge branch 'main' into fix/empty-secret-fallback
  • 42c90a3 style(web): format setting_security_test.go with gofumpt
  • 20c753e Merge branch 'main' into fix/empty-secret-fallback

📊 Changes

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

View changed files

📝 internal/web/service/setting.go (+7 -4)
📝 internal/web/service/setting_security_test.go (+25 -0)

📄 Description

Summary

Fixes unhandled HTTP 500 Internal Server Error ("securecookie: hash key is not set") and HTTP 403 Forbidden login failures by ensuring GetSecret() falls back to generating and persisting a default 32-byte secret when the database secret setting is empty.

Why

During database migrations, manual resets, or domain/SSL setup adjustments, if the secret value in SQLite settings becomes an empty string (""), GetSecret() returns empty bytes ([]byte("")). This causes cookie.NewStore in web.go to be initialized with an invalid key, breaking gorilla/securecookie session management and producing 500 errors on /csrf-token and 403 on /login.

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Documentation
  • Tests only
  • Build / CI / tooling
  • Other

Areas affected

  • Frontend (UI / panel pages)
  • Backend (API endpoints, login, settings)
  • Xray config generation
  • Subscription (share links / Clash / JSON)
  • Statistics / traffic counters
  • Database / migrations
  • Install / upgrade script
  • Docker image
  • Multi-node (sub-nodes)
  • Telegram bot

How was this tested?

  1. Added a unit test TestGetSecret_FallbacksOnEmptyDatabaseSetting in internal/web/service/setting_security_test.go that verifies GetSecret() generates and persists a 32-byte string when the database secret key is empty.
  2. Executed unit tests: go test -v ./internal/web/service/ -run TestGetSecret_FallbacksOnEmptyDatabaseSetting (PASSED).
  3. Verified live on a production instance with an empty secret setting: verified /csrf-token returns 200 OK and session authentication completes successfully.

Screenshots / recordings

N/A

Breaking changes

None

Checklist

  • I tested the change locally and confirmed the described behavior.
  • I added or updated tests for the new behavior (when applicable).
  • go build ./... and the test suite pass locally.
  • For frontend changes: npm run lint, npm run typecheck, and npm run build pass.
  • I updated the Wiki / README / API docs if user-facing behavior changed.
  • My commits follow the project's existing message style.
  • I have no unrelated changes mixed into this PR.

🔄 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/6189 **Author:** [@CaMeDoZa](https://github.com/CaMeDoZa) **Created:** 8/8/2026 **Status:** ✅ Merged **Merged:** 8/15/2026 **Merged by:** [@MHSanaei](https://github.com/MHSanaei) **Base:** `main` ← **Head:** `fix/empty-secret-fallback` --- ### 📝 Commits (4) - [`ff8dfe8`](https://github.com/MHSanaei/3x-ui/commit/ff8dfe87861ee377dcc1946110a6abc0dc0f7132) fix(web): fallback to default secret when database setting is empty - [`5f31383`](https://github.com/MHSanaei/3x-ui/commit/5f3138397974c60c095a03a6aa6ffd03aca6c59b) Merge branch 'main' into fix/empty-secret-fallback - [`42c90a3`](https://github.com/MHSanaei/3x-ui/commit/42c90a3e6a00b2bae208ab6194bd521404326434) style(web): format setting_security_test.go with gofumpt - [`20c753e`](https://github.com/MHSanaei/3x-ui/commit/20c753ecfed747e37510783165b9e2510a9d74e7) Merge branch 'main' into fix/empty-secret-fallback ### 📊 Changes **2 files changed** (+32 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `internal/web/service/setting.go` (+7 -4) 📝 `internal/web/service/setting_security_test.go` (+25 -0) </details> ### 📄 Description ## Summary Fixes unhandled `HTTP 500 Internal Server Error` ("securecookie: hash key is not set") and `HTTP 403 Forbidden` login failures by ensuring `GetSecret()` falls back to generating and persisting a default 32-byte secret when the database `secret` setting is empty. ## Why During database migrations, manual resets, or domain/SSL setup adjustments, if the `secret` value in SQLite `settings` becomes an empty string (`""`), `GetSecret()` returns empty bytes (`[]byte("")`). This causes `cookie.NewStore` in `web.go` to be initialized with an invalid key, breaking `gorilla/securecookie` session management and producing `500` errors on `/csrf-token` and `403` on `/login`. ## Type of change - [x] Bug fix - [ ] New feature - [ ] Refactoring (no behavior change) - [ ] Documentation - [x] Tests only - [ ] Build / CI / tooling - [ ] Other ## Areas affected - [ ] Frontend (UI / panel pages) - [x] Backend (API endpoints, login, settings) - [ ] Xray config generation - [ ] Subscription (share links / Clash / JSON) - [ ] Statistics / traffic counters - [x] Database / migrations - [ ] Install / upgrade script - [ ] Docker image - [ ] Multi-node (sub-nodes) - [ ] Telegram bot ## How was this tested? 1. Added a unit test `TestGetSecret_FallbacksOnEmptyDatabaseSetting` in `internal/web/service/setting_security_test.go` that verifies `GetSecret()` generates and persists a 32-byte string when the database `secret` key is empty. 2. Executed unit tests: `go test -v ./internal/web/service/ -run TestGetSecret_FallbacksOnEmptyDatabaseSetting` (PASSED). 3. Verified live on a production instance with an empty `secret` setting: verified `/csrf-token` returns `200 OK` and session authentication completes successfully. ## Screenshots / recordings N/A ## Breaking changes None ## Checklist - [x] I tested the change locally and confirmed the described behavior. - [x] I added or updated tests for the new behavior (when applicable). - [x] `go build ./...` and the test suite pass locally. - [x] For frontend changes: `npm run lint`, `npm run typecheck`, and `npm run build` pass. - [x] I updated the Wiki / README / API docs if user-facing behavior changed. - [x] My commits follow the project's existing message style. - [x] I have no unrelated changes mixed into this PR. --- <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:43 +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#6151
No description provided.