-
Notifications
You must be signed in to change notification settings - Fork 14
Closed as not planned
Labels
Description
Detail Bug Report
Summary
- Context: The
list_tokensfunction insrc/main.rsdisplays access tokens returned by thelist_access_tokensAPI call. - Bug: The function calls
.expect("expires_at")on anOption<u32>field that can beNone, causing a panic. - Actual vs. expected: When listing access tokens without an expiration date, the CLI panics instead of gracefully handling the missing value.
- Impact: Users cannot list access tokens if any token in their account lacks an expiration date, causing the entire CLI command to crash.
Code with bug
async fn list_tokens(
client_config: ClientConfig,
prefix: Option<String>,
start_after: Option<String>,
limit: Option<usize>,
no_auto_paginate: bool,
) -> Result<(), S2CliError> {
let account_service = AccountService::new(Client::new(client_config));
let tokens = account_service.list_access_tokens(
prefix.unwrap_or_default(),
start_after.unwrap_or_default(),
limit,
no_auto_paginate,
);
tokio::pin!(tokens);
while let Some(token) = tokens.next().await {
for token_info in token?.access_tokens {
let exp_date = token_info
.expires_at
.map(|exp| {
humantime::format_rfc3339_seconds(
UNIX_EPOCH + Duration::from_secs(exp as u64),
)
.to_string()
.green()
})
.expect("expires_at"); // <-- BUG π΄ Panics when expires_at is None
println!(
"{} {}",
token_info.id.parse::<String>().expect("id"),
exp_date
);
}
}
Ok(())
}Example
- Create an access token without an expiration date:
s2 issue-access-token --id my-token --ops read # No --expires-at flag
- List tokens:
s2 list-access-tokens
- The code path processes
expires_at = Noneand panics:token_info .expires_at // None .map(|exp| ...) // -> None .expect("expires_at"); // panics on None - Resulting error:
called 'Option::unwrap()' on a 'None' value: expires_at
Recommended fix
let exp_date = token_info
.expires_at
.map(|exp| {
humantime::format_rfc3339_seconds(
UNIX_EPOCH + Duration::from_secs(exp as u64),
)
.to_string()
.green()
})
.unwrap_or_else(|| "never".to_string().cyan()); // <-- FIX π’ Handle None case
println!(
"{} {}",
token_info.id.parse::<String>().expect("id"),
exp_date
);Reactions are currently unavailable