From 4b814ccca02faefa901c6890be441c701dedd129 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sat, 12 Aug 2023 18:59:34 -0700 Subject: [PATCH] Ignore invalid content type header values --- src/subcommand/server.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 4b5c8a0de8..c0504e20ad 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -788,9 +788,8 @@ impl Server { header::CONTENT_TYPE, inscription .content_type() - .unwrap_or("application/octet-stream") - .parse() - .unwrap(), + .and_then(|content_type| content_type.parse().ok()) + .unwrap_or(HeaderValue::from_static("application/octet-stream")), ); headers.insert( header::CONTENT_SECURITY_POLICY, @@ -2170,6 +2169,18 @@ mod tests { assert!(body.is_empty()); } + #[test] + fn content_response_bad_content_type() { + let (headers, body) = Server::content_response(Inscription::new( + Some("\n".as_bytes().to_vec()), + Some(Vec::new()), + )) + .unwrap(); + + assert_eq!(headers["content-type"], "application/octet-stream"); + assert!(body.is_empty()); + } + #[test] fn text_preview() { let server = TestServer::new_with_regtest();