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