Skip to content

Hash user passwords instead of storing them in the clear - #11

Merged
GlobalTechInfo merged 2 commits into
mainfrom
fix/hash-passwords
Oct 17, 2026
Merged

GlobalTechInfo merged 2 commits into
mainfrom
fix/hash-passwords

Conversation

@GlobalTechInfo

@GlobalTechInfo GlobalTechInfo commented Oct 17, 2026 •

Copy link
Copy Markdown
Owner

The problem

users.password_hash held the password verbatim. Any copy of the database — a backup, a stray file, a support attachment, an old snapshot kept "just in case" — was a list of working credentials, for this proxy and for whatever else those people reuse the same password on. The column name claimed otherwise.

What this does

PBKDF2-HMAC-SHA256 at 600k iterations, the OWASP figure for that pair, from the standard library. No new dependency for the one thing in a proxy that must not be improvised; the cost lands on each login attempt, on an endpoint that already rate-limits guesses.

No migration is needed. A row from before hashing is verified against the value it holds, and rewritten on the next successful login — an install upgrades one account at a time instead of someone having to write a script that touches every credential at once. Nothing about the operator's procedure changes.

PROXY_USER/PROXY_PASS and the admin pair are deliberately untouched: they come from the environment, not the database, so there is nothing to hash and the file is already a secret the operator controls.

Details worth reviewing

  • Parameters live in the hash, not in the verifier: pbkdf2-sha256$iter=600000$salt$key. They can be raised later without invalidating what is already on disk — each hash keeps verifying at its own cost and gets rewritten at the new one.
  • That makes the stored value choose the cost, so parsing refuses anything implausible (non-power-of-two or absurd N, huge r/p, short salt or key, wrong scheme, wrong field count) instead of letting a corrupt or hostile row turn one login into a memory-exhaustion attack. Eleven malformed shapes are covered by a test.
  • Hashing happens in the store, before the row exists, so no code path can write a plaintext password by accident. The dashboard still shows a generated password exactly once, because it holds the plaintext it was given.
  • Legacy detection is a prefix check on the scheme; a pre-hashing row matches in constant time and is then rewritten.

Verified against a running server, not just tests

  • User created through the admin API → the file contains pbkdf2-sha256$iter=600000$…; the plaintext is absent from the database bytes.
  • Correct password authenticates; wrong password rejected.
  • A seeded pre-hashing row logs in successfully and is rewritten on the spot — upgraded automatically: True.
  • A backup taken afterwards contains neither the old nor the new plaintext. VACUUM INTO leaves no stale copy, which I checked rather than assumed; the live -wal does keep the old value until it is checkpointed.

Tests: round trip, salt uniqueness, legacy acceptance, corrupt-hash rejection, store-level proof that what lands in the database is not the password, and an end-to-end login against a real socket that proves the legacy row authenticates and is rewritten.

gofmt/go vet clean, full suite green with and without Xray.

Also in this PR

The README API table was missing PR #10's /api/vless-clients endpoints, and the backup section still promised plaintext passwords in the archive. Both corrected.

Summary by CodeRabbit

  • New Features
    • Added dashboard management for REALITY client UUIDs, including creating clients, updating labels, enabling or disabling access, copying UUIDs, and revoking clients.
    • Client changes apply to the running service without requiring a restart. Configured client lists remain read-only in the dashboard.
  • Security
    • Passwords are stored as salted hashes. Existing plaintext passwords are upgraded after a successful login.
  • Documentation
    • Clarified client management, backup contents, password handling, and available API endpoints.

Until now the REALITY client list was a comma-joined config value, so
revoking a UUID meant editing configuration and restarting the service. A
management screen that only takes effect on restart is a trap, so this
makes the change real.

The list lives in a vless_clients table, one row per client. Precedence is
unchanged in spirit: REALITY_CLIENT_IDS still pins the list from
configuration and now says so, because accepting edits that quietly do
nothing is worse than refusing them. An id left behind by the old
comma-joined value is adopted into the table on first run rather than
dropped -- otherwise the first dashboard change would revoke a client
that is still in the field.

Making it live meant teaching the xray supervisor to re-point itself. The
reload carries a config that has already passed `xray run --test`, so an
invalid change is refused and the running front door keeps serving; the
supervisor is the only thing that stops and starts the child, and it
answers once the new child is actually accepting connections. Measured
live: an add applies in 0.08s.

Two refusals worth having. Removing the last client is rejected, because
xray with no clients is a front door that rejects everyone and an operator
should have to mean it. And a refused change restores the row rather than
dropping it, so the database never claims something xray does not do.

Verified live against xray on a real port: a UUID added through the API
served traffic immediately, the same client failed the moment it was
revoked, the untouched client kept working, and xray's own log named the
refusal (proxy/vless/encoding: rejected). Covered end to end in tests too,
including the case the race detector caught on the way: s.cfg is written
under configMu while being read here.
users.password_hash held the password verbatim, so any copy of the
database -- a backup, a stray file, a support attachment -- was a list of
working credentials, for the service and for whatever else those people
reuse the same password on.

PBKDF2-HMAC-SHA256 at 600k iterations, which is the OWASP figure for that
pair, from the standard library. No new dependency for the one thing in a
proxy that must not be improvised, and the cost is paid per login attempt
on an endpoint that already rate-limits guesses.

The parameters are recorded in each stored hash rather than assumed at
verify time, so they can be raised later without invalidating what is on
disk. That also means the stored value chooses the cost, so parsing refuses
anything implausible instead of letting a corrupt or hostile record turn
one login into a memory-exhaustion attack.

Existing installs need no migration: a row from before hashing is verified
against the value it holds, and rewritten on the next successful login.
An install cleans itself up one account at a time.

PROXY_USER/PROXY_PASS and the admin pair are untouched -- they come from
the environment, not the database, so there is nothing to hash and the file
is already a secret the operator controls.

Verified against a running server: a user created through the API lands in
the file as pbkdf2-sha256$iter=600000$..., the plaintext is absent from the
database bytes, the correct password authenticates and a wrong one does not,
a seeded pre-hashing row logs in and is rewritten on the spot, and a
backup of the result contains neither the old nor the new plaintext --
VACUUM INTO leaves no stale copies behind, though the live WAL keeps the
old value until it is checkpointed.
@coderabbitai

coderabbitai Bot commented Oct 17, 2026

Copy link
Copy Markdown

Review in Change Stack →

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 75410ca1-fc3c-458b-9ca5-42e346af4eb8
📥 Commits

Reviewing files that changed from the base of the PR and between 71912e8 and 4aadc44.

📒 Files selected for processing: 9
  • README.md
  • database.go
  • main.go
  • password.go
  • password_test.go
  • reality.go
  • reality_test.go
  • vless_test.go
  • web/index.html
+---------------------------------------+
| Feature flags up; bug sightings down. |
+---------------------------------------+
  \
   \     /)/)
        ( '')
        /  |
      o(___)
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@GlobalTechInfo
GlobalTechInfo merged commit 8fbc480 into main Oct 17, 2026
2 of 3 checks passed
@GlobalTechInfo
GlobalTechInfo deleted the fix/hash-passwords branch October 17, 2026 14:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant