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, 09 Sep 2026 15:45:35 +0000
Message-ID: <gh-pgjdbc-pgjdbc-4251-r5156617774@github.com> (raw)
In-Reply-To: <gh-pgjdbc-pgjdbc-4251@github.com>
References: <gh-pgjdbc-pgjdbc-4251@github.com>

I ran this branch against PostgreSQL 18.6 with `pg_oidc_validator` and Keycloak in Docker. All eight tests in `OAuthTest` pass, `./gradlew styleCheck` passes, and `-PenableCheckerframework classes` passes. The client message matches libpq byte for byte, including the discovery form `n,,\x01auth=\x01\x01`; `TOKEN_REGEX` matches the `b64token` grammar of RFC 6750 section 2.1; zeroing the token before `flush()` is safe, because `PgBufferedOutputStream` and `GSSOutputStream` both copy; and `OAuthAuthenticatorTest` covers the whole transport decision table with a negative control. The docker-compose work made all of this reproducible in a few minutes, so thank you for that.

Three findings below are blocking. I reproduced each one against a fake backend or the live server rather than reading it out of the code.

### A server that rejects the token still gets an authenticated connection

A fake backend sends `AuthenticationSASLContinue` carrying `{"status":"invalid_token"}`, reads the mandatory `0x01` response, and then sends `AuthenticationOk`:

```text
SERVER: sent AuthenticationSASLContinue(error json)
WARNING: Server rejected OAuth bearer token: {"status":"invalid_token","scope":"openid"}
SERVER: got 'p' dummy=[1]
SERVER: sent AuthenticationOk + ReadyForQuery AFTER the error
SERVER: post-auth client message Q:SET application_name = 'PostgreSQL JDBC Driver'
CLIENT: CONNECTION ESTABLISHED, autocommit=true
```

libpq refuses that sequence: `oauth_exchange` moves to `FE_OAUTH_SERVER_ERROR` and fails with `server sent additional OAuth data after error`. `requireAuth=oauth` does not change the outcome, so the setting that exists to establish which method authenticated the connection reports a guarantee that does not hold.

### The token goes to a peer whose certificate was never checked

`sslmode=require` encrypts without validating the certificate, and `checkTransportEncryption` tests only `instanceof SSLSocket`. Against a rogue endpoint holding a self-signed `CN=evil.example.com`, with `oauthAllowUnencryptedConnection` left at its default:

```text
ROGUE: TLS handshake completed, cipher=TLS_AES_256_GCM_SHA384
ROGUE: *** HARVESTED BEARER TOKEN: the-victims-real-bearer-token ***
```

There is a second half to this. Under `sslmode=prefer`, which is the default, a failed authentication over TLS makes the driver reconnect without encryption, and the same token goes out again in the clear:

```text
FE=> SASLInitialResponse(...)          first attempt, over TLS
WARNING Server rejected OAuth bearer token
FE=> SASLResponse(0x01)
WARNING OAUTHBEARER is being used over an unencrypted connection ...
FE=> SASLInitialResponse(...)          same token, plaintext
```

libpq guards this from the other side. `setup_oauth_parameters` requires `oauth_issuer` and `oauth_client_id` for every OAuth connection, including one whose token arrived through a hook, and it then compares the discovery URI the server advertises against the configured issuer, citing RFC 9207 mix-up attacks. Here `oauthIssuer` is optional, unused on the `oauthToken` path, and never compared with anything.

### The bearer token and the client secret reach the log

`BaseDataSource.getUrl()` appends every `PGProperty` that is set. `password` stays out of that URL on purpose, because it lives in its own field; `oauthToken` and `oauthClientSecret` do not. Both lines below come from a plain `PGSimpleDataSource.getConnection()` at `FINE`, carrying a real Keycloak token:

```text
org.postgresql.Driver.connect: Connecting with URL: jdbc:postgresql://127.0.0.1:15433/test?oauthAllowUnencryptedConnection=true&oauthToken=<<<THE-TOKEN>>>&sslmode=disable
org.postgresql.ds.common.BaseDataSource.getConnection: Created a Non-Pooling DataSource ... at ...&oauthToken=<<<THE-TOKEN>>>...
```

### RFC citations

`RFC 7628 ยง4` appears in the exception text, the log warning, the `PGProperty` javadoc, `use.md`, and two test patterns that match on it. Section 4 of RFC 7628 is "Examples". The TLS requirement is in section 3, "for the two mechanisms specified in this document, TLS MUST be used for OAUTHBEARER to protect the bearer token", and again in section 5. `matrix.mjs` cites `RFC 7632 Section 3`, and RFC 7632 is "Endpoint Security Posture Assessment: Enterprise Use Cases". The exception text is what a user pastes into a search engine, so this is worth one patch across all six places.

### Tests

The three unit test classes are well chosen, and in a default run they are the only OAuth tests that execute: every `OAuthTest` method needs `-Denable_oauth_tests=true`, so the protocol path runs nowhere except the `oauth` rows of the CI matrix. What I would ask for before merge, most of it as unit tests that need no server:

- The mechanism selection table in `AUTH_REQ_SASL`. Seven reachable columns, none covered. The one I would start with is `requireAuth=!oauth` with a server offering both mechanisms, where the driver is meant to fall through to SCRAM.
- The three new `PROTOCOL_VIOLATION` branches for SASL CONTINUE and FINAL out of order. `StrangeProxyServer` already gives you a socket-level fixture.
- `Arrays.fill` on the provider token, which `OAuthTokenProvider` documents as a contract. Delete the `finally` today and everything stays green.
- `oauthToken` taking precedence over `oauthTokenProviderClassName`. Swap the two blocks in `resolveOAuthToken` and nothing fails.
- `Unable to load OAuth token provider {0}` and `OAuth token provider returned no token`. Both are one short test each.
- The byte layout of `SASLInitialResponse` and of the `0x01` response. An off-by-one in either length is caught today only by the `oauth` CI rows.
- `AuthMethod.values()` against `fromString` and `REQUIRE_AUTH.getChoices()`, so the next constant added to the enum cannot skip either.

### Smaller things

`CHANGELOG.md` has no entry, and this adds nine connection properties plus a new public SPI. `TESTING.md` holds a copy of the `docker/bin/postgres-server` header that still lacks `OAUTH`, and it has sections for SSL, SCRAM, and authentication plugins but none for OAuth.

The commit messages need a pass. `ci: add job for OAuth tests run` adds a matrix axis rather than a job; `docs: document OAuth connection properties` names three properties out of nine and calls one of them `oauthAllowUnencrypted`, which does not exist; `feat:` has `berarer` in the body.

The description says discovery is deferred, and I read that as the right call. It is worth saying in the description that `OAuthTokenRequest` and the six discovery-shaped properties freeze the token-request API now, because that part is hard to change after a release.

The branch is behind `master` by more than twenty commits, so `git diff master..oauth` shows unrelated changes backwards. A rebase would make the next review round easier to read.

view thread (168+ messages)  latest in thread

Message-ID: <gh-pgjdbc-pgjdbc-4251-r5156617774@github.com>
Permalink:  ../gh-pgjdbc-pgjdbc-4251-r5156617774@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