fix(auth): require state version, atomic key creation, torn audit lines

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Lucas RayanandClaude Opus 5.5 committed 2026-11-04 19:01:11 -03:00
1 parent ef25bf7eb1
commit c757db209d
3 files changed
+139 -20

No files matched your search

+24 -1
View File
@@ -63,13 +63,18 @@ impl std::fmt::Debug for Bootstrap {
}
}
/// `version` is required: `{}` or an unrelated JSON object must not load as an
/// empty state that the next update would overwrite.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct AuthState {
pub version: u32,
#[serde(default)]
pub users: Vec<User>,
#[serde(default)]
pub sessions: Vec<Session>,
#[serde(default)]
pub tokens: Vec<ApiToken>,
#[serde(default)]
pub bootstrap: Option<Bootstrap>,
}
@@ -204,6 +209,24 @@ mod tests {
assert_eq!(fs::read_to_string(&path).unwrap(), "{not json");
}
#[test]
fn state_without_version_fails_closed() {
for body in ["{}", r#"{"foo":1}"#, r#"{"users":[]}"#, "[]", "null"] {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join(STATE_FILE);
fs::write(&path, body).unwrap();
assert!(matches!(
AuthStore::open(dir.path()).unwrap_err(),
AcauaError::Auth(AuthFailure::StateCorrupt(_))
));
let store = AuthStore {
dir: dir.path().to_path_buf(),
};
assert!(store.update(|_| Ok(())).is_err());
assert_eq!(fs::read_to_string(&path).unwrap(), body);
}
}
#[test]
fn future_version_rejected() {
let dir = tempfile::tempdir().unwrap();