Skip to content

Commit f0cacb0

Browse files
committed
feat: add protocol::Context::redacted() as convenient way to not leak secrets.
1 parent ca8d64c commit f0cacb0

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

gix-credentials/src/protocol/context/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@ mod access {
1414
use crate::protocol::Context;
1515

1616
impl Context {
17+
/// Replace existing secrets with the word `<redacted>`.
18+
pub fn redacted(mut self) -> Self {
19+
let Context {
20+
protocol: _,
21+
host: _,
22+
path: _,
23+
username: _,
24+
password,
25+
url: _,
26+
quit: _,
27+
} = &mut self;
28+
if let Some(pw) = password {
29+
*pw = "<redacted>".into();
30+
}
31+
self
32+
}
33+
1734
/// Convert all relevant fields into a URL for consumption.
1835
pub fn to_url(&self) -> Option<BString> {
1936
use bstr::{ByteSlice, ByteVec};

gix-credentials/src/protocol/mod.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,10 @@ pub struct Context {
5959
/// Convert the outcome of a helper invocation to a helper result, assuring that the identity is complete in the process.
6060
#[allow(clippy::result_large_err)]
6161
pub fn helper_outcome_to_result(outcome: Option<helper::Outcome>, action: helper::Action) -> Result {
62-
fn redact(mut ctx: Context) -> Context {
63-
if let Some(pw) = ctx.password.as_mut() {
64-
*pw = "<redacted>".into();
65-
}
66-
ctx
67-
}
6862
match (action, outcome) {
69-
(helper::Action::Get(ctx), None) => Err(Error::IdentityMissing { context: redact(ctx) }),
63+
(helper::Action::Get(ctx), None) => Err(Error::IdentityMissing {
64+
context: ctx.redacted(),
65+
}),
7066
(helper::Action::Get(ctx), Some(mut outcome)) => match outcome.consume_identity() {
7167
Some(identity) => Ok(Some(Outcome {
7268
identity,
@@ -75,7 +71,9 @@ pub fn helper_outcome_to_result(outcome: Option<helper::Outcome>, action: helper
7571
None => Err(if outcome.quit {
7672
Error::Quit
7773
} else {
78-
Error::IdentityMissing { context: redact(ctx) }
74+
Error::IdentityMissing {
75+
context: ctx.redacted(),
76+
}
7977
}),
8078
},
8179
(helper::Action::Store(_) | helper::Action::Erase(_), _ignore) => Ok(None),

0 commit comments

Comments
 (0)