1010#include "sigchain.h"
1111#include "strbuf.h"
1212#include "urlmatch.h"
13- #include "git-compat-util .h"
13+ #include "environment .h"
1414
1515void credential_init (struct credential * c )
1616{
@@ -72,6 +72,10 @@ static int credential_config_callback(const char *var, const char *value,
7272 }
7373 else if (!strcmp (key , "usehttppath" ))
7474 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 );
7579
7680 return 0 ;
7781}
@@ -169,7 +173,8 @@ static void credential_format(struct credential *c, struct strbuf *out)
169173 strbuf_addch (out , '@' );
170174 }
171175 if (c -> host )
172- strbuf_addstr (out , c -> host );
176+ strbuf_add_percentencode (out , c -> host ,
177+ STRBUF_ENCODE_HOST_AND_PORT );
173178 if (c -> path ) {
174179 strbuf_addch (out , '/' );
175180 strbuf_add_percentencode (out , c -> path , 0 );
@@ -183,7 +188,10 @@ static char *credential_ask_one(const char *what, struct credential *c,
183188 struct strbuf prompt = STRBUF_INIT ;
184189 char * r ;
185190
186- credential_describe (c , & desc );
191+ if (c -> sanitize_prompt )
192+ credential_format (c , & desc );
193+ else
194+ credential_describe (c , & desc );
187195 if (desc .len )
188196 strbuf_addf (& prompt , "%s for '%s': " , what , desc .buf );
189197 else
@@ -266,7 +274,8 @@ int credential_read(struct credential *c, FILE *fp)
266274 return 0 ;
267275}
268276
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 ,
270279 int required )
271280{
272281 if (!value && required )
@@ -275,24 +284,28 @@ static void credential_write_item(FILE *fp, const char *key, const char *value,
275284 return ;
276285 if (strchr (value , '\n' ))
277286 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 );
278291 fprintf (fp , "%s=%s\n" , key , value );
279292}
280293
281294void credential_write (const struct credential * c , FILE * fp )
282295{
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 );
289302 if (c -> password_expiry_utc != TIME_MAX ) {
290303 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 );
292305 free (s );
293306 }
294307 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 );
296309}
297310
298311static int run_credential_helper (struct credential * c ,
0 commit comments