Skip to content

fix(bigquery-jdbc): add single-pass URL parser, remove PARSE_CACHE, and validate property keys - #15289

Merged
Neenu1995 merged 5 commits into
mainfrom
fix/g01-url-parsing-and-sanitization
Dec 3, 2026
Merged

Neenu1995 merged 5 commits into
mainfrom
fix/g01-url-parsing-and-sanitization

Conversation

@Neenu1995

@Neenu1995 Neenu1995 commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Stack: PR 1 of 3 (base: main)
Bug: b/584839495, b/584839379, b/584840529

Summary

Introduces the lock-free, two-phase single-pass URL parser (ParsedConnectionUrl / parseConnectionUrl), removes the static PARSE_CACHE, adds endpoint-only display URL sanitization (sanitizeUrlForDisplay), ignores Properties keys that could inject delimiters or control characters (extractValidPropertyKey), and hardens parsePropertiesMapFromValue.

What Changed & Why

  • Remove static PARSE_CACHE (b/584839495): Eliminates cross-connection heap retention of plaintext credential-bearing URLs and removes global Collections.synchronizedMap lock contention during concurrent connect bursts.
  • Locale-safe property lookup: Uses Locale.ROOT across all PROPERTY_NAME_MAP initialization and parseUriProperty() lookups so property canonicalization works identically under non-English default locales (e.g., Turkish tr_TR).
  • Single-pass two-phase ParsedConnectionUrl (b/584839495, b/584840728):
    • Phase 1 (pre-connect / bootstrap lookup): Reads pre-connect logging/telemetry settings (EnableDiagnosticTelemetry, LogLevel, LogPath, enableGcpLogExporter) using last-wins semantics without throwing before logging opens.
    • Phase 2 (validation): Rejects malformed segments (including whitespace-only keys before =) and invalid percent-encoding using 1-based segment indices only (never echoing raw secret slices) and logs sanitized unknown keys.
  • Endpoint-only sanitizeUrlForDisplay() (b/584839495): Returns only the caller's normalized connection endpoint (for example jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443) and never any connection properties. Prefixes containing =, ?, or @ collapse to jdbc:bigquery:. The caller's host is kept so PSC and custom-universe connections stay identifiable.
  • SAFE_TO_LOG_PROPERTIES: Adds BigQueryConnection.isSafeToLogProperty(String) and 6 non-secret properties (SSLTrustStoreType, SSLTrustStoreProvider, gcpTelemetryProjectId, enableGcpTraceExporter, enableGcpLogExporter, useGlobalOpenTelemetry) to the log-masking allowlist, with an exhaustive classification unit test covering every key in PROPERTY_NAME_MAP.
  • Single-pass PartnerToken decoding (formatPartnerTokenHeaderValue): Decodes percent-escapes once, inside parseConnectionUrl(), so URL tokens are never double-decoded and discrete Properties/DataSource tokens preserve literal %xx sequences; rejects control characters before formatting into the user-agent header.
  • Property key handling & map parsing hardening (b/584839379, b/584840529):
    • extractValidPropertyKey() returns null for keys that are blank, have leading/trailing whitespace, or contain ;, =, or control characters; callers skip them and log a warning that does not echo the key.
    • Non-String keys throw BigQueryJdbcRuntimeException instead of being converted with toString() (Properties already rejects null keys).
    • parsePropertiesMapFromValue() trims entry keys, rejects empty keys and exact duplicate keys (case-variant session_id keys still reach the existing case-insensitive check), and sanitizes warning logs.

How It Was Tested

  • JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64 make lint — 0 reformatted.
  • mvn -B -ntp clean test — Enforcer, Checkstyle (0 violations), Java 8 animal-sniffer, and 1,471 unit tests (1 skipped, 0 errors; only the known pre-existing local failure BigQueryJdbcOAuthUtilityTest.testPrivateKeyFromP12Bytes loading /fake.p12).
  • mvn clean test compiles the integration test sources; they were not executed (live IT execution requires GOOGLE_APPLICATION_CREDENTIALS).

Stack Plan

  • PR 1 (this PR): Single-pass URL parser, PARSE_CACHE removal, endpoint-only display URL, property key handling (base: main)
  • PR 2: Typed DataSource snapshot/overlay and unified last-wins connect pipeline (base: fix/g01-url-parsing-and-sanitization)
  • PR 3: Endpoint-only DatabaseMetaData.getURL(), Driver.getPropertyInfo() redaction, toMaskedLogMap(), and legacy URL round-trip helper deletion (base: fix/g01-structured-connect-and-datasource-overrides)

@Neenu1995
Neenu1995 requested review from a team as code owners November 30, 2026 20:11

@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 refactors the BigQuery JDBC connection URL parsing logic to improve security and robustness. It removes the static PARSE_CACHE to prevent retaining plaintext credentials in memory and introduces a two-phase, non-throwing parser via the ParsedConnectionUrl class. Additionally, it adds validation to reject invalid property keys (containing delimiters, control characters, or duplicates) and registers several new safe-to-log properties. The review feedback highlights two opportunities to strengthen validation: first, by treating whitespace-only keys in URL segments as malformed rather than unknown, and second, by trimming keys in parsePropertiesMapFromValue before performing duplicate checks to prevent whitespace-based bypasses.

Comment on lines +1234 to +1235
String entryKey = keyValueParts.get(0);
if (entryKey.trim().isEmpty()) {

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.

medium

Trimming the entryKey directly ensures that any leading or trailing whitespace is removed before performing the duplicate key check (propertiesMap.containsKey(entryKey)). Otherwise, keys like key1 and key1 would bypass the duplicate check but might be treated as the same key by consumers, leading to unexpected behavior.

Suggested change
String entryKey = keyValueParts.get(0);
if (entryKey.trim().isEmpty()) {
String entryKey = keyValueParts.get(0).trim();
if (entryKey.isEmpty()) {

@Neenu1995
Neenu1995 added this pull request to stack #15292 November 30, 2026 20:14
@Neenu1995 Neenu1995 changed the title [WIP] fix(bigquery-jdbc): add single-pass URL parser, remove PARSE_CACHE, and validate property keys fix(bigquery-jdbc): add single-pass URL parser, remove PARSE_CACHE, and validate property keys Dec 1, 2026
…id Properties keys

Address review feedback on #15289: DatabaseMetaData display URL now returns only the caller's connection endpoint, validatePropertyKey is renamed to extractValidPropertyKey and rejects non-String keys, and keys that are blank, padded, or contain ';', '=', or control characters are ignored with a warning instead of failing the connection.
@Neenu1995
Neenu1995 requested a review from logachev December 2, 2026 19:48

@logachev logachev 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.

Please update PR description to reflect latest changes

@Neenu1995
Neenu1995 merged commit ac21060 into main Dec 3, 2026
192 checks passed
@Neenu1995
Neenu1995 deleted the fix/g01-url-parsing-and-sanitization branch December 3, 2026 03:39
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