-
-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Improve token redaction in CLI arg logging
#2115 aimed to redact auth tokens when logging the arguments to the CLI. Although that change addressed some cases where auth tokens were passed as a CLI argument, not all cases were addressed. For example, the following was redacted properly with #2115: ```sh sentry-cli --auth-token this-gets-redacted --log-level=info info ``` But, the following was not: ```sh sentry-cli --auth-token=this-does-not-get-redacted --log-level=info info ``` The difference is that in the second example, the auth token is passed with `--auth-token=token` rather than separated by whitespace `--auth-token token`. This change improves the redacting so that auth tokens passed like `--auth-token=token` are also redacted. The change also redacts any non-whitespace-containing substrings starting with `sntrys_` or `sntryu_` (prefixes that all auth tokens generated in the latest version of Sentry should start with), so that if an auth token appears where it is not expected, we redact it. For example, the following would be redacted with this change: ```sh sentry-cli --auth=sntrys_my-token-passed-as-non-existing-auth-argument --log-level=info info ``` Note that as in #2115, this change is only relevant in the case where the log level is set to `info` or `debug` (the default is `warn`) – command line arguments are logged at the `info` level.
- Loading branch information
1 parent
8b89630
commit 5c1ac1f
Showing
7 changed files
with
111 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use crate::utils::auth_token::{AuthToken, ORG_AUTH_TOKEN_PREFIX, USER_TOKEN_PREFIX}; | ||
use lazy_static::lazy_static; | ||
use regex::Regex; | ||
use std::borrow::Cow; | ||
|
||
pub fn redact_token_from_string<'r>(to_redact: &'r str, replacement: &'r str) -> Cow<'r, str> { | ||
if AuthToken::from(to_redact).format_recognized() { | ||
// The string is itself an auth token, redact the whole thing | ||
Cow::Borrowed(replacement) | ||
} else { | ||
// Redact any substrings consisting of non-whitespace characters starting with the org or | ||
// user auth token prefixes, as these are likely to be auth tokens. Note that this will | ||
// miss old-style user auth tokens that do not contain the prefix. | ||
lazy_static! { | ||
static ref AUTH_TOKEN_REGEX: Regex = Regex::new(&format!( | ||
"(({ORG_AUTH_TOKEN_PREFIX})|({USER_TOKEN_PREFIX}))\\S+" | ||
)) | ||
.unwrap(); | ||
} | ||
|
||
AUTH_TOKEN_REGEX.replace_all(to_redact, replacement) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::utils::auth_token::redacting::redact_token_from_string; | ||
|
||
#[test] | ||
fn test_no_redaction() { | ||
let input = "This string should remain unchanged."; | ||
|
||
let output = redact_token_from_string(input, "[REDACTED]"); | ||
assert_eq!(input, output); | ||
} | ||
|
||
#[test] | ||
fn test_redaction() { | ||
let input = "Here we have a usersntryu_user/auth@#tok3n\\which_should.be3redacted and a sntrys_org_auth_token,too."; | ||
let expected_output = "Here we have a user[REDACTED] and a [REDACTED]"; | ||
|
||
let output = redact_token_from_string(input, "[REDACTED]"); | ||
assert_eq!(expected_output, output); | ||
} | ||
|
||
#[test] | ||
fn test_redaction_org_auth_token() { | ||
let input = "sntrys_\ | ||
eyJpYXQiOjE3MDQyMDU4MDIuMTk5NzQzLCJ1cmwiOiJodHRwOi8vbG9jYWxob3N0OjgwMDAiLCJyZ\ | ||
Wdpb25fdXJsIjoiaHR0cDovL2xvY2FsaG9zdDo4MDAwIiwib3JnIjoic2VudHJ5In0=_\ | ||
lQ5ETt61cHhvJa35fxvxARsDXeVrd0pu4/smF4sRieA"; | ||
let expected_output = "[REDACTED]"; | ||
|
||
let output = redact_token_from_string(input, "[REDACTED]"); | ||
assert_eq!(expected_output, output); | ||
} | ||
|
||
#[test] | ||
fn test_redaction_old_user_token() { | ||
let input = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; | ||
let expected_output = "[REDACTED]"; | ||
|
||
let output = redact_token_from_string(input, "[REDACTED]"); | ||
assert_eq!(expected_output, output); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
``` | ||
$ sentry-cli sourcemaps upload --auth-token=not-following-token-format -o asdf --project=sntrys_project_looks_like_token ./file-sntryu_looks-like-token --log-level=info | ||
? failed | ||
[..] | ||
[..] | ||
[..]INFO[..] sentry-cli was invoked with the following command line: "[..]" "sourcemaps" "upload" "--auth-token=[REDACTED]" "-o" "asdf" "--project=[REDACTED]" "./file-[REDACTED]" "--log-level=info" | ||
... | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters