10
10
#include "sigchain.h"
11
11
#include "strbuf.h"
12
12
#include "urlmatch.h"
13
- #include "git-compat-util .h"
13
+ #include "environment .h"
14
14
15
15
void credential_init (struct credential * c )
16
16
{
@@ -72,6 +72,10 @@ static int credential_config_callback(const char *var, const char *value,
72
72
}
73
73
else if (!strcmp (key , "usehttppath" ))
74
74
c -> use_http_path = git_config_bool (var , value );
75
+ else if (!strcmp (key , "sanitizeprompt" ))
76
+ c -> sanitize_prompt = git_config_bool (var , value );
77
+ else if (!strcmp (key , "protectprotocol" ))
78
+ c -> protect_protocol = git_config_bool (var , value );
75
79
76
80
return 0 ;
77
81
}
@@ -169,7 +173,8 @@ static void credential_format(struct credential *c, struct strbuf *out)
169
173
strbuf_addch (out , '@' );
170
174
}
171
175
if (c -> host )
172
- strbuf_addstr (out , c -> host );
176
+ strbuf_add_percentencode (out , c -> host ,
177
+ STRBUF_ENCODE_HOST_AND_PORT );
173
178
if (c -> path ) {
174
179
strbuf_addch (out , '/' );
175
180
strbuf_add_percentencode (out , c -> path , 0 );
@@ -183,7 +188,10 @@ static char *credential_ask_one(const char *what, struct credential *c,
183
188
struct strbuf prompt = STRBUF_INIT ;
184
189
char * r ;
185
190
186
- credential_describe (c , & desc );
191
+ if (c -> sanitize_prompt )
192
+ credential_format (c , & desc );
193
+ else
194
+ credential_describe (c , & desc );
187
195
if (desc .len )
188
196
strbuf_addf (& prompt , "%s for '%s': " , what , desc .buf );
189
197
else
@@ -266,7 +274,8 @@ int credential_read(struct credential *c, FILE *fp)
266
274
return 0 ;
267
275
}
268
276
269
- static void credential_write_item (FILE * fp , const char * key , const char * value ,
277
+ static void credential_write_item (const struct credential * c ,
278
+ FILE * fp , const char * key , const char * value ,
270
279
int required )
271
280
{
272
281
if (!value && required )
@@ -275,24 +284,28 @@ static void credential_write_item(FILE *fp, const char *key, const char *value,
275
284
return ;
276
285
if (strchr (value , '\n' ))
277
286
die ("credential value for %s contains newline" , key );
287
+ if (c -> protect_protocol && strchr (value , '\r' ))
288
+ die ("credential value for %s contains carriage return\n"
289
+ "If this is intended, set `credential.protectProtocol=false`" ,
290
+ key );
278
291
fprintf (fp , "%s=%s\n" , key , value );
279
292
}
280
293
281
294
void credential_write (const struct credential * c , FILE * fp )
282
295
{
283
- credential_write_item (fp , "protocol" , c -> protocol , 1 );
284
- credential_write_item (fp , "host" , c -> host , 1 );
285
- credential_write_item (fp , "path" , c -> path , 0 );
286
- credential_write_item (fp , "username" , c -> username , 0 );
287
- credential_write_item (fp , "password" , c -> password , 0 );
288
- credential_write_item (fp , "oauth_refresh_token" , c -> oauth_refresh_token , 0 );
296
+ credential_write_item (c , fp , "protocol" , c -> protocol , 1 );
297
+ credential_write_item (c , fp , "host" , c -> host , 1 );
298
+ credential_write_item (c , fp , "path" , c -> path , 0 );
299
+ credential_write_item (c , fp , "username" , c -> username , 0 );
300
+ credential_write_item (c , fp , "password" , c -> password , 0 );
301
+ credential_write_item (c , fp , "oauth_refresh_token" , c -> oauth_refresh_token , 0 );
289
302
if (c -> password_expiry_utc != TIME_MAX ) {
290
303
char * s = xstrfmt ("%" PRItime , c -> password_expiry_utc );
291
- credential_write_item (fp , "password_expiry_utc" , s , 0 );
304
+ credential_write_item (c , fp , "password_expiry_utc" , s , 0 );
292
305
free (s );
293
306
}
294
307
for (size_t i = 0 ; i < c -> wwwauth_headers .nr ; i ++ )
295
- credential_write_item (fp , "wwwauth[]" , c -> wwwauth_headers .v [i ], 0 );
308
+ credential_write_item (c , fp , "wwwauth[]" , c -> wwwauth_headers .v [i ], 0 );
296
309
}
297
310
298
311
static int run_credential_helper (struct credential * c ,
0 commit comments