pgadmin4 icon indicating copy to clipboard operation
pgadmin4 copied to clipboard

incorrect use of PGADMIN_CONFIG_CONFIG_DATABASE_URI in entrypoint.sh

Open domfe opened this issue 6 months ago • 3 comments

Please note that security bugs or issues should be reported to [email protected].

Describe the bug

I have an error when starting pgadmin container (dpage/pgadmin:9.15.0).

` Traceback (most recent call last): File "", line 1, in from check_external_config_db import check_external_config_db; val = check_external_config_db("'postgresql+psycopg://user:pass@localhost:5432/pgadmin'"); print(val)

File "/pgadmin4/pgadmin/utils/check_external_config_db.py", line 18, in check_external_config_db engine = create_engine(database_uri) File "", line 2, in create_engine File "/venv/lib/python3.14/site-packages/sqlalchemy/util/deprecations.py", line 281, in warned return fn(*args, **kwargs) # type: ignore[no-any-return] File "/venv/lib/python3.14/site-packages/sqlalchemy/engine/create.py", line 564, in create_engine u = _url.make_url(url) File "/venv/lib/python3.14/site-packages/sqlalchemy/engine/url.py", line 856, in make_url return _parse_url(name_or_url) File "/venv/lib/python3.14/site-packages/sqlalchemy/engine/url.py", line 922, in _parse_url raise exc.ArgumentError( "Could not parse SQLAlchemy URL from given URL string" ) sqlalchemy.exc.ArgumentError: Could not parse SQLAlchemy URL from given URL string `

The check for PGADMIN_CONFIG_CONFIG_DATABASE_URI in entrypoint.sh is incorrect. The line 153 put double quotes around environment variable but the var should be specified with single quotes for correct generation of config_distro.py.

external_config_db_exists=$(cd /pgadmin4/pgadmin/utils && $SU_EXEC /venv/bin/python3 -c "from check_external_config_db import check_external_config_db; val = check_external_config_db(\"${PGADMIN_CONFIG_CONFIG_DATABASE_URI}\"); print(val)")

The error doesn't prevent the start of pgadmin.

To Reproduce

Steps to reproduce the behavior:

  1. configure PGADMIN_CONFIG_CONFIG_DATABASE_URI
  2. start container
  3. check container log

Expected behavior

No error reported

domfe avatar May 28 '26 10:05 domfe

Indeed: without quotes config.py fails with "SyntaxError: invalid syntax" on CONFIG_DATABASE_URI = postgresql+psycopg://…, but with quotes in docker variable, double quotes are used in check_external_config_db("'postgresql+psycopg://…'"), which confuses SQLAlchemy. Manually overriding entrypoint.sh with a copy with quotes removed from line 153 fixes this error, but runs into another problem, where PGADMIN_DEFAULT_EMAIL and PGADMIN_DEFAULT_PASSWORD are not used anymore.

angs avatar Jun 03 '26 10:06 angs

Thanks for the report — and to @angs for the follow-up confirming the PGADMIN_DEFAULT_EMAIL/PGADMIN_DEFAULT_PASSWORD regression that comes with it. Both symptoms have the same root cause; diagnosed and fixed in draft PR #10009.

Diagnosis

pkg/docker/entrypoint.sh constructs a Python -c argument by shell-substituting ${PGADMIN_CONFIG_CONFIG_DATABASE_URI} inside a double-quoted Python string:

val = check_external_config_db("${PGADMIN_CONFIG_CONFIG_DATABASE_URI}")

That collides with the long-standing config_distro.py convention, which requires the env var's value to be a Python literal (so the entrypoint's CONFIG_DATABASE_URI = $val append produces valid Python). So users correctly set the env var to 'postgresql+psycopg://...'. The entrypoint then re-wraps that literal in another set of quotes, producing check_external_config_db("'postgresql+psycopg://...'") — a string with literal single quotes inside, which SQLAlchemy can't parse → ArgumentError.

The PGADMIN_DEFAULT_EMAIL symptom is the same crash propagating downstream: Python raises, $(...) captures empty stdout, so external_config_db_exists="". The later check [ "${external_config_db_exists}" = "False" ] then evaluates to false (empty ≠ "False"), so the entire first-launch user-setup block at line 224 is silently skipped — and your default email/password are never applied.

Regression history

Bisected to commit 1fe840fca ("Allow to pass PGADMIN_CONFIG_CONFIG_DATABASE_URI from docker secrets", #5869, Oct 2024), which incidentally added the shell "…" quoting around the expansion in an otherwise focused docker-secrets PR. The pre-Oct-2024 form (bare ${VAR} expansion, ed211a2bb, Sep 2024) worked correctly with the convention.

Why both env-var forms are broken on shipped images

Env var form Result
PGADMIN_CONFIG_CONFIG_DATABASE_URI="'postgresql+psycopg://…'" (with quotes, the documented convention) SQLAlchemy ArgumentError + silent PGADMIN_DEFAULT_EMAIL skip — what you hit
PGADMIN_CONFIG_CONFIG_DATABASE_URI=postgresql+psycopg://… (without quotes) config_distro.py becomes invalid Python → container exits on startup — what @angs hit

Fix shape (in PR #10009)

Removes the shell from the quoting question altogether — Python reads the env var via os.environ and uses ast.literal_eval to unwrap any Python-literal quoting. All three forms ('url', "url", raw url) now parse to the same clean URI and round-trip through check_external_config_db correctly. The "False" default is also preserved on any Python failure, fixing the secondary symptom.

Workaround until the fix lands

Either pin a pre-Oct-2024 image (e.g. v9.13), or override pkg/docker/entrypoint.sh lines 197-201 in your image with this:

external_config_db_exists="False"
if [ -n "${PGADMIN_CONFIG_CONFIG_DATABASE_URI}" ]; then
    result=$(cd /pgadmin4/pgadmin/utils && $SU_EXEC /venv/bin/python3 -c "
import os, ast
from check_external_config_db import check_external_config_db
raw = os.environ['PGADMIN_CONFIG_CONFIG_DATABASE_URI']
try:
    uri = ast.literal_eval(raw)
except (ValueError, SyntaxError):
    uri = raw
print(check_external_config_db(uri))
" 2>/dev/null)
    if [ -n "$result" ]; then
        external_config_db_exists="$result"
    fi
fi

That's the patch PR #10009 lands. Restores both the URI parse path and the default-user creation path.

asheshv avatar Jun 07 '26 04:06 asheshv

This was fixed and is included in pgAdmin 4 9.16 (via commit c88c8f612). The fix is listed in the 9.16 release notes; the auto-close keyword on the merged PR didn't fire, so closing manually. Please reopen if you still see it on 9.16+.

dpage avatar Jun 09 '26 14:06 dpage