Skip to content

fix(proxy-extras): accept unencoded DB passwords in the v2 migration lock and redact its errors - #46592

Merged
yassin-berriai merged 1 commit into
mainfrom
litellm_v2_lock_password_p1001
Oct 15, 2026
Merged

yassin-berriai merged 1 commit into
mainfrom
litellm_v2_lock_password_p1001

Conversation

@yassin-berriai

@yassin-berriai yassin-berriai commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • v2 resolver boot fails for DB passwords with spaces or quotes
  • The boot error prints the raw database password
  • An unreachable database no longer reports P1001

How it solves it:

  • Percent-encode URL userinfo before psycopg sees it
  • Redact configured DB passwords from coordinator lock errors
  • Name P1001 when the lock cannot reach the server

User Flow

Before: an operator whose DB password has a space or quote cannot boot the proxy, and the error shows the password

  1. They start the proxy with --use_v2_migration_resolver and DATABASE_URL=postgresql://user:wr ong'pw9@host:5432/db
  2. Boot exits 2 with unexpected spaces found in "wr ong'pw9" even though the database is up
  3. With the database down, the same error appears and no P1001 is printed

After: the same URL boots, and failures name the cause without the password

  1. They start the proxy the same way with the same DATABASE_URL
  2. Migrations apply, and http://localhost:4000/ui/?page=playground answers a chat request
  3. With the database down, boot exits 2 with P1001: Can't reach database server at host:port and no password

Files changed

File Change
litellm-proxy-extras/litellm_proxy_extras/utils.py _strip_prisma_query_params now re-encodes the user and password for libpq, also when the URL has no query string. The redaction helpers moved out to _redaction.py and are imported back
litellm-proxy-extras/litellm_proxy_extras/_redaction.py New leaf module holding the existing DB password redaction helpers, moved verbatim so migration_lock.py can use them without importing utils.py (which imports it)
litellm-proxy-extras/litellm_proxy_extras/migration_lock.py Connection setup is split from the held lock. Every psycopg error is redacted, and a connect failure the server never answered is reported as P1001: Can't reach database server at host:port under the existing message prefix

When --use_v2_migration_resolver runs prisma migrate deploy, it first opens a psycopg connection to hold the coordinator lock (added in #46368). psycopg parses the URL itself and rejects an unencoded password that Prisma accepts, before any socket is opened, and the raw psycopg message went straight into the boot error. Prisma never ran, so P1001 was never printed either

flowchart LR
  A[proxy boot, v2 resolver] --> B[_strip_prisma_query_params, now encodes userinfo]
  B --> C[migration_lock psycopg connect]
  C -- connect fails, no server reply --> D[P1001 + redacted detail, exit 2]
  C -- server rejects, e.g. bad password --> E[redacted detail, exit 2]
  C -- lock held --> F[prisma migrate deploy]
Loading

The URL fix lives in _strip_prisma_query_params, so the other psycopg legs that already go through it (migration ledger reads, the Lens safety check, request log index build) accept the same passwords too

Affected release

Regression from #46368, on main only

Linear ticket

Resolves LIT-9639

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • Every automated review on my PR's latest commit has finished, and I have addressed or replied to each of its findings before requesting a maintainer review

Screenshots / Proof of Fix

Same commands on both sides against a local Postgres 16. PYTHONPATH points at this checkout, and the build is identified by whether litellm_proxy_extras.migration_lock exposes _redact_credentials (False at the base, True at the tip). After rebasing onto 84e8204 the unreachable, reachable and wrong-password legs were re-run on both sides with the same results as below (base exits 2 with the password in the error for both the closed port and the live database, the tip prints P1001 for the closed port, applies all 219 migrations on the live database and exits 0, and keeps the wrong-password failure free of P1001 and of every password fragment). The password wr ong'pw9 is a throwaway test value for a throwaway role

Before (84e8204, merge base)

Unreachable database

  1. DATABASE_URL="postgresql://lit9639:wr ong'pw9@127.0.0.1:41127/lit9639" litellm --config /tmp/lit9639.yaml --use_v2_migration_resolver --skip_server_startup
  2. Boot stops with the raw password in the error and no P1001, exit 2
LiteLLM Proxy: Database migration cannot proceed. Lost or could not establish v2 migration coordination with the database: unexpected spaces found in "wr ong'pw9", use percent-encoded spaces (%20) instead
exit=2

Reachable database, same password

  1. DATABASE_URL="postgresql://lit9639:wr ong'pw9@localhost:5432/lit9639" litellm --config /tmp/lit9639.yaml --use_v2_migration_resolver --skip_server_startup
  2. Boot fails the same way even though the database is up, and no migration is applied
LiteLLM Proxy: Database migration cannot proceed. Lost or could not establish v2 migration coordination with the database: unexpected spaces found in "wr ong'pw9", use percent-encoded spaces (%20) instead
exit=2
applied migrations: ERROR:  relation "_prisma_migrations" does not exist

Wrong password

Not run at the base, since the base rejects this URL shape before authentication like the case above

After (2fe5c14)

Unreachable database

  1. Same command against a closed port
  2. Boot names P1001 and the address, the password is absent, exit 2
LiteLLM Proxy: Database migration cannot proceed. Lost or could not establish v2 migration coordination with the database: P1001: Can't reach database server at `127.0.0.1:47777`. connection failed: connection to server at "127.0.0.1", port 47777 failed: Connection refused
	Is the server running on that host and accepting TCP/IP connections?
exit=2

Reachable database, same password

  1. Same command against the running database
  2. All migrations apply and setup completes
LiteLLM: Setup complete. Skipping server startup as requested.
exit=0
applied migrations: 219
  1. Full proxy boot on the same URL (python litellm/proxy/proxy_cli.py --config /tmp/lit9639_proxy.yaml --port 4000 --use_v2_migration_resolver, model openai/gpt-5.4-mini), then a real call
$ curl -s localhost:4000/v1/chat/completions -H "Authorization: Bearer sk-lit9639-master" -H "Content-Type: application/json" -d @body.json
{"model":"gpt-5.4-mini","messages":[{"role":"user","content":"Reply with the single word: pong"}]}
{"id": "chatcmpl-EZ5bAgecT9br86tLcxxgcWdQsLrd8", "model": "gpt-5.4-mini", "content": "pong"}
$ curl -s localhost:4000/health/readiness
{'status': 'healthy', 'db': 'connected'}
  1. Open http://localhost:4000/ui/?page=playground, log in as admin with the master key, pick gpt-5.4-mini, send Reply with the single word: pong. The reply comes back through the proxy that booted from the unencoded-password URL

Playground reply on the proxy booted with the unencoded-password DATABASE_URL

  1. Open http://localhost:4000/ui/?page=logs. The Success row for that request is stored in the same database. The Failure rows below it are requests from a stale browser session token issued by an earlier database and are unrelated to this change

Logs page showing the stored request

Wrong password

  1. DATABASE_URL="postgresql://lit9639:wr ong'pw8@localhost:5432/lit9639" litellm --config /tmp/lit9639.yaml --use_v2_migration_resolver --skip_server_startup
  2. The server answered, so no P1001, and the password is absent, exit 2
LiteLLM Proxy: Database migration cannot proceed. Lost or could not establish v2 migration coordination with the database: connection failed: connection to server at "127.0.0.1", port 5432 failed: FATAL:  password authentication failed for user "lit9639"
exit=2

TLS failure on a reachable database

  1. DATABASE_URL="postgresql://lit9639:wr ong'pw9@localhost:5432/lit9639?sslmode=verify-full&sslrootcert=/nonexistent" with the same command
  2. At 66a77fe this printed P1001: Can't reach database server at 127.0.0.1:5432 before the certificate detail. Now the label is gone, the detail stays, exit 2
LiteLLM Proxy: Database migration cannot proceed. Lost or could not establish v2 migration coordination with the database: connection failed: connection to server at "127.0.0.1", port 5432 failed: root certificate file "/nonexistent" does not exist
exit=2
  1. An unresolvable host named certificate-db.invalid prints P1001: Can't reach database server at certificate-db.invalid:5432, exit 2
  2. A closed port with ?sslmode=require still prints P1001 and exits 2, and the reachable database without sslmode still exits 0

Tests: tests/integration/database/test_v1_migration_error_logging.py::test_v2_resolver_unreachable_database_exits_2_and_names_p1001 (the CI failure from the ticket) now passes unchanged, and new cases cover the reachable boot, the wrong password, the libpq URL rewrite and the lock error. Mutation checks: dropping the userinfo encoding, the redaction, the P1001 branch, or the FATAL: guard each fails at least one of them

Taxonomy audit (A-BB) on the final diff

M3 fixed: the FATAL: check ran on redacted text, so a password containing FATAL could turn an auth failure into P1001. It now reads the raw psycopg text, with a regression test

M1 fixed: raise ... from exc kept the raw psycopg error in __cause__, so a traceback printed the password. Both wrappers now use from None, and the integration test asserts the formatted traceback has no password fragment

H5, H6 not changed: _REDACTED was already a module constant in utils.py and moved as is. litellm-proxy-extras is a separate package and cannot import litellm.constants

F3 covered: the other psycopg legs (ledger reads, Lens check, request log index build) already go through _strip_prisma_query_params, so they get the same URL handling

Greptile P2 fixed: a reachable server with a TLS or certificate error was labeled P1001. Messages carrying SSL or certificate now keep the plain detail, with unit tests for three TLS shapes and a Connection refused control

Greptile P2 fixed: a hostname such as certificate-db.internal hid the P1001 label. The markers are now matched with quoted segments removed, since libpq and psycopg quote hosts, users and file paths

U3, U6 checked: userinfo is decoded then re-encoded, so already-encoded passwords keep their meaning, and URLs built by construct_database_url_from_env_vars with quote_plus reach libpq exactly as before

T1-T5, T17 checked: no assertion was removed under tests/ relative to the merge base, and the core integration case fails at the base

Every other item was judged not applicable to this diff

Type

🐛 Bug Fix

Caveats (if any)

Low

  • P1001 vs server-answered relies on libpq's FATAL:, SSL, certificate text
    • Non-English lc_messages could show P1001 for an auth failure
  • URL fix touches userinfo only, query handling unchanged

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

Link to Devin session: https://app.devin.ai/sessions/d7f3f94494444264a7f763ef1a2907ea
Open in Devin Desktop: https://app.devin.ai/desktop/session/d7f3f94494444264a7f763ef1a2907ea?variant=devin
Requested by: @yassin-berriai

@devin-ai-integration

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@yassin-berriai

Copy link
Copy Markdown
Contributor Author

@greptileai please review this PR at 66a77fe

@greptile-apps

greptile-apps Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Critical impact] The PR appears safe to merge; no actionable issue remains.

Summary

This PR makes database URLs with spaces or quotes in credentials usable by the migration coordinator.

  • Database URLs with special-character passwords work with psycopg.
  • Migration lock errors hide passwords and label unreachable databases.

Both earlier, unnumbered findings are addressed. No new actionable issue was found. The full PR diff was reviewed because the rebased head did not pass the previous-review ancestry checks. Tests were inspected but not run.

Reviews (4) · Last reviewed commit: "fix(proxy-extras): accept unencoded DB p..." · Reviewed by Greptile

@codecov

codecov Bot commented Oct 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@devin-ai-integration
devin-ai-integration Bot force-pushed the litellm_v2_lock_password_p1001 branch from 66a77fe to 8fd989a Compare October 15, 2026 03:22
@yassin-berriai

Copy link
Copy Markdown
Contributor Author

@greptileai please re-review at 8fd989a82e, TLS and certificate errors no longer get the P1001 label

@yassin-berriai

Copy link
Copy Markdown
Contributor Author

@greptileai please re-review at 5022e26, TLS markers now ignore quoted hostnames so unreachable hosts keep P1001

…lock and redact its errors

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration
devin-ai-integration Bot force-pushed the litellm_v2_lock_password_p1001 branch from 5022e26 to 2fe5c14 Compare October 15, 2026 09:01
@yassin-berriai

Copy link
Copy Markdown
Contributor Author

@greptileai review at 2fe5c14 after rebasing onto main

@yassin-berriai

Copy link
Copy Markdown
Contributor Author

@litereview code-review

@litereview

litereview Bot commented Oct 15, 2026

Copy link
Copy Markdown

✅ code-review

5/5 · Ready for human review

Scope: full · Full PR review.

Completed · Some checks not run.

No correctness findings.

Checks and execution details
  • Passed · Real testing evidence (required): Not applicable: no LLM translation changes in the scoped paths. The PR only touches litellm-proxy-extras/litellm_proxy_extras/{_redaction.py,migration_lock.py,utils.py} and tests. It changes nothing under litellm/llms/** or litellm/litellm_core_utils/prompt_templates/**.
  • Passed · Unit tests for proxy-extras utils (optional): Ran pytest tests/unit/litellm_proxy_extras/test_litellm_proxy_extras_utils.py --noconftest in a fresh venv with psycopg[binary], the editable litellm-proxy-extras and the editable litellm installed: 163 passed. Without litellm installed, 16 redaction tests failed because _secret_shape_redactor falls back to identity. That failure comes from the environment and also happens on the base.
  • Passed · Userinfo encoding probe (optional): I checked _strip_prisma_query_params against psycopg.conninfo.conninfo_to_dict with these URLs: unencoded space/quote passwords, an unencoded @​, +, a pre-encoded %40 user, a multi-host netloc and a host-less socket URL. In every case psycopg parsed the user and password correctly and kept the host and port unchanged.
  • Not run · Postgres-backed integration tests (tests/integration/database/test_v1_migration_error_logging.py) (optional): These tests need a live Postgres and Prisma toolchain, and neither is available in this sandbox. The new code path (connect split from the held lock, _coordination_failure classification) has unit coverage and was inspected by hand.

View review · Reviewed 2fe5c14e60cd

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.

3 participants