Skip to content

Commit 5b4cb83

Browse files
committed
Also provide prompt configuration to allow Cascade::invoke() to be called with all arguments (#450)
1 parent 4dbbf51 commit 5b4cb83

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

git-repository/src/config/cache/init.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ impl Cache {
2424
git_prefix,
2525
home: home_env,
2626
xdg_config_home: xdg_config_home_env,
27+
ssh_prefix: _,
2728
}: repository::permissions::Environment,
2829
repository::permissions::Config {
2930
system: use_system,

git-repository/src/config/snapshot/credential_helpers.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ mod error {
2020
}
2121

2222
impl Snapshot<'_> {
23-
/// Returns the configuration for all git-credential helpers that apply to the given `url` along with an action
24-
/// preconfigured to invoke the cascade with. This includes `url` which may be altered to contain a user-name
25-
/// as configured.
23+
/// Returns the configuration for all git-credential helpers from trusted configuration that apply
24+
/// to the given `url` along with an action preconfigured to invoke the cascade with.
25+
/// This includes `url` which may be altered to contain a user-name as configured.
2626
///
2727
/// These can be invoked to obtain credentials. Note that the `url` is expected to be the one used
2828
/// to connect to a remote, and thus should already have passed the url-rewrite engine.
@@ -39,7 +39,14 @@ impl Snapshot<'_> {
3939
pub fn credential_helpers(
4040
&self,
4141
mut url: git_url::Url,
42-
) -> Result<(git_credentials::helper::Cascade, git_credentials::helper::Action), Error> {
42+
) -> Result<
43+
(
44+
git_credentials::helper::Cascade,
45+
git_credentials::helper::Action,
46+
git_prompt::Options<'static>,
47+
),
48+
Error,
49+
> {
4350
let mut programs = Vec::new();
4451
let mut use_http_path = false;
4552
let url_had_user_initially = url.user().is_some();
@@ -110,13 +117,18 @@ impl Snapshot<'_> {
110117
}
111118
}
112119

120+
let allow_git_env = *self.repo.options.permissions.env.git_prefix == git_sec::Permission::Allow;
121+
let allow_ssh_env = *self.repo.options.permissions.env.ssh_prefix == git_sec::Permission::Allow;
122+
let prompt_options =
123+
git_prompt::Options::default().apply_environment(allow_git_env, allow_ssh_env, allow_git_env);
113124
Ok((
114125
git_credentials::helper::Cascade {
115126
programs,
116127
use_http_path,
117128
..Default::default()
118129
},
119130
git_credentials::helper::Action::get_for_url(url.to_bstring()),
131+
prompt_options,
120132
))
121133
}
122134
}

git-repository/src/repository/permissions.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ pub struct Environment {
6666
pub home: permission::env_var::Resource,
6767
/// Control if resources pointed to by `GIT_*` prefixed environment variables can be used.
6868
pub git_prefix: permission::env_var::Resource,
69+
/// Control if resources pointed to by `SSH_*` prefixed environment variables can be used (like `SSH_ASKPASS`)
70+
pub ssh_prefix: permission::env_var::Resource,
6971
}
7072

7173
impl Environment {
@@ -75,6 +77,7 @@ impl Environment {
7577
xdg_config_home: Access::resource(git_sec::Permission::Allow),
7678
home: Access::resource(git_sec::Permission::Allow),
7779
git_prefix: Access::resource(git_sec::Permission::Allow),
80+
ssh_prefix: Access::resource(git_sec::Permission::Allow),
7881
}
7982
}
8083
}
@@ -116,6 +119,7 @@ impl Permissions {
116119
Environment {
117120
xdg_config_home: deny.clone(),
118121
home: deny.clone(),
122+
ssh_prefix: deny.clone(),
119123
git_prefix: deny,
120124
}
121125
},

git-repository/tests/repository/config/config_snapshot/credential_helpers.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use git_testtools::Env;
2+
13
mod baseline {
24
use crate::remote;
35
use git_object::bstr::BString;
@@ -64,11 +66,20 @@ mod baseline {
6466

6567
fn agrees_with_inner(url: &str, ignore_expected_prompt_port: bool, lowercase_prompt_host: bool) {
6668
let repo = remote::repo("credential-helpers");
67-
let (cascade, mut action) = repo
69+
let (cascade, mut action, prompt_options) = repo
6870
.config_snapshot()
6971
.credential_helpers(git::url::parse(url.into()).expect("valid input URL"))
7072
.unwrap();
7173

74+
assert_ne!(
75+
prompt_options.mode,
76+
git_prompt::Mode::Disable,
77+
"isolated repos may show prompts"
78+
);
79+
assert!(
80+
prompt_options.askpass.is_none(),
81+
"isolation does not allow environment variables to be read"
82+
);
7283
let actual_helpers: Vec<BString> = cascade
7384
.programs
7485
.iter()
@@ -162,15 +173,19 @@ fn subdomain_globs_match_on_their_level() {
162173
}
163174

164175
#[test]
176+
#[serial_test::serial]
165177
fn http_urls_match_the_host_without_path_as_well() {
178+
let _env = Env::new().set("GIT_ASKPASS", "foo");
166179
baseline::agrees_with("http://example.com:8080/other/path");
167180
baseline::agrees_with_but_drops_default_port_in_prompt("http://example.com:80/");
168181
baseline::agrees_with_but_drops_default_port_in_prompt("http://example.com:80");
169182
baseline::agrees_with("http://example.com");
170183
}
171184

172185
#[test]
186+
#[serial_test::serial]
173187
fn user_rules_only_match_urls_with_user() {
188+
let _env = Env::new().set("SSH_ASKPASS", "foo");
174189
baseline::agrees_with("https://[email protected]/with-user");
175190
baseline::agrees_with("https://example.com/with-user");
176191
baseline::agrees_with("ssh://user@host/with-user");

0 commit comments

Comments
 (0)