From 14fa6a6e5fc79f30f111d0a4610a41d1b2669545 Mon Sep 17 00:00:00 2001 From: Shenyang Cai Date: Tue, 1 Dec 2026 00:15:27 +0000 Subject: [PATCH] fix(pandas-gbq): deprecate auth_local_webserver=False and auth_redirect_uri - Emit FutureWarning in pandas_gbq.auth.get_credentials() when auth_local_webserver=False or auth_redirect_uri is provided, while keeping behavior non-breaking. - Switch pydata_google_auth.default() call from deprecated auth_local_webserver= to use_local_webserver=. - Raise minimum dependency versions to google-auth-oauthlib >=1.5.0 (default PKCE + ExclusiveWSGIServer loopback protections) and pydata-google-auth >=1.9.1 (native Colab auth) in setup.py and testing/constraints-3.11.txt. - Update read_gbq/to_gbq docstrings and authentication docs to deprecate auth_local_webserver=False and auth_redirect_uri and guide headless users to gcloud auth application-default login --no-browser, service accounts/WIF, or Colab auth. BUG=581917075 --- .../pandas-gbq/docs/howto/authentication.rst | 43 ++++++++++-- ...cation_highly_constrained_environments.rst | 29 ++++++-- packages/pandas-gbq/docs/oauth.rst | 8 +++ packages/pandas-gbq/pandas_gbq/auth.py | 30 +++++++- packages/pandas-gbq/pandas_gbq/gbq.py | 34 ++++++--- packages/pandas-gbq/setup.py | 4 +- .../pandas-gbq/testing/constraints-3.11.txt | 4 +- packages/pandas-gbq/tests/unit/test_auth.py | 69 +++++++++++++++++++ packages/pandas-gbq/tests/unit/test_gbq.py | 38 ++++++++++ 9 files changed, 236 insertions(+), 23 deletions(-) diff --git a/packages/pandas-gbq/docs/howto/authentication.rst b/packages/pandas-gbq/docs/howto/authentication.rst index a031d1e31ccb..7a47854a476a 100644 --- a/packages/pandas-gbq/docs/howto/authentication.rst +++ b/packages/pandas-gbq/docs/howto/authentication.rst @@ -184,11 +184,10 @@ credentials are not found. credentials = pydata_google_auth.get_user_credentials( SCOPES, # Note, this doesn't work if you're running from a notebook on a - # remote sever, such as over SSH or with Google Colab. In those cases, + # remote server, such as over SSH or with Google Colab. In those cases, # install the gcloud command line interface and authenticate with the - # `gcloud auth application-default login` command and the `--no-browser` - # option. - auth_local_webserver=True, + # `gcloud auth application-default login --no-browser` command. + use_local_webserver=True, ) df = pandas_gbq.read_gbq( @@ -212,15 +211,51 @@ credentials are not found. SCOPES, # Use the NOOP cache to avoid writing credentials to disk. credentials_cache=pydata_google_auth.cache.NOOP, + use_local_webserver=True, ) Additional information on the user credentials authentication mechanism can be found in the `Google Cloud authentication guide `__. +Authenticating in Headless or Remote Environments +------------------------------------------------- + +When running on a remote server, SSH session, or container where a local web +browser cannot reach ``localhost``, use one of the following recommended +authentication methods instead of ``auth_local_webserver=False`` (which is +deprecated and will be removed in a future release): + +1. **Application Default Credentials with the gcloud CLI (recommended):** + Run the following command on the remote machine and follow the remote + bootstrap prompts on a machine with a browser: + + .. code-block:: shell + + $ gcloud auth application-default login --no-browser + + ``pandas-gbq`` automatically discovers Application Default Credentials + before falling back to interactive user OAuth. + +2. **Service accounts or Workload Identity Federation:** + For non-interactive servers, CI/CD, and containers, attach a service + account, configure Workload Identity Federation, set + ``GOOGLE_APPLICATION_CREDENTIALS``, or pass explicit ``credentials``. + +3. **Google Colab:** + ``pandas-gbq`` automatically uses native Colab authentication + (``google.colab.auth.authenticate_user()``) when running in Colab. + Authenticating from Highly Constrained Development Environments --------------------------------------------------------------- +.. warning:: + + The legacy ``auth_local_webserver=False`` and ``auth_redirect_uri`` + workflows described in the constrained-environment guide are deprecated and + will be removed in a future release due to OAuth security risks. Prefer the + methods in `Authenticating in Headless or Remote Environments`_ above. + The instructions above may not be adequate for users who are working in a *highly constrained development environment*: diff --git a/packages/pandas-gbq/docs/howto/authentication_highly_constrained_environments.rst b/packages/pandas-gbq/docs/howto/authentication_highly_constrained_environments.rst index a8c100d8de97..a3186508cc57 100644 --- a/packages/pandas-gbq/docs/howto/authentication_highly_constrained_environments.rst +++ b/packages/pandas-gbq/docs/howto/authentication_highly_constrained_environments.rst @@ -14,6 +14,27 @@ the ``credentials`` argument to explicitly pass in Google .. _authentication_hce: +.. warning:: + + The ``auth_local_webserver=False`` and ``auth_redirect_uri`` workflows on + this page are **deprecated** and will be removed in a future release due to + OAuth security risks in the console/webapp redirect flow (including + unvalidated OAuth ``state`` and static redirect page code exposure). + + Instead, use one of these recommended alternatives for headless or remote + environments: + + * **Application Default Credentials via gcloud CLI:** run + ``gcloud auth application-default login --no-browser`` on the remote + machine and complete the remote-bootstrap prompt on a browser-equipped + machine. + * **Service accounts / Workload Identity Federation:** configure ADC or pass + an explicit ``credentials`` object. + * **Google Colab:** use native Colab authentication (automatically detected + by ``pandas-gbq``). + + See the `Authentication guide `_ for details. + Authenticating from Highly Constrained Development Environments --------------------------------------------------------------- @@ -47,8 +68,8 @@ page. you/your institution can create and host an institution-specific OAuth authentication page and associate a redirect URI to that Cloud Project. -OPTION 1 - Testing Mode -^^^^^^^^^^^^^^^^^^^^^^^ +OPTION 1 - Testing Mode (Deprecated) +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This approach is for limited use, such as when testing your product. It is not intended for production use. If you have fewer than 100 users, it is possible to @@ -145,8 +166,8 @@ To run this code sample, you will need to have ``python-bigquery-pandas`` instal print(df) -OPTION 2 - Institution-specific authentication page -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +OPTION 2 - Institution-specific authentication page (Deprecated) +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ To access Bigquery programmatically, you will need your Client ID and your Client Secret, an OAuth authorization page, and an assigned redirect URI. diff --git a/packages/pandas-gbq/docs/oauth.rst b/packages/pandas-gbq/docs/oauth.rst index 9d633a4f8668..fde86a1a3fb3 100644 --- a/packages/pandas-gbq/docs/oauth.rst +++ b/packages/pandas-gbq/docs/oauth.rst @@ -5,6 +5,14 @@ Sign in to BigQuery =================== +.. warning:: + + This copy-paste OAuth redirect flow (``auth_local_webserver=False`` / + ``auth_redirect_uri``) is deprecated and will be removed in a future release + of ``pandas-gbq``. Use ``auth_local_webserver=True`` (the default) locally, + or authenticate headless environments with + ``gcloud auth application-default login --no-browser``. + You are seeing this page because you are attempting to access BigQuery via one of several possible methods, including: diff --git a/packages/pandas-gbq/pandas_gbq/auth.py b/packages/pandas-gbq/pandas_gbq/auth.py index d5bc8e396e97..53e7d7addf7e 100644 --- a/packages/pandas-gbq/pandas_gbq/auth.py +++ b/packages/pandas-gbq/pandas_gbq/auth.py @@ -5,6 +5,7 @@ """Private module for fetching Google BigQuery credentials.""" import logging +import warnings logger = logging.getLogger(__name__) @@ -37,12 +38,39 @@ def get_credentials( method from the google-auth package.""" ) + # TODO(security): Raise NotImplementedError when + # auth_local_webserver=False or auth_redirect_uri is provided, and force + # use_local_webserver=True to avoid pydata-google-auth's _run_webapp() flow. + if not auth_local_webserver: + warnings.warn( + "auth_local_webserver=False is deprecated and will be removed in " + "a future version due to security risks in the console/webapp " + "OAuth redirect flow. Use auth_local_webserver=True (the default) " + "for local development, or authenticate headless environments " + "using Application Default Credentials " + "(`gcloud auth application-default login --no-browser`) or " + "explicit `credentials`.", + FutureWarning, + stacklevel=2, + ) + + if auth_redirect_uri is not None: + warnings.warn( + "auth_redirect_uri is deprecated and will be removed in a future " + "version along with auth_local_webserver=False. Use Application " + "Default Credentials " + "(`gcloud auth application-default login --no-browser`) or pass " + "explicit `credentials` instead.", + FutureWarning, + stacklevel=2, + ) + credentials, default_project_id = pydata_google_auth.default( SCOPES, client_id=client_id, client_secret=client_secret, credentials_cache=get_credentials_cache(reauth), - auth_local_webserver=auth_local_webserver, + use_local_webserver=auth_local_webserver, redirect_uri=auth_redirect_uri, ) diff --git a/packages/pandas-gbq/pandas_gbq/gbq.py b/packages/pandas-gbq/pandas_gbq/gbq.py index 09b40341860e..b9106472fc83 100644 --- a/packages/pandas-gbq/pandas_gbq/gbq.py +++ b/packages/pandas-gbq/pandas_gbq/gbq.py @@ -172,12 +172,17 @@ def read_gbq( auth_local_webserver : bool, default True Use the `local webserver flow `_ - instead of the `console flow - `_ when getting user credentials. Your code must run on the same machine as your web browser and your web browser can access your application via ``localhost:808X``. + Setting ``auth_local_webserver=False`` is deprecated and will be + removed in a future version due to OAuth security risks in the + console/webapp redirect flow. For headless or remote environments, + authenticate with Application Default Credentials + (``gcloud auth application-default login --no-browser``) or pass + explicit ``credentials``. + .. versionadded:: 0.2.0 dialect : str, default 'standard' Note: The default value changed to 'standard' in version 0.10.0. @@ -269,9 +274,11 @@ def read_gbq( or :func:`google.oauth2.service_account.Credentials.from_service_account_file` instead. - auth_redirect_uri : str - Path to the authentication page for organization-specific authentication - workflows. Used when ``auth_local_webserver=False``. + auth_redirect_uri : str, deprecated + Deprecated. Will be removed in a future version along with + ``auth_local_webserver=False``. Path to the authentication page for + organization-specific authentication workflows. Used when + ``auth_local_webserver=False``. client_id : str The Client ID for the Google Cloud Project the user is attempting to connect to. @@ -470,12 +477,17 @@ def to_gbq( auth_local_webserver : bool, default True Use the `local webserver flow `_ - instead of the `console flow - `_ when getting user credentials. Your code must run on the same machine as your web browser and your web browser can access your application via ``localhost:808X``. + Setting ``auth_local_webserver=False`` is deprecated and will be + removed in a future version due to OAuth security risks in the + console/webapp redirect flow. For headless or remote environments, + authenticate with Application Default Credentials + (``gcloud auth application-default login --no-browser``) or pass + explicit ``credentials``. + .. versionadded:: 0.2.0 table_schema : list of dicts, optional List of BigQuery table fields to which according DataFrame @@ -533,9 +545,11 @@ def to_gbq( or :func:`google.oauth2.service_account.Credentials.from_service_account_file` instead. - auth_redirect_uri : str - Path to the authentication page for organization-specific authentication - workflows. Used when ``auth_local_webserver=False``. + auth_redirect_uri : str, deprecated + Deprecated. Will be removed in a future version along with + ``auth_local_webserver=False``. Path to the authentication page for + organization-specific authentication workflows. Used when + ``auth_local_webserver=False``. client_id : str The Client ID for the Google Cloud Project the user is attempting to connect to. diff --git a/packages/pandas-gbq/setup.py b/packages/pandas-gbq/setup.py index 901f27489849..59b24bfa8f37 100644 --- a/packages/pandas-gbq/setup.py +++ b/packages/pandas-gbq/setup.py @@ -27,14 +27,14 @@ "pyarrow >= 12.0.0", # See https://arrow.apache.org/release/22.0.0.html "pyarrow >= 22.0.0; python_version >= '3.14'", - "pydata-google-auth >=1.5.0", + "pydata-google-auth >=1.9.1", "psutil >=5.9.8", # Note: google-api-core and google-auth are also included via transitive # dependency on google-cloud-bigquery, but this library also uses them # directly. "google-api-core >= 2.34.0, <3.0.0", "google-auth >=2.62.0", - "google-auth-oauthlib >=0.7.0", + "google-auth-oauthlib >=1.5.0", # Please also update the minimum version in pandas_gbq/features.py to # allow pandas-gbq to detect invalid package versions at runtime. "google-cloud-bigquery >=3.43.0,<4.0.0", diff --git a/packages/pandas-gbq/testing/constraints-3.11.txt b/packages/pandas-gbq/testing/constraints-3.11.txt index 31033f50b278..eb1573a31477 100644 --- a/packages/pandas-gbq/testing/constraints-3.11.txt +++ b/packages/pandas-gbq/testing/constraints-3.11.txt @@ -9,11 +9,11 @@ db-dtypes==1.1.1 numpy==1.26.4 pandas==1.5.3 pyarrow==12.0.0 -pydata-google-auth==1.5.0 +pydata-google-auth==1.9.1 psutil==5.9.8 google-api-core==2.34.0 google-auth==2.62.0 -google-auth-oauthlib==0.7.0 +google-auth-oauthlib==1.5.0 google-cloud-bigquery==3.43.0 packaging==24.2.0 # Extras diff --git a/packages/pandas-gbq/tests/unit/test_auth.py b/packages/pandas-gbq/tests/unit/test_auth.py index d44c6380b575..53a7f4a562f5 100644 --- a/packages/pandas-gbq/tests/unit/test_auth.py +++ b/packages/pandas-gbq/tests/unit/test_auth.py @@ -5,6 +5,7 @@ # -*- coding: utf-8 -*- import json +import warnings from unittest import mock import pytest @@ -65,3 +66,71 @@ def test_get_credentials_cache_w_reauth(): cache = auth.get_credentials_cache(True) assert isinstance(cache, pydata_google_auth.cache.WriteOnlyCredentialsCache) + + +def test_get_credentials_uses_local_webserver_kwarg(monkeypatch): + import google.auth.credentials + import pydata_google_auth + + mock_user_credentials = mock.create_autospec(google.auth.credentials.Credentials) + captured_kwargs = {} + + def mock_default_credentials(scopes, **kwargs): + captured_kwargs.update(kwargs) + return (mock_user_credentials, "test-project") + + monkeypatch.setattr(pydata_google_auth, "default", mock_default_credentials) + + with warnings.catch_warnings(record=True) as recorded: + warnings.simplefilter("always", FutureWarning) + credentials, project = auth.get_credentials() + + assert credentials is mock_user_credentials + assert project == "test-project" + assert captured_kwargs["use_local_webserver"] is True + assert "auth_local_webserver" not in captured_kwargs + assert captured_kwargs["redirect_uri"] is None + assert recorded == [] + + +def test_get_credentials_auth_local_webserver_false_warns_deprecated(monkeypatch): + import google.auth.credentials + import pydata_google_auth + + mock_user_credentials = mock.create_autospec(google.auth.credentials.Credentials) + captured_kwargs = {} + + def mock_default_credentials(scopes, **kwargs): + captured_kwargs.update(kwargs) + return (mock_user_credentials, "test-project") + + monkeypatch.setattr(pydata_google_auth, "default", mock_default_credentials) + + with pytest.warns(FutureWarning, match="auth_local_webserver=False is deprecated"): + credentials, project = auth.get_credentials(auth_local_webserver=False) + + assert credentials is mock_user_credentials + assert project == "test-project" + assert captured_kwargs["use_local_webserver"] is False + + +def test_get_credentials_auth_redirect_uri_warns_deprecated(monkeypatch): + import google.auth.credentials + import pydata_google_auth + + mock_user_credentials = mock.create_autospec(google.auth.credentials.Credentials) + captured_kwargs = {} + + def mock_default_credentials(scopes, **kwargs): + captured_kwargs.update(kwargs) + return (mock_user_credentials, "test-project") + + monkeypatch.setattr(pydata_google_auth, "default", mock_default_credentials) + + redirect_uri = "https://example.com/oauth.html" + with pytest.warns(FutureWarning, match="auth_redirect_uri is deprecated"): + credentials, project = auth.get_credentials(auth_redirect_uri=redirect_uri) + + assert credentials is mock_user_credentials + assert project == "test-project" + assert captured_kwargs["redirect_uri"] == redirect_uri diff --git a/packages/pandas-gbq/tests/unit/test_gbq.py b/packages/pandas-gbq/tests/unit/test_gbq.py index 257021ecf248..f7ce1cab8983 100644 --- a/packages/pandas-gbq/tests/unit/test_gbq.py +++ b/packages/pandas-gbq/tests/unit/test_gbq.py @@ -282,6 +282,26 @@ def test_to_gbq_with_private_key_raises_notimplementederror(): ) +def test_to_gbq_with_auth_local_webserver_false_warns_deprecated(): + with pytest.warns(FutureWarning, match="auth_local_webserver=False is deprecated"): + gbq.to_gbq( + DataFrame([[1]]), + "dataset.tablename", + project_id="my-project", + auth_local_webserver=False, + ) + + +def test_to_gbq_with_auth_redirect_uri_warns_deprecated(): + with pytest.warns(FutureWarning, match="auth_redirect_uri is deprecated"): + gbq.to_gbq( + DataFrame([[1]]), + "dataset.tablename", + project_id="my-project", + auth_redirect_uri="https://example.com/oauth.html", + ) + + def test_to_gbq_doesnt_run_query(mock_bigquery_client): gbq.to_gbq(DataFrame([[1]]), "dataset.tablename", project_id="my-project") mock_bigquery_client.query.assert_not_called() @@ -594,6 +614,24 @@ def test_read_gbq_with_private_raises_notimplmentederror(): ) +def test_read_gbq_with_auth_local_webserver_false_warns_deprecated(): + with pytest.warns(FutureWarning, match="auth_local_webserver=False is deprecated"): + gbq.read_gbq( + "SELECT 1", + project_id="my-project", + auth_local_webserver=False, + ) + + +def test_read_gbq_with_auth_redirect_uri_warns_deprecated(): + with pytest.warns(FutureWarning, match="auth_redirect_uri is deprecated"): + gbq.read_gbq( + "SELECT 1", + project_id="my-project", + auth_redirect_uri="https://example.com/oauth.html", + ) + + def test_read_gbq_with_invalid_dialect(): with pytest.raises(ValueError, match="is not valid for dialect"): gbq.read_gbq("SELECT 1", dialect="invalid")