From d08d2c69581a29bfd15dc8099d24b8e9165b1987 Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Thu, 4 Feb 2021 15:44:06 +0900 Subject: [PATCH] Care the case of invalid header value --- plume-common/src/activity_pub/mod.rs | 2 +- plume-common/src/activity_pub/request.rs | 26 ++++++++++++------------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/plume-common/src/activity_pub/mod.rs b/plume-common/src/activity_pub/mod.rs index 179f9864..bf643d89 100644 --- a/plume-common/src/activity_pub/mod.rs +++ b/plume-common/src/activity_pub/mod.rs @@ -155,7 +155,7 @@ where }; let host_header_value = HeaderValue::from_str(&url.host_str().expect("Unreachable")); if host_header_value.is_err() { - warn!("Header valid is invalid: {:?}", url.host_str()); + warn!("Header value is invalid: {:?}", url.host_str()); continue; } headers.insert("Host", host_header_value.unwrap()); diff --git a/plume-common/src/activity_pub/request.rs b/plume-common/src/activity_pub/request.rs index 6768f2b0..04a07c83 100644 --- a/plume-common/src/activity_pub/request.rs +++ b/plume-common/src/activity_pub/request.rs @@ -3,6 +3,7 @@ use openssl::hash::{Hasher, MessageDigest}; use reqwest::header::{HeaderMap, HeaderValue, ACCEPT, CONTENT_TYPE, DATE, USER_AGENT}; use std::ops::Deref; use std::time::SystemTime; +use tracing::warn; use crate::activity_pub::sign::Signer; use crate::activity_pub::{ap_accept_header, AP_CONTENT_TYPE}; @@ -129,25 +130,24 @@ pub fn signature( path.to_string() }; - let mut headers = headers - .iter() - .map(|(h, v)| { - ( - h.as_str().to_lowercase(), - v.to_str() - .expect("request::signature: invalid header error"), - ) - }) - .collect::>(); + let mut headers_vec = Vec::with_capacity(headers.len()); + for (h, v) in headers.iter() { + let v = v.to_str(); + if v.is_err() { + warn!("invalid header error: {:?}", v.unwrap_err()); + return Err(Error()); + } + headers_vec.push((h.as_str().to_lowercase(), v.expect("Unreachable"))); + } let request_target = format!("{} {}", method.to_lowercase(), origin_form); - headers.push(("(request-target)".to_string(), &request_target)); + headers_vec.push(("(request-target)".to_string(), &request_target)); - let signed_string = headers + let signed_string = headers_vec .iter() .map(|(h, v)| format!("{}: {}", h, v)) .collect::>() .join("\n"); - let signed_headers = headers + let signed_headers = headers_vec .iter() .map(|(h, _)| h.as_ref()) .collect::>()