Validate a session ID to prevent path traversal. Rejects blank IDs, IDs containing `..`, `/`, `\` or null bytes, and IDs longer than 255 UTF-8 bytes (the filesystem component limit).
(session_id: &str)
| 1413 | /// Rejects blank IDs, IDs containing `..`, `/`, `\` or null bytes, and IDs |
| 1414 | /// longer than 255 UTF-8 bytes (the filesystem component limit). |
| 1415 | pub fn validate_session_id(session_id: &str) -> AgentResult<()> { |
| 1416 | if session_id.trim().is_empty() { |
| 1417 | return Err(AgentError::SessionIdInvalid { |
| 1418 | session_id: "(empty)".to_string(), |
| 1419 | }); |
| 1420 | } |
| 1421 | |
| 1422 | if session_id.contains("..") |
| 1423 | || session_id.contains('/') |
| 1424 | || session_id.contains('\\') |
| 1425 | || session_id.contains('\0') |
| 1426 | { |
| 1427 | return Err(AgentError::SessionIdInvalid { |
| 1428 | session_id: session_id.to_string(), |
| 1429 | }); |
| 1430 | } |
| 1431 | |
| 1432 | // Reject very long session IDs (filesystem path limits) |
| 1433 | if session_id.len() > 255 { |
| 1434 | return Err(AgentError::SessionIdInvalid { |
| 1435 | session_id: format!( |
| 1436 | "{}... (too long: {} bytes)", |
| 1437 | session_id.chars().take(20).collect::<String>(), |
| 1438 | session_id.len() |
| 1439 | ), |
| 1440 | }); |
| 1441 | } |
| 1442 | |
| 1443 | Ok(()) |
| 1444 | } |
| 1445 | |
| 1446 | /// Whether a writer pid is still alive (CB-13D ::24 R6): a claim file |
| 1447 | /// whose writer is gone belongs to a crashed consumer and is replayed. |