Skip to content

Commit bd6d88f

Browse files
committed
feat: add dunder key guardrail to feature gating
1 parent 71330d9 commit bd6d88f

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

packages/google-api-core/google/api_core/feature_gating_helpers.py renamed to packages/google-api-core/google/api_core/_feature_gating_helpers.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ def _has_provider(
5656
if configuration is None:
5757
return False
5858

59+
if feature_key.startswith("__"):
60+
return False
61+
5962
if isinstance(configuration, dict):
6063
return configuration.get(feature_key) is not None
6164

packages/google-api-core/tests/unit/test_feature_gating_helpers.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,27 @@ def test_resolve_feature_flags_options_without_key(configuration):
197197
configuration=configuration,
198198
)
199199
assert result is False
200+
201+
202+
def test_resolve_feature_flags_rejects_dunder_keys(monkeypatch):
203+
"""Verify that dunder keys are rejected early in _has_provider."""
204+
# We use a dunder key that exists on all objects (__class__)
205+
# If the guardrail is missing, getattr might return it and return True
206+
configuration = {"__class__": "some_value"}
207+
208+
result = feature_gating_helpers.resolve_feature_flags(
209+
env_var="GOOGLE_SDK_PYTHON_TRACING_ENABLED",
210+
feature_key="__class__",
211+
configuration=configuration,
212+
)
213+
assert result is False
214+
215+
class MockWithClass:
216+
__class__ = "some_value"
217+
218+
result = feature_gating_helpers.resolve_feature_flags(
219+
env_var="GOOGLE_SDK_PYTHON_TRACING_ENABLED",
220+
feature_key="__class__",
221+
configuration=MockWithClass(),
222+
)
223+
assert result is False

0 commit comments

Comments
 (0)