Skip to content

fix(bigquery-jdbc): use loopback binding, PKCE, and state verification in user account OAuth flow - #15241

Merged
keshavdandeva merged 4 commits into
mainfrom
jdbc/fix-oauth1
Nov 27, 2026
Merged

keshavdandeva merged 4 commits into
mainfrom
jdbc/fix-oauth1

Conversation

@keshavdandeva

@keshavdandeva keshavdandeva commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

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.

@keshavdandeva
keshavdandeva requested review from a team as code owners November 25, 2026 15:58

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request significantly enhances the OAuth user authentication flow in the BigQuery JDBC driver by introducing a custom PKCE provider (Rfc7636PkceProvider) to comply with RFC 7636 and implementing a robust local HTTP callback server. The new server handles timeouts, drains headers, validates state parameters, and sanitizes error codes, backed by comprehensive unit tests. The review feedback highlights a robustness issue where catching any HttpCallbackException immediately aborts the authentication flow (which makes it fragile to local port probes) and recommends preserving the original exception as the cause when throwing BigQueryJdbcRuntimeException.

@keshavdandeva
keshavdandeva marked this pull request as draft November 25, 2026 16:06
@keshavdandeva
keshavdandeva marked this pull request as ready for review November 25, 2026 16:46
- Extract loopback callback socket/HTTP handling into package-private
  BigQueryJdbcOAuthCallbackServer and lower per-socket read timeout to 1000ms
- Preserve literal '+' in callback query decoding and handle malformed '%'
  sequences as HttpCallbackException
- Propagate explicit universeDomain override onto UserCredentials in
  getCredentialsFromCode and merge origin/main
- Request Google Drive scope at authorization consent time when
  RequestGoogleDriveScope is enabled for OAuthType=1
- Raise callback header drain limit to 64 KiB, call shutdownOutput() after
  flushing responses, and add nosniff and CSP response headers
- Document Desktop app OAuth client requirement in docs/USER_GUIDE.md
@keshavdandeva
keshavdandeva merged commit d61b6d3 into main Nov 27, 2026
195 checks passed
@keshavdandeva
keshavdandeva deleted the jdbc/fix-oauth1 branch November 27, 2026 17:07
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.

2 participants