Skip to content

Commit 32cc7a0

Browse files
committed
fix: Deprecating generic load methods and adding warnings on few cred types
1 parent 1e8a867 commit 32cc7a0

File tree

6 files changed

+165
-1
lines changed

6 files changed

+165
-1
lines changed

google/auth/_default.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,50 @@
5959
https://cloud.google.com/docs/authentication/adc-troubleshooting/user-creds. \
6060
"""
6161

62+
_GENERIC_LOAD_METHOD_WARNING = """\
63+
The {} method is being deprecated because of a potential security risk.
64+
65+
This method does not validate the credential configuration. The security
66+
risk occurs when a credential configuration is accepted from a source that
67+
is not under your control and used without validation on your side.
68+
69+
If you know that you will be loading credential configurations of a
70+
specific type, it is recommended to use a credential-type-specific
71+
load method.
72+
This will ensure that an unexpected credential type with potential for
73+
malicious intent is not loaded unintentionally. You might still have to do
74+
validation for certain credential types. Please follow the recommendations
75+
for that method. For example, if you want to load only service accounts,
76+
you can create the service account credentials explicitly:
77+
78+
```
79+
from google.oauth2 import service_account
80+
creds = service_account.Credentials.from_service_account_file(filename)
81+
```
82+
83+
If you are loading your credential configuration from an untrusted source and have
84+
not mitigated the risks (e.g. by validating the configuration yourself), make
85+
these changes as soon as possible to prevent security risks to your environment.
86+
87+
Regardless of the method used, it is always your responsibility to validate
88+
configurations received from external sources.
89+
90+
Refer to https://cloud.google.com/docs/authentication/external/externally-sourced-credentials
91+
for more details.
92+
"""
93+
6294
# The subject token type used for AWS external_account credentials.
6395
_AWS_SUBJECT_TOKEN_TYPE = "urn:ietf:params:aws:token-type:aws4_request"
6496

6597

98+
class GenericLoadMethodWarning(DeprecationWarning): # pragma: NO COVER
99+
"""
100+
Deprecation warning raised when a generic load method is used.
101+
"""
102+
103+
pass
104+
105+
66106
def _warn_about_problematic_credentials(credentials):
67107
"""Determines if the credentials are problematic.
68108
@@ -75,6 +115,23 @@ def _warn_about_problematic_credentials(credentials):
75115
if credentials.client_id == _cloud_sdk.CLOUD_SDK_CLIENT_ID:
76116
warnings.warn(_CLOUD_SDK_CREDENTIALS_WARNING)
77117

118+
def _warn_about_generic_load_method(method_name): # pragma: NO COVER
119+
"""Warns that a generic load method is being used.
120+
121+
This is to discourage use of the generic load methods in favor of
122+
more specific methods. The generic methods are more likely to lead to
123+
security issues if the input is not validated.
124+
125+
Args:
126+
method_name (str): The name of the method being used.
127+
"""
128+
129+
130+
warnings.warn(
131+
_GENERIC_LOAD_METHOD_WARNING.format(method_name),
132+
GenericLoadMethodWarning,
133+
)
134+
78135

79136
def load_credentials_from_file(
80137
filename, scopes=None, default_scopes=None, quota_project_id=None, request=None
@@ -121,6 +178,8 @@ def load_credentials_from_file(
121178
google.auth.exceptions.DefaultCredentialsError: if the file is in the
122179
wrong format or is missing.
123180
"""
181+
_warn_about_generic_load_method("load_credentials_from_file")
182+
124183
if not os.path.exists(filename):
125184
raise exceptions.DefaultCredentialsError(
126185
"File {} was not found.".format(filename)
@@ -184,6 +243,7 @@ def load_credentials_from_dict(
184243
google.auth.exceptions.DefaultCredentialsError: if the file is in the
185244
wrong format or is missing.
186245
"""
246+
_warn_about_generic_load_method("load_credentials_from_dict")
187247
if not isinstance(info, dict):
188248
raise exceptions.DefaultCredentialsError(
189249
"info object was of type {} but dict type was expected.".format(type(info))

google/auth/external_account.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ class Credentials(
8989
credentials for Google access token and authorizing requests to Google APIs.
9090
The base class implements the common logic for exchanging external account
9191
credentials for Google access tokens.
92+
93+
**IMPORTANT**:
94+
This method does not validate the credential configuration. A security
95+
risk occurs when a credential configuration configured with malicious urls
96+
is used.
97+
When the credential configuration is accepted from an
98+
untrusted source, you should validate it before using with this method.
99+
Refer https://cloud.google.com/docs/authentication/external/externally-sourced-credentials for more details.
92100
"""
93101

94102
def __init__(
@@ -576,6 +584,14 @@ def _get_mtls_cert_and_key_paths(self):
576584
def from_info(cls, info, **kwargs):
577585
"""Creates a Credentials instance from parsed external account info.
578586
587+
**IMPORTANT**:
588+
This method does not validate the credential configuration. A security
589+
risk occurs when a credential configuration configured with malicious urls
590+
is used.
591+
When the credential configuration is accepted from an
592+
untrusted source, you should validate it before using with this method.
593+
Refer https://cloud.google.com/docs/authentication/external/externally-sourced-credentials for more details.
594+
579595
Args:
580596
info (Mapping[str, str]): The external account info in Google
581597
format.
@@ -615,6 +631,14 @@ def from_info(cls, info, **kwargs):
615631
def from_file(cls, filename, **kwargs):
616632
"""Creates a Credentials instance from an external account json file.
617633
634+
**IMPORTANT**:
635+
This method does not validate the credential configuration. A security
636+
risk occurs when a credential configuration configured with malicious urls
637+
is used.
638+
When the credential configuration is accepted from an
639+
untrusted source, you should validate it before using with this method.
640+
Refer https://cloud.google.com/docs/authentication/external/externally-sourced-credentials for more details.
641+
618642
Args:
619643
filename (str): The path to the external account json file.
620644
kwargs: Additional arguments to pass to the constructor.

google/auth/external_account_authorized_user.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ class Credentials(
6060
The credentials are considered immutable. If you want to modify the
6161
quota project, use `with_quota_project` and if you want to modify the token
6262
uri, use `with_token_uri`.
63+
64+
**IMPORTANT**:
65+
This method does not validate the credential configuration. A security
66+
risk occurs when a credential configuration configured with malicious urls
67+
is used.
68+
When the credential configuration is accepted from an
69+
untrusted source, you should validate it before using with this method.
70+
Refer https://cloud.google.com/docs/authentication/external/externally-sourced-credentials for more details.
6371
"""
6472

6573
def __init__(
@@ -328,6 +336,14 @@ def with_universe_domain(self, universe_domain):
328336
def from_info(cls, info, **kwargs):
329337
"""Creates a Credentials instance from parsed external account info.
330338
339+
**IMPORTANT**:
340+
This method does not validate the credential configuration. A security
341+
risk occurs when a credential configuration configured with malicious urls
342+
is used.
343+
When the credential configuration is accepted from an
344+
untrusted source, you should validate it before using with this method.
345+
Refer https://cloud.google.com/docs/authentication/external/externally-sourced-credentials for more details.
346+
331347
Args:
332348
info (Mapping[str, str]): The external account info in Google
333349
format.
@@ -367,6 +383,14 @@ def from_info(cls, info, **kwargs):
367383
def from_file(cls, filename, **kwargs):
368384
"""Creates a Credentials instance from an external account json file.
369385
386+
**IMPORTANT**:
387+
This method does not validate the credential configuration. A security
388+
risk occurs when a credential configuration configured with malicious urls
389+
is used.
390+
When the credential configuration is accepted from an
391+
untrusted source, you should validate it before using with this method.
392+
Refer https://cloud.google.com/docs/authentication/external/externally-sourced-credentials for more details.
393+
370394
Args:
371395
filename (str): The path to the external account json file.
372396
kwargs: Additional arguments to pass to the constructor.

google/auth/identity_pool.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,14 @@ def _validate_single_source(self):
497497
def from_info(cls, info, **kwargs):
498498
"""Creates an Identity Pool Credentials instance from parsed external account info.
499499
500+
**IMPORTANT**:
501+
This method does not validate the credential configuration. A security
502+
risk occurs when a credential configuration configured with malicious urls
503+
is used.
504+
When the credential configuration is accepted from an
505+
untrusted source, you should validate it before using with this method.
506+
Refer https://cloud.google.com/docs/authentication/external/externally-sourced-credentials for more details.
507+
500508
Args:
501509
info (Mapping[str, str]): The Identity Pool external account info in Google
502510
format.
@@ -517,6 +525,14 @@ def from_info(cls, info, **kwargs):
517525
def from_file(cls, filename, **kwargs):
518526
"""Creates an IdentityPool Credentials instance from an external account json file.
519527
528+
**IMPORTANT**:
529+
This method does not validate the credential configuration. A security
530+
risk occurs when a credential configuration configured with malicious urls
531+
is used.
532+
When the credential configuration is accepted from an
533+
untrusted source, you should validate it before using with this method.
534+
Refer https://cloud.google.com/docs/authentication/external/externally-sourced-credentials for more details.
535+
520536
Args:
521537
filename (str): The path to the IdentityPool external account json file.
522538
kwargs: Additional arguments to pass to the constructor.

google/auth/impersonated_credentials.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ class Credentials(
184184
buckets = client.list_buckets(project='your_project')
185185
for bucket in buckets:
186186
print(bucket.name)
187+
188+
**IMPORTANT**:
189+
This method does not validate the credential configuration. A security
190+
risk occurs when a credential configuration configured with malicious urls
191+
is used.
192+
When the credential configuration is accepted from an
193+
untrusted source, you should validate it before using with this method.
194+
Refer https://cloud.google.com/docs/authentication/external/externally-sourced-credentials for more details.
187195
"""
188196

189197
def __init__(
@@ -454,6 +462,14 @@ def with_scopes(self, scopes, default_scopes=None):
454462
def from_impersonated_service_account_info(cls, info, scopes=None):
455463
"""Creates a Credentials instance from parsed impersonated service account credentials info.
456464
465+
**IMPORTANT**:
466+
This method does not validate the credential configuration. A security
467+
risk occurs when a credential configuration configured with malicious urls
468+
is used.
469+
When the credential configuration is accepted from an
470+
untrusted source, you should validate it before using with this method.
471+
Refer https://cloud.google.com/docs/authentication/external/externally-sourced-credentials for more details.
472+
457473
Args:
458474
info (Mapping[str, str]): The impersonated service account credentials info in Google
459475
format.

google/auth/pluggable.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@
5757

5858

5959
class Credentials(external_account.Credentials):
60-
"""External account credentials sourced from executables."""
60+
"""External account credentials sourced from executables.
61+
62+
**IMPORTANT**:
63+
This method does not validate the credential configuration. A security
64+
risk occurs when a credential configuration configured with malicious urls
65+
is used.
66+
When the credential configuration is accepted from an
67+
untrusted source, you should validate it before using with this method.
68+
Refer https://cloud.google.com/docs/authentication/external/externally-sourced-credentials for more details."""
6169

6270
def __init__(
6371
self,
@@ -300,6 +308,14 @@ def external_account_id(self):
300308
def from_info(cls, info, **kwargs):
301309
"""Creates a Pluggable Credentials instance from parsed external account info.
302310
311+
**IMPORTANT**:
312+
This method does not validate the credential configuration. A security
313+
risk occurs when a credential configuration configured with malicious urls
314+
is used.
315+
When the credential configuration is accepted from an
316+
untrusted source, you should validate it before using with this method.
317+
Refer https://cloud.google.com/docs/authentication/external/externally-sourced-credentials for more details.
318+
303319
Args:
304320
info (Mapping[str, str]): The Pluggable external account info in Google
305321
format.
@@ -319,6 +335,14 @@ def from_info(cls, info, **kwargs):
319335
def from_file(cls, filename, **kwargs):
320336
"""Creates an Pluggable Credentials instance from an external account json file.
321337
338+
**IMPORTANT**:
339+
This method does not validate the credential configuration. A security
340+
risk occurs when a credential configuration configured with malicious urls
341+
is used.
342+
When the credential configuration is accepted from an
343+
untrusted source, you should validate it before using with this method.
344+
Refer https://cloud.google.com/docs/authentication/external/externally-sourced-credentials for more details.
345+
322346
Args:
323347
filename (str): The path to the Pluggable external account json file.
324348
kwargs: Additional arguments to pass to the constructor.

0 commit comments

Comments
 (0)