fix: use 256-bit entropy for pairing tokens (#351)

Merges #413
This commit is contained in:
Argenis 2026-02-16 13:48:03 -05:00 committed by GitHub
parent b161fff9ef
commit dc5a85c85c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 14 additions and 2 deletions

1
Cargo.lock generated
View file

@ -4843,6 +4843,7 @@ dependencies = [
"pdf-extract",
"probe-rs",
"prometheus",
"rand 0.8.5",
"reqwest",
"rppal",
"rusqlite",

View file

@ -57,6 +57,9 @@ hmac = "0.12"
sha2 = "0.10"
hex = "0.4"
# CSPRNG for secure token generation
rand = "0.8"
# Landlock (Linux sandbox) - optional dependency
landlock = { version = "0.4", optional = true }

View file

@ -201,9 +201,17 @@ fn generate_code() -> String {
}
}
/// Generate a cryptographically-adequate bearer token (hex-encoded).
/// Generate a cryptographically-adequate bearer token with 256-bit entropy.
///
/// Uses `rand::thread_rng()` which is backed by the OS CSPRNG
/// (/dev/urandom on Linux, BCryptGenRandom on Windows, SecRandomCopyBytes
/// on macOS). The 32 random bytes (256 bits) are hex-encoded for a
/// 64-character token, providing 256 bits of entropy.
fn generate_token() -> String {
format!("zc_{}", uuid::Uuid::new_v4().as_simple())
use rand::RngCore;
let mut bytes = [0u8; 32];
rand::thread_rng().fill_bytes(&mut bytes);
format!("zc_{}", hex::encode(&bytes))
}
/// SHA-256 hash a bearer token for storage. Returns lowercase hex.