first commit

This commit is contained in:
gustavooth
2026-07-26 21:12:38 -03:00
commit e6badb0d79
133 changed files with 38368 additions and 0 deletions
+34
View File
@@ -0,0 +1,34 @@
use actix_web::{HttpRequest, http::header::HeaderName};
use uuid::Uuid;
pub const REQUEST_ID_HEADER: &str = "x-request-id";
pub fn from_request(request: &HttpRequest) -> String {
request
.headers()
.get(HeaderName::from_static(REQUEST_ID_HEADER))
.and_then(|value| value.to_str().ok())
.map(str::trim)
.filter(|value| is_valid(value))
.map(str::to_owned)
.unwrap_or_else(|| Uuid::new_v4().to_string())
}
fn is_valid(value: &str) -> bool {
!value.is_empty()
&& value.len() <= 128
&& value
.chars()
.all(|character| character.is_ascii_alphanumeric() || "-_:.".contains(character))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rejects_header_injection() {
assert!(!is_valid("abc\nerror"));
assert!(is_valid("req-123"));
}
}