Repository navigation
Commit d61b6d3
authored
fix(bigquery-jdbc): use loopback binding, PKCE, and state verification in user account OAuth flow (#15241)
b/584840384
### Summary
Aligns interactive user-account authentication (`OAuthType=1`) in
`BigQueryJdbcOAuthUtility` with RFC 8252 and RFC 7636, extracts the
local loopback HTTP callback listener into
`BigQueryJdbcOAuthCallbackServer`, and ensures `RequestGoogleDriveScope`
and explicit `universeDomain` settings apply to user-account
credentials.
### What Changed & Why
- **Desktop pre-check & deterministic socket cleanup:**
`getGoogleUserAccountCredentials` checks `isDesktopBrowseSupported()`
(`GraphicsEnvironment.isHeadless()`, `Desktop.isDesktopSupported()`, and
`Desktop.Action.BROWSE`) before opening a local socket, and wraps
`BigQueryJdbcOAuthCallbackServer` and accepted `Socket` instances in
`try-with-resources` so the listener closes prior to token exchange and
on any error or timeout.
- **RFC 8252 loopback redirect URI
(`BigQueryJdbcOAuthCallbackServer`):** `openLoopbackServerSocket()`
binds `127.0.0.1` (falling back to `::1` on IPv6-only hosts, `backlog =
8`) using `java.net.ServerSocket` (`java.base`, avoiding a
`jdk.httpserver` module dependency in custom `jlink` / OSGi runtimes)
and `buildLoopbackCallbackUri()` formats `http://127.0.0.1:<port>` or
`http://[::1]:<port>` for `getUserAuthorizer`. Matching the bound IP
family avoids `Connection Refused` errors when the browser and JVM
resolve `localhost` to different address families. (The 9-argument
`getGoogleUserAccountCredentials` and 1-argument
`openLoopbackServerSocket` package-private overloads are deterministic
test seams to which the production entry points delegate.)
- **RFC 7636 `S256` PKCE, OAuth `state`, & `RequestGoogleDriveScope`:**
Configured `UserAuthorizer` with `Rfc7636PkceProvider` (used instead of
`DefaultPKCEProvider`, which in `google-auth-library-oauth2-http`
`1.58.0` emits a 172-character `=`-padded `code_verifier` outside RFC
7636 Section 4.1's 43–128 unreserved-character range), added `state`
generation and callback verification, and wired
`RequestGoogleDriveScope` (`BIGQUERY_WITH_DRIVE_SCOPES` vs.
`DEFAULT_BIGQUERY_SCOPES`) at authorization consent time.
- **Explicit `universeDomain` propagation:** `getCredentialsFromCode`
propagates `UNIVERSE_DOMAIN_OVERRIDE_PROPERTY_NAME` from
`overrideProperties` onto `UserCredentials.Builder`, matching non-JSON
credential handling for custom universe domains.
- **Reliable HTTP/1.1 callback parsing & responses:**
- Enforces a `1000 ms` per-socket read timeout within the `120 s`
callback window, skipping empty browser TCP preconnects, returning `404
Not Found` for secondary requests such as `/favicon.ico`, and continuing
to listen after malformed or unverified HTTP requests (responding `400
Bad Request` and recording the last rejected request reason in case the
callback window times out).
- Parses the callback request target via `URI`, UTF-8 URL-decodes query
parameters while preserving literal `+` characters per RFC 3986 (`code`,
`state`, `error`), and immediately aborts when a `state`-verified
callback reports an OAuth `error=` or omits `code`.
- Drains up to `64 KiB` of request headers (accommodating `127.0.0.1`
cookies) and calls `socket.shutdownOutput()` after flushing
`\r\n`-formatted HTTP/1.1 responses (`200 OK`, `400 Bad Request`, `404
Not Found`) with `Cache-Control: no-store`, `X-Content-Type-Options:
nosniff`, `Content-Security-Policy: default-src 'none'`, `Connection:
close`, and exact UTF-8 byte `Content-Length`.
- **Documentation (`docs/USER_GUIDE.md`):** Documented that custom
`OAuthClientId` values used with `OAuthType=1` must be configured as a
**Desktop app** OAuth 2.0 client.
### Compatibility Notes
- Existing `OAuthType=1` flows already used ephemeral ports
(`ServerSocket(0)`), which require a Google **Desktop app** OAuth 2.0
client (default or custom `OAuthClientId`). Google Desktop OAuth clients
natively support `http://127.0.0.1:<port>` and `http://[::1]:<port>`
redirect URIs and `S256` PKCE without Cloud Console changes.
### Testing
- `BigQueryJdbcOAuthUtilityTest` covers `S256` PKCE challenge/verifier
exchange, `RequestGoogleDriveScope` consent scope selection, default vs.
explicit `universeDomain` propagation (including ignoring
`GOOGLE_CLOUD_UNIVERSE_DOMAIN` for non-JSON credentials), IPv4/IPv6
loopback binding without mocking JDK networking classes, RFC 3986 `+`
and `%2B` query decoding and malformed `%` handling, `state` and
`error=` callback validation, `> 16 KiB` and `> 64 KiB` header draining,
resilience to browser TCP preconnects / `/favicon.ico` / unverified
local HTTP probes, timeout diagnostics and socket cleanup paths, and
non-desktop environment fast-fail.
- Live interactive browser consent was not run in automated CI as
`OAuthType=1` requires desktop user interaction.1 parent 1c0a358 commit d61b6d3
4 files changed
Lines changed: 1650 additions & 91 deletions
File tree
- java-bigquery-jdbc
- docs
- src
- main/java/com/google/cloud/bigquery/jdbc
- test/java/com/google/cloud/bigquery/jdbc
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
109 | 109 | | |
110 | 110 | | |
111 | 111 | | |
112 | | - | |
| 112 | + | |
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
| |||
160 | 160 | | |
161 | 161 | | |
162 | 162 | | |
163 | | - | |
| 163 | + | |
164 | 164 | | |
165 | 165 | | |
166 | 166 | | |
| |||
0 commit comments