pg.ddx.io  GitHub: pgjdbc/pgjdbc (issues + PRs as email threads)  
help / color / mirror / Atom feed
From: vlsi (@vlsi) <noreply+vlsi@github.com>
To: pgjdbc/pgjdbc <noreply+pgjdbc-pgjdbc@github.com>
Subject: Re: [pgjdbc/pgjdbc] PR #4251: OAuth implementation
Date: Wed, 24 Jun 2026 17:49:28 +0000
Message-ID: <gh-pgjdbc-pgjdbc-4251-c4792136979@github.com> (raw)
In-Reply-To: <gh-pgjdbc-pgjdbc-4251@github.com>
References: <gh-pgjdbc-pgjdbc-4251@github.com>

Thanks for this. The wire protocol is implemented correctly, edge cases are handled defensively, and the TLS/GSS enforcement plus the integration tests are a clear step up from #4087. Notes below, grouped by the review axes and sorted by impact. Nothing here is a blocker; most are doc or design refinements.

## Security

* **A bearer token is a plaintext-equivalent secret, and the encryption check accepts any TLS, not just `verify-full`.** Unlike SCRAM (challenge/response, the password never crosses the wire), the token is sent to the server in clear inside the SASL message, so its exposure model matches `password`. `OAuthAuthenticator.checkTransportEncryption` passes for `getSocket() instanceof SSLSocket || isGssEncrypted()`, which also accepts `sslmode=require` (encryption without certificate verification). An active MITM under `sslmode=require` therefore captures a valid token. RFC 7628 §4 asks for a confidential channel, but encryption is not server authentication. libpq takes the same stance (encryption required, verification left to the user), so this is not a blocker, but for bearer tokens it is worth calling out in the docs that `sslmode=verify-full` is strongly recommended and that `require` leaves the channel open to MITM.
* Good, please keep: `TOKEN_REGEX` (RFC 6750 §2.1) rejects `SOH`/`CR`/`LF`/space, which prevents injecting extra `kvpair`s into the SASL message. This is an important property.
* Good: the token is not logged. The `FINEST` line logs the literal `auth=Bearer <token>`, not the value.
* A static `oauthToken` passed in the URL lands in the `Connecting with URL: {0}` `FINE` log, exactly like `password`. Worth a doc note that the provider is preferable for that reason.

## Correctness and robustness

* **The discovery branch is currently dead code and the comment is misleading.** In the OAuth branch `resolveOAuthToken` always returns non-null or throws: `isOAuthConfigured` guarantees a token or a provider, a static token is checked for non-emptiness, and a provider returning an empty token throws. So `if (oauthToken != null)` in `ConnectionFactoryImpl` is always true, and the `handleAuthenticationSASL(null)` / `buildTokenMessage(null)` paths never run in production (they live only in the unit tests as scaffolding for discovery). The comment "If token was empty, then server will send discovery information" describes a path that cannot be reached yet. Until discovery lands, either simplify this or mark it explicitly as scaffolding, so the next reader does not have to reverse-engineer it.
* **The error message is misleading when `requireAuth` is the real cause.** When the server offers only OAUTHBEARER and `requireAuth=!oauth-bearer`, the user gets "the server requested SASL authentication with mechanisms [OAUTHBEARER], but non of them configured or supported by the driver", even though the actual reason is that `requireAuth` forbids the method. This is baked into the `requireAuthForbidsOAuth` test. The MD5 and password paths throw the specific "Authentication method is not allowed by requireAuth"; the OAuth/SCRAM routing should surface the same reason here.
* Verified the SASL exchange against the PG protocol for all three outcomes: success (`SASLInitialResponse` then `AuthenticationOk`), failure (`SASLContinue` with the error JSON, then the dummy `0x01`, then `ErrorResponse`), and the defensive `PROTOCOL_VIOLATION` on an unexpected `SASL_FINAL` for OAuth. `setFinishedAuthenticationRequests()` correctly stops the final `AuthenticationOk` from being treated as trust/NONE. `requireAuth` downgrade protection is preserved (OAuth routes via the non-throwing `isAllowed`, while the other branches still throw via `checkAuth`). Moving the mechanism read into `readMechanisms` and passing the list to `ScramAuthenticator` is clean and does not change SCRAM behavior.

## Performance

* **`OAuthTokenProvider.getToken()` runs on every connection, and the provider is re-instantiated each time.** `resolveOAuthToken` creates the provider via `ObjectFactory.instantiate` on each `doAuthentication`, so an implementation cannot cache a token in an instance field (only in a static). If the implementation fetches a token over the network, that is per-connect latency, which hurts most under a connection pool. The interface defines no caching or TTL contract. At minimum document the expectation to cache; ideally reuse the provider instance.
* Minor: `ScramMechanism.supportedMechanisms()` is recomputed for every element of the stream (it allocates a fresh `List` plus an `O(n)` `contains`). Hoist it into a local before the `anyMatch`.

## RFC compliance

All checked and correct: the RFC 7628 §3.1 message shape (`n,,` gs2-header, `\x01`, `auth=Bearer <token>`, `\x01\x01`), the RFC 6750 §2.1 `b64token` validation (covers base64url JWTs with dots), the §3.2.3 dummy `\x01` response on error (length 5, payload `0x01`), and omitting the optional `host`/`port` kvpairs as libpq does.

## libpq compliance

* The TLS-or-GSS requirement with an override (`oauthAllowUnencrypted`) matches libpq and the pgsql-hackers discussion you linked. GSS as a confidential channel is accepted too, which also matches.
* Please confirm the `requireAuth` keyword. pgjdbc uses `oauth-bearer`; it is worth checking how the method is named in libpq's `require_auth` (it may be `oauth`) so that users migrating from libpq are not surprised. Align or document the difference.

## Extensibility

* **`OAuthTokenProvider.getToken()` receives no context, which will force a breaking change when discovery lands.** libpq in PG 18 hands its custom hook a `PGoauthBearerRequest` with `issuer` / `scope` / `openid-configuration` (obtained during discovery). Here `getToken()` takes no arguments, so when you add discovery in a follow-up the interface will have to change incompatibly to pass issuer/scope/host/user. Designing it now as `getToken(OAuthBearerRequest ctx)` (host, user, requested scope/issuer, some fields empty for now) avoids breaking a public plugin API later. As written the provider also cannot see the `HostSpec` or user, so a single implementation cannot issue different tokens per host.
* No built-in Device Authorization Grant (RFC 8628). Understood as deliberately deferred.

## Test infrastructure and maintenance

* **`platform: linux/amd64` is pinned on the shared `pgdb` service, which every docker scenario uses (SSL, XA, SCRAM, replication), not just OAuth.** The only reason it is needed is that `pg_oidc_validator` ships an amd64-only `.deb`, installed by `entrypoint.sh` when `OAUTH=yes`. As written it forces amd64 emulation for all docker test runs on arm64 hosts (Apple Silicon) even when `OAUTH=no`, slowing everyone down for a dependency they never load. On CI it is a no-op, since `ubuntu-latest` runners are already amd64. Suggestion: keep the native platform by default and make the pin opt-in for the OAuth path, for example `platform: ${PG_PLATFORM:-}` (empty resolves to the daemon default, i.e. native) with `PG_PLATFORM=linux/amd64` exported by `docker/bin/oauth-server` and the CI oauth job. Longer term, an arm64 build of the validator (or building it in the entrypoint) removes the constraint entirely.
* Mutable, non-reproducible test dependencies will eventually flake CI: the `.deb` is fetched from the `latest` release tag (`Percona-Lab/pg_oidc_validator/releases/download/latest/...`) with no checksum, and `keycloak:latest` is unpinned. Pin both to concrete versions.
* The test-only relaxations (`sslRequired: "none"`, `issuer=http://...`, `KC_HOSTNAME_STRICT: "false"`) are fine for tests, but please make sure they do not get copied into production-facing docs as an example config.

## Wording nits

* `ConnectionFactoryImpl`: "but non of them configured or supported by the driver" should read "but none of them are configured or supported by the driver" (and update the `assertEquals` in `OAuthTest.requireAuthForbidsOAuth`, which pins the current text).
* Commit message: "OAuth berarer authentication" should be "bearer".


view thread (168+ messages)  latest in thread

Message-ID: <gh-pgjdbc-pgjdbc-4251-c4792136979@github.com>
Permalink:  ../gh-pgjdbc-pgjdbc-4251-c4792136979@github.com/

 · 


This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox