5959 https://cloud.google.com/docs/authentication/adc-troubleshooting/user-creds. \
6060 """
6161
62+ _GENERIC_LOAD_METHOD_WARNING = """\
63+ The {} method is 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
@@ -76,6 +108,20 @@ def _warn_about_problematic_credentials(credentials):
76108 warnings .warn (_CLOUD_SDK_CREDENTIALS_WARNING )
77109
78110
111+ def _warn_about_generic_load_method (method_name ): # pragma: NO COVER
112+ """Warns that a generic load method is being used.
113+
114+ This is to discourage use of the generic load methods in favor of
115+ more specific methods. The generic methods are more likely to lead to
116+ security issues if the input is not validated.
117+
118+ Args:
119+ method_name (str): The name of the method being used.
120+ """
121+
122+ warnings .warn (_GENERIC_LOAD_METHOD_WARNING .format (method_name ), DeprecationWarning )
123+
124+
79125def load_credentials_from_file (
80126 filename , scopes = None , default_scopes = None , quota_project_id = None , request = None
81127):
@@ -121,6 +167,8 @@ def load_credentials_from_file(
121167 google.auth.exceptions.DefaultCredentialsError: if the file is in the
122168 wrong format or is missing.
123169 """
170+ _warn_about_generic_load_method ("load_credentials_from_file" )
171+
124172 if not os .path .exists (filename ):
125173 raise exceptions .DefaultCredentialsError (
126174 "File {} was not found." .format (filename )
@@ -184,6 +232,7 @@ def load_credentials_from_dict(
184232 google.auth.exceptions.DefaultCredentialsError: if the file is in the
185233 wrong format or is missing.
186234 """
235+ _warn_about_generic_load_method ("load_credentials_from_dict" )
187236 if not isinstance (info , dict ):
188237 raise exceptions .DefaultCredentialsError (
189238 "info object was of type {} but dict type was expected." .format (type (info ))
0 commit comments