Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion monitor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ sqlx = { version = "0.9", features = [
# rust_crypto instead of aws_lc_rs: pure Rust, no cmake/assembly dependency for static musl builds
jsonwebtoken = { version = "11.0", default-features = false, features = ["rust_crypto"] }
bcrypt = "0.19"
sha2 = "0.10"
# sha2 0.11 returns a `hybrid_array::Array`, which has no `LowerHex` — hence hex
sha2 = "0.11"
hex = "0.4"

# Configuration
dotenvy = "0.15"
Expand Down
24 changes: 15 additions & 9 deletions monitor/src/api/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,10 @@ async fn login(
}))
}

/// Lowercase hex of the SHA-256, which is what the `watch_tokens` rows already
/// hold — `hex::encode` writes the same bytes the `{:x}` of digest 0.10 did.
fn watch_token_hash(token: &str) -> String {
format!("{:x}", Sha256::digest(token.as_bytes()))
hex::encode(Sha256::digest(token.as_bytes()))
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

fn validate_watch_client_id(client_id: &str) -> Result<&str> {
Expand Down Expand Up @@ -1222,17 +1224,27 @@ mod watch_token_tests {
pool
}

/// The published SHA-256 of `abc`, so the row below is seeded with a value
/// this crate did not produce. Hashing the token through
/// `watch_token_hash` on both sides would agree with itself whatever the
/// encoding became, and an agent's `watch_tokens` rows outlive the build
/// that wrote them: a change there invalidates every paired watch, silently.
const TOKEN: &str = "abc";
const TOKEN_HASH: &str =
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad";

#[tokio::test]
async fn watch_tokens_are_hashed_expiring_and_revocable() {
let pool = pool().await;
let token = "sbw_secret";
let token = TOKEN;
assert_eq!(watch_token_hash(token), TOKEN_HASH);
sqlx::query(
"INSERT INTO watch_tokens(subject, client_id, token_hash, created_at, expires_at) \
VALUES (?, ?, ?, ?, ?)",
)
.bind("admin")
.bind("watch:one")
.bind(watch_token_hash(token))
.bind(TOKEN_HASH)
.bind(10_i64)
.bind(20_i64)
.execute(&pool)
Expand All @@ -1241,12 +1253,6 @@ mod watch_token_tests {

assert_eq!(verify_watch_token(&pool, token, 19).await.unwrap(), "admin");
assert!(verify_watch_token(&pool, token, 20).await.is_err());
let stored: String = sqlx::query_scalar("SELECT token_hash FROM watch_tokens")
.fetch_one(&pool)
.await
.unwrap();
assert_ne!(stored, token);

sqlx::query("DELETE FROM watch_tokens WHERE client_id = ?")
.bind("watch:one")
.execute(&pool)
Expand Down
Loading