Skip to content

Commit b11a883

Browse files
fix: Ensure URL and Auth Token come from same source
Backport of #3378 to v2
1 parent 9b27ca7 commit b11a883

8 files changed

Lines changed: 467 additions & 170 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Changelog
22

3-
## 2.58.6
3+
## Unreleased
4+
5+
### Fixes
6+
7+
- Do not combine a URL and non-embedded auth token from different configuration files. When configuration sources provide only one of these values, an existing value from another source may be ignored with a warning. Configure the URL and token in the same file or through CLI arguments and environment variables, which are treated as one runtime source. This change does not alter parent-config discovery or how other configuration keys are selected ([#3378](https://github.com/getsentry/sentry-cli/pull/3378/)).
48

59
### Security Fixes
610

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class SentryCli {
3838
*
3939
* @param {string | null} [configFile] - Path to Sentry CLI config properties, as described in https://docs.sentry.io/learn/cli/configuration/#properties-files.
4040
* By default, the config file is looked for upwards from the current path and defaults from ~/.sentryclirc are always loaded.
41+
* A URL and a non-embedded auth token must be configured in the same file or through runtime options and environment variables.
4142
* This value will update `SENTRY_PROPERTIES` env variable.
4243
* @param {SentryCliOptions} [options] - More options to pass to the CLI
4344
*/

src/commands/login.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ pub fn make_command(command: Command) -> Command {
1919
)
2020
}
2121

22-
fn update_config(config: &Config, token: AuthToken) -> Result<()> {
22+
fn update_config(config: &Config, token: AuthToken, url: &str) -> Result<()> {
2323
let mut new_cfg = config.clone();
24-
new_cfg.set_auth(Auth::Token(token));
24+
new_cfg.set_auth_and_url(Auth::Token(token), url);
2525
new_cfg.save()?;
2626
Ok(())
2727
}
@@ -61,7 +61,7 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
6161
}
6262

6363
let mut token;
64-
loop {
64+
let validated_url = loop {
6565
token = if let Some(token) = predefined_token {
6666
token.to_owned()
6767
} else {
@@ -72,6 +72,7 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
7272
cfg.set_auth(Auth::Token(token.clone()));
7373
Ok(())
7474
})?;
75+
let tested_url = test_cfg.get_base_url()?.to_owned();
7576

7677
match Api::with_config(test_cfg).authenticated()?.get_auth_info() {
7778
Ok(info) => {
@@ -85,7 +86,7 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
8586
println!("Valid org token");
8687
}
8788
}
88-
break;
89+
break tested_url;
8990
}
9091
Err(err) => {
9192
// Convert to anyhow error to take advantage of anyhow's Debug impl
@@ -98,20 +99,21 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
9899
}
99100
}
100101
}
101-
}
102+
};
102103

103104
let config_to_update = if matches.get_flag("global") {
104105
Config::global()?
105106
} else {
106-
Config::from_cli_config()?
107+
Config::from_cli_config(None, None, None)?
107108
};
108109

109-
if should_warn_about_overwrite(config_to_update.get_auth(), &token) {
110+
let persisted_auth = config_to_update.get_persisted_auth();
111+
if should_warn_about_overwrite(persisted_auth.as_ref(), &token) {
110112
println!();
111113
println!("Warning: You are about to overwrite an existing token!");
112114

113115
// Show organization information
114-
if let Some(existing_auth) = config_to_update.get_auth() {
116+
if let Some(existing_auth) = persisted_auth.as_ref() {
115117
let existing_org = get_org_from_auth(existing_auth);
116118
let new_org = get_org_from_token(&token);
117119

@@ -126,7 +128,7 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
126128
}
127129
}
128130

129-
update_config(&config_to_update, token)?;
131+
update_config(&config_to_update, token, &validated_url)?;
130132
println!();
131133
println!(
132134
"Stored token in {}",

src/commands/mod.rs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::process;
1010
use std::{env, iter};
1111

1212
use crate::api::Api;
13-
use crate::config::{Auth, Config};
13+
use crate::config::Config;
1414
use crate::constants::{ARCH, PLATFORM, VERSION};
1515
use crate::utils::auth_token::{redact_token_from_string, AuthToken};
1616
use crate::utils::logging::set_quiet_mode;
@@ -137,26 +137,6 @@ fn preexecute_hooks() -> Result<bool> {
137137
}
138138

139139
fn configure_args(config: &mut Config, matches: &ArgMatches) {
140-
if let Some(api_key) = matches.get_one::<String>("api_key") {
141-
log::warn!(
142-
"[DEPRECTATION NOTICE] API key authentication and the --api-key argument are \
143-
deprecated. \
144-
Please generate an auth token, and use the --auth-token argument instead."
145-
);
146-
147-
#[expect(deprecated, reason = "Auth key is deprecated.")]
148-
let auth = Auth::Key(api_key.to_owned());
149-
config.set_auth(auth);
150-
}
151-
152-
if let Some(auth_token) = matches.get_one::<AuthToken>("auth_token") {
153-
config.set_auth(Auth::Token(auth_token.to_owned()));
154-
}
155-
156-
if let Some(url) = matches.get_one::<String>("url") {
157-
config.set_base_url(url);
158-
}
159-
160140
if let Some(headers) = matches.get_many::<String>("headers") {
161141
let headers = headers.map(|h| h.to_owned()).collect();
162142
config.set_headers(headers);
@@ -280,7 +260,11 @@ pub fn execute() -> Result<()> {
280260
if let Some(&log_level) = log_level {
281261
set_max_level(log_level);
282262
}
283-
let mut config = Config::from_cli_config()?;
263+
let mut config = Config::from_cli_config(
264+
matches.get_one::<String>("url").map(String::as_str),
265+
matches.get_one::<AuthToken>("auth_token"),
266+
matches.get_one::<String>("api_key").map(String::as_str),
267+
)?;
284268
configure_args(&mut config, &matches);
285269
set_quiet_mode(matches.get_flag("quiet"));
286270

0 commit comments

Comments
 (0)