File tree Expand file tree Collapse file tree 2 files changed +21
-1
lines changed Expand file tree Collapse file tree 2 files changed +21
-1
lines changed Original file line number Diff line number Diff line change @@ -81,7 +81,7 @@ def __init__(
81
81
:param user_agent: user agent to identify requests, defaults to ""
82
82
:param timeout: request timeout, defaults to 20s
83
83
84
- :raises ValueError: if the protocol is invalid
84
+ :raises ValueError: if the protocol or the api_key is invalid
85
85
"""
86
86
87
87
if isinstance (base_uri , str ):
@@ -93,6 +93,18 @@ def __init__(
93
93
if not isinstance (api_key , str ):
94
94
raise TypeError ("api_key is not a string" )
95
95
96
+ try :
97
+ # The requests module encodes headers in latin-1, if api_key contains
98
+ # characters which cannot be encoded in latin-1, the raised exception is
99
+ # going to be very obscure. Catch the problem early to raise a clearer
100
+ # exception.
101
+ # See https://github.com/GitGuardian/ggshield/issues/101
102
+ api_key .encode ("latin-1" )
103
+ except UnicodeEncodeError :
104
+ raise ValueError (
105
+ "Invalid value for API Key: must be only latin-1 characters."
106
+ )
107
+
96
108
self .base_uri = base_uri
97
109
self .api_key = api_key
98
110
self .session = session if isinstance (session , Session ) else Session ()
Original file line number Diff line number Diff line change 179
179
None ,
180
180
id = "Custom headers" ,
181
181
),
182
+ pytest .param (
183
+ "–––––––FILL-ME–––––––––" ,
184
+ "https://api.gitguardian.com/" ,
185
+ "None" ,
186
+ 30.0 ,
187
+ ValueError ,
188
+ id = "U+2013 dash characters in API key" ,
189
+ ),
182
190
],
183
191
)
184
192
def test_client_creation (
You can’t perform that action at this time.
0 commit comments