Skip to content

Commit 4ff5286

Browse files
fix: Validate SENTRY_RELEASE environment variable (#2807)
Apply the same validation to `SENTRY_RELEASE` that we also apply to the `--release` argument. Fixes #2444 Fixes [CLI-7](https://linear.app/getsentry/issue/CLI-7/validate-release-strings-locally) <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Validate SENTRY_RELEASE with the same rules as --release and refactor release validation/parsing for clap. > > - **Config (`src/config.rs`)**: > - Validate `SENTRY_RELEASE` via `args::validate_release`; ignore with warning if invalid; require release if missing. > - **CLI Args (`src/utils/args.rs`)**: > - Refactor release validation: expose `validate_release` returning `anyhow::Result<()>` and add `parse_release` for clap. > - Use `parse_release` for `--release` and `--version` value parsers. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 383ec69. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 5e5933f commit 4ff5286

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

src/config.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use crate::constants::CONFIG_INI_FILE_PATH;
2121
use crate::constants::DEFAULT_MAX_DIF_ITEM_SIZE;
2222
use crate::constants::DEFAULT_MAX_DIF_UPLOAD_SIZE;
2323
use crate::constants::{CONFIG_RC_FILE_NAME, DEFAULT_RETRIES, DEFAULT_URL};
24+
use crate::utils::args;
2425
use crate::utils::auth_token::AuthToken;
2526
use crate::utils::auth_token::AuthTokenPayload;
2627
use crate::utils::http::is_absolute_url;
@@ -370,11 +371,21 @@ impl Config {
370371
.get_one::<String>("release")
371372
.cloned()
372373
.or_else(|| {
373-
env::var("SENTRY_RELEASE")
374-
.ok()
375-
.filter(|v| !v.is_empty())
374+
env::var("SENTRY_RELEASE").ok().filter(|v| {
375+
!v.is_empty()
376+
&& args::validate_release(v)
377+
.inspect_err(|e| {
378+
warn!("Ignoring invalid SENTRY_RELEASE environment variable: {e}")
379+
})
380+
.is_ok()
381+
})
382+
})
383+
.ok_or_else(|| {
384+
format_err!(
385+
"A release slug is required (provide with --release or by \
386+
setting the SENTRY_RELEASE environment variable)"
387+
)
376388
})
377-
.ok_or_else(|| format_err!("A release slug is required (provide with --release or by setting the SENTRY_RELEASE environment variable)"))
378389
}
379390

380391
// Backward compatibility with `releases files <VERSION>` commands.

src/utils/args.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,28 @@ pub fn validate_project(v: &str) -> Result<String, String> {
2828
}
2929
}
3030

31-
fn validate_release(v: &str) -> Result<String, String> {
31+
/// Validate a release string.
32+
pub fn validate_release(v: &str) -> Result<()> {
3233
if v.trim() != v {
33-
Err(
34+
anyhow::bail!(
3435
"Invalid release version. Releases must not contain leading or trailing spaces."
35-
.to_owned(),
36-
)
36+
);
3737
} else if v.is_empty()
3838
|| v == "."
3939
|| v == ".."
4040
|| v.find(&['\n', '\t', '\x0b', '\x0c', '\t', '/'][..])
4141
.is_some()
4242
{
43-
Err(
43+
anyhow::bail!(
4444
"Invalid release version. Slashes and certain whitespace characters are not permitted."
45-
.to_owned(),
46-
)
47-
} else {
48-
Ok(v.to_owned())
45+
);
4946
}
47+
48+
Ok(())
49+
}
50+
51+
fn parse_release(v: &str) -> Result<String> {
52+
validate_release(v).map(|_| v.to_owned())
5053
}
5154

5255
pub fn validate_distribution(v: &str) -> Result<String, String> {
@@ -123,7 +126,7 @@ impl ArgExt for Command {
123126
.short('r')
124127
.global(true)
125128
.allow_hyphen_values(true)
126-
.value_parser(validate_release)
129+
.value_parser(parse_release)
127130
.help("The release slug."),
128131
)
129132
}
@@ -135,7 +138,7 @@ impl ArgExt for Command {
135138
// either specified for subcommands (global=true) or for this command (required=true)
136139
.required(!global)
137140
.global(global)
138-
.value_parser(validate_release)
141+
.value_parser(parse_release)
139142
.help("The version of the release"),
140143
)
141144
}

0 commit comments

Comments
 (0)