Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/google-auth/google/auth/transport/grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,25 @@ def my_client_cert_callback():
ssl_credentials, google_auth_credentials
)

# Set default max_metadata_size to 32768 (32 KB) to prevent gRPC C-core metadata
# limit rejections (default 8 KB) when Post-Quantum Cryptography (PQC) bearer tokens
# (ML-DSA-65 is 4.5 KB) or large trace headers are used.
options = kwargs.pop("options", None)
if options is None:
kwargs["options"] = [("grpc.max_metadata_size", 32768)]
elif isinstance(options, (list, tuple)):
opts = list(options)
if not any(
isinstance(opt, (list, tuple))
and len(opt) >= 2
and opt[0] == "grpc.max_metadata_size"
for opt in opts
):
opts.append(("grpc.max_metadata_size", 32768))
kwargs["options"] = opts
else:
kwargs["options"] = options

return grpc.secure_channel(target, composite_credentials, **kwargs)


Expand Down
22 changes: 22 additions & 0 deletions packages/google-auth/tests/transport/test_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,28 @@ def test_secure_authorized_channel_explicit_ssl(
ssl_credentials, metadata_call_credentials.return_value
)

def test_secure_authorized_channel_pqc_max_metadata_size(
self,
secure_channel,
ssl_channel_credentials,
metadata_call_credentials,
composite_channel_credentials,
get_client_ssl_credentials,
):
credentials = mock.Mock()
request = mock.Mock()
target = "example.com:80"
ssl_credentials = mock.Mock()

# Call without explicit options; should automatically add grpc.max_metadata_size=32768
google.auth.transport.grpc.secure_authorized_channel(
credentials, request, target, ssl_credentials=ssl_credentials
)

_, kwargs = secure_channel.call_args
assert "options" in kwargs
assert ("grpc.max_metadata_size", 32768) in kwargs["options"]
Comment on lines +278 to +298

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

The current test only verifies the case where options is not provided. To ensure full coverage and robust behavior, we should also test when options is explicitly passed (both with and without grpc.max_metadata_size already set).

    def test_secure_authorized_channel_pqc_max_metadata_size(
        self,
        secure_channel,
        ssl_channel_credentials,
        metadata_call_credentials,
        composite_channel_credentials,
        get_client_ssl_credentials,
    ):
        credentials = mock.Mock()
        request = mock.Mock()
        target = "example.com:80"
        ssl_credentials = mock.Mock()

        # Call without explicit options; should automatically add grpc.max_metadata_size=32768
        google.auth.transport.grpc.secure_authorized_channel(
            credentials, request, target, ssl_credentials=ssl_credentials
        )

        _, kwargs = secure_channel.call_args
        assert "options" in kwargs
        assert ("grpc.max_metadata_size", 32768) in kwargs["options"]

        # Call with explicit options not containing grpc.max_metadata_size
        google.auth.transport.grpc.secure_authorized_channel(
            credentials,
            request,
            target,
            ssl_credentials=ssl_credentials,
            options=[("some_option", 1)],
        )
        _, kwargs = secure_channel.call_args
        assert ("some_option", 1) in kwargs["options"]
        assert ("grpc.max_metadata_size", 32768) in kwargs["options"]

        # Call with explicit options already containing grpc.max_metadata_size
        google.auth.transport.grpc.secure_authorized_channel(
            credentials,
            request,
            target,
            ssl_credentials=ssl_credentials,
            options=[("grpc.max_metadata_size", 1024)],
        )
        _, kwargs = secure_channel.call_args
        assert ("grpc.max_metadata_size", 1024) in kwargs["options"]
        assert ("grpc.max_metadata_size", 32768) not in kwargs["options"]


def test_secure_authorized_channel_mutual_exclusive(
self,
secure_channel,
Expand Down
Loading