Skip to content

Commit 450b851

Browse files
committed
fix(auth): 忽略空白环境鉴权令牌
1 parent 7954d02 commit 450b851

3 files changed

Lines changed: 23 additions & 10 deletions

File tree

src/cortex-cli/src/cli/handlers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ pub async fn run_whoami() -> Result<()> {
538538

539539
// Check environment variables first
540540
if let Ok(token) = std::env::var("CORTEX_AUTH_TOKEN")
541-
&& !token.is_empty()
541+
&& !token.trim().is_empty()
542542
{
543543
println!(
544544
"Authenticated via CORTEX_AUTH_TOKEN: {}",
@@ -548,7 +548,7 @@ pub async fn run_whoami() -> Result<()> {
548548
}
549549

550550
if let Ok(token) = std::env::var("CORTEX_API_KEY")
551-
&& !token.is_empty()
551+
&& !token.trim().is_empty()
552552
{
553553
println!(
554554
"Authenticated via CORTEX_API_KEY: {}",

src/cortex-cli/src/login.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ pub async fn run_login_status(config_overrides: CliConfigOverrides) -> ! {
132132

133133
// Check environment variables first (CORTEX_AUTH_TOKEN and CORTEX_API_KEY)
134134
if let Ok(token) = std::env::var("CORTEX_AUTH_TOKEN")
135-
&& !token.is_empty()
135+
&& !token.trim().is_empty()
136136
{
137137
print_success(&format!(
138138
"Authenticated via CORTEX_AUTH_TOKEN environment variable: {}",
@@ -142,7 +142,7 @@ pub async fn run_login_status(config_overrides: CliConfigOverrides) -> ! {
142142
}
143143

144144
if let Ok(token) = std::env::var("CORTEX_API_KEY")
145-
&& !token.is_empty()
145+
&& !token.trim().is_empty()
146146
{
147147
print_success(&format!(
148148
"Authenticated via CORTEX_API_KEY environment variable: {}",

src/cortex-engine/src/auth_token.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
1818
use crate::error::{CortexError, Result};
1919

20+
fn has_token_value(token: &str) -> bool {
21+
!token.trim().is_empty()
22+
}
23+
2024
/// Get authentication token with optional instance override.
2125
///
2226
/// Priority order:
@@ -44,15 +48,15 @@ use crate::error::{CortexError, Result};
4448
pub fn get_auth_token(instance_token: Option<&str>) -> Result<String> {
4549
// Priority 1: Instance token (if provided and non-empty)
4650
if let Some(token) = instance_token {
47-
if !token.is_empty() {
51+
if has_token_value(token) {
4852
tracing::debug!(source = "instance", "Using auth token from client instance");
4953
return Ok(token.to_string());
5054
}
5155
}
5256

5357
// Priority 2: CORTEX_AUTH_TOKEN environment variable
5458
if let Ok(token) = std::env::var("CORTEX_AUTH_TOKEN") {
55-
if !token.is_empty() {
59+
if has_token_value(&token) {
5660
tracing::debug!(
5761
source = "env_var",
5862
"Using auth token from CORTEX_AUTH_TOKEN"
@@ -63,7 +67,7 @@ pub fn get_auth_token(instance_token: Option<&str>) -> Result<String> {
6367

6468
// Priority 3: CORTEX_API_KEY environment variable (alias for GitHub Actions workflow)
6569
if let Ok(token) = std::env::var("CORTEX_API_KEY") {
66-
if !token.is_empty() {
70+
if has_token_value(&token) {
6771
tracing::debug!(source = "env_var", "Using auth token from CORTEX_API_KEY");
6872
return Ok(token);
6973
}
@@ -95,17 +99,17 @@ pub fn get_auth_token_optional(instance_token: Option<&str>) -> Option<String> {
9599
/// Useful for fast availability checks in UI.
96100
pub fn is_authenticated(instance_token: Option<&str>) -> bool {
97101
// Check instance token
98-
if instance_token.map_or(false, |t| !t.is_empty()) {
102+
if instance_token.is_some_and(has_token_value) {
99103
return true;
100104
}
101105

102106
// Check CORTEX_AUTH_TOKEN env var
103-
if std::env::var("CORTEX_AUTH_TOKEN").map_or(false, |t| !t.is_empty()) {
107+
if std::env::var("CORTEX_AUTH_TOKEN").is_ok_and(|t| has_token_value(&t)) {
104108
return true;
105109
}
106110

107111
// Check CORTEX_API_KEY env var (alias)
108-
if std::env::var("CORTEX_API_KEY").map_or(false, |t| !t.is_empty()) {
112+
if std::env::var("CORTEX_API_KEY").is_ok_and(|t| has_token_value(&t)) {
109113
return true;
110114
}
111115

@@ -142,9 +146,18 @@ mod tests {
142146
fn test_is_authenticated_with_instance() {
143147
assert!(is_authenticated(Some("token")));
144148
assert!(!is_authenticated(Some("")));
149+
assert!(!is_authenticated(Some(" ")));
145150
assert!(!is_authenticated(None));
146151
}
147152

153+
#[test]
154+
fn test_has_token_value_rejects_whitespace_only_tokens() {
155+
assert!(has_token_value("token"));
156+
assert!(!has_token_value(""));
157+
assert!(!has_token_value(" "));
158+
assert!(!has_token_value("\n\t"));
159+
}
160+
148161
#[test]
149162
fn test_auth_header_format() {
150163
let header = auth_header(Some("my-token"));

0 commit comments

Comments
 (0)