Share session token shape validation

This commit is contained in:
Dylan M. Taylor committed 2026-11-15 18:59:28 +00:00
1 parent 82276297a6
commit 2e6878d973
1 file changed
+11
+11
View File
@@ -3,6 +3,11 @@ use serde_json::{Value, json};
use std::io::{BufRead, BufReader, Read, Write};
use subtle::ConstantTimeEq;
/// Whether a session token has the control protocol's 64 hexadecimal character form.
pub fn valid_token(token: &str) -> bool {
token.len() == 64 && token.bytes().all(|b| b.is_ascii_hexdigit())
}
/// One parsed line.
enum Line {
Request {
@@ -116,6 +121,12 @@ pub fn serve_lines<R: Read, W: Write>(read: R, mut out: W, token: &str, max_line
mod tests {
use super::*;
#[test]
fn validates_session_token_shape() {
assert!(valid_token(&"aB09".repeat(16)));
assert!(!valid_token(&"a".repeat(63)));
assert!(!valid_token(&"g".repeat(64)));
}
#[test]
fn authenticates_each_request_and_closes_after_failure() {
let input =
b"{\"id\":1,\"method\":\"first\",\"token\":\"secret\"}\n{\"id\":2,\"method\":\"second\"}\n{\"method\":\"third\",\"token\":\"secret\"}\n";