WS-D: harden file modes, token names; more auth tests
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YShymaPrXmc3KYRUg2HMSs
This commit is contained in:
1 parent
b6c814a18f
commit
e25f0e83bf
3 files changed
+121
-4
No files matched your search
@@ -189,4 +189,58 @@ mod tests {
|
||||
c.server.bind = "0.0.0.0".parse().unwrap();
|
||||
assert!(c.validate().is_err());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn fails_closed_without_db() {
|
||||
let mut state = fake_state(Config::default());
|
||||
state.db = None;
|
||||
let app = app(state);
|
||||
let (st, _) = status(&app, "/api/v1/x", Some("Bearer slinuxm_abc")).await;
|
||||
assert_eq!(st, StatusCode::SERVICE_UNAVAILABLE);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn db_error_is_503() {
|
||||
let p = pool().await;
|
||||
let mut state = fake_state(Config::default());
|
||||
state.db = Some(p.clone());
|
||||
let app = app(state);
|
||||
p.close().await;
|
||||
let (st, _) = status(&app, "/api/v1/x", Some("Bearer slinuxm_abc")).await;
|
||||
assert_eq!(st, StatusCode::SERVICE_UNAVAILABLE);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn scheme_is_case_insensitive() {
|
||||
let p = pool().await;
|
||||
let mut state = fake_state(Config::default());
|
||||
state.db = Some(p.clone());
|
||||
let app = app(state);
|
||||
let t = tokens::create(&p, "c").await.unwrap();
|
||||
for scheme in ["bearer", "BEARER", "Bearer"] {
|
||||
let h = format!("{scheme} {t}");
|
||||
assert_eq!(status(&app, "/api/v1/x", Some(&h)).await.0, StatusCode::OK);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn last_used_is_throttled() {
|
||||
let p = pool().await;
|
||||
let t = tokens::create(&p, "t").await.unwrap();
|
||||
assert!(tokens::verify(&p, &t).await.unwrap());
|
||||
let first = tokens::list(&p).await.unwrap()[0].last_used_at.clone();
|
||||
assert!(first.is_some());
|
||||
assert!(tokens::verify(&p, &t).await.unwrap());
|
||||
assert_eq!(tokens::list(&p).await.unwrap()[0].last_used_at, first);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn token_names_are_trimmed_and_validated() {
|
||||
let p = pool().await;
|
||||
tokens::create(&p, " ci ").await.unwrap();
|
||||
assert_eq!(tokens::list(&p).await.unwrap()[0].name, "ci");
|
||||
assert!(tokens::create(&p, "ci").await.is_err());
|
||||
assert!(tokens::create(&p, "a\nb").await.is_err());
|
||||
assert!(tokens::create(&p, " ").await.is_err());
|
||||
}
|
||||
}
|
||||
+5
-1
@@ -54,9 +54,13 @@ fn now_string() -> String {
|
||||
|
||||
/// Create a token and return the plaintext (shown once, never stored).
|
||||
pub async fn create(pool: &SqlitePool, name: &str) -> Result<String> {
|
||||
if name.trim().is_empty() {
|
||||
let name = name.trim();
|
||||
if name.is_empty() {
|
||||
bail!("token name must not be empty");
|
||||
}
|
||||
if name.chars().any(char::is_control) {
|
||||
bail!("token name must not contain control characters");
|
||||
}
|
||||
let mut bytes = [0u8; 32];
|
||||
SysRng
|
||||
.try_fill_bytes(&mut bytes)
|
||||
|
||||
Reference in new issue
Block a user