Skip to content

Commit 22acd55

Browse files
committed
credential: add new interactive config option
When scripts or background maintenance wish to perform HTTP(S) requests, there is a risk that our stored credentials might be invalid. At the moment, this causes the credential helper to ping the user and block the process. Even if the credential helper does not ping the user, Git falls back to the 'askpass' method, which includes a direct ping to the user via the terminal. Even setting the 'core.askPass' config as something like 'echo' will causes Git to fallback to a terminal prompt. It uses git_terminal_prompt(), which finds the terminal from the environment and ignores whether stdin has been redirected. This can also block the process awaiting input. Create a new config option to prevent user interaction, favoring a failure to a blocked process. The chosen name, 'credential.interactive', is taken from the config option used by Git Credential Manager to already avoid user interactivity, so there is already one credential helper that integrates with this option. However, older versions of Git Credential Manager also accepted other string values, including 'auto', 'never', and 'always'. The modern use is to use a boolean value, but we should still be careful that some users could have these non-booleans. Further, we should respect 'never' the same as 'false'. This is respected by the implementation and test, but not mentioned in the documentation. The implementation for the Git interactions takes place within credential_getpass(). The method prototype is modified to return an 'int' instead of 'void'. This allows us to detect that no attempt was made to fill the given credential, changing the single caller slightly. Also, a new trace2 region is added around the interactive portion of the credential request. This provides a way to measure the amount of time spent in that region for commands that _are_ interactive. It also makes a conventient way to test that the config option works with 'test_region'. Signed-off-by: Derrick Stolee <[email protected]>
1 parent c3a3d55 commit 22acd55

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

Documentation/config/credential.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ credential.helper::
99
Note that multiple helpers may be defined. See linkgit:gitcredentials[7]
1010
for details and examples.
1111

12+
credential.interactive::
13+
By default, Git and any configured credential helpers will ask for
14+
user input when new credentials are required. Many of these helpers
15+
will succeed based on stored credentials if those credentials are
16+
still valid. To avoid the possibility of user interactivity from
17+
Git, set `credential.interactive=false`. Some credential helpers
18+
respect this option as well.
19+
1220
credential.useHttpPath::
1321
When acquiring credentials, consider the "path" component of an http
1422
or https URL to be important. Defaults to false. See

credential.c

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include "strbuf.h"
1212
#include "urlmatch.h"
1313
#include "git-compat-util.h"
14+
#include "trace2.h"
15+
#include "repository.h"
1416

1517
void credential_init(struct credential *c)
1618
{
@@ -198,14 +200,36 @@ static char *credential_ask_one(const char *what, struct credential *c,
198200
return xstrdup(r);
199201
}
200202

201-
static void credential_getpass(struct credential *c)
203+
static int credential_getpass(struct credential *c)
202204
{
205+
int interactive;
206+
char *value;
207+
if (!git_config_get_maybe_bool("credential.interactive", &interactive) &&
208+
!interactive) {
209+
trace2_data_intmax("credential", the_repository,
210+
"interactive/skipped", 1);
211+
return -1;
212+
}
213+
if (!git_config_get_string("credential.interactive", &value)) {
214+
int same = !strcmp(value, "never");
215+
free(value);
216+
if (same) {
217+
trace2_data_intmax("credential", the_repository,
218+
"interactive/skipped", 1);
219+
return -1;
220+
}
221+
}
222+
223+
trace2_region_enter("credential", "interactive", the_repository);
203224
if (!c->username)
204225
c->username = credential_ask_one("Username", c,
205226
PROMPT_ASKPASS|PROMPT_ECHO);
206227
if (!c->password)
207228
c->password = credential_ask_one("Password", c,
208229
PROMPT_ASKPASS);
230+
trace2_region_leave("credential", "interactive", the_repository);
231+
232+
return 0;
209233
}
210234

211235
int credential_read(struct credential *c, FILE *fp)
@@ -383,8 +407,8 @@ void credential_fill(struct credential *c)
383407
c->helpers.items[i].string);
384408
}
385409

386-
credential_getpass(c);
387-
if (!c->username && !c->password)
410+
if (credential_getpass(c) ||
411+
(!c->username && !c->password))
388412
die("unable to get password from user");
389413
}
390414

t/t5551-http-fetch-smart.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,28 @@ test_expect_success 'clone from password-protected repository' '
186186
test_cmp expect actual
187187
'
188188

189+
test_expect_success 'credential.interactive=false skips askpass' '
190+
set_askpass bogus nonsense &&
191+
(
192+
GIT_TRACE2_EVENT="$(pwd)/interactive-true" &&
193+
export GIT_TRACE2_EVENT &&
194+
test_must_fail git clone --bare "$HTTPD_URL/auth/smart/repo.git" interactive-true-dir &&
195+
test_region credential interactive interactive-true &&
196+
197+
GIT_TRACE2_EVENT="$(pwd)/interactive-false" &&
198+
export GIT_TRACE2_EVENT &&
199+
test_must_fail git -c credential.interactive=false \
200+
clone --bare "$HTTPD_URL/auth/smart/repo.git" interactive-false-dir &&
201+
test_region ! credential interactive interactive-false &&
202+
203+
GIT_TRACE2_EVENT="$(pwd)/interactive-never" &&
204+
export GIT_TRACE2_EVENT &&
205+
test_must_fail git -c credential.interactive=never \
206+
clone --bare "$HTTPD_URL/auth/smart/repo.git" interactive-never-dir &&
207+
test_region ! credential interactive interactive-never
208+
)
209+
'
210+
189211
test_expect_success 'clone from auth-only-for-push repository' '
190212
echo two >expect &&
191213
set_askpass wrong &&

0 commit comments

Comments
 (0)